2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2019-10-26 09:58:04 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2019-10-26 09:58:04 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import _ from 'lodash';
|
2022-11-08 06:28:21 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2019-10-26 09:58:04 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function mandrillApiRequest(
|
|
|
|
this: IExecuteFunctions | IHookFunctions | ILoadOptionsFunctions,
|
|
|
|
resource: string,
|
|
|
|
method: string,
|
|
|
|
action: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
headers?: object,
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('mandrillApi');
|
2019-10-26 09:58:04 -07:00
|
|
|
|
2019-11-04 12:29:36 -08:00
|
|
|
const data = Object.assign({}, body, { key: credentials.apiKey });
|
2019-10-26 09:58:04 -07:00
|
|
|
|
|
|
|
const endpoint = 'mandrillapp.com/api/1.0';
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
2019-11-04 09:29:20 -08:00
|
|
|
headers,
|
2019-10-26 09:58:04 -07:00
|
|
|
method,
|
|
|
|
uri: `https://${endpoint}${resource}${action}.json`,
|
2019-11-04 12:29:36 -08:00
|
|
|
body: data,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-04 12:29:36 -08:00
|
|
|
};
|
|
|
|
|
2019-10-26 09:58:04 -07:00
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2019-10-26 09:58:04 -07:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-11-04 12:29:36 -08:00
|
|
|
}
|
2019-10-26 09:58:04 -07:00
|
|
|
}
|
2019-10-28 14:23:25 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function getToEmailArray(toEmail: string): any {
|
2019-11-04 12:29:36 -08:00
|
|
|
let toEmailArray;
|
|
|
|
if (toEmail.split(',').length > 0) {
|
|
|
|
const array = toEmail.split(',');
|
|
|
|
toEmailArray = _.map(array, (email) => {
|
|
|
|
return {
|
|
|
|
email,
|
2020-10-22 06:46:03 -07:00
|
|
|
type: 'to',
|
2019-11-04 12:29:36 -08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
toEmailArray = [
|
|
|
|
{
|
|
|
|
email: toEmail,
|
|
|
|
type: 'to',
|
|
|
|
},
|
|
|
|
];
|
2019-11-04 12:29:36 -08:00
|
|
|
}
|
|
|
|
return toEmailArray;
|
|
|
|
}
|
2019-10-28 14:23:25 -07:00
|
|
|
|
2019-11-04 12:29:36 -08:00
|
|
|
export function getGoogleAnalyticsDomainsArray(s: string): string[] {
|
|
|
|
let array: string[] = [];
|
|
|
|
if (s.split(',').length > 0) {
|
|
|
|
array = s.split(',');
|
|
|
|
} else {
|
|
|
|
array = [s];
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
2019-10-28 14:23:25 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function getTags(s: string): any[] {
|
2019-11-04 12:29:36 -08:00
|
|
|
let array = [];
|
|
|
|
if (s.split(',').length > 0) {
|
|
|
|
array = s.split(',');
|
|
|
|
} else {
|
|
|
|
array = [s];
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2019-11-04 12:29:36 -08:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = [];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|