Skip to content

Commit 3f0034b

Browse files
authored
Merge pull request #13 from juanmacebal/add-json-support
added support for JSON format in global constants input
2 parents 8b80186 + 6e720e6 commit 3f0034b

File tree

7 files changed

+72
-22
lines changed

7 files changed

+72
-22
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ This node uses N8N Credential to store the global variables.
1717
1. Add "`Global Constants`" node to your workflow.
1818
![Global Constants node](./docs/images/usage/1_select_node.png)
1919
2. In the node configuration, either select an existing credential or create a new one.
20-
3. Add the key-value pairs you want to use as global constants.
21-
![Define constants](./docs/images/usage/2_define_constants.png)
22-
4. Use the global constants in your workflow
23-
![Use constants](./docs/images/usage/3_use_node.png)
24-
20+
3. Select the format for your global constants:
21+
- Key-value pairs format: Use "name=value" pairs, one per line
22+
- JSON format: Provide constants as a JSON object
23+
4. Add your constants according to the chosen format:
24+
25+
KeyKey-value pairs
26+
![Define constants String](./docs/images/usage/2_define_constants_string.png)
27+
28+
JSON
29+
![Define constants](./docs/images/usage/2_define_constants_json.png)
30+
5. Use the global constants in your workflow
31+
![Use constants](./docs/images/usage/3_use_node.png)
2532

credentials/CredentialsUtils.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
export function splitConstants(globalConstantsMultiline: string): { [key: string]: string } {
2-
const lines = globalConstantsMultiline.split('\n');
3-
var retArr: { [key: string]: string } = {};
4-
for (const line of lines) {
5-
// trim the line
6-
const constant = line.trim();
7-
if (!constant) {
8-
continue;
2+
3+
// Check if the string is a JSON object
4+
try {
5+
const jsonObj = JSON.parse(globalConstantsMultiline.trim());
6+
return jsonObj as { [key: string]: string };
7+
} catch (e) {
8+
// Not a JSON object, continue with the old logic
9+
const lines = globalConstantsMultiline.split('\n');
10+
var retArr: { [key: string]: string } = {};
11+
for (const line of lines) {
12+
// trim the line
13+
const constant = line.trim();
14+
if (!constant) {
15+
continue;
16+
}
17+
// skip if it doesn't contain "="
18+
if (!constant.includes('=')) {
19+
continue;
20+
}
21+
// split only first "=" to allow values with "=" in them
22+
const [name, ...value] = constant.split('=');
23+
retArr[name.trim()] = value.join('=').trim();
924
}
10-
// skip if it doesn't contain "="
11-
if (!constant.includes('=')) {
12-
continue;
13-
}
14-
// split only first "=" to allow values with "=" in them
15-
const [name, ...value] = constant.split('=');
16-
retArr[name.trim()] = value.join('=').trim();
25+
return retArr;
1726
}
18-
return retArr;
1927
}

credentials/GlobalConstantsCredentials.credentials.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ export class GlobalConstantsCredentials implements ICredentialType {
99
displayName = 'Global Constants';
1010

1111
properties: INodeProperties[] = [
12+
{
13+
displayName: 'Format',
14+
name: 'format',
15+
type: 'options',
16+
options: [
17+
{
18+
name: 'Key-value pairs',
19+
value: 'string',
20+
},
21+
{
22+
name: 'JSON',
23+
value: 'json',
24+
},
25+
],
26+
default: 'string',
27+
description: 'Choose the format for your global constants',
28+
},
1229
{
1330
displayName: 'Global Constants',
1431
name: 'globalConstants',
@@ -19,10 +36,28 @@ export class GlobalConstantsCredentials implements ICredentialType {
1936
typeOptions: {
2037
rows: 10,
2138
},
39+
displayOptions: {
40+
show: {
41+
format: ['string'],
42+
},
43+
},
44+
},
45+
{
46+
displayName: 'Global Constants',
47+
name: 'globalConstants',
48+
type: 'json',
49+
default: '{}',
50+
hint: 'Provide your constants as a JSON object. Example: { "obj": { "key": "value" }, "list": ["value1", "value2"] }',
51+
displayOptions: {
52+
show: {
53+
format: ['json'],
54+
},
55+
},
2256
},
2357
];
2458
}
2559

2660
export interface GlobalConstantsCredentialsData {
61+
format: 'string' | 'json';
2762
globalConstants: string;
2863
}
-39.3 KB
Binary file not shown.
144 KB
Loading
143 KB
Loading

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)