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>(
workflow: Workflow,
node: INode,
credType: string | (new () => ICredentialType),
type: string,
additionalData: IWorkflowExecuteAdditionalData,
mode: WorkflowExecuteMode,
executeData?: IExecuteData,
@ -2006,7 +2006,6 @@ export async function getCredentials<T extends object = ICredentialDataDecrypted
connectionInputData?: INodeExecutionData[],
itemIndex?: number,
): Promise<T> {
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<FunctionsBase, 'getCredentials'> => ({
): Omit<FunctionsBase, 'getCredentials' | 'getCredential'> => ({
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');

View file

@ -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,
);

View file

@ -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++) {

View file

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

View file

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