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

102 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-10-26 09:58:04 -07:00
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions
} from 'n8n-core';
import * as _ from 'lodash';
2019-11-04 09:29:20 -08:00
import { IDataObject } from 'n8n-workflow';
2019-10-26 09:58:04 -07:00
export async function mandrillApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, action: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('mandrillApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
2019-11-04 09:29:20 -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`,
body: data,
json: true
2019-10-28 14:23:25 -07:00
};
2019-10-26 09:58:04 -07:00
try {
return await this.helpers.request!(options);
} catch (error) {
console.error(error);
const errorMessage = error.response.body.message || error.response.body.Message;
if (error.name === 'Invalid_Key') {
throw new Error('The provided API key is not a valid Mandrill API key');
} else if (error.name === 'ValidationError') {
throw new Error('The parameters passed to the API call are invalid or not provided when required');
} else if (error.name === 'GeneralError') {
throw new Error('An unexpected error occurred processing the request. Mandrill developers will be notified.');
}
if (errorMessage !== undefined) {
throw errorMessage;
}
throw error.response.body;
}
}
2019-11-04 09:29:20 -08:00
// @ts-ignore
export function getToEmailArray(toEmail: string): any {
let toEmailArray;
2019-10-26 09:58:04 -07:00
if (toEmail.split(',').length > 0) {
2019-11-04 09:29:20 -08:00
const array = toEmail.split(',');
2019-10-26 09:58:04 -07:00
toEmailArray = _.map(array, (email) => {
return {
2019-11-04 09:29:20 -08:00
email,
2019-10-26 09:58:04 -07:00
type: 'to'
2019-11-04 09:29:20 -08:00
};
});
2019-10-26 09:58:04 -07:00
} else {
toEmailArray = [{
email: toEmail,
type: 'to'
2019-11-04 09:29:20 -08:00
}];
2019-10-26 09:58:04 -07:00
}
2019-11-04 09:29:20 -08:00
return toEmailArray;
2019-10-26 09:58:04 -07:00
}
2019-10-28 14:23:25 -07:00
2019-11-04 09:29:20 -08:00
export function getGoogleAnalyticsDomainsArray(s: string): string[] {
let array: string[] = [];
if (s.split(',').length > 0) {
array = s.split(',');
2019-10-28 14:23:25 -07:00
} else {
2019-11-04 09:29:20 -08:00
array = [s];
2019-10-28 14:23:25 -07:00
}
2019-11-04 09:29:20 -08:00
return array;
2019-10-28 14:23:25 -07:00
}
2019-11-04 09:29:20 -08:00
export function getTags(s: string): Array<any> {
let array = [];
if (s.split(',').length > 0) {
array = s.split(',');
2019-10-28 14:23:25 -07:00
} else {
2019-11-04 09:29:20 -08:00
array = [s];
2019-10-28 14:23:25 -07:00
}
2019-11-04 09:29:20 -08:00
return array;
2019-10-28 14:23:25 -07:00
}
2019-11-04 09:29:20 -08:00
export function validateJSON(json: string | undefined): any {
let result;
2019-10-28 14:23:25 -07:00
try {
2019-11-04 09:29:20 -08:00
result = JSON.parse(json!);
2019-10-28 14:23:25 -07:00
} catch (exception) {
2019-11-04 09:29:20 -08:00
result = [];
2019-10-28 14:23:25 -07:00
}
2019-11-04 09:29:20 -08:00
return result;
2019-10-28 14:23:25 -07:00
}