mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 05:17:28 -08:00
✨ Add SendGrid node (#1321)
* ✨Add SendGrid node * 👕Fix lint issue * ⚡ Improvements * ⚡ Improvements * ⚡ Improvements * ⚡ Fix SendGrid-Node Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
parent
4d446229c3
commit
70eea2d5ca
18
packages/nodes-base/credentials/SendGridApi.credentials.ts
Normal file
18
packages/nodes-base/credentials/SendGridApi.credentials.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import {
|
||||
ICredentialType,
|
||||
NodePropertyTypes,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export class SendGridApi implements ICredentialType {
|
||||
name = 'sendGridApi';
|
||||
displayName = 'SendGrid API';
|
||||
documentationUrl = 'sendgrid';
|
||||
properties = [
|
||||
{
|
||||
displayName: 'API Key',
|
||||
name: 'apiKey',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: '',
|
||||
},
|
||||
];
|
||||
}
|
404
packages/nodes-base/nodes/SendGrid/ContactDescription.ts
Normal file
404
packages/nodes-base/nodes/SendGrid/ContactDescription.ts
Normal file
|
@ -0,0 +1,404 @@
|
|||
import {
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export const contactOperations = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create/Update',
|
||||
value: 'upsert',
|
||||
description: 'Create/update a contact',
|
||||
},
|
||||
{
|
||||
name: 'Delete',
|
||||
value: 'delete',
|
||||
description: 'Delete a contact',
|
||||
},
|
||||
{
|
||||
name: 'Get',
|
||||
value: 'get',
|
||||
description: 'Get a contact by ID',
|
||||
},
|
||||
{
|
||||
name: 'Get All',
|
||||
value: 'getAll',
|
||||
description: 'Get all contacts',
|
||||
},
|
||||
],
|
||||
default: 'upsert',
|
||||
description: 'The operation to perform.',
|
||||
},
|
||||
] as INodeProperties[];
|
||||
|
||||
export const contactFields = [
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* contact:getAll */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
operation: [
|
||||
'getAll',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'If set to true, all the results will be returned.',
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
operation: [
|
||||
'getAll',
|
||||
],
|
||||
returnAll: [
|
||||
false,
|
||||
],
|
||||
},
|
||||
},
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
maxValue: 1000,
|
||||
},
|
||||
default: 100,
|
||||
description: 'How many results to return.',
|
||||
},
|
||||
{
|
||||
displayName: 'Filters',
|
||||
name: 'filters',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
operation: [
|
||||
'getAll',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Query',
|
||||
name: 'query',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'The query field accepts valid <a href="https://sendgrid.com/docs/for-developers/sending-email/segmentation-query-language/" target="_blank">SGQL</a> for searching for a contact.',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* contact:create */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Email',
|
||||
name: 'email',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'upsert',
|
||||
],
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'Primary email for the contact.',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
operation: [
|
||||
'upsert',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Address',
|
||||
name: 'addressUi',
|
||||
placeholder: 'Address',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: false,
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
name: 'addressValues',
|
||||
displayName: 'Address',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Address Line 1',
|
||||
name: 'address1',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Address Line 2',
|
||||
name: 'address2',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Alternate Emails',
|
||||
name: 'alternateEmails',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'City',
|
||||
name: 'city',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Country',
|
||||
name: 'country',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'First Name',
|
||||
name: 'firstName',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Last Name',
|
||||
name: 'lastName',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Postal Code',
|
||||
name: 'postalCode',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'State/Province/Region',
|
||||
name: 'stateProvinceRegion',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'List IDs',
|
||||
name: 'listIdsUi',
|
||||
placeholder: 'List IDs',
|
||||
description: 'Adds a custom field to set also values which have not been predefined.',
|
||||
type: 'fixedCollection',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
name: 'listIdValues',
|
||||
displayName: 'List IDs',
|
||||
values: [
|
||||
{
|
||||
displayName: 'List IDs',
|
||||
name: 'listIds',
|
||||
type: 'multiOptions',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getListIds',
|
||||
},
|
||||
default: '',
|
||||
description: 'ID of the field to set.',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Custom Fields',
|
||||
name: 'customFieldsUi',
|
||||
placeholder: 'Add Custom Fields',
|
||||
description: 'Adds custom fields',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
name: 'customFieldValues',
|
||||
displayName: 'Field',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Field ID',
|
||||
name: 'fieldId',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getCustomFields',
|
||||
},
|
||||
default: '',
|
||||
description: 'ID of the field',
|
||||
},
|
||||
{
|
||||
displayName: 'Field Value',
|
||||
name: 'fieldValue',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'Value for the field',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* contact:delete */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Contact IDs',
|
||||
name: 'ids',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
operation: [
|
||||
'delete',
|
||||
],
|
||||
deleteAll: [
|
||||
false,
|
||||
],
|
||||
},
|
||||
},
|
||||
description: 'ID of the contact. Multiple can be added separated by comma.',
|
||||
},
|
||||
{
|
||||
displayName: 'Delete All',
|
||||
name: 'deleteAll',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
operation: [
|
||||
'delete',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'If set to true, all contacts will be deleted.',
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* contact:get */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'By',
|
||||
name: 'by',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'ID',
|
||||
value: 'id',
|
||||
},
|
||||
{
|
||||
name: 'Email',
|
||||
value: 'email',
|
||||
},
|
||||
],
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: 'id',
|
||||
description: 'Search the user by ID or email.',
|
||||
},
|
||||
{
|
||||
displayName: 'Contact ID',
|
||||
name: 'contactId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
by: [
|
||||
'id',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'ID of the contact.',
|
||||
},
|
||||
{
|
||||
displayName: 'Email',
|
||||
name: 'email',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
by: [
|
||||
'email',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'Email of the contact.',
|
||||
},
|
||||
] as INodeProperties[];
|
74
packages/nodes-base/nodes/SendGrid/GenericFunctions.ts
Normal file
74
packages/nodes-base/nodes/SendGrid/GenericFunctions.ts
Normal file
|
@ -0,0 +1,74 @@
|
|||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IExecuteSingleFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export async function sendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, qs: IDataObject = {}, uri?: string | undefined): Promise<any> { // tslint:disable-line:no-any
|
||||
const credentials = this.getCredentials('sendGridApi') as IDataObject;
|
||||
|
||||
const host = 'api.sendgrid.com/v3';
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${credentials.apiKey}`,
|
||||
},
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
uri: uri || `https://${host}${endpoint}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
try {
|
||||
//@ts-ignore
|
||||
return await this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
if (error.response && error.response.body && error.response.body.errors) {
|
||||
|
||||
let errors = error.response.body.errors;
|
||||
|
||||
errors = errors.map((e: IDataObject) => e.message);
|
||||
// Try to return the error prettier
|
||||
throw new Error(
|
||||
`SendGrid error response [${error.statusCode}]: ${errors.join('|')}`,
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendGridApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, endpoint: string, method: string, propertyName: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
let responseData;
|
||||
|
||||
let uri;
|
||||
|
||||
do {
|
||||
responseData = await sendGridApiRequest.call(this, endpoint, method, body, query, uri);
|
||||
uri = responseData._metadata.next;
|
||||
returnData.push.apply(returnData, responseData[propertyName]);
|
||||
if (query.limit && returnData.length >= query.limit) {
|
||||
return returnData;
|
||||
}
|
||||
} while (
|
||||
responseData._metadata.next !== undefined
|
||||
);
|
||||
|
||||
return returnData;
|
||||
}
|
233
packages/nodes-base/nodes/SendGrid/ListDescription.ts
Normal file
233
packages/nodes-base/nodes/SendGrid/ListDescription.ts
Normal file
|
@ -0,0 +1,233 @@
|
|||
import {
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export const listOperations = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create',
|
||||
value: 'create',
|
||||
description: 'Create a list',
|
||||
},
|
||||
{
|
||||
name: 'Delete',
|
||||
value: 'delete',
|
||||
description: 'Delete a list',
|
||||
},
|
||||
{
|
||||
name: 'Get',
|
||||
value: 'get',
|
||||
description: 'Get a list',
|
||||
},
|
||||
{
|
||||
name: 'Get All',
|
||||
value: 'getAll',
|
||||
description: 'Get all lists',
|
||||
},
|
||||
{
|
||||
name: 'Update',
|
||||
value: 'update',
|
||||
description: 'Update a list',
|
||||
},
|
||||
],
|
||||
default: 'create',
|
||||
description: 'The operation to perform.',
|
||||
},
|
||||
] as INodeProperties[];
|
||||
|
||||
export const listFields = [
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* list:getAll */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
operation: [
|
||||
'getAll',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'If set to true, all the results will be returned.',
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
operation: [
|
||||
'getAll',
|
||||
],
|
||||
returnAll: [
|
||||
false,
|
||||
],
|
||||
},
|
||||
},
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
maxValue: 1000,
|
||||
},
|
||||
default: 100,
|
||||
description: 'How many results to return.',
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* list:create */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Name',
|
||||
name: 'name',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'create',
|
||||
],
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'Name of the list.',
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* list:delete */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'List ID',
|
||||
name: 'listId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'delete',
|
||||
],
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'ID of the list.',
|
||||
},
|
||||
{
|
||||
displayName: 'Delete Contacts',
|
||||
name: 'deleteContacts',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'delete',
|
||||
],
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
description: 'Delete all contacts on the list.',
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* list:get */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'List ID',
|
||||
name: 'listId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'ID of the list.',
|
||||
},
|
||||
{
|
||||
displayName: 'Contact Sample',
|
||||
name: 'contactSample',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
description: 'Return the contact sample.',
|
||||
},
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* list:update */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'List ID',
|
||||
name: 'listId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'update',
|
||||
],
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'ID of the list.',
|
||||
},
|
||||
{
|
||||
displayName: 'Name',
|
||||
name: 'name',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'update',
|
||||
],
|
||||
resource: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'Name of the list.',
|
||||
},
|
||||
] as INodeProperties[];
|
294
packages/nodes-base/nodes/SendGrid/SendGrid.node.ts
Normal file
294
packages/nodes-base/nodes/SendGrid/SendGrid.node.ts
Normal file
|
@ -0,0 +1,294 @@
|
|||
import {
|
||||
IExecuteFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
INodePropertyOptions,
|
||||
INodeType,
|
||||
INodeTypeDescription
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
listFields,
|
||||
listOperations,
|
||||
} from './ListDescription';
|
||||
|
||||
import {
|
||||
contactFields,
|
||||
contactOperations
|
||||
} from './ContactDescription';
|
||||
|
||||
import {
|
||||
sendGridApiRequest,
|
||||
sendGridApiRequestAllItems,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class SendGrid implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'SendGrid',
|
||||
name: 'sendGrid',
|
||||
icon: 'file:sendGrid.svg',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
|
||||
description: 'Consume SendGrid API',
|
||||
defaults: {
|
||||
name: 'SendGrid',
|
||||
color: '#1A82E2',
|
||||
},
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'sendGridApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
// Node properties which the user gets displayed and
|
||||
// can change on the node.
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Contact',
|
||||
value: 'contact',
|
||||
},
|
||||
{
|
||||
name: 'List',
|
||||
value: 'list',
|
||||
},
|
||||
],
|
||||
default: 'list',
|
||||
required: true,
|
||||
description: 'Resource to consume',
|
||||
},
|
||||
...listOperations,
|
||||
...listFields,
|
||||
...contactOperations,
|
||||
...contactFields,
|
||||
],
|
||||
};
|
||||
|
||||
methods ={
|
||||
loadOptions: {
|
||||
// Get custom fields to display to user so that they can select them easily
|
||||
async getCustomFields(this: ILoadOptionsFunctions,):Promise<INodePropertyOptions[]>{
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const { custom_fields } = await sendGridApiRequest.call(this, '/marketing/field_definitions', 'GET', {}, {});
|
||||
if (custom_fields !== undefined) {
|
||||
for (const customField of custom_fields){
|
||||
returnData.push({
|
||||
name: customField.name,
|
||||
value: customField.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
// Get lists to display to user so that they can select them easily
|
||||
async getListIds(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const lists = await sendGridApiRequestAllItems.call(this, `/marketing/lists`, 'GET', 'result', {}, {});
|
||||
for (const list of lists) {
|
||||
returnData.push({
|
||||
name: list.name,
|
||||
value: list.id,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const length = (items.length as unknown) as number;
|
||||
const qs: IDataObject = {};
|
||||
let responseData;
|
||||
const returnData: IDataObject[] = [];
|
||||
const resource = this.getNodeParameter('resource', 0) as string;
|
||||
const operation = this.getNodeParameter('operation', 0) as string;
|
||||
// https://sendgrid.com/docs/api-reference/
|
||||
if (resource === 'contact') {
|
||||
if (operation === 'getAll') {
|
||||
for (let i = 0; i < length; i++) {
|
||||
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
||||
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
||||
let endpoint = '/marketing/contacts';
|
||||
let method = 'GET';
|
||||
const body: IDataObject = {};
|
||||
if (filters.query && filters.query !== '') {
|
||||
endpoint = '/marketing/contacts/search';
|
||||
method = 'POST';
|
||||
Object.assign(body, { query: filters.query });
|
||||
}
|
||||
responseData = await sendGridApiRequestAllItems.call(this, endpoint, method, 'result', body, qs);
|
||||
if (returnAll === false) {
|
||||
const limit = this.getNodeParameter('limit', i) as number;
|
||||
responseData = responseData.splice(0, limit);
|
||||
}
|
||||
returnData.push.apply(returnData, responseData);
|
||||
}
|
||||
}
|
||||
if (operation === 'get') {
|
||||
const by = this.getNodeParameter('by', 0) as string;
|
||||
let endpoint;
|
||||
let method;
|
||||
const body: IDataObject = {};
|
||||
for (let i = 0; i < length; i++) {
|
||||
if (by === 'id') {
|
||||
method = 'GET';
|
||||
const contactId = this.getNodeParameter('contactId', i) as string;
|
||||
endpoint = `/marketing/contacts/${contactId}`;
|
||||
} else {
|
||||
const email = this.getNodeParameter('email', i) as string;
|
||||
endpoint = '/marketing/contacts/search';
|
||||
method = 'POST';
|
||||
Object.assign(body, { query: `email LIKE '${email}' `});
|
||||
}
|
||||
responseData = await sendGridApiRequest.call(this, endpoint, method, body, qs);
|
||||
responseData = responseData.result || responseData;
|
||||
if (Array.isArray(responseData)) {
|
||||
responseData = responseData[0];
|
||||
}
|
||||
returnData.push(responseData);
|
||||
}
|
||||
}
|
||||
if (operation === 'upsert') {
|
||||
const contacts = [];
|
||||
for (let i = 0; i < length; i++) {
|
||||
const email = this.getNodeParameter('email',i) as string;
|
||||
const additionalFields = this.getNodeParameter(
|
||||
'additionalFields',
|
||||
i,
|
||||
) as IDataObject;
|
||||
const contact: IDataObject = {
|
||||
email,
|
||||
};
|
||||
if (additionalFields.addressUi) {
|
||||
const addressValues = (additionalFields.addressUi as IDataObject).addressValues as IDataObject;
|
||||
const addressLine1 = addressValues.address1 as string;
|
||||
const addressLine2 = addressValues.address2 as string;
|
||||
if (addressLine2){
|
||||
Object.assign(contact, { address_line_2: addressLine2 });
|
||||
}
|
||||
Object.assign(contact, { address_line_1: addressLine1 });
|
||||
}
|
||||
if (additionalFields.city) {
|
||||
const city = additionalFields.city as string;
|
||||
Object.assign(contact, { city });
|
||||
}
|
||||
if (additionalFields.country) {
|
||||
const country = additionalFields.country as string;
|
||||
Object.assign(contact, { country });
|
||||
}
|
||||
if (additionalFields.firstName) {
|
||||
const firstName = additionalFields.firstName as string;
|
||||
Object.assign(contact, { first_name: firstName });
|
||||
}
|
||||
if (additionalFields.lastName) {
|
||||
const lastName = additionalFields.lastName as string;
|
||||
Object.assign(contact, { last_name:lastName});
|
||||
}
|
||||
if (additionalFields.postalCode) {
|
||||
const postalCode = additionalFields.postalCode as string;
|
||||
Object.assign(contact, { postal_code: postalCode });
|
||||
}
|
||||
if (additionalFields.stateProvinceRegion) {
|
||||
const stateProvinceRegion = additionalFields.stateProvinceRegion as string;
|
||||
Object.assign(contact, { state_province_region: stateProvinceRegion });
|
||||
}
|
||||
if (additionalFields.alternateEmails) {
|
||||
const alternateEmails = ((additionalFields.alternateEmails as string).split(',') as string[]).filter(email => !!email);
|
||||
if (alternateEmails.length !== 0) {
|
||||
Object.assign(contact, { alternate_emails: alternateEmails });
|
||||
}
|
||||
}
|
||||
if (additionalFields.listIdsUi) {
|
||||
const listIdValues = (additionalFields.listIdsUi as IDataObject).listIdValues as IDataObject;
|
||||
const listIds = listIdValues.listIds as IDataObject[];
|
||||
Object.assign(contact, { list_ids: listIds });
|
||||
}
|
||||
if (additionalFields.customFieldsUi) {
|
||||
const customFields = (additionalFields.customFieldsUi as IDataObject).customFieldValues as IDataObject[];
|
||||
if (customFields) {
|
||||
const data = customFields.reduce((obj, value) => Object.assign(obj, { [`${value.fieldId}`]: value.fieldValue }), {});
|
||||
Object.assign(contact, { custom_fields: data });
|
||||
}
|
||||
}
|
||||
contacts.push(contact);
|
||||
}
|
||||
responseData = await sendGridApiRequest.call(this, '/marketing/contacts', 'PUT', { contacts }, qs);
|
||||
|
||||
console.log('contacts');
|
||||
console.log(contacts);
|
||||
console.log('responseData');
|
||||
console.log(responseData);
|
||||
returnData.push(responseData);
|
||||
}
|
||||
if (operation === 'delete') {
|
||||
for (let i = 0; i < length; i++) {
|
||||
const deleteAll = this.getNodeParameter('deleteAll', i) as boolean;
|
||||
if(deleteAll === true) {
|
||||
qs.delete_all_contacts = 'true';
|
||||
}
|
||||
qs.ids = (this.getNodeParameter('ids',i) as string).replace(/\s/g, '');
|
||||
responseData = await sendGridApiRequest.call(this, `/marketing/contacts`, 'DELETE', {}, qs);
|
||||
returnData.push(responseData);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (resource === 'list') {
|
||||
if (operation === 'getAll'){
|
||||
for (let i = 0; i < length; i++) {
|
||||
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
||||
responseData = await sendGridApiRequestAllItems.call(this, `/marketing/lists`, 'GET', 'result', {}, qs);
|
||||
if (returnAll === false) {
|
||||
const limit = this.getNodeParameter('limit', i) as number;
|
||||
responseData = responseData.splice(0, limit);
|
||||
}
|
||||
returnData.push.apply(returnData, responseData);
|
||||
}
|
||||
}
|
||||
if (operation === 'get') {
|
||||
for (let i = 0; i < length; i++) {
|
||||
const listId = this.getNodeParameter('listId',i) as string;
|
||||
qs.contact_sample = this.getNodeParameter('contactSample', i) as boolean;
|
||||
responseData = await sendGridApiRequest.call(this, `/marketing/lists/${listId}`, 'GET', {}, qs);
|
||||
returnData.push(responseData);
|
||||
}
|
||||
}
|
||||
if (operation === 'create') {
|
||||
for (let i = 0; i < length; i++) {
|
||||
const name = this.getNodeParameter('name',i) as string;
|
||||
responseData = await sendGridApiRequest.call(this, '/marketing/lists', 'POST', { name }, qs);
|
||||
returnData.push(responseData);
|
||||
}
|
||||
}
|
||||
if (operation === 'delete') {
|
||||
for (let i = 0; i < length; i++) {
|
||||
const listId = this.getNodeParameter('listId',i) as string;
|
||||
qs.delete_contacts = this.getNodeParameter('deleteContacts', i) as boolean;
|
||||
responseData = await sendGridApiRequest.call(this, `/marketing/lists/${listId}`, 'DELETE', {}, qs);
|
||||
responseData = { success: true };
|
||||
returnData.push(responseData);
|
||||
}
|
||||
}
|
||||
if (operation=== 'update'){
|
||||
for (let i = 0; i < length; i++) {
|
||||
const name = this.getNodeParameter('name',i) as string;
|
||||
const listId = this.getNodeParameter('listId',i) as string;
|
||||
responseData = await sendGridApiRequest.call(this, `/marketing/lists/${listId}`, 'PATCH', { name }, qs);
|
||||
returnData.push(responseData);
|
||||
}
|
||||
}
|
||||
}
|
||||
return [this.helpers.returnJsonArray(returnData)];
|
||||
}
|
||||
}
|
1
packages/nodes-base/nodes/SendGrid/sendGrid.svg
Normal file
1
packages/nodes-base/nodes/SendGrid/sendGrid.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 66 65" fill="#fff" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round"><use xlink:href="#A" x=".5" y=".5"/><symbol id="A" overflow="visible"><g stroke="none" fill-rule="nonzero"><path d="M0 21.25h21.374v21.374H0z"/><path d="M0 21.25h21.374v21.374H0z" fill="#99e1f4"/><path d="M21.374 42.626h21.25v21.25h-21.25z"/><path d="M21.374 42.626h21.25v21.25h-21.25z" fill="#99e1f4"/><path d="M0 63.877h21.374V64H0zm0-21.25h21.374v21.25H0z" fill="#1a82e2"/><path d="M21.374 0h21.25v21.25h-21.25zm21.252 21.374H64v21.25H42.626z" fill="#00b3e3"/><path d="M21.374 42.626h21.25V21.25h-21.25z" fill="#009dd9"/><g fill="#1a82e2"><path d="M42.626 0H64v21.25H42.626z"/><path d="M42.626 21.25H64v.123H42.626z"/></g></g></symbol></svg>
|
After Width: | Height: | Size: 840 B |
|
@ -183,6 +183,7 @@
|
|||
"dist/credentials/SalesforceOAuth2Api.credentials.js",
|
||||
"dist/credentials/SalesmateApi.credentials.js",
|
||||
"dist/credentials/SegmentApi.credentials.js",
|
||||
"dist/credentials/SendGridApi.credentials.js",
|
||||
"dist/credentials/SendyApi.credentials.js",
|
||||
"dist/credentials/SentryIoApi.credentials.js",
|
||||
"dist/credentials/SentryIoServerApi.credentials.js",
|
||||
|
@ -428,6 +429,7 @@
|
|||
"dist/nodes/Salesforce/Salesforce.node.js",
|
||||
"dist/nodes/Set.node.js",
|
||||
"dist/nodes/SentryIo/SentryIo.node.js",
|
||||
"dist/nodes/SendGrid/SendGrid.node.js",
|
||||
"dist/nodes/Shopify/Shopify.node.js",
|
||||
"dist/nodes/Shopify/ShopifyTrigger.node.js",
|
||||
"dist/nodes/Signl4/Signl4.node.js",
|
||||
|
|
Loading…
Reference in a new issue