2020-04-23 23:05:28 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2019-10-26 09:58:04 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
2019-11-04 12:29:36 -08:00
|
|
|
ILoadOptionsFunctions,
|
2019-10-26 09:58:04 -07:00
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import _ from 'lodash';
|
2021-04-16 09:33:36 -07:00
|
|
|
import { NodeApiError, NodeOperationError, } from 'n8n-workflow';
|
2019-10-26 09:58:04 -07:00
|
|
|
|
2020-04-23 23:05:28 -07:00
|
|
|
export async function mandrillApiRequest(this: IExecuteFunctions | IHookFunctions | ILoadOptionsFunctions, resource: string, method: string, action: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-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 {
|
2019-11-04 12:29:36 -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
|
|
|
|
2019-11-04 12:29:36 -08:00
|
|
|
export function getToEmailArray(toEmail: string): any { // tslint:disable-line:no-any
|
|
|
|
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 {
|
|
|
|
toEmailArray = [{
|
|
|
|
email: toEmail,
|
2020-10-22 06:46:03 -07:00
|
|
|
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
|
|
|
|
2019-11-04 12:29:36 -08:00
|
|
|
export function getTags(s: string): any[] { // tslint:disable-line:no-any
|
|
|
|
let array = [];
|
|
|
|
if (s.split(',').length > 0) {
|
|
|
|
array = s.split(',');
|
|
|
|
} else {
|
|
|
|
array = [s];
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = [];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|