mirror of
https://github.com/n8n-io/n8n-nodes-starter.git
synced 2025-10-31 15:22:25 -05:00
Adding FriendGrid example node (updated version of the one in the n8n docs)
This commit is contained in:
parent
1e43acf743
commit
a7dffc4df0
8 changed files with 263 additions and 72 deletions
53
nodes/FriendGrid/GenericFunctions.ts
Normal file
53
nodes/FriendGrid/GenericFunctions.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IExecuteSingleFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
NodeApiError,
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export async function friendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||
method: string, endpoint: string, body?: object, query?: object, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
//Get credentials the user provided for this node
|
||||
const credentials = await this.getCredentials('friendGridApi') as IDataObject;
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
||||
}
|
||||
|
||||
//Make http request according to <https://sendgrid.com/docs/api-reference/>
|
||||
const options: OptionsWithUri = {
|
||||
method,
|
||||
headers: {
|
||||
'Accept': ' application/json',
|
||||
'Authorization': `Bearer ${credentials.apiKey}`,
|
||||
},
|
||||
qs: query,
|
||||
body,
|
||||
uri: uri || `https://api.sendgrid.com/v3/marketing/${endpoint}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(options.qs).length === 0) {
|
||||
delete options.qs;
|
||||
}
|
||||
if (Object.keys(options.body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
try {
|
||||
return this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue