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

@ -13,11 +13,5 @@ export class MailchimpApi implements ICredentialType {
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
}, },
{
displayName: 'Datacenter',
name: 'datacenter',
type: 'string' as NodePropertyTypes,
default: '',
},
]; ];
} }

View file

@ -7,9 +7,8 @@ import {
IExecuteSingleFunctions IExecuteSingleFunctions
} from 'n8n-core'; } 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 credentials = this.getCredentials('mailchimpApi');
const datacenter = credentials!.datacenter as string;
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); 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 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 = { const options: OptionsWithUri = {
headers: headerWithAuthentication, headers: headerWithAuthentication,
method, method,
uri: `https://${datacenter}.${endpoint}${resource}`, uri: `https://${datacenter}.${host}${endpoint}`,
json: true json: true,
}; };
if (Object.keys(body).length !== 0) { if (Object.keys(body).length !== 0) {

View file

@ -1,26 +1,27 @@
import * as moment from 'moment';
import { import {
IExecuteSingleFunctions, IExecuteSingleFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject, IDataObject,
ILoadOptionsFunctions,
INodeTypeDescription, INodeTypeDescription,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
ILoadOptionsFunctions,
INodePropertyOptions, INodePropertyOptions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
mailchimpApiRequest, mailchimpApiRequest,
validateJSON validateJSON,
} from './GenericFunctions'; } from './GenericFunctions';
import * as moment from 'moment';
enum Status { enum Status {
subscribe = 'subscribe', subscribe = 'subscribe',
unsubscribed = 'unsubscribe', unsubscribed = 'unsubscribe',
cleaned = 'cleaned', cleaned = 'cleaned',
pending = 'pending', pending = 'pending',
transactional = 'transactional' transactional = 'transactional',
} }
interface ILocation { interface ILocation {
@ -78,7 +79,7 @@ export class Mailchimp implements INodeType {
description: 'Add member to list', description: 'Add member to list',
}, },
], ],
default: '', default: 'member',
required: true, required: true,
description: 'Resource to consume.', description: 'Resource to consume.',
}, },
@ -101,7 +102,7 @@ export class Mailchimp implements INodeType {
description: 'Create a new member on list', description: 'Create a new member on list',
}, },
], ],
default: '', default: 'create',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
{ {
@ -114,7 +115,7 @@ export class Mailchimp implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'member' 'member',
], ],
operation: [ operation: [
'create', 'create',
@ -124,7 +125,7 @@ export class Mailchimp implements INodeType {
default: '', default: '',
options: [], options: [],
required: true, required: true,
description: 'List of lists' description: 'List of lists',
}, },
{ {
displayName: 'Email', displayName: 'Email',
@ -215,7 +216,7 @@ export class Mailchimp implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource:[ resource:[
'member' 'member',
], ],
operation: [ operation: [
'create', 'create',
@ -298,12 +299,12 @@ export class Mailchimp implements INodeType {
name: 'locationFieldsUi', name: 'locationFieldsUi',
type: 'fixedCollection', type: 'fixedCollection',
placeholder: 'Add Location', placeholder: 'Add Location',
default: false, default: {},
description: `Subscriber location information.n`, description: `Subscriber location information.n`,
displayOptions: { displayOptions: {
show: { show: {
resource:[ resource:[
'member' 'member',
], ],
operation: [ operation: [
'create', 'create',
@ -324,7 +325,7 @@ export class Mailchimp implements INodeType {
type: 'string', type: 'string',
required: true, required: true,
description: 'The location latitude.', description: 'The location latitude.',
default: '' default: '',
}, },
{ {
displayName: 'Longitude', displayName: 'Longitude',
@ -332,7 +333,7 @@ export class Mailchimp implements INodeType {
type: 'string', type: 'string',
required: true, required: true,
description: 'The location longitude.', description: 'The location longitude.',
default: '' default: '',
}, },
], ],
} }
@ -343,7 +344,7 @@ export class Mailchimp implements INodeType {
name: 'mergeFieldsUi', name: 'mergeFieldsUi',
placeholder: 'Add Merge Fields', placeholder: 'Add Merge Fields',
type: 'fixedCollection', type: 'fixedCollection',
default: '', default: {},
typeOptions: { typeOptions: {
multipleValues: true, multipleValues: true,
}, },
@ -366,19 +367,19 @@ export class Mailchimp implements INodeType {
name: 'mergeFieldsValues', name: 'mergeFieldsValues',
displayName: 'Field', displayName: 'Field',
typeOptions: { typeOptions: {
multipleValueButtonText: 'Add Field' multipleValueButtonText: 'Add Field',
}, },
values: [ values: [
{ {
displayName: 'Merge Field Name', displayName: 'Field Name',
name: 'name', name: 'name',
type: 'string', type: 'string',
required: true, required: true,
description: 'Merge Field name', description: 'Merge Field name',
default: '' default: '',
}, },
{ {
displayName: 'Merge Field Value', displayName: 'Field Value',
name: 'value', name: 'value',
required: true, required: true,
type: 'string', type: 'string',
@ -394,20 +395,20 @@ export class Mailchimp implements INodeType {
name: 'mergeFieldsJson', name: 'mergeFieldsJson',
type: 'json', type: 'json',
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true alwaysOpenEditWindow: true,
}, },
default: '', default: '',
description: '', description: '',
displayOptions: { displayOptions: {
show: { show: {
resource:[ resource:[
'member' 'member',
], ],
operation: [ operation: [
'create', 'create',
], ],
jsonParameters: [ jsonParameters: [
true true,
], ],
}, },
}, },
@ -417,20 +418,20 @@ export class Mailchimp implements INodeType {
name: 'locationJson', name: 'locationJson',
type: 'json', type: 'json',
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true alwaysOpenEditWindow: true,
}, },
default: '', default: '',
description: '', description: '',
displayOptions: { displayOptions: {
show: { show: {
resource:[ resource:[
'member' 'member',
], ],
operation: [ operation: [
'create', 'create',
], ],
jsonParameters: [ jsonParameters: [
true true,
], ],
}, },
}, },
@ -442,7 +443,6 @@ export class Mailchimp implements INodeType {
methods = { methods = {
loadOptions: { loadOptions: {
// Get all the available lists to display them to user so that he can // Get all the available lists to display them to user so that he can
// select them easily // select them easily
async getLists(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getLists(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
@ -552,7 +552,7 @@ export class Mailchimp implements INodeType {
} }
} }
return { return {
json: response json: response,
}; };
} }
} }

View file

@ -42,8 +42,8 @@
"dist/credentials/HttpHeaderAuth.credentials.js", "dist/credentials/HttpHeaderAuth.credentials.js",
"dist/credentials/Imap.credentials.js", "dist/credentials/Imap.credentials.js",
"dist/credentials/LinkFishApi.credentials.js", "dist/credentials/LinkFishApi.credentials.js",
"dist/credentials/MailgunApi.credentials.js", "dist/credentials/MailchimpApi.credentials.js",
"dist/credentials/MailchimpApi.credentials.js", "dist/credentials/MailgunApi.credentials.js",
"dist/credentials/MandrillApi.credentials.js", "dist/credentials/MandrillApi.credentials.js",
"dist/credentials/MattermostApi.credentials.js", "dist/credentials/MattermostApi.credentials.js",
"dist/credentials/MongoDb.credentials.js", "dist/credentials/MongoDb.credentials.js",
@ -98,8 +98,8 @@
"dist/nodes/If.node.js", "dist/nodes/If.node.js",
"dist/nodes/Interval.node.js", "dist/nodes/Interval.node.js",
"dist/nodes/LinkFish/LinkFish.node.js", "dist/nodes/LinkFish/LinkFish.node.js",
"dist/nodes/Mailgun/Mailgun.node.js", "dist/nodes/Mailchimp/Mailchimp.node.js",
"dist/nodes/Mailchimp/Mailchimp.node.js", "dist/nodes/Mailgun/Mailgun.node.js",
"dist/nodes/Mandrill/Mandrill.node.js", "dist/nodes/Mandrill/Mandrill.node.js",
"dist/nodes/Mattermost/Mattermost.node.js", "dist/nodes/Mattermost/Mattermost.node.js",
"dist/nodes/Merge.node.js", "dist/nodes/Merge.node.js",