Files
n8n-enterprise-unlocked/packages/frontend/editor-ui/src/utils/nodes/nodeTransforms.ts

89 lines
2.6 KiB
TypeScript

import type { INodeUi } from '@/Interface';
import type { NodeTypeProvider } from '@/utils/nodeTypes/nodeTypeTransforms';
import type { INodeCredentialDescription, FromAIArgument } from 'n8n-workflow';
import { NodeHelpers, traverseNodeParameters } 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,
nodeType,
) ?? node.parameters;
const displayableCredentials = nodeTypeCreds.filter((credentialTypeDescription) => {
return NodeHelpers.displayParameter(nodeParameters, credentialTypeDescription, node, nodeType);
});
return displayableCredentials;
}
/**
* Checks if the given node has credentials that can be filled.
*/
export function doesNodeHaveCredentialsToFill(
nodeTypeProvider: NodeTypeProvider,
node: Pick<INodeUi, 'parameters' | 'type' | 'typeVersion'>,
): boolean {
const requiredCredentials = getNodeTypeDisplayableCredentials(nodeTypeProvider, node);
return requiredCredentials.length > 0;
}
/**
* Does node has the given credential filled
*
* @param credentialName E.g. "telegramApi"
*/
export function hasNodeCredentialFilled(
node: Pick<INodeUi, 'credentials'>,
credentialName: string,
): boolean {
if (!node.credentials) {
return false;
}
return !!node.credentials[credentialName];
}
/**
* Checks if the given node has all credentials filled.
*/
export function doesNodeHaveAllCredentialsFilled(
nodeTypeProvider: NodeTypeProvider,
node: Pick<INodeUi, 'parameters' | 'type' | 'typeVersion' | 'credentials'>,
): boolean {
const requiredCredentials = getNodeTypeDisplayableCredentials(nodeTypeProvider, node);
return requiredCredentials.every((cred) => hasNodeCredentialFilled(node, cred.name));
}
/**
* Checks if the given node has any fromAi expressions in its parameters.
*/
export function hasFromAiExpressions(node: Pick<INodeUi, 'parameters'>) {
const collectedArgs: FromAIArgument[] = [];
traverseNodeParameters(node.parameters, collectedArgs);
return collectedArgs.length > 0;
}