refactor(core): Allow custom types on getCredentials (no-changelog) (#10567)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-08-27 15:23:58 +02:00
committed by GitHub
parent 52c574d83f
commit be52176585
65 changed files with 132 additions and 123 deletions

View File

@@ -3,20 +3,22 @@ import type { ICredentialDataDecryptedObject, ICredentialsEncrypted } from 'n8n-
import { ApplicationError, ICredentials, jsonParse } from 'n8n-workflow';
import { Cipher } from './Cipher';
export class Credentials extends ICredentials {
export class Credentials<
T extends object = ICredentialDataDecryptedObject,
> extends ICredentials<T> {
private readonly cipher = Container.get(Cipher);
/**
* Sets new credential object
*/
setData(data: ICredentialDataDecryptedObject): void {
setData(data: T): void {
this.data = this.cipher.encrypt(data);
}
/**
* Returns the decrypted credential object
*/
getData(): ICredentialDataDecryptedObject {
getData(): T {
if (this.data === undefined) {
throw new ApplicationError('No data is set so nothing can be returned.');
}

View File

@@ -1656,7 +1656,8 @@ export async function httpRequestWithAuthentication(
if (additionalCredentialOptions?.credentialsDecrypted) {
credentialsDecrypted = additionalCredentialOptions.credentialsDecrypted.data;
} else {
credentialsDecrypted = await this.getCredentials(credentialsType);
credentialsDecrypted =
await this.getCredentials<ICredentialDataDecryptedObject>(credentialsType);
}
if (credentialsDecrypted === undefined) {
@@ -1853,7 +1854,10 @@ export async function requestWithAuthentication(
if (additionalCredentialOptions?.credentialsDecrypted) {
credentialsDecrypted = additionalCredentialOptions.credentialsDecrypted.data;
} else {
credentialsDecrypted = await this.getCredentials(credentialsType, itemIndex);
credentialsDecrypted = await this.getCredentials<ICredentialDataDecryptedObject>(
credentialsType,
itemIndex,
);
}
if (credentialsDecrypted === undefined) {
@@ -1988,7 +1992,7 @@ export function getAdditionalKeys(
* @param {INode} node Node which request the data
* @param {string} type The credential type to return
*/
export async function getCredentials(
export async function getCredentials<T extends object = ICredentialDataDecryptedObject>(
workflow: Workflow,
node: INode,
type: string,
@@ -1999,7 +2003,7 @@ export async function getCredentials(
runIndex?: number,
connectionInputData?: INodeExecutionData[],
itemIndex?: number,
): Promise<ICredentialDataDecryptedObject> {
): Promise<T> {
// 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) {
@@ -2117,7 +2121,7 @@ export async function getCredentials(
expressionResolveValues,
);
return decryptedDataObject;
return decryptedDataObject as T;
}
/**