mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat: Add once for each item support for JS task runner (no-changelog) (#11109)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { EventEmitter } from 'events';
|
||||
import type { IExecuteFunctions, INodeExecutionData, IWorkflowDataProxyData } from 'n8n-workflow';
|
||||
import { ValidationError } from './ValidationError';
|
||||
|
||||
import { isObject } from './utils';
|
||||
import { ValidationError } from './ValidationError';
|
||||
|
||||
interface SandboxTextKeys {
|
||||
object: {
|
||||
@@ -39,26 +40,28 @@ export function getSandboxContext(this: IExecuteFunctions, index: number): Sandb
|
||||
export abstract class Sandbox extends EventEmitter {
|
||||
constructor(
|
||||
private textKeys: SandboxTextKeys,
|
||||
protected itemIndex: number | undefined,
|
||||
protected helpers: IExecuteFunctions['helpers'],
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
abstract runCode(): Promise<unknown>;
|
||||
abstract runCode<T = unknown>(): Promise<T>;
|
||||
|
||||
abstract runCodeAllItems(): Promise<INodeExecutionData[] | INodeExecutionData[][]>;
|
||||
|
||||
abstract runCodeEachItem(): Promise<INodeExecutionData | undefined>;
|
||||
abstract runCodeEachItem(itemIndex: number): Promise<INodeExecutionData | undefined>;
|
||||
|
||||
validateRunCodeEachItem(executionResult: INodeExecutionData | undefined): INodeExecutionData {
|
||||
validateRunCodeEachItem(
|
||||
executionResult: INodeExecutionData | undefined,
|
||||
itemIndex: number,
|
||||
): INodeExecutionData {
|
||||
if (typeof executionResult !== 'object') {
|
||||
throw new ValidationError({
|
||||
message: `Code doesn't return ${this.getTextKey('object', { includeArticle: true })}`,
|
||||
description: `Please return ${this.getTextKey('object', {
|
||||
includeArticle: true,
|
||||
})} representing the output item. ('${executionResult}' was returned instead.)`,
|
||||
itemIndex: this.itemIndex,
|
||||
itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,25 +73,24 @@ export abstract class Sandbox extends EventEmitter {
|
||||
throw new ValidationError({
|
||||
message: `Code doesn't return a single ${this.getTextKey('object')}`,
|
||||
description: `${firstSentence} If you need to output multiple items, please use the 'Run Once for All Items' mode instead.`,
|
||||
itemIndex: this.itemIndex,
|
||||
itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
const [returnData] = this.helpers.normalizeItems([executionResult]);
|
||||
|
||||
this.validateItem(returnData);
|
||||
this.validateItem(returnData, itemIndex);
|
||||
|
||||
// If at least one top-level key is a supported item key (`json`, `binary`, etc.),
|
||||
// and another top-level key is unrecognized, then the user mis-added a property
|
||||
// directly on the item, when they intended to add it on the `json` property
|
||||
this.validateTopLevelKeys(returnData);
|
||||
this.validateTopLevelKeys(returnData, itemIndex);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
|
||||
validateRunCodeAllItems(
|
||||
executionResult: INodeExecutionData | INodeExecutionData[] | undefined,
|
||||
itemIndex?: number,
|
||||
): INodeExecutionData[] {
|
||||
if (typeof executionResult !== 'object') {
|
||||
throw new ValidationError({
|
||||
@@ -96,7 +98,6 @@ export abstract class Sandbox extends EventEmitter {
|
||||
description: `Please return an array of ${this.getTextKey('object', {
|
||||
plural: true,
|
||||
})}, one for each item you would like to output.`,
|
||||
itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -113,14 +114,15 @@ export abstract class Sandbox extends EventEmitter {
|
||||
);
|
||||
|
||||
if (mustHaveTopLevelN8nKey) {
|
||||
for (const item of executionResult) {
|
||||
this.validateTopLevelKeys(item);
|
||||
for (let index = 0; index < executionResult.length; index++) {
|
||||
const item = executionResult[index];
|
||||
this.validateTopLevelKeys(item, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const returnData = this.helpers.normalizeItems(executionResult);
|
||||
returnData.forEach((item) => this.validateItem(item));
|
||||
returnData.forEach((item, index) => this.validateItem(item, index));
|
||||
return returnData;
|
||||
}
|
||||
|
||||
@@ -138,7 +140,7 @@ export abstract class Sandbox extends EventEmitter {
|
||||
return `a ${response}`;
|
||||
}
|
||||
|
||||
private validateItem({ json, binary }: INodeExecutionData) {
|
||||
private validateItem({ json, binary }: INodeExecutionData, itemIndex: number) {
|
||||
if (json === undefined || !isObject(json)) {
|
||||
throw new ValidationError({
|
||||
message: `A 'json' property isn't ${this.getTextKey('object', { includeArticle: true })}`,
|
||||
@@ -146,7 +148,7 @@ export abstract class Sandbox extends EventEmitter {
|
||||
'object',
|
||||
{ includeArticle: true },
|
||||
)}.`,
|
||||
itemIndex: this.itemIndex,
|
||||
itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -157,18 +159,18 @@ export abstract class Sandbox extends EventEmitter {
|
||||
'object',
|
||||
{ includeArticle: true },
|
||||
)}.`,
|
||||
itemIndex: this.itemIndex,
|
||||
itemIndex,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private validateTopLevelKeys(item: INodeExecutionData) {
|
||||
private validateTopLevelKeys(item: INodeExecutionData, itemIndex: number) {
|
||||
Object.keys(item).forEach((key) => {
|
||||
if (REQUIRED_N8N_ITEM_KEYS.has(key)) return;
|
||||
throw new ValidationError({
|
||||
message: `Unknown top-level item key: ${key}`,
|
||||
description: 'Access the properties of an item under `.json`, e.g. `item.json`',
|
||||
itemIndex: this.itemIndex,
|
||||
itemIndex,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user