Add method to create data from parameters

This commit is contained in:
Krzysztof Janda 2020-04-11 20:25:20 +02:00
parent 348afdaf5c
commit 5d2f2ff493

View file

@ -44,3 +44,29 @@ export async function cockpitApiRequest(this: IExecuteFunctions | IExecuteSingle
throw new Error(`Cockpit error [${error.statusCode}]: ` + errorMessage);
}
}
export function createDataFromParameters(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, itemIndex: number): IDataObject {
const dataFieldsAreJson = this.getNodeParameter('jsonDataFields', itemIndex) as boolean;
if (dataFieldsAreJson) {
// Parameters are defined as JSON
return JSON.parse(this.getNodeParameter('dataFieldsJson', itemIndex, {}) as string);
}
// Parameters are defined in UI
return unpackUiDataFields(this.getNodeParameter('dataFieldsUi', itemIndex, {}) as IDataObject);
}
function unpackUiDataFields(uiDataFields: IDataObject): IDataObject {
const unpacked: IDataObject = {};
if (uiDataFields.field === undefined) {
return unpacked;
}
for (const field of uiDataFields!.field as IDataObject[]) {
unpacked[field!.name as string] = field!.value;
}
return unpacked;
}