diff --git a/packages/nodes-base/credentials/AirtableApi.credentials.ts b/packages/nodes-base/credentials/AirtableApi.credentials.ts index 2a3d8cdbfc..a9b023b2c8 100644 --- a/packages/nodes-base/credentials/AirtableApi.credentials.ts +++ b/packages/nodes-base/credentials/AirtableApi.credentials.ts @@ -1,4 +1,5 @@ import { + IAuthenticateGeneric, ICredentialType, INodeProperties, } from 'n8n-workflow'; @@ -16,4 +17,12 @@ export class AirtableApi implements ICredentialType { default: '', }, ]; + authenticate: IAuthenticateGeneric = { + type: 'generic', + properties: { + headers: { + Authorization: '={{$credentials.apiKey}}', + }, + }, + }; } diff --git a/packages/nodes-base/credentials/NotionApi.credentials.ts b/packages/nodes-base/credentials/NotionApi.credentials.ts index 322da31698..1c8e53a79f 100644 --- a/packages/nodes-base/credentials/NotionApi.credentials.ts +++ b/packages/nodes-base/credentials/NotionApi.credentials.ts @@ -1,4 +1,6 @@ import { + IAuthenticateGeneric, + ICredentialTestRequest, ICredentialType, INodeProperties, } from 'n8n-workflow'; @@ -15,4 +17,18 @@ export class NotionApi implements ICredentialType { default: '', }, ]; + test: ICredentialTestRequest = { + request: { + baseURL: 'https://api.notion.com/v1', + url: '/users', + }, + }; + authenticate: IAuthenticateGeneric = { + type: 'generic', + properties: { + headers: { + 'Authorization': '=Bearer {{$credentials.apiKey}}', + }, + }, + }; } diff --git a/packages/nodes-base/credentials/SendGridApi.credentials.ts b/packages/nodes-base/credentials/SendGridApi.credentials.ts index d2338f4419..95ee72d1c5 100644 --- a/packages/nodes-base/credentials/SendGridApi.credentials.ts +++ b/packages/nodes-base/credentials/SendGridApi.credentials.ts @@ -1,4 +1,5 @@ import { + IAuthenticateGeneric, ICredentialType, INodeProperties, } from 'n8n-workflow'; @@ -15,4 +16,12 @@ export class SendGridApi implements ICredentialType { default: '', }, ]; + authenticate = { + type: 'generic', + properties: { + headers: { + Authorization: '=Bearer {{$credentials.apiKey}}', + }, + }, + } as IAuthenticateGeneric; } diff --git a/packages/nodes-base/credentials/TwilioApi.credentials.ts b/packages/nodes-base/credentials/TwilioApi.credentials.ts index df5e1c1f12..802d4917ee 100644 --- a/packages/nodes-base/credentials/TwilioApi.credentials.ts +++ b/packages/nodes-base/credentials/TwilioApi.credentials.ts @@ -1,5 +1,9 @@ import { + IAuthenticateGeneric, + ICredentialDataDecryptedObject, + ICredentialTestRequest, ICredentialType, + IHttpRequestOptions, INodeProperties, } from 'n8n-workflow'; @@ -74,4 +78,14 @@ export class TwilioApi implements ICredentialType { }, }, ]; + + authenticate: IAuthenticateGeneric = { + type: 'generic', + properties: { + auth: { + username: '={{ $credentials.authType === "apiKey" ? $credentials.apiKeySid : $credentials.accountSid }}', + password: '={{ $credentials.authType === "apiKey" ? $credentials.apiKeySecret : $credentials.authToken }}', + }, + }, + }; } diff --git a/packages/nodes-base/credentials/UrlScanIoApi.credentials.ts b/packages/nodes-base/credentials/UrlScanIoApi.credentials.ts index df29c66cf8..299386d59b 100644 --- a/packages/nodes-base/credentials/UrlScanIoApi.credentials.ts +++ b/packages/nodes-base/credentials/UrlScanIoApi.credentials.ts @@ -1,4 +1,6 @@ import { + IAuthenticateGeneric, + ICredentialTestRequest, ICredentialType, INodeProperties, } from 'n8n-workflow'; @@ -16,4 +18,19 @@ export class UrlScanIoApi implements ICredentialType { required: true, }, ]; + authenticate = { + type: 'generic', + properties: { + headers: { + 'API-KEY': '={{$credentials.apiKey}}', + }, + }, + } as IAuthenticateGeneric; + + test: ICredentialTestRequest = { + request: { + baseURL: 'https://urlscan.io', + url: '/user/quotas', + }, + }; } diff --git a/packages/nodes-base/credentials/WordpressApi.credentials.ts b/packages/nodes-base/credentials/WordpressApi.credentials.ts index ade71b8582..47dd3585d2 100644 --- a/packages/nodes-base/credentials/WordpressApi.credentials.ts +++ b/packages/nodes-base/credentials/WordpressApi.credentials.ts @@ -50,3 +50,4 @@ export class WordpressApi implements ICredentialType { }, }; } + diff --git a/packages/nodes-base/nodes/Airtable/GenericFunctions.ts b/packages/nodes-base/nodes/Airtable/GenericFunctions.ts index 1f6b5b2ffb..05057a6e3f 100644 --- a/packages/nodes-base/nodes/Airtable/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Airtable/GenericFunctions.ts @@ -1,7 +1,6 @@ import { IExecuteFunctions, - IHookFunctions, - ILoadOptionsFunctions, + IPollFunctions, } from 'n8n-core'; import { @@ -11,10 +10,9 @@ import { import { IBinaryKeyData, IDataObject, + ILoadOptionsFunctions, INodeExecutionData, - IPollFunctions, NodeApiError, - NodeOperationError, } from 'n8n-workflow'; @@ -39,7 +37,7 @@ export interface IRecord { * @param {object} body * @returns {Promise} */ -export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: object, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any +export async function apiRequest(this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: object, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any const credentials = await this.getCredentials('airtableApi'); query = query || {}; @@ -47,7 +45,7 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa // For some reason for some endpoints the bearer auth does not work // and it returns 404 like for the /meta request. So we always send // it as query string. - query.api_key = credentials.apiKey; + // query.api_key = credentials.apiKey; const options: OptionsWithUri = { headers: { @@ -69,7 +67,7 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa } try { - return await this.helpers.request!(options); + return await this.helpers.requestWithAuthentication.call(this, 'airtableApi', options); } catch (error) { throw new NodeApiError(this.getNode(), error); } @@ -81,14 +79,14 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa * and return all results * * @export - * @param {(IHookFunctions | IExecuteFunctions)} this + * @param {(IExecuteFunctions | IExecuteFunctions)} this * @param {string} method * @param {string} endpoint * @param {IDataObject} body * @param {IDataObject} [query] * @returns {Promise} */ -export async function apiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise { // tslint:disable-line:no-any +export async function apiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise { // tslint:disable-line:no-any if (query === undefined) { query = {}; diff --git a/packages/nodes-base/nodes/Notion/GenericFunctions.ts b/packages/nodes-base/nodes/Notion/GenericFunctions.ts index 7344545879..942e5856bd 100644 --- a/packages/nodes-base/nodes/Notion/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Notion/GenericFunctions.ts @@ -55,14 +55,12 @@ export async function notionApiRequest(this: IHookFunctions | IExecuteFunctions json: true, }; options = Object.assign({}, options, option); - const credentials = await this.getCredentials('notionApi'); - if (!uri) { - //do not include the API Key when downloading files, else the request fails - options!.headers!['Authorization'] = `Bearer ${credentials.apiKey}`; - } if (Object.keys(body).length === 0) { delete options.body; } + if (!uri) { + return this.helpers.requestWithAuthentication.call(this,'notionApi', options ); + } return this.helpers.request!(options); } catch (error) { diff --git a/packages/nodes-base/nodes/SendGrid/GenericFunctions.ts b/packages/nodes-base/nodes/SendGrid/GenericFunctions.ts index 4787312b6b..aacf6da506 100644 --- a/packages/nodes-base/nodes/SendGrid/GenericFunctions.ts +++ b/packages/nodes-base/nodes/SendGrid/GenericFunctions.ts @@ -14,14 +14,10 @@ import { } from 'n8n-workflow'; export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, qs: IDataObject = {}, option: IDataObject = {}): Promise { // tslint:disable-line:no-any - const credentials = await this.getCredentials('sendGridApi'); const host = 'api.sendgrid.com/v3'; const options: OptionsWithUri = { - headers: { - Authorization: `Bearer ${credentials.apiKey}`, - }, method, qs, body, @@ -39,7 +35,7 @@ export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunction try { //@ts-ignore - return await this.helpers.request!(options); + return await this.helpers.requestWithAuthentication.call(this, 'sendGridApi', options); } catch (error) { throw new NodeApiError(this.getNode(), error); } diff --git a/packages/nodes-base/nodes/Twilio/GenericFunctions.ts b/packages/nodes-base/nodes/Twilio/GenericFunctions.ts index e1612dbe38..2d0a37a72c 100644 --- a/packages/nodes-base/nodes/Twilio/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Twilio/GenericFunctions.ts @@ -41,20 +41,8 @@ export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions, json: true, }; - if (credentials.authType === 'apiKey') { - options.auth = { - user: credentials.apiKeySid, - password: credentials.apiKeySecret, - }; - } else if (credentials.authType === 'authToken') { - options.auth = { - user: credentials.accountSid, - pass: credentials.authToken, - }; - } - try { - return await this.helpers.request(options); + return await this.helpers.requestWithAuthentication.call(this, 'twilioApi', options); } catch (error) { throw new NodeApiError(this.getNode(), error); } diff --git a/packages/nodes-base/nodes/UrlScanIo/GenericFunctions.ts b/packages/nodes-base/nodes/UrlScanIo/GenericFunctions.ts index 8a3d5deaf6..f58604ae8e 100644 --- a/packages/nodes-base/nodes/UrlScanIo/GenericFunctions.ts +++ b/packages/nodes-base/nodes/UrlScanIo/GenericFunctions.ts @@ -18,12 +18,8 @@ export async function urlScanIoApiRequest( body: IDataObject = {}, qs: IDataObject = {}, ) { - const { apiKey } = await this.getCredentials('urlScanIoApi') as { apiKey: string }; const options: OptionsWithUri = { - headers: { - 'API-KEY': apiKey, - }, method, body, qs, diff --git a/packages/nodes-base/nodes/UrlScanIo/UrlScanIo.node.ts b/packages/nodes-base/nodes/UrlScanIo/UrlScanIo.node.ts index 78076b1d1b..b7e9dd7b28 100644 --- a/packages/nodes-base/nodes/UrlScanIo/UrlScanIo.node.ts +++ b/packages/nodes-base/nodes/UrlScanIo/UrlScanIo.node.ts @@ -46,7 +46,6 @@ export class UrlScanIo implements INodeType { { name: 'urlScanIoApi', required: true, - testedBy: 'urlScanIoApiTest', }, ], properties: [ @@ -68,39 +67,6 @@ export class UrlScanIo implements INodeType { ], }; - methods = { - credentialTest: { - async urlScanIoApiTest( - this: ICredentialTestFunctions, - credentials: ICredentialsDecrypted, - ): Promise { - const { apiKey } = credentials.data as { apiKey: string }; - - const options: OptionsWithUri = { - headers: { - 'API-KEY': apiKey, - }, - method: 'GET', - uri: 'https://urlscan.io/user/quotas', - json: true, - }; - - try { - await this.helpers.request(options); - return { - status: 'OK', - message: 'Authentication successful', - }; - } catch (error) { - return { - status: 'Error', - message: error.message, - }; - } - }, - }, - }; - async execute(this: IExecuteFunctions): Promise { const items = this.getInputData(); const returnData: IDataObject[] = [];