feat: Updated multiple credentials with tests and allow to be used on HTTP Request Node (#3670)

* Notion cred updated

* Airtable new cred

* revamped twilio cred

* urlscanlo revamp cred

* Wordpress revamp cred with testing

* SendGrid cred revamped

* 🐛 Fix imports

* 🐛  Fixes imports in urlscanio

* Fix airtable cred injection

* Fixes notion request, changes way of cred injection

* Change auth type from method to generic

* Fix minor issues

* Fix lint issue

Co-authored-by: Omar Ajoue <krynble@gmail.com>
This commit is contained in:
agobrech
2022-07-10 12:32:19 +02:00
committed by GitHub
parent ece1836c45
commit d5d4dd3845
12 changed files with 78 additions and 70 deletions

View File

@@ -1,4 +1,5 @@
import { import {
IAuthenticateGeneric,
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@@ -16,4 +17,12 @@ export class AirtableApi implements ICredentialType {
default: '', default: '',
}, },
]; ];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '={{$credentials.apiKey}}',
},
},
};
} }

View File

@@ -1,4 +1,6 @@
import { import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@@ -15,4 +17,18 @@ export class NotionApi implements ICredentialType {
default: '', default: '',
}, },
]; ];
test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.notion.com/v1',
url: '/users',
},
};
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'Authorization': '=Bearer {{$credentials.apiKey}}',
},
},
};
} }

View File

@@ -1,4 +1,5 @@
import { import {
IAuthenticateGeneric,
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@@ -15,4 +16,12 @@ export class SendGridApi implements ICredentialType {
default: '', default: '',
}, },
]; ];
authenticate = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
},
},
} as IAuthenticateGeneric;
} }

View File

@@ -1,5 +1,9 @@
import { import {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType, ICredentialType,
IHttpRequestOptions,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } 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 }}',
},
},
};
} }

View File

@@ -1,4 +1,6 @@
import { import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@@ -16,4 +18,19 @@ export class UrlScanIoApi implements ICredentialType {
required: true, required: true,
}, },
]; ];
authenticate = {
type: 'generic',
properties: {
headers: {
'API-KEY': '={{$credentials.apiKey}}',
},
},
} as IAuthenticateGeneric;
test: ICredentialTestRequest = {
request: {
baseURL: 'https://urlscan.io',
url: '/user/quotas',
},
};
} }

View File

@@ -50,3 +50,4 @@ export class WordpressApi implements ICredentialType {
}, },
}; };
} }

View File

@@ -1,7 +1,6 @@
import { import {
IExecuteFunctions, IExecuteFunctions,
IHookFunctions, IPollFunctions,
ILoadOptionsFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
@@ -11,10 +10,9 @@ import {
import { import {
IBinaryKeyData, IBinaryKeyData,
IDataObject, IDataObject,
ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
IPollFunctions,
NodeApiError, NodeApiError,
NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
@@ -39,7 +37,7 @@ export interface IRecord {
* @param {object} body * @param {object} body
* @returns {Promise<any>} * @returns {Promise<any>}
*/ */
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: object, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise<any> { // 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<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('airtableApi'); const credentials = await this.getCredentials('airtableApi');
query = query || {}; 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 // 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 // and it returns 404 like for the /meta request. So we always send
// it as query string. // it as query string.
query.api_key = credentials.apiKey; // query.api_key = credentials.apiKey;
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: { headers: {
@@ -69,7 +67,7 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
} }
try { try {
return await this.helpers.request!(options); return await this.helpers.requestWithAuthentication.call(this, 'airtableApi', options);
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error); throw new NodeApiError(this.getNode(), error);
} }
@@ -81,14 +79,14 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
* and return all results * and return all results
* *
* @export * @export
* @param {(IHookFunctions | IExecuteFunctions)} this * @param {(IExecuteFunctions | IExecuteFunctions)} this
* @param {string} method * @param {string} method
* @param {string} endpoint * @param {string} endpoint
* @param {IDataObject} body * @param {IDataObject} body
* @param {IDataObject} [query] * @param {IDataObject} [query]
* @returns {Promise<any>} * @returns {Promise<any>}
*/ */
export async function apiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any export async function apiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
if (query === undefined) { if (query === undefined) {
query = {}; query = {};

View File

@@ -55,14 +55,12 @@ export async function notionApiRequest(this: IHookFunctions | IExecuteFunctions
json: true, json: true,
}; };
options = Object.assign({}, options, option); 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) { if (Object.keys(body).length === 0) {
delete options.body; delete options.body;
} }
if (!uri) {
return this.helpers.requestWithAuthentication.call(this,'notionApi', options );
}
return this.helpers.request!(options); return this.helpers.request!(options);
} catch (error) { } catch (error) {

View File

@@ -14,14 +14,10 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, qs: IDataObject = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, qs: IDataObject = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('sendGridApi');
const host = 'api.sendgrid.com/v3'; const host = 'api.sendgrid.com/v3';
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
},
method, method,
qs, qs,
body, body,
@@ -39,7 +35,7 @@ export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunction
try { try {
//@ts-ignore //@ts-ignore
return await this.helpers.request!(options); return await this.helpers.requestWithAuthentication.call(this, 'sendGridApi', options);
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error); throw new NodeApiError(this.getNode(), error);
} }

View File

@@ -41,20 +41,8 @@ export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions,
json: true, 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 { try {
return await this.helpers.request(options); return await this.helpers.requestWithAuthentication.call(this, 'twilioApi', options);
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error); throw new NodeApiError(this.getNode(), error);
} }

View File

@@ -18,12 +18,8 @@ export async function urlScanIoApiRequest(
body: IDataObject = {}, body: IDataObject = {},
qs: IDataObject = {}, qs: IDataObject = {},
) { ) {
const { apiKey } = await this.getCredentials('urlScanIoApi') as { apiKey: string };
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: {
'API-KEY': apiKey,
},
method, method,
body, body,
qs, qs,

View File

@@ -46,7 +46,6 @@ export class UrlScanIo implements INodeType {
{ {
name: 'urlScanIoApi', name: 'urlScanIoApi',
required: true, required: true,
testedBy: 'urlScanIoApiTest',
}, },
], ],
properties: [ properties: [
@@ -68,39 +67,6 @@ export class UrlScanIo implements INodeType {
], ],
}; };
methods = {
credentialTest: {
async urlScanIoApiTest(
this: ICredentialTestFunctions,
credentials: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
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<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];