n8n/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts

92 lines
1.8 KiB
TypeScript
Raw Normal View History

import type { OptionsWithUri } from 'request';
2019-10-26 09:58:04 -07:00
import map from 'lodash/map';
import type {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2019-10-26 09:58:04 -07:00
export async function mandrillApiRequest(
this: IExecuteFunctions | IHookFunctions | ILoadOptionsFunctions,
resource: string,
method: string,
action: string,
body: any = {},
headers?: object,
): Promise<any> {
const credentials = await this.getCredentials('mandrillApi');
2019-10-26 09:58:04 -07: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`,
body: data,
2020-10-22 06:46:03 -07:00
json: true,
};
2019-10-26 09:58:04 -07:00
try {
return await this.helpers.request(options);
2019-10-26 09:58:04 -07:00
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
2019-10-26 09:58:04 -07:00
}
2019-10-28 14:23:25 -07:00
export function getToEmailArray(toEmail: string): 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',
};
});
} else {
toEmailArray = [
{
email: toEmail,
type: 'to',
},
];
}
return toEmailArray;
}
2019-10-28 14:23:25 -07: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
export function getTags(s: string): any[] {
let array = [];
if (s.split(',').length > 0) {
array = s.split(',');
} else {
array = [s];
}
return array;
}
export function validateJSON(json: string | undefined): any {
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = [];
}
return result;
}