UNPKG

n8n-nodes-variables

Version:

n8n community node for managing typed global variables accessible from any workflow stage - like programming language variables with strong typing support

135 lines 5.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GlobalVariables = void 0; class GlobalVariables { constructor() { this.description = { displayName: 'Global Variables', name: 'globalVariables', icon: 'fa:arrows-alt-h', group: ['utility'], version: 1, description: 'Sets or retrieves workflow-scoped global variables using workflowStaticData', defaults: { name: 'Global Variables', }, inputs: ["main"], outputs: ["main"], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', options: [ { name: 'Set Variable', value: 'set', description: 'Store a value in a global variable', action: 'Store a value in a global variable', }, { name: 'Get Variable', value: 'get', description: 'Retrieve a value from a global variable', action: 'Retrieve a value from a global variable', }, ], default: 'set', noDataExpression: true, required: true, }, { displayName: 'Variable Name', name: 'variableName', type: 'string', default: '', required: true, placeholder: 'e.g., myCounter or user.ID', description: 'The unique name for the global variable (case-sensitive)', }, { displayName: 'Value', name: 'value', type: 'json', default: '', displayOptions: { show: { operation: ['set'], }, }, placeholder: 'Enter value or use an expression', description: 'The value to store. Can be a simple value or a JSON structure (use expressions for objects/arrays).', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const length = items.length; const staticData = this.getWorkflowStaticData('global'); for (let i = 0; i < length; i++) { try { const operation = this.getNodeParameter('operation', i, 'set'); const variableName = this.getNodeParameter('variableName', i, ''); if (!variableName) { throw new Error('Variable Name cannot be empty.'); } if (operation === 'set') { let valueToStore = this.getNodeParameter('value', i); if (typeof valueToStore === 'string') { try { const parsed = JSON.parse(valueToStore); if (typeof parsed !== 'number' || valueToStore.trim() !== String(parsed)) { valueToStore = parsed; } } catch (e) { } } staticData[variableName] = valueToStore; const newItem = { json: { ...items[i].json, _globalVariableSet: { name: variableName, value: valueToStore, status: 'success', }, }, pairedItem: items[i].pairedItem, }; returnData.push(newItem); } else if (operation === 'get') { const retrievedValue = staticData[variableName]; const newItem = { json: { ...items[i].json, [variableName]: retrievedValue, }, pairedItem: items[i].pairedItem, }; returnData.push(newItem); } } catch (error) { if (this.continueOnFail()) { const errorItem = { json: { error: error.message, originalItem: items[i].json, }, pairedItem: items[i].pairedItem, }; returnData.push(errorItem); continue; } throw error; } } return [returnData]; } } exports.GlobalVariables = GlobalVariables; //# sourceMappingURL=GlobalVariables.node.js.map