mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
⚡ Tolerate missing json key in function node output (#2885)
* 🔨 implemented Tolerate missing json key in function node output * 🔨 clean up * Small change to code * ⚡ tolerate returning object * ⚡ Rename function Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
@@ -1132,6 +1132,51 @@ export function returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExe
|
||||
return returnData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically put the objects under a 'json' key and don't error,
|
||||
* if some objects contain json/binary keys and others don't, throws error 'Inconsistent item format'
|
||||
*
|
||||
* @export
|
||||
* @param {INodeExecutionData | INodeExecutionData[]} executionData
|
||||
* @returns {INodeExecutionData[]}
|
||||
*/
|
||||
export function normalizeItems(
|
||||
executionData: INodeExecutionData | INodeExecutionData[],
|
||||
): INodeExecutionData[] {
|
||||
if (typeof executionData === 'object' && !Array.isArray(executionData))
|
||||
executionData = [{ json: executionData as IDataObject }];
|
||||
if (executionData.every((item) => typeof item === 'object' && 'json' in item))
|
||||
return executionData;
|
||||
|
||||
if (executionData.some((item) => typeof item === 'object' && 'json' in item)) {
|
||||
throw new Error('Inconsistent item format');
|
||||
}
|
||||
|
||||
if (executionData.every((item) => typeof item === 'object' && 'binary' in item)) {
|
||||
const normalizedItems: INodeExecutionData[] = [];
|
||||
executionData.forEach((item) => {
|
||||
const json = Object.keys(item).reduce((acc, key) => {
|
||||
if (key === 'binary') return acc;
|
||||
return { ...acc, [key]: item[key] };
|
||||
}, {});
|
||||
|
||||
normalizedItems.push({
|
||||
json,
|
||||
binary: item.binary,
|
||||
});
|
||||
});
|
||||
return normalizedItems;
|
||||
}
|
||||
|
||||
if (executionData.some((item) => typeof item === 'object' && 'binary' in item)) {
|
||||
throw new Error('Inconsistent item format');
|
||||
}
|
||||
|
||||
return executionData.map((item) => {
|
||||
return { json: item };
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Move up later
|
||||
export async function requestWithAuthentication(
|
||||
this: IAllExecuteFunctions,
|
||||
@@ -2080,6 +2125,7 @@ export function getExecuteFunctions(
|
||||
);
|
||||
},
|
||||
returnJsonArray,
|
||||
normalizeItems,
|
||||
},
|
||||
};
|
||||
})(workflow, runExecutionData, connectionInputData, inputData, node);
|
||||
|
||||
Reference in New Issue
Block a user