mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-21 03:42:16 +00:00
refactor(core): Allow custom types on getCredentials (no-changelog) (#10567)
This commit is contained in:
committed by
GitHub
parent
52c574d83f
commit
be52176585
@@ -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.');
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user