fix: Fix template credential setup for nodes that dont have credentials (#8208)

Fix template credential setup for templates whose workflow includes
nodes that require credentials but the workflow definition does not have
them defined. Like for example
https://n8n.io/workflows/1344-save-email-attachments-to-nextcloud/
This commit is contained in:
Tomi Turtiainen
2024-01-04 10:21:36 +02:00
committed by GitHub
parent 4186884740
commit cd3f5b5b1f
15 changed files with 5596 additions and 348 deletions

View File

@@ -0,0 +1,33 @@
import type { INodeUi } from '@/Interface';
import type { NodeTypeProvider } from '@/utils/nodeTypes/nodeTypeTransforms';
import type { INodeCredentialDescription } from 'n8n-workflow';
import { NodeHelpers } from 'n8n-workflow';
/**
* Returns the credentials that are displayable for the given node.
*/
export function getNodeTypeDisplayableCredentials(
nodeTypeProvider: NodeTypeProvider,
node: Pick<INodeUi, 'parameters' | 'type' | 'typeVersion'>,
): INodeCredentialDescription[] {
const nodeType = nodeTypeProvider.getNodeType(node.type, node.typeVersion);
if (!nodeType?.credentials) {
return [];
}
const nodeTypeCreds = nodeType.credentials;
// We must populate the node's parameters with the default values
// before we can check which credentials are available, because
// credentials can have conditional requirements that depend on
// node parameters.
const nodeParameters =
NodeHelpers.getNodeParameters(nodeType.properties, node.parameters, true, false, node) ??
node.parameters;
const displayableCredentials = nodeTypeCreds.filter((credentialTypeDescription) => {
return NodeHelpers.displayParameter(nodeParameters, credentialTypeDescription, node);
});
return displayableCredentials;
}