2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2021-01-19 15:16:25 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
2021-01-19 15:16:25 -08:00
|
|
|
ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2021-01-19 15:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function sendGridApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
endpoint: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-01-19 15:16:25 -08:00
|
|
|
const host = 'api.sendgrid.com/v3';
|
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2021-01-19 15:16:25 -08:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2021-01-19 15:16:25 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2021-03-26 00:48:52 -07:00
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await 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,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
propertyName: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-01-19 15:16:25 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let uri;
|
|
|
|
|
|
|
|
do {
|
2023-03-30 04:59:59 -07:00
|
|
|
responseData = await sendGridApiRequest.call(this, endpoint, method, body, query, uri); // possible bug, as function does not have uri parameter
|
2021-01-19 15:16:25 -08:00
|
|
|
uri = responseData._metadata.next;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2023-03-30 04:59:59 -07:00
|
|
|
const limit = query.limit as number | undefined;
|
|
|
|
if (limit && returnData.length >= limit) {
|
2021-01-19 15:16:25 -08:00
|
|
|
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;
|
|
|
|
}
|