2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-01-19 15:16:25 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-11-11 07:07:50 -08:00
|
|
|
import { IDataObject } from 'n8n-workflow';
|
2021-01-19 15:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function sendGridApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
endpoint: string,
|
|
|
|
method: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-01-19 15:16:25 -08:00
|
|
|
const host = 'api.sendgrid.com/v3';
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2021-03-26 00:48:52 -07:00
|
|
|
uri: `https://${host}${endpoint}`,
|
2021-01-19 15:16:25 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2021-03-26 00:48:52 -07:00
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, 'sendGridApi', options);
|
2021-01-19 15:16:25 -08:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function sendGridApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
endpoint: string,
|
|
|
|
method: string,
|
|
|
|
propertyName: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-01-19 15:16:25 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let uri;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await sendGridApiRequest.call(this, endpoint, method, body, query, uri);
|
|
|
|
uri = responseData._metadata.next;
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
if (query.limit && returnData.length >= query.limit) {
|
|
|
|
return returnData;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData._metadata.next !== undefined);
|
2021-01-19 15:16:25 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|