mirror of
https://github.com/n8n-io/n8n-nodes-starter.git
synced 2025-10-29 06:22:24 -05:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IDataObject,
|
|
IExecuteFunctions,
|
|
IExecuteSingleFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
export async function friendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
method: string, endpoint: string, body: object = {}, qs: 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,
|
|
body,
|
|
uri: uri || `https://api.sendgrid.com/v3/${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);
|
|
// tslint:disable-next-line:no-any
|
|
} catch (error: any) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|