Skip to content

Commit

Permalink
refactor(core): Allow custom types on getCredentials (no-changelog) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy authored Aug 27, 2024
1 parent 52c574d commit be52176
Show file tree
Hide file tree
Showing 65 changed files with 132 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export class EmbeddingsAzureOpenAi implements INodeType {

async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
this.logger.verbose('Supply data for embeddings');
const credentials = (await this.getCredentials('azureOpenAiApi')) as {
const credentials = await this.getCredentials<{
apiKey: string;
resourceName: string;
apiVersion: string;
};
}>('azureOpenAiApi');
const modelName = this.getNodeParameter('model', itemIndex) as string;

const options = this.getNodeParameter('options', itemIndex, {}) as {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class EmbeddingsCohere implements INodeType {
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
this.logger.verbose('Supply data for embeddings Cohere');
const modelName = this.getNodeParameter('modelName', itemIndex, 'embed-english-v2.0') as string;
const credentials = (await this.getCredentials('cohereApi')) as { apiKey: string };
const credentials = await this.getCredentials<{ apiKey: string }>('cohereApi');
const embeddings = new CohereEmbeddings({
apiKey: credentials.apiKey,
model: modelName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ export class LmChatAzureOpenAi implements INodeType {
};

async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = (await this.getCredentials('azureOpenAiApi')) as {
const credentials = await this.getCredentials<{
apiKey: string;
resourceName: string;
apiVersion: string;
};
}>('azureOpenAiApi');

const modelName = this.getNodeParameter('model', itemIndex) as string;
const options = this.getNodeParameter('options', itemIndex, {}) as {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class MemoryPostgresChat implements INodeType {
};

async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = (await this.getCredentials('postgres')) as PostgresNodeCredentials;
const credentials = await this.getCredentials<PostgresNodeCredentials>('postgres');
const tableName = this.getNodeParameter('tableName', itemIndex, 'n8n_chat_histories') as string;
const sessionId = getSessionId(this, itemIndex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ export class MemoryZep implements INodeType {
};

async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = (await this.getCredentials('zepApi')) as {
const credentials = await this.getCredentials<{
apiKey?: string;
apiUrl?: string;
cloud?: boolean;
};
}>('zepApi');

const nodeVersion = this.getNode().typeVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function validateAuth(context: IWebhookFunctions) {
// Basic authorization is needed to call webhook
let expectedAuth: ICredentialDataDecryptedObject | undefined;
try {
expectedAuth = await context.getCredentials('httpBasicAuth');
expectedAuth = await context.getCredentials<ICredentialDataDecryptedObject>('httpBasicAuth');
} catch {}

if (expectedAuth === undefined || !expectedAuth.user || !expectedAuth.password) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export const VectorStoreZep = createVectorStoreNode({
embeddingDimensions?: number;
}) || {};

const credentials = (await context.getCredentials('zepApi')) as {
const credentials = await context.getCredentials<{
apiKey?: string;
apiUrl: string;
};
}>('zepApi');

const zepConfig: IZepConfig = {
apiUrl: credentials.apiUrl,
Expand All @@ -102,10 +102,10 @@ export const VectorStoreZep = createVectorStoreNode({
embeddingDimensions?: number;
}) || {};

const credentials = (await context.getCredentials('zepApi')) as {
const credentials = await context.getCredentials<{
apiKey?: string;
apiUrl: string;
};
}>('zepApi');

const zepConfig = {
apiUrl: credentials.apiUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export class VectorStoreZepInsert implements INodeType {
embeddingDimensions?: number;
}) || {};

const credentials = (await this.getCredentials('zepApi')) as {
const credentials = await this.getCredentials<{
apiKey?: string;
apiUrl: string;
};
}>('zepApi');

const documentInput = (await this.getInputConnectionData(NodeConnectionType.AiDocument, 0)) as
| N8nJsonLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ export class VectorStoreZepLoad implements INodeType {
embeddingDimensions?: number;
}) || {};

const credentials = (await this.getCredentials('zepApi')) as {
const credentials = await this.getCredentials<{
apiKey?: string;
apiUrl: string;
};
}>('zepApi');
const embeddings = (await this.getInputConnectionData(
NodeConnectionType.AiEmbedding,
0,
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/Credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -2117,7 +2121,7 @@ export async function getCredentials(
expressionResolveValues,
);

return decryptedDataObject;
return decryptedDataObject as T;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Automizy/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function automizyApiRequest(
qs: IDataObject = {},
option = {},
): Promise<any> {
const credentials = (await this.getCredentials('automizyApi')) as IDataObject;
const credentials = await this.getCredentials<{ apiToken: string }>('automizyApi');

const options: IRequestOptions = {
headers: {
Expand Down
6 changes: 2 additions & 4 deletions packages/nodes-base/nodes/Autopilot/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ export async function autopilotApiRequest(
uri?: string,
_option: IDataObject = {},
): Promise<any> {
const credentials = (await this.getCredentials('autopilotApi')) as IDataObject;

const apiKey = `${credentials.apiKey}`;
const credentials = await this.getCredentials<{ apiKey: string }>('autopilotApi');

const endpoint = 'https://api2.autopilothq.com/v1';

const options: IRequestOptions = {
headers: {
'Content-Type': 'application/json',
autopilotapikey: apiKey,
autopilotapikey: credentials.apiKey,
},
method,
body,
Expand Down
8 changes: 4 additions & 4 deletions packages/nodes-base/nodes/Baserow/Baserow.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class Baserow implements INodeType {
methods = {
loadOptions: {
async getDatabaseIds(this: ILoadOptionsFunctions) {
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
const credentials = await this.getCredentials<BaserowCredentials>('baserowApi');
const jwtToken = await getJwtToken.call(this, credentials);
const endpoint = '/api/applications/';
const databases = (await baserowApiRequest.call(
Expand All @@ -124,7 +124,7 @@ export class Baserow implements INodeType {
},

async getTableIds(this: ILoadOptionsFunctions) {
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
const credentials = await this.getCredentials<BaserowCredentials>('baserowApi');
const jwtToken = await getJwtToken.call(this, credentials);
const databaseId = this.getNodeParameter('databaseId', 0) as string;
const endpoint = `/api/database/tables/database/${databaseId}/`;
Expand All @@ -138,7 +138,7 @@ export class Baserow implements INodeType {
},

async getTableFields(this: ILoadOptionsFunctions) {
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
const credentials = await this.getCredentials<BaserowCredentials>('baserowApi');
const jwtToken = await getJwtToken.call(this, credentials);
const tableId = this.getNodeParameter('tableId', 0) as string;
const endpoint = `/api/database/fields/table/${tableId}/`;
Expand All @@ -160,7 +160,7 @@ export class Baserow implements INodeType {
const operation = this.getNodeParameter('operation', 0) as Operation;

const tableId = this.getNodeParameter('tableId', 0) as string;
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
const credentials = await this.getCredentials<BaserowCredentials>('baserowApi');
const jwtToken = await getJwtToken.call(this, credentials);
const fields = await mapper.getTableFields.call(this, tableId, jwtToken);
mapper.createMappings(fields);
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Baserow/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function baserowApiRequest(
body: IDataObject = {},
qs: IDataObject = {},
) {
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
const credentials = await this.getCredentials<BaserowCredentials>('baserowApi');

const options: IRequestOptions = {
headers: {
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Calendly/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getAuthenticationType(
): Promise<'accessToken' | 'apiKey'> {
const authentication = this.getNodeParameter('authentication', 0) as string;
if (authentication === 'apiKey') {
const { apiKey } = (await this.getCredentials('calendlyApi')) as { apiKey: string };
const { apiKey } = await this.getCredentials<{ apiKey: string }>('calendlyApi');
return getAuthenticationTypeFromApiKey(apiKey);
} else {
return 'accessToken';
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Dhl/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function dhlApiRequest(
uri?: string,
option: IDataObject = {},
): Promise<any> {
const credentials = (await this.getCredentials('dhlApi')) as { apiKey: string };
const credentials = await this.getCredentials<{ apiKey: string }>('dhlApi');

let options: IRequestOptions = {
headers: {
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Discourse/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function discourseApiRequest(
qs: IDataObject = {},
_option = {},
): Promise<any> {
const credentials = (await this.getCredentials('discourseApi')) as { url: string };
const credentials = await this.getCredentials<{ url: string }>('discourseApi');

const options: IRequestOptions = {
method,
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Disqus/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function disqusApiRequest(
body: IDataObject = {},
option: IDataObject = {},
): Promise<any> {
const credentials = (await this.getCredentials('disqusApi')) as IDataObject;
const credentials = await this.getCredentials<{ accessToken: string }>('disqusApi');
qs.api_key = credentials.accessToken;

// Convert to query string into a format the API can read
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Dropbox/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export function simplify(data: IDataObject[]) {
export async function getCredentials(this: IExecuteFunctions) {
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
if (authenticationMethod === 'accessToken') {
return (await this.getCredentials('dropboxApi')) as IDataObject;
return await this.getCredentials('dropboxApi');
} else {
return (await this.getCredentials('dropboxOAuth2Api')) as IDataObject;
return await this.getCredentials('dropboxOAuth2Api');
}
}
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/ERPNext/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function erpNextApiRequest(
uri?: string,
option: IDataObject = {},
) {
const credentials = (await this.getCredentials('erpNextApi')) as ERPNextApiCredentials;
const credentials = await this.getCredentials<ERPNextApiCredentials>('erpNextApi');
const baseUrl = getBaseUrl(credentials);

let options: IRequestOptions = {
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Emelia/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function emeliaApiRequest(
body: object = {},
qs: IDataObject = {},
) {
const { apiKey } = (await this.getCredentials('emeliaApi')) as { apiKey: string };
const { apiKey } = await this.getCredentials<{ apiKey: string }>('emeliaApi');

const options: IRequestOptions = {
headers: {
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/FormIo/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function formIoApiRequest(
body = {},
qs = {},
): Promise<any> {
const credentials = (await this.getCredentials('formIoApi')) as unknown as IFormIoCredentials;
const credentials = await this.getCredentials<IFormIoCredentials>('formIoApi');

const token = await getToken.call(this, credentials);

Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Formstack/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function apiRequest(

try {
if (authenticationMethod === 'accessToken') {
const credentials = (await this.getCredentials('formstackApi')) as IDataObject;
const credentials = await this.getCredentials<{ accessToken: string }>('formstackApi');

options.headers!.Authorization = `Bearer ${credentials.accessToken}`;
return await this.helpers.request(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function freshworksCrmApiRequest(
body: IDataObject = {},
qs: IDataObject = {},
) {
const { domain } = (await this.getCredentials('freshworksCrmApi')) as FreshworksCrmApiCredentials;
const { domain } = await this.getCredentials<FreshworksCrmApiCredentials>('freshworksCrmApi');

const options: IRequestOptions = {
method,
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Ftp/Ftp.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ export class Ftp implements INodeType {
const protocol = this.getNodeParameter('protocol', 0) as string;

if (protocol === 'sftp') {
credentials = await this.getCredentials('sftp');
credentials = await this.getCredentials<ICredentialDataDecryptedObject>('sftp');
} else {
credentials = await this.getCredentials('ftp');
credentials = await this.getCredentials<ICredentialDataDecryptedObject>('ftp');
}
let ftp: ftpClient;
let sftp: sftpClient;
Expand Down
Loading

0 comments on commit be52176

Please sign in to comment.