Small adjustments on Mailchimp node

This commit is contained in:
Jan Oberhauser
2019-11-15 21:44:46 +01:00
parent 325fff318e
commit b5544cba94
4 changed files with 40 additions and 41 deletions

View File

@@ -7,9 +7,8 @@ import {
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
export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: 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!');
@@ -17,13 +16,19 @@ export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctio
const headerWithAuthentication = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
const endpoint = 'api.mailchimp.com/3.0';
if (!(credentials.apiKey as string).includes('-')) {
throw new Error('The API key is not valid!');
}
const datacenter = (credentials.apiKey as string).split('-').pop();
const host = 'api.mailchimp.com/3.0';
const options: OptionsWithUri = {
headers: headerWithAuthentication,
method,
uri: `https://${datacenter}.${endpoint}${resource}`,
json: true
uri: `https://${datacenter}.${host}${endpoint}`,
json: true,
};
if (Object.keys(body).length !== 0) {