Skip to content

Commit

Permalink
feat: Updated multiple credentials with tests and allow to be used on…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
agobrech and krynble authored Jul 10, 2022
1 parent ece1836 commit d5d4dd3
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 70 deletions.
9 changes: 9 additions & 0 deletions packages/nodes-base/credentials/AirtableApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
IAuthenticateGeneric,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -16,4 +17,12 @@ export class AirtableApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '={{$credentials.apiKey}}',
},
},
};
}
16 changes: 16 additions & 0 deletions packages/nodes-base/credentials/NotionApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -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}}',
},
},
};
}
9 changes: 9 additions & 0 deletions packages/nodes-base/credentials/SendGridApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
IAuthenticateGeneric,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -15,4 +16,12 @@ export class SendGridApi implements ICredentialType {
default: '',
},
];
authenticate = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
},
},
} as IAuthenticateGeneric;
}
14 changes: 14 additions & 0 deletions packages/nodes-base/credentials/TwilioApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';

Expand Down Expand Up @@ -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 }}',
},
},
};
}
17 changes: 17 additions & 0 deletions packages/nodes-base/credentials/UrlScanIoApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -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',
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ export class WordpressApi implements ICredentialType {
},
};
}

16 changes: 7 additions & 9 deletions packages/nodes-base/nodes/Airtable/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IPollFunctions,
} from 'n8n-core';

import {
Expand All @@ -11,10 +10,9 @@ import {
import {
IBinaryKeyData,
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
IPollFunctions,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';


Expand All @@ -39,15 +37,15 @@ export interface IRecord {
* @param {object} body
* @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');

query = query || {};

// 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: {
Expand All @@ -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);
}
Expand All @@ -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<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) {
query = {};
Expand Down
8 changes: 3 additions & 5 deletions packages/nodes-base/nodes/Notion/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/SendGrid/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> { // 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,
Expand All @@ -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);
}
Expand Down
14 changes: 1 addition & 13 deletions packages/nodes-base/nodes/Twilio/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/nodes-base/nodes/UrlScanIo/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 0 additions & 34 deletions packages/nodes-base/nodes/UrlScanIo/UrlScanIo.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export class UrlScanIo implements INodeType {
{
name: 'urlScanIoApi',
required: true,
testedBy: 'urlScanIoApiTest',
},
],
properties: [
Expand All @@ -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[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
Expand Down

0 comments on commit d5d4dd3

Please sign in to comment.