Add Iterable Node (#1215)

*  Iterable Node

*  Improvements

*  Improvements

*  Small improvements to Iterable-Node

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2020-12-03 02:05:54 -05:00 committed by GitHub
parent 5d7840b1f6
commit d426586006
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 791 additions and 0 deletions

View file

@ -0,0 +1,18 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class IterableApi implements ICredentialType {
name = 'iterableApi';
displayName = 'Iterable API';
documentationUrl = 'iterable';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,145 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const eventOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'event',
],
},
},
options: [
{
name: 'Track',
value: 'track',
description: 'Record the actions a user perform',
},
],
default: 'track',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const eventFields = [
/* -------------------------------------------------------------------------- */
/* event:track */
/* -------------------------------------------------------------------------- */
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'event',
],
operation: [
'track',
],
},
},
description: 'The name of the event to track.',
default: '',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'event',
],
operation: [
'track',
],
},
},
options: [
{
displayName: 'Campaign ID',
name: 'campaignId',
type: 'string',
default: '',
description: `Campaign tied to conversion`,
},
{
displayName: 'Created At',
name: 'createdAt',
type: 'dateTime',
default: '',
description: `Time event happened.`,
},
{
displayName: 'Data Fields',
name: 'dataFieldsUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Data Field',
typeOptions: {
multipleValues: true,
},
options: [
{
name: 'dataFieldValues',
displayName: 'Data Field',
values: [
{
displayName: 'Key',
name: 'key',
type: 'string',
default: '',
description: 'The end event specified key of the event defined data.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'The end event specified value of the event defined data.',
},
],
},
],
},
{
displayName: 'Email',
name: 'email',
type: 'string',
default: '',
description: `Either email or userId must be passed in to identify the user. If both are passed in, email takes precedence.`,
},
{
displayName: 'ID',
name: 'id',
type: 'string',
default: '',
description: `Optional event id. If an event exists with that id, the event will be updated. If none is specified, a new id will automatically be generated and returned.`,
},
{
displayName: 'Template ID',
name: 'templateId',
type: 'string',
default: '',
description: `Template id`,
},
{
displayName: 'User ID',
name: 'userId',
type: 'string',
default: '',
description: `userId that was passed into the updateUser call`,
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,71 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function iterableApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('iterableApi') as IDataObject;
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
'Api_Key': credentials.apiKey,
},
method,
body,
qs,
uri: uri || `https://api.iterable.com/api${resource}`,
json: true,
};
try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
//@ts-ignore
return await this.helpers.request.call(this, options);
} catch (error) {
if (error.response && error.response.body && error.response.body.msg) {
const message = error.response.body.msg;
// Try to return the error prettier
throw new Error(
`Iterable error response [${error.statusCode}]: ${message}`,
);
}
throw error;
}
}
export async function iterableApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
query.maxResults = 100;
do {
responseData = await iterableApiRequest.call(this, method, endpoint, body, query);
query.pageToken = responseData['nextPageToken'];
returnData.push.apply(returnData, responseData[propertyName]);
} while (
responseData['nextPageToken'] !== undefined &&
responseData['nextPageToken'] !== ''
);
return returnData;
}

View file

