mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-27 13:39:44 -08:00
698d96a617
* refactor: use consistent folder structure across workflow, core, and cli * setup typescript project references across workflow, core, and cli
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
/* eslint-disable no-prototype-builtins */
|
|
import { INode, IWorkflowCredentials } from 'n8n-workflow';
|
|
import * as Db from '@/Db';
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
export async function WorkflowCredentials(nodes: INode[]): Promise<IWorkflowCredentials> {
|
|
// Go through all nodes to find which credentials are needed to execute the workflow
|
|
const returnCredentials: IWorkflowCredentials = {};
|
|
|
|
let node;
|
|
let type;
|
|
let nodeCredentials;
|
|
let foundCredentials;
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
for (node of nodes) {
|
|
if (node.disabled === true || !node.credentials) {
|
|
// eslint-disable-next-line no-continue
|
|
continue;
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
for (type of Object.keys(node.credentials)) {
|
|
if (!returnCredentials[type]) {
|
|
returnCredentials[type] = {};
|
|
}
|
|
nodeCredentials = node.credentials[type];
|
|
|
|
if (!nodeCredentials.id) {
|
|
throw new Error(
|
|
`Credentials with name "${nodeCredentials.name}" for type "${type}" miss an ID.`,
|
|
);
|
|
}
|
|
|
|
if (!returnCredentials[type][nodeCredentials.id]) {
|
|
// eslint-disable-next-line no-await-in-loop
|
|
foundCredentials = await Db.collections.Credentials.findOne({
|
|
id: nodeCredentials.id,
|
|
type,
|
|
});
|
|
if (!foundCredentials) {
|
|
throw new Error(
|
|
`Could not find credentials for type "${type}" with ID "${nodeCredentials.id}".`,
|
|
);
|
|
}
|
|
// eslint-disable-next-line prefer-destructuring
|
|
returnCredentials[type][nodeCredentials.id] = foundCredentials;
|
|
}
|
|
}
|
|
}
|
|
|
|
return returnCredentials;
|
|
}
|