Skip to content

Commit

Permalink
fix: allow "=" symbols in constant value
Browse files Browse the repository at this point in the history
  • Loading branch information
umanamente committed Aug 16, 2024
1 parent 0d74983 commit 39e9b9f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 79 deletions.
19 changes: 19 additions & 0 deletions credentials/CredentialsUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
152 changes: 73 additions & 79 deletions nodes/GlobalConstants/GlobalConstants.node.ts
Original file line number Diff line number Diff line change
@@ -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<INodeExecutionData[][] > {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][] > {
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];
}
}

0 comments on commit 39e9b9f

Please sign in to comment.