From 5d2f2ff4936894fb11df417f118cb73a7841e261 Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 20:25:20 +0200 Subject: [PATCH] Add method to create data from parameters --- .../nodes/Cockpit/GenericFunctions.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts b/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts index ed923d3bda..8db5c5b7ad 100644 --- a/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts @@ -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; +}