mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 05:17:28 -08:00
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:
parent
ece1836c45
commit
d5d4dd3845
|
@ -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}}',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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}}',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 }}',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -50,3 +50,4 @@ export class WordpressApi implements ICredentialType {
|
|||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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<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 || {};
|
||||
|
@ -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<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 = {};
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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,
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<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[] = [];
|
||||
|
|
Loading…
Reference in a new issue