2023-12-05 02:17:08 -08:00
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2021-04-24 10:34:59 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
2021-04-24 10:34:59 -07:00
|
|
|
ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
2021-04-24 10:34:59 -07:00
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} 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,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
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
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
let options: IRequestOptions = {
|
2021-04-24 10:34:59 -07:00
|
|
|
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);
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2021-04-24 10:34:59 -07:00
|
|
|
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
|
2023-12-05 02:17:08 -08:00
|
|
|
throw new ApplicationError(
|
2022-08-17 08:50:24 -07:00
|
|
|
`Mailcheck error response [${error.statusCode}]: ${error.response.body.message}`,
|
2023-12-05 02:17:08 -08:00
|
|
|
{ level: 'warning' },
|
2022-08-17 08:50:24 -07:00
|
|
|
);
|
2021-04-24 10:34:59 -07:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2022-04-14 23:00:47 -07:00
|
|
|
}
|