n8n/packages/nodes-base/nodes/Mandrill/GenericFunctions.ts
Omar Ajoue 7ce7285f7a
Load credentials from the database (#1741)
* Changes to types so that credentials can be always loaded from DB

This first commit changes all return types from the execute functions
and calls to get credentials to be async so we can use await.

This is a first step as previously credentials were loaded in memory and
always available. We will now be loading them from the DB which requires
turning the whole call chain async.

* Fix updated files

* Removed unnecessary credential loading to improve performance

* Fix typo

*  Fix issue

* Updated new nodes to load credentials async

*  Remove not needed comment

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-08-20 18:57:30 +02:00

89 lines
2 KiB
TypeScript

import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import * as _ from 'lodash';
import { NodeApiError, NodeOperationError, } from 'n8n-workflow';
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
const credentials = await this.getCredentials('mandrillApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
const data = Object.assign({}, body, { key: credentials.apiKey });
const endpoint = 'mandrillapp.com/api/1.0';
const options: OptionsWithUri = {
headers,
method,
uri: `https://${endpoint}${resource}${action}.json`,
body: data,
json: true,
};
try {
return await this.helpers.request!(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
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,
type: 'to',
};
});
} else {
toEmailArray = [{
email: toEmail,
type: 'to',
}];
}
return toEmailArray;
}
export function getGoogleAnalyticsDomainsArray(s: string): string[] {
let array: string[] = [];
if (s.split(',').length > 0) {
array = s.split(',');
} else {
array = [s];
}
return array;
}
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;
}