Create new method instead of overload

This commit is contained in:
Elias Meire 2024-10-08 11:08:16 +02:00
parent 1853069724
commit 69fc10bfdd
No known key found for this signature in database
5 changed files with 94 additions and 20 deletions

View file

@ -1997,7 +1997,7 @@ export function getAdditionalKeys(
export async function getCredentials<T extends object = ICredentialDataDecryptedObject>( export async function getCredentials<T extends object = ICredentialDataDecryptedObject>(
workflow: Workflow, workflow: Workflow,
node: INode, node: INode,
credType: string | (new () => ICredentialType), type: string,
additionalData: IWorkflowExecuteAdditionalData, additionalData: IWorkflowExecuteAdditionalData,
mode: WorkflowExecuteMode, mode: WorkflowExecuteMode,
executeData?: IExecuteData, executeData?: IExecuteData,
@ -2006,7 +2006,6 @@ export async function getCredentials<T extends object = ICredentialDataDecrypted
connectionInputData?: INodeExecutionData[], connectionInputData?: INodeExecutionData[],
itemIndex?: number, itemIndex?: number,
): Promise<T> { ): Promise<T> {
const type = typeof credType === 'string' ? credType : new credType().name;
// Get the NodeType as it has the information if the credentials are required // Get the NodeType as it has the information if the credentials are required
const nodeType = workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion); const nodeType = workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion);
if (nodeType === undefined) { if (nodeType === undefined) {
@ -2933,7 +2932,7 @@ const getCommonWorkflowFunctions = (
workflow: Workflow, workflow: Workflow,
node: INode, node: INode,
additionalData: IWorkflowExecuteAdditionalData, additionalData: IWorkflowExecuteAdditionalData,
): Omit<FunctionsBase, 'getCredentials'> => ({ ): Omit<FunctionsBase, 'getCredentials' | 'getCredential'> => ({
logger: Logger, logger: Logger,
getExecutionId: () => additionalData.executionId!, getExecutionId: () => additionalData.executionId!,
getNode: () => deepCopy(node), 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. * Returns the execute functions the poll nodes have access to.
*/ */
@ -3499,10 +3502,16 @@ export function getExecutePollFunctions(
}, },
getMode: () => mode, getMode: () => mode,
getActivationMode: () => activation, getActivationMode: () => activation,
getCredentials: async (type: string | (new () => ICredentialType)) => { getCredentials: async (type) =>
const typeString = typeof type === 'string' ? type : type.name; await getCredentials(workflow, node, type, additionalData, mode),
return await getCredentials(workflow, node, typeString, additionalData, mode); getCredential: async (CredentialType) =>
}, await getCredentials(
workflow,
node,
credentialClassToType(CredentialType),
additionalData,
mode,
),
getNodeParameter: ( getNodeParameter: (
parameterName: string, parameterName: string,
fallbackValue?: any, fallbackValue?: any,
@ -3565,8 +3574,16 @@ export function getExecuteTriggerFunctions(
}, },
getMode: () => mode, getMode: () => mode,
getActivationMode: () => activation, getActivationMode: () => activation,
getCredentials: async (type: string | (new () => ICredentialType)) => getCredentials: async (type) =>
await getCredentials(workflow, node, type, additionalData, mode), await getCredentials(workflow, node, type, additionalData, mode),
getCredential: async (CredentialType) =>
await getCredentials(
workflow,
node,
credentialClassToType(CredentialType),
additionalData,
mode,
),
getNodeParameter: ( getNodeParameter: (
parameterName: string, parameterName: string,
fallbackValue?: any, fallbackValue?: any,
@ -3625,7 +3642,7 @@ export function getExecuteFunctions(
...getCommonWorkflowFunctions(workflow, node, additionalData), ...getCommonWorkflowFunctions(workflow, node, additionalData),
...executionCancellationFunctions(abortSignal), ...executionCancellationFunctions(abortSignal),
getMode: () => mode, getMode: () => mode,
getCredentials: async (type: string | (new () => ICredentialType), itemIndex: number) => getCredentials: async (type, itemIndex) =>
await getCredentials( await getCredentials(
workflow, workflow,
node, node,
@ -3638,6 +3655,19 @@ export function getExecuteFunctions(
connectionInputData, connectionInputData,
itemIndex, itemIndex,
), ),
getCredential: async (CredentialType, itemIndex) =>
await getCredentials(
workflow,
node,
credentialClassToType(CredentialType),
additionalData,
mode,
executeData,
runExecutionData,
runIndex,
connectionInputData,
itemIndex,
),
getExecuteData: () => executeData, getExecuteData: () => executeData,
continueOnFail: () => { continueOnFail: () => {
return continueOnFail(node); return continueOnFail(node);
@ -3977,7 +4007,7 @@ export function getExecuteSingleFunctions(
getContext(type: ContextType): IContextObject { getContext(type: ContextType): IContextObject {
return NodeHelpers.getContext(runExecutionData, type, node); return NodeHelpers.getContext(runExecutionData, type, node);
}, },
getCredentials: async (type: string | (new () => ICredentialType)) => getCredentials: async (type) =>
await getCredentials( await getCredentials(
workflow, workflow,
node, node,
@ -3990,6 +4020,19 @@ export function getExecuteSingleFunctions(
connectionInputData, connectionInputData,
itemIndex, itemIndex,
), ),
getCredential: async (CredentialType, itemIndex) =>
await getCredentials(
workflow,
node,
credentialClassToType(CredentialType),
additionalData,
mode,
executeData,
runExecutionData,
runIndex,
connectionInputData,
itemIndex,
),
getInputData: (inputIndex = 0, inputName = 'main') => { getInputData: (inputIndex = 0, inputName = 'main') => {
if (!inputData.hasOwnProperty(inputName)) { if (!inputData.hasOwnProperty(inputName)) {
// Return empty array because else it would throw error when nothing is connected to input // 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 ((workflow: Workflow, node: INode, path: string) => {
return { return {
...getCommonWorkflowFunctions(workflow, node, additionalData), ...getCommonWorkflowFunctions(workflow, node, additionalData),
getCredentials: async (type: string | (new () => ICredentialType)) => getCredentials: async (type) =>
await getCredentials(workflow, node, type, additionalData, 'internal'), await getCredentials(workflow, node, type, additionalData, 'internal'),
getCredential: async (CredentialType) =>
await getCredentials(
workflow,
node,
credentialClassToType(CredentialType),
additionalData,
'internal',
),
getCurrentNodeParameter: ( getCurrentNodeParameter: (
parameterPath: string, parameterPath: string,
options?: IGetNodeParameterOptions, options?: IGetNodeParameterOptions,
@ -4198,8 +4249,16 @@ export function getExecuteHookFunctions(
return ((workflow: Workflow, node: INode) => { return ((workflow: Workflow, node: INode) => {
return { return {
...getCommonWorkflowFunctions(workflow, node, additionalData), ...getCommonWorkflowFunctions(workflow, node, additionalData),
getCredentials: async (type: string | (new () => ICredentialType)) => getCredentials: async (type) =>
await getCredentials(workflow, node, type, additionalData, mode), await getCredentials(workflow, node, type, additionalData, mode),
getCredential: async (CredentialType) =>
await getCredentials(
workflow,
node,
credentialClassToType(CredentialType),
additionalData,
mode,
),
getMode: () => mode, getMode: () => mode,
getActivationMode: () => activation, getActivationMode: () => activation,
getNodeParameter: ( getNodeParameter: (
@ -4272,8 +4331,16 @@ export function getExecuteWebhookFunctions(
} }
return additionalData.httpRequest.body; return additionalData.httpRequest.body;
}, },
getCredentials: async (type: string | (new () => ICredentialType)) => getCredentials: async (type) =>
await getCredentials(workflow, node, type, additionalData, mode), await getCredentials(workflow, node, type, additionalData, mode),
getCredential: async (CredentialType) =>
await getCredentials(
workflow,
node,
credentialClassToType(CredentialType),
additionalData,
mode,
),
getHeaderData(): IncomingHttpHeaders { getHeaderData(): IncomingHttpHeaders {
if (additionalData.httpRequest === undefined) { if (additionalData.httpRequest === undefined) {
throw new ApplicationError('Request is missing'); throw new ApplicationError('Request is missing');

View file

@ -30,7 +30,7 @@ export async function strapiApiRequest(
headers: IDataObject = {}, headers: IDataObject = {},
) { ) {
const authenticationMethod = this.getNodeParameter('authentication', 0); const authenticationMethod = this.getNodeParameter('authentication', 0);
const credentials = await this.getCredentials( const credentials = await this.getCredential(
authenticationMethod === 'password' ? StrapiApi : StrapiTokenApi, authenticationMethod === 'password' ? StrapiApi : StrapiTokenApi,
); );

View file

@ -149,10 +149,10 @@ export class Strapi implements INodeType {
if (authenticationMethod === 'password') { if (authenticationMethod === 'password') {
const { jwt } = await getToken.call(this); const { jwt } = await getToken.call(this);
apiVersion = (await this.getCredentials(StrapiApi)).apiVersion; apiVersion = (await this.getCredential(StrapiApi)).apiVersion;
headers.Authorization = `Bearer ${jwt}`; headers.Authorization = `Bearer ${jwt}`;
} else { } else {
apiVersion = (await this.getCredentials(StrapiTokenApi)).apiVersion; apiVersion = (await this.getCredential(StrapiTokenApi)).apiVersion;
} }
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {

View file

@ -183,6 +183,8 @@ type NumberMetadata = BaseMetadata &
default: number; default: number;
}>; }>;
type PasswordMetadata = Omit<Optional<StringMetadata, 'label'>, 'password'>;
type Option<V extends string> = { type Option<V extends string> = {
label: string; label: string;
value: V; value: V;
@ -224,7 +226,7 @@ export const CredentialSchema = {
return new CredentialSchemaRootObject(shape); return new CredentialSchemaRootObject(shape);
}, },
password(options: Omit<Optional<StringMetadata, 'label'>, 'password'> = {}) { password<M extends PasswordMetadata>(options: M = {} as M) {
return new CredentialSchemaString( return new CredentialSchemaString(
{ {
password: true, password: true,
@ -234,18 +236,20 @@ export const CredentialSchema = {
z.string(), z.string(),
); );
}, },
// eslint-disable-next-line id-denylist // eslint-disable-next-line id-denylist
string<M extends StringMetadata>(options: M) { string<M extends StringMetadata>(options: M) {
return new CredentialSchemaString(options, z.string()); return new CredentialSchemaString(options, z.string());
}, },
// eslint-disable-next-line id-denylist // eslint-disable-next-line id-denylist
number<M extends NumberMetadata>(options: M) { number<M extends NumberMetadata>(options: M) {
return new CredentialSchemaNumber(options, z.number()); return new CredentialSchemaNumber(options, z.number());
}, },
url(options: Optional<StringMetadata, 'label'> = {}) { url<M extends Optional<StringMetadata, 'label'>>(options: M) {
return new CredentialSchemaString({ label: 'URL', ...options }, z.string().url()); return new CredentialSchemaString({ label: 'URL', ...options }, z.string().url());
}, },
email(options: Optional<StringMetadata, 'label'> = {}) { email<M extends Optional<StringMetadata, 'label'>>(options: M) {
return new CredentialSchemaString({ label: 'Email', ...options }, z.string().email()); return new CredentialSchemaString({ label: 'Email', ...options }, z.string().email());
}, },
options<V extends string, M extends OptionsMetadata<V>>(options: M) { options<V extends string, M extends OptionsMetadata<V>>(options: M) {

View file

@ -863,11 +863,14 @@ export type NodeTypeAndVersion = {
export interface FunctionsBase { export interface FunctionsBase {
logger: Logger; logger: Logger;
/**
* @deprecated Use getCredential instead for better type safety
* */
getCredentials<T extends object = ICredentialDataDecryptedObject>( getCredentials<T extends object = ICredentialDataDecryptedObject>(
type: string, type: string,
itemIndex?: number, itemIndex?: number,
): Promise<T>; ): Promise<T>;
getCredentials<T extends new () => ICredentialType>( getCredential<T extends new () => ICredentialType>(
type: T, type: T,
itemIndex?: number, itemIndex?: number,
): Promise< ): Promise<