2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-04-24 10:34:59 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject } from 'n8n-workflow';
|
2021-04-24 10:34:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function mailCheckApiRequest(
|
|
|
|
this: IWebhookFunctions | IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('mailcheckApi');
|
2021-04-24 10:34:59 -07:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2022-08-17 08:50:24 -07:00
|
|
|
Authorization: `Bearer ${credentials.apiKey}`,
|
2021-04-24 10:34:59 -07:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://api.mailcheck.co/v1${resource}`,
|
2021-04-24 10:34:59 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
if (Object.keys(headers).length !== 0) {
|
|
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request.call(this, options);
|
2021-04-24 10:34:59 -07:00
|
|
|
} catch (error) {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (error.response?.body?.message) {
|
2021-04-24 10:34:59 -07:00
|
|
|
// Try to return the error prettier
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new Error(
|
|
|
|
`Mailcheck error response [${error.statusCode}]: ${error.response.body.message}`,
|
|
|
|
);
|
2021-04-24 10:34:59 -07:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2022-04-14 23:00:47 -07:00
|
|
|
}
|