diff --git a/packages/nodes-base/credentials/MailchimpApi.credentials.ts b/packages/nodes-base/credentials/MailchimpApi.credentials.ts new file mode 100644 index 0000000000..407d112f53 --- /dev/null +++ b/packages/nodes-base/credentials/MailchimpApi.credentials.ts @@ -0,0 +1,23 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + +export class Mailchimp implements ICredentialType { + name = 'mailchimpApi'; + displayName = 'Mailchimp API'; + properties = [ + { + displayName: 'API Key', + name: 'apiKey', + type: 'string' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Datacenter', + name: 'datacenter', + type: 'string' as NodePropertyTypes, + default: '', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Mailchimp/GenericFunctions.ts b/packages/nodes-base/nodes/Mailchimp/GenericFunctions.ts new file mode 100644 index 0000000000..27fda7364a --- /dev/null +++ b/packages/nodes-base/nodes/Mailchimp/GenericFunctions.ts @@ -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 { // 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; + } +} diff --git a/packages/nodes-base/nodes/Mailchimp/Mailchimp.node.ts b/packages/nodes-base/nodes/Mailchimp/Mailchimp.node.ts new file mode 100644 index 0000000000..f2b3eca677 --- /dev/null +++ b/packages/nodes-base/nodes/Mailchimp/Mailchimp.node.ts @@ -0,0 +1,117 @@ +import { + IExecuteSingleFunctions, +} from 'n8n-core'; +import { + IDataObject, + INodeTypeDescription, + INodeExecutionData, + INodeType, + ILoadOptionsFunctions, + INodePropertyOptions, +} from 'n8n-workflow'; +import { + mailchimpApiRequest, +} from './GenericFunctions'; + +export class Mailchimp implements INodeType { + + description: INodeTypeDescription = { + displayName: 'Mailchimp', + name: 'mailchimp', + icon: 'file:mailchimp.png', + group: ['output'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Consume Mailchimp API', + defaults: { + name: 'Mailchimp', + color: '#c02428', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'mailchimpApi', + required: true, + } + ], + properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Member', + value: 'member', + description: 'Add member to list', + }, + ], + default: '', + required: true, + description: 'Resource to consume.', + }, + { + displayName: 'Operation', + name: 'operation', + type: 'options', + required: true, + displayOptions: { + show: { + resource: [ + 'member', + ], + }, + }, + options: [ + { + name: 'Create', + value: 'create', + description: 'Create a new member', + }, + ], + default: '', + description: 'The operation to perform.', + }, + ] + }; + + + methods = { + loadOptions: { + // Get all the available projects to display them to user so that he can + // select them easily + async getLists(this: ILoadOptionsFunctions): Promise { + const returnData: INodePropertyOptions[] = []; + let lists, response; + try { + response = await mailchimpApiRequest.call(this, '/lists', 'GET'); + lists = response.lists; + } catch (err) { + throw new Error(`Mailchimp Error: ${err}`); + } + for (const list of lists) { + const listName = list.name; + const listId = list.id; + + returnData.push({ + name: listName, + value: listId, + }); + } + + return returnData; + }, + } + }; + + async executeSingle(this: IExecuteSingleFunctions): Promise { + + const resource = this.getNodeParameter('resource') as string; + const opeation = this.getNodeParameter('operation') as string; + + return { + json: {} + }; + } +} diff --git a/packages/nodes-base/nodes/Mailchimp/mailchimp.png b/packages/nodes-base/nodes/Mailchimp/mailchimp.png new file mode 100644 index 0000000000..a4998396c3 Binary files /dev/null and b/packages/nodes-base/nodes/Mailchimp/mailchimp.png differ diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 0343726121..6de583c5b3 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -42,7 +42,8 @@ "dist/credentials/HttpHeaderAuth.credentials.js", "dist/credentials/Imap.credentials.js", "dist/credentials/LinkFishApi.credentials.js", - "dist/credentials/MailgunApi.credentials.js", + "dist/credentials/MailgunApi.credentials.js", + "dist/credentials/MailchimpApi.credentials.js", "dist/credentials/MandrillApi.credentials.js", "dist/credentials/MattermostApi.credentials.js", "dist/credentials/MongoDb.credentials.js", @@ -93,7 +94,8 @@ "dist/nodes/If.node.js", "dist/nodes/Interval.node.js", "dist/nodes/LinkFish/LinkFish.node.js", - "dist/nodes/Mailgun/Mailgun.node.js", + "dist/nodes/Mailgun/Mailgun.node.js", + "dist/nodes/Mailchimp/Mailchimp.node.js", "dist/nodes/Mandrill/Mandrill.node.js", "dist/nodes/Mattermost/Mattermost.node.js", "dist/nodes/Merge.node.js",