🚀 node-setup

This commit is contained in:
Ricardo Espinoza
2019-11-12 15:48:45 -05:00
parent 883d0936cb
commit a3594ae7a2
5 changed files with 187 additions and 2 deletions

View File

@@ -0,0 +1,43 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions
} from 'n8n-core';
export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('mailchimpApi');
const datacenter = credentials!.datacenter as string;
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const headerWithAuthentication = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
const endpoint = 'api.mailchimp.com/3.0';
const options: OptionsWithUri = {
headers: headerWithAuthentication,
method,
uri: `https://${datacenter}.${endpoint}${resource}`,
json: true
};
if (Object.keys(body).length !== 0) {
options.body = body;
}
try {
return await this.helpers.request!(options);
} catch (error) {
const errorMessage = error.response.body.message || error.response.body.Message;
if (errorMessage !== undefined) {
throw errorMessage;
}
throw error.response.body;
}
}