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
330 lines • 13.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VariablesDashboard = void 0;
function getVariableType(value) {
if (value === null)
return 'null';
if (value === undefined)
return 'undefined';
if (Array.isArray(value))
return 'array';
if (typeof value === 'object')
return 'object';
return typeof value;
}
function processValueByType(value, type) {
switch (type) {
case 'string':
return String(value);
case 'number':
const num = Number(value);
if (isNaN(num))
throw new Error(`Cannot convert "${value}" to number`);
return num;
case 'boolean':
if (typeof value === 'boolean')
return value;
if (typeof value === 'string') {
const lower = value.toLowerCase();
if (lower === 'true')
return true;
if (lower === 'false')
return false;
}
throw new Error(`Cannot convert "${value}" to boolean`);
case 'object':
case 'array':
if (typeof value === 'string') {
try {
return JSON.parse(value);
}
catch (e) {
throw new Error(`Invalid JSON: ${e.message}`);
}
}
return value;
default:
return value;
}
}
function sortVariables(variables, sortType) {
switch (sortType) {
case 'nameAsc':
variables.sort((a, b) => a.name.localeCompare(b.name));
break;
case 'nameDesc':
variables.sort((a, b) => b.name.localeCompare(a.name));
break;
case 'type':
variables.sort((a, b) => {
if (a.type === b.type)
return a.name.localeCompare(b.name);
return a.type.localeCompare(b.type);
});
break;
case 'none':
default:
break;
}
}
function createSummary(variables) {
const summary = {
totalCount: variables.length,
byType: {},
};
variables.forEach(variable => {
const type = variable.type || 'unknown';
const typeCount = summary.byType[type] || 0;
summary.byType[type] = typeCount + 1;
});
return summary;
}
class VariablesDashboard {
constructor() {
this.description = {
displayName: 'Variables Dashboard',
name: 'variablesDashboard',
icon: 'fa:table',
group: ['utility'],
version: 1,
description: 'View and manage all workflow global variables in a dashboard table format',
defaults: {
name: 'Variables Dashboard',
},
inputs: [],
outputs: ["main"],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'View All Variables',
value: 'viewAll',
description: 'Display all current global variables in a table',
action: 'Display all current global variables in a table',
},
{
name: 'Bulk Set Variables',
value: 'bulkSet',
description: 'Set multiple variables at once from a table format',
action: 'Set multiple variables at once from a table format',
},
{
name: 'Clear All Variables',
value: 'clearAll',
description: 'Remove all global variables',
action: 'Remove all global variables',
},
],
default: 'viewAll',
noDataExpression: true,
},
{
displayName: 'Variables Table',
name: 'variablesTable',
type: 'fixedCollection',
default: { variables: [] },
displayOptions: {
show: {
operation: ['bulkSet'],
},
},
placeholder: 'Add Variable',
typeOptions: {
multipleValues: true,
sortable: true,
},
options: [
{
name: 'variables',
displayName: 'Variable',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
placeholder: 'variableName',
description: 'The name of the variable',
required: true,
},
{
displayName: 'Value',
name: 'value',
type: 'json',
default: '',
placeholder: 'Variable value',
description: 'The value to store (string, number, boolean, object, or array)',
},
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{ name: 'String', value: 'string' },
{ name: 'Number', value: 'number' },
{ name: 'Boolean', value: 'boolean' },
{ name: 'Object', value: 'object' },
{ name: 'Array', value: 'array' },
],
default: 'string',
description: 'The type of the variable for proper parsing',
},
],
},
],
description: 'Define multiple variables in a table format',
},
{
displayName: 'Confirm Clear All',
name: 'confirmClear',
type: 'boolean',
default: false,
displayOptions: {
show: {
operation: ['clearAll'],
},
},
description: 'Confirm that you want to delete ALL global variables',
},
{
displayName: 'Display Options',
name: 'displayOptions',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Show Variable Types',
name: 'showTypes',
type: 'boolean',
default: true,
description: 'Include variable types in the output',
},
{
displayName: 'Format JSON',
name: 'formatJson',
type: 'boolean',
default: true,
description: 'Pretty-format JSON values for better readability',
},
{
displayName: 'Sort Variables',
name: 'sortVariables',
type: 'options',
options: [
{ name: 'Name (A-Z)', value: 'nameAsc' },
{ name: 'Name (Z-A)', value: 'nameDesc' },
{ name: 'Type', value: 'type' },
{ name: 'None', value: 'none' },
],
default: 'nameAsc',
description: 'How to sort the variables in the output',
},
],
},
],
};
}
async execute() {
const operation = this.getNodeParameter('operation', 0);
const staticData = this.getWorkflowStaticData('global');
const displayOptions = this.getNodeParameter('displayOptions', 0, {});
const showTypes = displayOptions.showTypes !== false;
const formatJson = displayOptions.formatJson !== false;
const sortType = displayOptions.sortVariables || 'nameAsc';
let returnData = [];
if (operation === 'viewAll') {
const variables = Object.keys(staticData).map(key => {
const value = staticData[key];
const type = getVariableType(value);
return {
name: key,
value: formatJson && (type === 'object' || type === 'array')
? JSON.stringify(value, null, 2)
: value,
type: showTypes ? type : undefined,
rawValue: value,
};
});
sortVariables(variables, sortType);
const dashboardData = {
dashboard: 'Global Variables',
totalVariables: variables.length,
variables: variables,
variablesSummary: createSummary(variables),
lastUpdated: new Date().toISOString(),
};
returnData.push({
json: dashboardData,
});
}
else if (operation === 'bulkSet') {
const variablesTable = this.getNodeParameter('variablesTable', 0, { variables: [] });
const setResults = [];
for (const variable of variablesTable.variables) {
try {
if (!variable.name) {
setResults.push({
name: variable.name || 'unnamed',
value: variable.value,
status: 'error',
error: 'Variable name is required',
});
continue;
}
let processedValue = processValueByType(variable.value, variable.type);
staticData[variable.name] = processedValue;
setResults.push({
name: variable.name,
value: processedValue,
status: 'success',
});
}
catch (error) {
setResults.push({
name: variable.name || 'unnamed',
value: variable.value,
status: 'error',
error: error.message,
});
}
}
returnData.push({
json: {
operation: 'bulkSet',
variablesProcessed: setResults.length,
successCount: setResults.filter(r => r.status === 'success').length,
errorCount: setResults.filter(r => r.status === 'error').length,
results: setResults,
timestamp: new Date().toISOString(),
},
});
}
else if (operation === 'clearAll') {
const confirmClear = this.getNodeParameter('confirmClear', 0, false);
if (!confirmClear) {
throw new Error('Please confirm that you want to clear all variables by checking the confirmation checkbox');
}
const variableNames = Object.keys(staticData);
const clearedCount = variableNames.length;
for (const key of variableNames) {
delete staticData[key];
}
returnData.push({
json: {
operation: 'clearAll',
clearedVariables: variableNames,
clearedCount: clearedCount,
timestamp: new Date().toISOString(),
message: `Successfully cleared ${clearedCount} variables`,
},
});
}
return [returnData];
}
}
exports.VariablesDashboard = VariablesDashboard;
//# sourceMappingURL=VariablesDashboard.node.js.map