From 39e9b9ff45b8538fcf5c1ab697f1f3879a94cc7b Mon Sep 17 00:00:00 2001 From: VM Date: Thu, 15 Aug 2024 21:49:07 -0700 Subject: [PATCH] fix: allow "=" symbols in constant value --- credentials/CredentialsUtils.ts | 19 +++ nodes/GlobalConstants/GlobalConstants.node.ts | 152 +++++++++--------- 2 files changed, 92 insertions(+), 79 deletions(-) create mode 100644 credentials/CredentialsUtils.ts diff --git a/credentials/CredentialsUtils.ts b/credentials/CredentialsUtils.ts new file mode 100644 index 0000000..5cc14b7 --- /dev/null +++ b/credentials/CredentialsUtils.ts @@ -0,0 +1,19 @@ +export function splitConstants(globalConstantsMultiline: string): { [key: string]: string } { + const lines = globalConstantsMultiline.split('\n'); + var retArr: { [key: string]: string } = {}; + for (const line of lines) { + // trim the line + const constant = line.trim(); + if (!constant) { + continue; + } + // skip if it doesn't contain "=" + if (!constant.includes('=')) { + continue; + } + // split only first "=" to allow values with "=" in them + const [name, ...value] = constant.split('='); + retArr[name.trim()] = value.join('=').trim(); + } + return retArr; +} diff --git a/nodes/GlobalConstants/GlobalConstants.node.ts b/nodes/GlobalConstants/GlobalConstants.node.ts index e6e34f2..f3b3b09 100644 --- a/nodes/GlobalConstants/GlobalConstants.node.ts +++ b/nodes/GlobalConstants/GlobalConstants.node.ts @@ -1,92 +1,86 @@ import { IExecuteFunctions, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow'; import { GLOBAL_CONSTANTS_CREDENTIALS_NAME, GlobalConstantsCredentialsData } from '../../credentials/GlobalConstantsCredentials.credentials'; +import { splitConstants } from '../../credentials/CredentialsUtils'; export class GlobalConstants implements INodeType { - description: INodeTypeDescription = { - displayName: 'Global Constants', - name: 'globalConstants', - // eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg - icon: 'file:globals-icon-60px.png', - group: ['transform', 'output'], - version: 1, - description: 'Global Constants', - subtitle: '={{$parameter["resource"]}}', - defaults: { - name: 'Global Constants', - }, - inputs: ['main'], - outputs: ['main'], - credentials: [ - { - name: GLOBAL_CONSTANTS_CREDENTIALS_NAME, - required: true, - } - ], - properties: [ - { - displayName: 'Put All Constants in One Key', - name: 'putAllInOneKey', - type: "boolean", - default: true, - description: "Whether to put all constants in one key or use separate keys for each constant", - }, - { - displayName: "Constants Key Name", - name: "constantsKeyName", - type: "string", - default: "constants", - displayOptions: { - show: { - putAllInOneKey: [true], - }, - }, - }, - ], - }; + description: INodeTypeDescription = { + displayName: 'Global Constants', + name: 'globalConstants', + // eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg + icon: 'file:globals-icon-60px.png', + group: ['transform', 'output'], + version: 1, + description: 'Global Constants', + subtitle: '={{$parameter["resource"]}}', + defaults: { + name: 'Global Constants', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: GLOBAL_CONSTANTS_CREDENTIALS_NAME, + required: true, + } + ], + properties: [ + { + displayName: 'Put All Constants in One Key', + name: 'putAllInOneKey', + type: "boolean", + default: true, + description: "Whether to put all constants in one key or use separate keys for each constant", + }, + { + displayName: "Constants Key Name", + name: "constantsKeyName", + type: "string", + default: "constants", + displayOptions: { + show: { + putAllInOneKey: [true], + }, + }, + }, + ], + }; - async execute(this: IExecuteFunctions): Promise { + async execute(this: IExecuteFunctions): Promise { const credentials = await this.getCredentials(GLOBAL_CONSTANTS_CREDENTIALS_NAME) as unknown as GlobalConstantsCredentialsData; - const globalConstants = credentials.globalConstants.split('\n').map((constant) => { - const [name, value] = constant.split('=').map((part) => part.trim()); - return { name, value }; - }); - var constantsData : {[key: string]: any} = {}; + const globalConstants = splitConstants(credentials.globalConstants); - const putAllInOneKey = this.getNodeParameter('putAllInOneKey', 0) as boolean; + var constantsData : {[key: string]: any} = {}; - if (putAllInOneKey) { - const constantsKeyName = this.getNodeParameter('constantsKeyName', 0) as string; - const constants : {[key: string]: string} = {}; - globalConstants.forEach(({ name, value }) => { - constants[name] = value; - }); - constantsData = { - [constantsKeyName]: constants, - }; - } else { - // Create a new key for each constant - for (let i = 0; i < globalConstants.length; i++) { - constantsData[globalConstants[i].name] = globalConstants[i].value; - } - } + const putAllInOneKey = this.getNodeParameter('putAllInOneKey', 0) as boolean; - // for each input, add the constants data - const returnData = this.getInputData(); - if (returnData.length === 0) { - // create a new item with the constants data - returnData.push({ json: constantsData }); - } else { - // add the constants data to each item - returnData.forEach((item) => { - item.json = { - ...item.json, - ...constantsData, - }; - }); - } + if (putAllInOneKey) { + const constantsKeyName = this.getNodeParameter('constantsKeyName', 0) as string; + constantsData = { + [constantsKeyName]: globalConstants, + }; + } else { + // Create a new key for each constant + constantsData = globalConstants; - return [returnData]; - } + } + + // for each input, add the constants data + const returnData = this.getInputData(); + if (returnData.length === 0) { + // create a new item with the constants data + returnData.push({ json: constantsData }); + } else { + // add the constants data to each item + returnData.forEach((item) => { + item.json = { + ...item.json, + ...constantsData, + }; + }); + } + + return [returnData]; + } }