Skip to content

Commit

Permalink
Improve return type of code node
Browse files Browse the repository at this point in the history
  • Loading branch information
elsmr committed Dec 20, 2024
1 parent 4fe1f7c commit d8e87d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ import type { DateTime } from 'luxon';
export {};

declare global {
type OutputItemWithoutJsonKey = {
[key: string]: unknown;
} & { json?: never };

type OutputItemWithJsonKey = {
json: {
[key: string]: unknown;
};
};

type MaybePromise<T> = Promise<T> | T;

type OneOutputItem = OutputItemWithJsonKey | OutputItemWithoutJsonKey;
type AllOutputItems = OneOutputItem | Array<OneOutputItem>;

type N8nOutputItem = MaybePromise<OneOutputItem>;
type N8nOutputItems = MaybePromise<AllOutputItems>;

interface N8nJson {
[key: string]: any;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,5 @@ declare global {
}

export function returnTypeForMode(mode: CodeExecutionMode): string {
const returnItem = '{ json: { [key: string]: unknown } } | { [key: string]: unknown }';
if (mode === 'runOnceForAllItems') {
return `Promise<Array<${returnItem}>> | Array<${returnItem}>`;
}

return `Promise<${returnItem}> | ${returnItem}`;
return mode === 'runOnceForAllItems' ? 'N8nOutputItems' : 'N8nOutputItem';
}

0 comments on commit d8e87d3

Please sign in to comment.