From 2ba03345f6203e813f8fd7023ccb1a0c467f766e Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sun, 7 Jul 2019 19:00:05 +0200 Subject: [PATCH] :sparkles: Add Airtable-Node --- .../nodes/Airtable/Airtable.node.ts | 567 ++++++++++++++++++ .../nodes/Airtable/GenericFunctions.ts | 103 ++++ .../nodes-base/nodes/Airtable/airtable.png | Bin 0 -> 2388 bytes packages/nodes-base/package.json | 2 + 4 files changed, 672 insertions(+) create mode 100644 packages/nodes-base/nodes/Airtable/Airtable.node.ts create mode 100644 packages/nodes-base/nodes/Airtable/GenericFunctions.ts create mode 100644 packages/nodes-base/nodes/Airtable/airtable.png diff --git a/packages/nodes-base/nodes/Airtable/Airtable.node.ts b/packages/nodes-base/nodes/Airtable/Airtable.node.ts new file mode 100644 index 0000000000..6480efc46c --- /dev/null +++ b/packages/nodes-base/nodes/Airtable/Airtable.node.ts @@ -0,0 +1,567 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; +import { + IDataObject, + ILoadOptionsFunctions, + INodePropertyOptions, + INodeTypeDescription, + INodeExecutionData, + INodeType, +} from 'n8n-workflow'; + +import { + apiRequest, + apiRequestAllItems, +} from './GenericFunctions'; + +export class Airtable implements INodeType { + description: INodeTypeDescription = { + displayName: 'Airtable', + name: 'airtable', + icon: 'file:airtable.png', + group: ['input'], + version: 1, + description: 'Read, update, write and delete data to Airtable', + defaults: { + name: 'Airtable', + color: '#445599', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'airtableApi', + required: true, + } + ], + properties: [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + options: [ + { + name: 'Append', + value: 'append', + description: 'Appends the data to a table', + }, + { + name: 'Delete', + value: 'delete', + description: 'Deletes data from a table' + }, + { + name: 'List', + value: 'list', + description: 'List data from a table' + }, + { + name: 'Read', + value: 'read', + description: 'Reads data from a table' + }, + { + name: 'Update', + value: 'update', + description: 'Updates data in a table' + }, + ], + default: 'read', + description: 'The operation to perform.', + }, + + // ---------------------------------- + // All + // ---------------------------------- + { + displayName: 'Application', + name: 'application', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getApplications', + }, + options: [], + default: '', + required: true, + description: 'The application to access', + }, + { + displayName: 'Table', + name: 'table', + type: 'string', + default: '', + placeholder: 'Stories', + required: true, + description: 'The name of table to access', + }, + + // ---------------------------------- + // append + // ---------------------------------- + { + displayName: 'Add All Fields', + name: 'addAllFields', + type: 'boolean', + displayOptions: { + show: { + operation: [ + 'append', + ], + }, + }, + default: true, + description: 'If all fields should be send to Airtable or only specific ones.', + }, + { + displayName: 'Fields', + name: 'fields', + type: 'string', + typeOptions: { + multipleValues: true, + multipleValueButtonText: 'Add Field', + }, + displayOptions: { + show: { + addAllFields: [ + false, + ], + operation: [ + 'append', + ], + }, + }, + default: '', + placeholder: 'Name', + required: true, + description: 'The name of fields of which the data should be send to Airtable.', + }, + + // ---------------------------------- + // delete + // ---------------------------------- + { + displayName: 'Id', + name: 'id', + type: 'string', + displayOptions: { + show: { + operation: [ + 'delete', + ], + }, + }, + default: '', + required: true, + description: 'Id of the record to delete.', + }, + + // ---------------------------------- + // list + // ---------------------------------- + { + displayName: 'Return All', + name: 'returnAll', + type: 'boolean', + displayOptions: { + show: { + operation: [ + 'list', + ], + }, + }, + default: true, + description: 'If all results should be returned or only up to a given limit.', + }, + { + displayName: 'Limit', + name: 'limit', + type: 'number', + displayOptions: { + show: { + operation: [ + 'list', + ], + returnAll: [ + false, + ], + }, + }, + typeOptions: { + minValue: 1, + maxValue: 100, + }, + default: 100, + description: 'How many results to return.', + }, + + { + displayName: 'Additional Options', + name: 'additionalOptions', + type: 'collection', + displayOptions: { + show: { + operation: [ + 'list' + ], + }, + }, + default: {}, + description: 'Additional options which decide which records should be returned', + placeholder: 'Add Option', + options: [ + { + displayName: 'Fields', + name: 'fields', + type: 'string', + typeOptions: { + multipleValues: true, + multipleValueButtonText: 'Add Field', + }, + default: '', + placeholder: 'Name', + description: 'Only data for fields whose names are in this list will be included in the records.', + }, + { + displayName: 'Filter By Formula', + name: 'filterByFormula', + type: 'string', + default: '', + placeholder: 'NOT({Name} = \'\')', + description: 'A formula used to filter records. The formula will be evaluated for each
record, and if the result is not 0, false, "", NaN, [], or #Error!
the record will be included in the response.', + }, + { + displayName: 'Sort', + name: 'sort', + placeholder: 'Add Sort Rule', + description: 'Defines how the returned records should be ordered.', + type: 'fixedCollection', + typeOptions: { + multipleValues: true, + }, + default: {}, + options: [ + { + name: 'property', + displayName: 'Property', + values: [ + { + displayName: 'Field', + name: 'field', + type: 'string', + default: '', + description: 'Name of the field to sort on.', + }, + { + displayName: 'Direction', + name: 'direction', + type: 'options', + options: [ + { + name: 'ASC', + value: 'asc', + description: 'Sort in ascending order (small -> large)', + }, + { + name: 'DESC', + value: 'desc', + description: 'Sort in descending order (large -> small)' + }, + ], + default: 'asc', + description: 'The sort direction.', + }, + ] + }, + ], + }, + { + displayName: 'View', + name: 'view', + type: 'string', + default: '', + placeholder: 'All Stories', + description: 'The name or ID of a view in the Stories table. If set,
only the records in that view will be returned. The records
will be sorted according to the order of the view.', + }, + ], + }, + + // ---------------------------------- + // read + // ---------------------------------- + { + displayName: 'Id', + name: 'id', + type: 'string', + displayOptions: { + show: { + operation: [ + 'read', + ], + }, + }, + default: '', + required: true, + description: 'Id of the record to return.', + }, + + // ---------------------------------- + // update + // ---------------------------------- + { + displayName: 'Id', + name: 'id', + type: 'string', + displayOptions: { + show: { + operation: [ + 'update', + ], + }, + }, + default: '', + required: true, + description: 'Id of the record to update.', + }, + { + displayName: 'Update All Fields', + name: 'updateAllFields', + type: 'boolean', + displayOptions: { + show: { + operation: [ + 'update', + ], + }, + }, + default: true, + description: 'If all fields should be send to Airtable or only specific ones.', + }, + { + displayName: 'Fields', + name: 'fields', + type: 'string', + typeOptions: { + multipleValues: true, + multipleValueButtonText: 'Add Field', + }, + displayOptions: { + show: { + updateAllFields: [ + false, + ], + operation: [ + 'update', + ], + }, + }, + default: '', + placeholder: 'Name', + required: true, + description: 'The name of fields of which the data should be send to Airtable.', + }, + ], + }; + + methods = { + loadOptions: { + // Get all the available applications to display them to user so that he can + // select them easily + async getApplications(this: ILoadOptionsFunctions): Promise { + const endpoint = 'meta'; + const responseData = await apiRequest.call(this, 'GET', endpoint, {}); + + if (responseData.applications === undefined) { + throw new Error('No data got returned'); + } + + const returnData: INodePropertyOptions[] = []; + for (const baseData of responseData.applications) { + returnData.push({ + name: baseData.name, + value: baseData.id, + }); + } + + return returnData; + }, + }, + }; + + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + let responseData; + + const operation = this.getNodeParameter('operation', 0) as string; + + const application = this.getNodeParameter('application', 0) as string; + const table = this.getNodeParameter('table', 0) as string; + + let returnAll = false; + let endpoint = ''; + let requestMethod = ''; + + const body: IDataObject = {}; + const qs: IDataObject = {}; + + if (operation === 'append') { + // ---------------------------------- + // append + // ---------------------------------- + + requestMethod = 'POST'; + endpoint = `${application}/${table}`; + + let addAllFields: boolean; + let fields: string[]; + for (let i = 0; i < items.length; i++) { + addAllFields = this.getNodeParameter('addAllFields', i) as boolean; + + if (addAllFields === true) { + // Add all the fields the item has + body.fields = items[i].json; + } else { + // Add only the specified fields + body.fields = {} as IDataObject; + + fields = this.getNodeParameter('fields', i, []) as string[]; + + for (const fieldName of fields) { + // @ts-ignore + body.fields[fieldName] = items[i].json[fieldName]; + } + } + + responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs); + + returnData.push(responseData); + } + + } else if (operation === 'delete') { + requestMethod = 'DELETE'; + + let id: string; + for (let i = 0; i < items.length; i++) { + id = this.getNodeParameter('id', i) as string; + + endpoint = `${application}/${table}/${id}`; + + // Make one request after another. This is slower but makes + // sure that we do not run into the rate limit they have in + // place and so block for 30 seconds. Later some global + // functionality in core should make it easy to make requests + // according to specific rules like not more than 5 requests + // per seconds. + responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs); + + returnData.push(responseData); + } + + } else if (operation === 'list') { + // ---------------------------------- + // list + // ---------------------------------- + + requestMethod = 'GET'; + endpoint = `${application}/${table}`; + + returnAll = this.getNodeParameter('returnAll', 0) as boolean; + + const additionalOptions = this.getNodeParameter('additionalOptions', 0, {}) as IDataObject; + + for (const key of Object.keys(additionalOptions)) { + if (key === 'sort' && (additionalOptions.sort as IDataObject).property !== undefined) { + qs[key] = (additionalOptions[key] as IDataObject).property; + } else { + qs[key] = additionalOptions[key]; + } + } + + if (returnAll === true) { + responseData = await apiRequestAllItems.call(this, requestMethod, endpoint, body, qs); + } else { + qs.maxRecords = this.getNodeParameter('limit', 0) as number; + responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs); + } + + returnData.push.apply(returnData, responseData.records); + + } else if (operation === 'read') { + // ---------------------------------- + // read + // ---------------------------------- + + requestMethod = 'GET'; + + let id: string; + for (let i = 0; i < items.length; i++) { + + id = this.getNodeParameter('id', i) as string; + + endpoint = `${application}/${table}/${id}`; + + // Make one request after another. This is slower but makes + // sure that we do not run into the rate limit they have in + // place and so block for 30 seconds. Later some global + // functionality in core should make it easy to make requests + // according to specific rules like not more than 5 requests + // per seconds. + + responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs); + + returnData.push(responseData); + } + + } else if (operation === 'update') { + // ---------------------------------- + // update + // ---------------------------------- + + requestMethod = 'PATCH'; + + let id: string; + let updateAllFields: boolean; + let fields: string[]; + for (let i = 0; i < items.length; i++) { + updateAllFields = this.getNodeParameter('updateAllFields', i) as boolean; + + if (updateAllFields === true) { + // Update all the fields the item has + body.fields = items[i].json; + } else { + // Update only the specified fields + body.fields = {} as IDataObject; + + fields = this.getNodeParameter('fields', i, []) as string[]; + + for (const fieldName of fields) { + // @ts-ignore + body.fields[fieldName] = items[i].json[fieldName]; + } + } + + id = this.getNodeParameter('id', i) as string; + + endpoint = `${application}/${table}/${id}`; + + // Make one request after another. This is slower but makes + // sure that we do not run into the rate limit they have in + // place and so block for 30 seconds. Later some global + // functionality in core should make it easy to make requests + // according to specific rules like not more than 5 requests + // per seconds. + + responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs); + + returnData.push(responseData); + } + + } else { + throw new Error(`The operation "${operation}" is not known!`); + } + + return [this.helpers.returnJsonArray(returnData)]; + } +} diff --git a/packages/nodes-base/nodes/Airtable/GenericFunctions.ts b/packages/nodes-base/nodes/Airtable/GenericFunctions.ts new file mode 100644 index 0000000000..de73443644 --- /dev/null +++ b/packages/nodes-base/nodes/Airtable/GenericFunctions.ts @@ -0,0 +1,103 @@ +import { + IExecuteFunctions, + IHookFunctions, + ILoadOptionsFunctions, +} from 'n8n-core'; + +import { OptionsWithUri } from 'request'; +import { IDataObject } from 'n8n-workflow'; + + +/** + * Make an API request to Airtable + * + * @param {IHookFunctions} this + * @param {string} method + * @param {string} url + * @param {object} body + * @returns {Promise} + */ +export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject): Promise { // tslint:disable-line:no-any + const credentials = this.getCredentials('airtableApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + query = query || {}; + + // For some reason for some endpoints the bearer auth does not work + // and it returns 404 like for the /meta request. So we always send + // it as query string. + query.api_key = credentials.apiKey; + + const options: OptionsWithUri = { + headers: { + }, + method, + body, + qs: query, + uri: `https://api.airtable.com/v0/${endpoint}`, + json: true, + }; + + try { + return await this.helpers.request!(options); + } catch (error) { + if (error.statusCode === 401) { + // Return a clear error + throw new Error('The Airtable credentials are not valid!'); + } + + if (error.response && error.response.body && error.response.body.error) { + // Try to return the error prettier + + const airtableError = error.response.body.error; + + if (airtableError.type && airtableError.message) { + throw new Error(`Airtable error response [${airtableError.type}]: ${airtableError.message}`); + } + } + + // Expected error data did not get returned so rhow the actual error + throw error; + } +} + + +/** + * Make an API request to paginated Airtable endpoint + * and return all results + * + * @export + * @param {(IHookFunctions | IExecuteFunctions)} this + * @param {string} method + * @param {string} endpoint + * @param {IDataObject} body + * @param {IDataObject} [query] + * @returns {Promise} + */ +export async function apiRequestAllItems(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise { // tslint:disable-line:no-any + + if (query === undefined) { + query = {}; + } + query.pageSize = 100; + + const returnData: IDataObject[] = []; + + let responseData; + + do { + responseData = await apiRequest.call(this, method, endpoint, body, query); + returnData.push.apply(returnData, responseData.records); + + query.offset = responseData.offset; + } while ( + responseData.offset !== undefined + ); + + return { + records: returnData + }; +} diff --git a/packages/nodes-base/nodes/Airtable/airtable.png b/packages/nodes-base/nodes/Airtable/airtable.png new file mode 100644 index 0000000000000000000000000000000000000000..c9466bff962ddbdbfe3168373608ed6a0fc4d067 GIT binary patch literal 2388 zcmV-a39I&rP)Nyy^q5coPZj;~qE0B3+Rz!~5Sa0WO7oB_@N4_C9KcNt3_&f0)BD=<|C zcGLGe==?-BP!^;QU2}l?pN|5K5^$P^UZl}Il&fE&z8ps}O2Q2&k52{@cNycw+5h0Q z?nOXtLZ71@J{9;*960C#taPlWu@2_~tqI^83NVqz>7()b=s2IgH`{?3F~F^*<|)hq zzQv|LSD%AM6SHVDF(L5~N@EYgp;a5d56sIZ?uQnC0#>x6D83yrwvqap=ssnHC438@ zx{+8}5%Sar5KSyanOYBv$TL9I8(gn{Y$%64u?iFMIQrQ!6vj6qEcTLO^?vz)2c#uP zns#jx+OPnH^3!n9IZyQ~X^fE^#| ziseV)*B*sklp0-{34rMa3;j7N>w=qYhS$CabL4}_(&Fi^a(ciKNw-4=kN6eZ#B+33 zFeH^V16(!qQwFoVgk#EYkU$KpKYv2$B_53wZ+mPUU965&so>g5k5n6o1`KxeD+`c^ zg5F7`=4Dz}ek|k2hL{>F)}Dh?dK;X1Ml44Y*X%n?bNE-JBxl8H`A?m9RW+Mu!>I-f^V zN$qT&53bhWuqnW;RWq8-XsF7Ig^pecdp4K5{M(&(ioWWnG`BP4SSU@FmxCz}Koj|7 zOQyujPmN(i2t8;-kgxx5$@G2yC|%aOBffmhM~xHevfWr*P>@(aSN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