generated from n8n-io/n8n-nodes-starter
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow "=" symbols in constant value
- Loading branch information
1 parent
0d74983
commit 39e9b9f
Showing
2 changed files
with
92 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
|