@ -0,0 +1,239 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
iterableApiRequest,
} from './GenericFunctions';
import {
eventFields,
eventOperations,
} from './EventDescription';
import {
userFields,
userOperations,
} from './UserDescription';
import * as moment from 'moment-timezone';
export class Iterable implements INodeType {
description: INodeTypeDescription = {
displayName: 'Iterable',
name: 'iterable',
icon: 'file:iterable.png',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Iterable API.',
defaults: {
name: 'Iterable',
color: '#725ed8',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'iterableApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Event',
value: 'event',
},
{
name: 'User',
value: 'user',
},
],
default: 'user',
description: 'The resource to operate on.',
},
...eventOperations,
...eventFields,
...userOperations,
...userFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
const timezone = this.getTimezone();
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
if (resource === 'event') {
if (operation === 'track') {
// https://api.iterable.com/api/docs#events_trackBulk
const events = [];
for (let i = 0; i < length; i++) {
const name = this.getNodeParameter('name', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (!additionalFields.email && !additionalFields.id) {
throw new Error('Either email or userId must be passed in to identify the user. Please add one of both via "Additional Fields". If both are passed in, email takes precedence.');
}
const body: IDataObject = {
eventName: name,
};
Object.assign(body, additionalFields);
if (body.dataFieldsUi) {
const dataFields = (body.dataFieldsUi as IDataObject).dataFieldValues as IDataObject[];
const data: IDataObject = {};
for (const dataField of dataFields) {
data[dataField.key as string] = dataField.value;
}
body.dataFields = data;
delete body.dataFieldsUi;
}
if (body.createdAt) {
body.createdAt = moment.tz(body.createdAt, timezone).unix();
}
events.push(body);
}
responseData = await iterableApiRequest.call(this, 'POST', '/events/trackBulk', { events });
returnData.push(responseData);
}
}
if (resource === 'user') {
if (operation === 'upsert') {
// https://api.iterable.com/api/docs#users_updateUser
for (let i = 0; i < length; i++) {
const identifier = this.getNodeParameter('identifier', i) as string;
const value = this.getNodeParameter('value', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IDataObject = {};
if (identifier === 'email') {
body.email = value;
} else {
body.preferUserId = this.getNodeParameter('preferUserId', i) as boolean;
body.userId = value;
}
Object.assign(body, additionalFields);
if (body.dataFieldsUi) {
const dataFields = (body.dataFieldsUi as IDataObject).dataFieldValues as IDataObject[];
const data: IDataObject = {};
for (const dataField of dataFields) {
data[dataField.key as string] = dataField.value;
}
body.dataFields = data;
delete body.dataFieldsUi;
}
responseData = await iterableApiRequest.call(this, 'POST', '/users/update', body);
if (this.continueOnFail() === false) {
if (responseData.code !== 'Success') {
throw new Error(
`Iterable error response [400]: ${responseData.msg}`,
);
}
}
returnData.push(responseData);
}
}
if (operation === 'delete') {
// https://api.iterable.com/api/docs#users_delete
// https://api.iterable.com/api/docs#users_delete_0
for (let i = 0; i < length; i++) {
const by = this.getNodeParameter('by', i) as string;
let endpoint;
if (by === 'email') {
const email = this.getNodeParameter('email', i) as string;
endpoint = `/users/${email}`;
} else {
const userId = this.getNodeParameter('userId', i) as string;
endpoint = `/users/byUserId/${userId}`;
}
responseData = await iterableApiRequest.call(this, 'DELETE', endpoint);
if (this.continueOnFail() === false) {
if (responseData.code !== 'Success') {
throw new Error(
`Iterable error response [400]: ${responseData.msg}`,
);
}
}
returnData.push(responseData);
}
}
if (operation === 'get') {
// https://api.iterable.com/api/docs#users_getUser
// https://api.iterable.com/api/docs#users_getUserById
for (let i = 0; i < length; i++) {
const by = this.getNodeParameter('by', i) as string;
let endpoint;
if (by === 'email') {
const email = this.getNodeParameter('email', i) as string;
endpoint = `/users/getByEmail`;
qs.email = email;
} else {
const userId = this.getNodeParameter('userId', i) as string;
endpoint = `/users/byUserId/${userId}`;
}
responseData = await iterableApiRequest.call(this, 'GET', endpoint, {}, qs);
if (this.continueOnFail() === false) {
if (Object.keys(responseData).length === 0) {
throw new Error(
`Iterable error response [404]: User not found`,
);
}
}
responseData = responseData.user || {};
returnData.push(responseData);
}
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,316 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const userOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'user',
],
},
},
options: [
{
name: 'Create/Update',
value: 'upsert',
description: 'Create/Update a user',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a user',
},
{
name: 'Get',
value: 'get',
description: 'Get a user',
},
],
default: 'upsert',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const userFields = [
/* -------------------------------------------------------------------------- */
/* user:upsert */
/* -------------------------------------------------------------------------- */
{
displayName: 'Identifier',
name: 'identifier',
type: 'options',
required: true,
options: [
{
name: 'Email',
value: 'email',
},
{
name: 'User ID',
value: 'userId',
},
],
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'upsert',
],
},
},
default: '',
description: 'Identifier to be used',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'upsert',
],
},
},
default: '',
},
{
displayName: `Create If Doesn't exist`,
name: 'preferUserId',
type: 'boolean',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'upsert',
],
identifier: [
'userId',
],
},
},
default: true,
description: 'Create a new user if the idetifier does not exist.',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'upsert',
],
},
},
options: [
{
displayName: 'Data Fields',
name: 'dataFieldsUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Data Field',
typeOptions: {
multipleValues: true,
},
options: [
{
name: 'dataFieldValues',
displayName: 'Data Field',
values: [
{
displayName: 'Key',
name: 'key',
type: 'string',
default: '',
description: 'The end user specified key of the user defined data.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'The end user specified value of the user defined data.',
},
],
},
],
},
{
displayName: 'Merge Nested Objects',
name: 'mergeNestedObjects',
type: 'boolean',
default: false,
description: `Merge top level objects instead of overwriting (default: false).<br>
e.g. if user profile has data: {mySettings:{mobile:true}} and change contact field has data: {mySettings:{email:true}},<br>
the resulting profile: {mySettings:{mobile:true,email:true}}`,
},
],
},
/* -------------------------------------------------------------------------- */
/* user:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'By',
name: 'by',
type: 'options',
required: true,
options: [
{
name: 'Email',
value: 'email',
},
{
name: 'User ID',
value: 'userId',
},
],
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'delete',
],
},
},
default: 'email',
description: 'Identifier to be used',
},
{
displayName: 'User ID',
name: 'userId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'delete',
],
by: [
'userId',
],
},
},
default: '',
description: 'Unique identifier for a particular user',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'delete',
],
by: [
'email',
],
},
},
default: '',
description: 'Email for a particular user',
},
/* -------------------------------------------------------------------------- */
/* user:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'By',
name: 'by',
type: 'options',
required: true,
options: [
{
name: 'Email',
value: 'email',
},
{
name: 'User ID',
value: 'userId',
},
],
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'get',
],
},
},
default: 'email',
description: 'Identifier to be used',
},
{
displayName: 'User ID',
name: 'userId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'get',
],
by: [
'userId',
],
},
},
default: '',
description: 'Unique identifier for a particular user',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'get',
],
by: [
'email',
],
},
},
default: '',
description: 'Email for a particular user',
},
] as INodeProperties[];

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -106,6 +106,7 @@
"dist/credentials/HubspotOAuth2Api.credentials.js",
"dist/credentials/HumanticAiApi.credentials.js",
"dist/credentials/HunterApi.credentials.js",
"dist/credentials/IterableApi.credentials.js",
"dist/credentials/Imap.credentials.js",
"dist/credentials/IntercomApi.credentials.js",
"dist/credentials/InvoiceNinjaApi.credentials.js",
@ -327,6 +328,7 @@
"dist/nodes/HumanticAI/HumanticAi.node.js",
"dist/nodes/Hunter/Hunter.node.js",
"dist/nodes/If.node.js",
"dist/nodes/Iterable/Iterable.node.js",
"dist/nodes/Intercom/Intercom.node.js",
"dist/nodes/Interval.node.js",
"dist/nodes/InvoiceNinja/InvoiceNinja.node.js",