diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index d2669b3ca9..219aef44f4 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -1997,7 +1997,7 @@ export function getAdditionalKeys( export async function getCredentials( workflow: Workflow, node: INode, - credType: string | (new () => ICredentialType), + type: string, additionalData: IWorkflowExecuteAdditionalData, mode: WorkflowExecuteMode, executeData?: IExecuteData, @@ -2006,7 +2006,6 @@ export async function getCredentials { - const type = typeof credType === 'string' ? credType : new credType().name; // Get the NodeType as it has the information if the credentials are required const nodeType = workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion); if (nodeType === undefined) { @@ -2933,7 +2932,7 @@ const getCommonWorkflowFunctions = ( workflow: Workflow, node: INode, additionalData: IWorkflowExecuteAdditionalData, -): Omit => ({ +): Omit => ({ logger: Logger, getExecutionId: () => additionalData.executionId!, getNode: () => deepCopy(node), @@ -3473,6 +3472,10 @@ export function copyInputItems(items: INodeExecutionData[], properties: string[] }); } +function credentialClassToType(CredentialType: new () => ICredentialType) { + return new CredentialType().name; +} + /** * Returns the execute functions the poll nodes have access to. */ @@ -3499,10 +3502,16 @@ export function getExecutePollFunctions( }, getMode: () => mode, getActivationMode: () => activation, - getCredentials: async (type: string | (new () => ICredentialType)) => { - const typeString = typeof type === 'string' ? type : type.name; - return await getCredentials(workflow, node, typeString, additionalData, mode); - }, + getCredentials: async (type) => + await getCredentials(workflow, node, type, additionalData, mode), + getCredential: async (CredentialType) => + await getCredentials( + workflow, + node, + credentialClassToType(CredentialType), + additionalData, + mode, + ), getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -3565,8 +3574,16 @@ export function getExecuteTriggerFunctions( }, getMode: () => mode, getActivationMode: () => activation, - getCredentials: async (type: string | (new () => ICredentialType)) => + getCredentials: async (type) => await getCredentials(workflow, node, type, additionalData, mode), + getCredential: async (CredentialType) => + await getCredentials( + workflow, + node, + credentialClassToType(CredentialType), + additionalData, + mode, + ), getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -3625,7 +3642,7 @@ export function getExecuteFunctions( ...getCommonWorkflowFunctions(workflow, node, additionalData), ...executionCancellationFunctions(abortSignal), getMode: () => mode, - getCredentials: async (type: string | (new () => ICredentialType), itemIndex: number) => + getCredentials: async (type, itemIndex) => await getCredentials( workflow, node, @@ -3638,6 +3655,19 @@ export function getExecuteFunctions( connectionInputData, itemIndex, ), + getCredential: async (CredentialType, itemIndex) => + await getCredentials( + workflow, + node, + credentialClassToType(CredentialType), + additionalData, + mode, + executeData, + runExecutionData, + runIndex, + connectionInputData, + itemIndex, + ), getExecuteData: () => executeData, continueOnFail: () => { return continueOnFail(node); @@ -3977,7 +4007,7 @@ export function getExecuteSingleFunctions( getContext(type: ContextType): IContextObject { return NodeHelpers.getContext(runExecutionData, type, node); }, - getCredentials: async (type: string | (new () => ICredentialType)) => + getCredentials: async (type) => await getCredentials( workflow, node, @@ -3990,6 +4020,19 @@ export function getExecuteSingleFunctions( connectionInputData, itemIndex, ), + getCredential: async (CredentialType, itemIndex) => + await getCredentials( + workflow, + node, + credentialClassToType(CredentialType), + additionalData, + mode, + executeData, + runExecutionData, + runIndex, + connectionInputData, + itemIndex, + ), getInputData: (inputIndex = 0, inputName = 'main') => { if (!inputData.hasOwnProperty(inputName)) { // Return empty array because else it would throw error when nothing is connected to input @@ -4117,8 +4160,16 @@ export function getLoadOptionsFunctions( return ((workflow: Workflow, node: INode, path: string) => { return { ...getCommonWorkflowFunctions(workflow, node, additionalData), - getCredentials: async (type: string | (new () => ICredentialType)) => + getCredentials: async (type) => await getCredentials(workflow, node, type, additionalData, 'internal'), + getCredential: async (CredentialType) => + await getCredentials( + workflow, + node, + credentialClassToType(CredentialType), + additionalData, + 'internal', + ), getCurrentNodeParameter: ( parameterPath: string, options?: IGetNodeParameterOptions, @@ -4198,8 +4249,16 @@ export function getExecuteHookFunctions( return ((workflow: Workflow, node: INode) => { return { ...getCommonWorkflowFunctions(workflow, node, additionalData), - getCredentials: async (type: string | (new () => ICredentialType)) => + getCredentials: async (type) => await getCredentials(workflow, node, type, additionalData, mode), + getCredential: async (CredentialType) => + await getCredentials( + workflow, + node, + credentialClassToType(CredentialType), + additionalData, + mode, + ), getMode: () => mode, getActivationMode: () => activation, getNodeParameter: ( @@ -4272,8 +4331,16 @@ export function getExecuteWebhookFunctions( } return additionalData.httpRequest.body; }, - getCredentials: async (type: string | (new () => ICredentialType)) => + getCredentials: async (type) => await getCredentials(workflow, node, type, additionalData, mode), + getCredential: async (CredentialType) => + await getCredentials( + workflow, + node, + credentialClassToType(CredentialType), + additionalData, + mode, + ), getHeaderData(): IncomingHttpHeaders { if (additionalData.httpRequest === undefined) { throw new ApplicationError('Request is missing'); diff --git a/packages/nodes-base/nodes/Strapi/GenericFunctions.ts b/packages/nodes-base/nodes/Strapi/GenericFunctions.ts index 5996ef2e3d..6d2d464b2c 100644 --- a/packages/nodes-base/nodes/Strapi/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Strapi/GenericFunctions.ts @@ -30,7 +30,7 @@ export async function strapiApiRequest( headers: IDataObject = {}, ) { const authenticationMethod = this.getNodeParameter('authentication', 0); - const credentials = await this.getCredentials( + const credentials = await this.getCredential( authenticationMethod === 'password' ? StrapiApi : StrapiTokenApi, ); diff --git a/packages/nodes-base/nodes/Strapi/Strapi.node.ts b/packages/nodes-base/nodes/Strapi/Strapi.node.ts index 86c8058f9c..1175de355a 100644 --- a/packages/nodes-base/nodes/Strapi/Strapi.node.ts +++ b/packages/nodes-base/nodes/Strapi/Strapi.node.ts @@ -149,10 +149,10 @@ export class Strapi implements INodeType { if (authenticationMethod === 'password') { const { jwt } = await getToken.call(this); - apiVersion = (await this.getCredentials(StrapiApi)).apiVersion; + apiVersion = (await this.getCredential(StrapiApi)).apiVersion; headers.Authorization = `Bearer ${jwt}`; } else { - apiVersion = (await this.getCredentials(StrapiTokenApi)).apiVersion; + apiVersion = (await this.getCredential(StrapiTokenApi)).apiVersion; } for (let i = 0; i < length; i++) { diff --git a/packages/workflow/src/CredentialSchema/CredentialSchema.ts b/packages/workflow/src/CredentialSchema/CredentialSchema.ts index 4841b9ad96..ed9a0bdebc 100644 --- a/packages/workflow/src/CredentialSchema/CredentialSchema.ts +++ b/packages/workflow/src/CredentialSchema/CredentialSchema.ts @@ -183,6 +183,8 @@ type NumberMetadata = BaseMetadata & default: number; }>; +type PasswordMetadata = Omit, 'password'>; + type Option = { label: string; value: V; @@ -224,7 +226,7 @@ export const CredentialSchema = { return new CredentialSchemaRootObject(shape); }, - password(options: Omit, 'password'> = {}) { + password(options: M = {} as M) { return new CredentialSchemaString( { password: true, @@ -234,18 +236,20 @@ export const CredentialSchema = { z.string(), ); }, + // eslint-disable-next-line id-denylist string(options: M) { return new CredentialSchemaString(options, z.string()); }, + // eslint-disable-next-line id-denylist number(options: M) { return new CredentialSchemaNumber(options, z.number()); }, - url(options: Optional = {}) { + url>(options: M) { return new CredentialSchemaString({ label: 'URL', ...options }, z.string().url()); }, - email(options: Optional = {}) { + email>(options: M) { return new CredentialSchemaString({ label: 'Email', ...options }, z.string().email()); }, options>(options: M) { diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index c6a90653b4..4d831b9355 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -863,11 +863,14 @@ export type NodeTypeAndVersion = { export interface FunctionsBase { logger: Logger; + /** + * @deprecated Use getCredential instead for better type safety + * */ getCredentials( type: string, itemIndex?: number, ): Promise; - getCredentials ICredentialType>( + getCredential ICredentialType>( type: T, itemIndex?: number, ): Promise<