diff --git a/packages/nodes-base/credentials/CodaApi.credentials.ts b/packages/nodes-base/credentials/CodaApi.credentials.ts new file mode 100644 index 0000000000..226dd6ee68 --- /dev/null +++ b/packages/nodes-base/credentials/CodaApi.credentials.ts @@ -0,0 +1,17 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + +export class CodaApi implements ICredentialType { + name = 'codaApi'; + displayName = 'Coda API'; + properties = [ + { + displayName: 'Access Token', + name: 'accessToken', + type: 'string' as NodePropertyTypes, + default: '', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Coda/Coda.node.ts b/packages/nodes-base/nodes/Coda/Coda.node.ts new file mode 100644 index 0000000000..4a54bf41c0 --- /dev/null +++ b/packages/nodes-base/nodes/Coda/Coda.node.ts @@ -0,0 +1,195 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; +import { + IDataObject, + INodeTypeDescription, + INodeExecutionData, + INodeType, + ILoadOptionsFunctions, + INodePropertyOptions, +} from 'n8n-workflow'; +import { + codaApiRequest, + codaApiRequestAllItems, +} from './GenericFunctions'; +import { + rowOpeations, + rowFields +} from './RowDescription'; + +export class Coda implements INodeType { + description: INodeTypeDescription = { + displayName: 'Coda', + name: 'Coda', + icon: 'file:coda.png', + group: ['output'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Consume Coda Beta API', + defaults: { + name: 'Coda', + color: '#c02428', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'codaApi', + required: true, + } + ], + properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Rows', + value: 'row', + description: `You'll likely use this part of the API the most. + These endpoints let you retrieve row data from tables in Coda as well + as create, upsert, update, and delete them.`, + }, + ], + default: 'row', + description: 'Resource to consume.', + }, + ...rowOpeations, + ...rowFields, + ], + }; + + methods = { + loadOptions: { + // Get all the available docs to display them to user so that he can + // select them easily + async getDocs(this: ILoadOptionsFunctions): Promise { + const returnData: INodePropertyOptions[] = []; + const qs = {}; + let docs; + try { + docs = await codaApiRequestAllItems.call(this,'items', 'GET', `/docs`, {}, qs); + } catch (err) { + throw new Error(`Coda Error: ${err}`); + } + for (const doc of docs) { + const docName = doc.name; + const docId = doc.id; + returnData.push({ + name: docName, + value: docId, + }); + } + return returnData; + }, + }, + }; + + async execute(this: IExecuteFunctions): Promise { + const returnData: IDataObject[] = []; + const items = this.getInputData(); + let responseData; + const qs: IDataObject = {}; + const resource = this.getNodeParameter('resource', 0) as string; + const operation = this.getNodeParameter('operation', 0) as string; + if (resource === 'row') { + //https://coda.io/developers/apis/v1beta1#operation/upsertRows + if (operation === 'create') { + const docId = this.getNodeParameter('docId', 0) as string; + const tableId = this.getNodeParameter('tableId', 0) as string; + const additionalFields = this.getNodeParameter('additionalFields', 0) as IDataObject; + const endpoint = `/docs/${docId}/tables/${tableId}/rows`; + if (additionalFields.keyColumns) { + // @ts-ignore + items[0].json['keyColumns'] = additionalFields.keyColumns.split(',') as string[]; + } + if (additionalFields.disableParsing) { + qs.disableParsing = additionalFields.disableParsing as boolean; + } + try { + responseData = await codaApiRequest.call(this, 'POST', endpoint, items[0].json, qs); + } catch (err) { + throw new Error(`Coda Error: ${err.message}`); + } + } + //https://coda.io/developers/apis/v1beta1#operation/getRow + if (operation === 'get') { + const docId = this.getNodeParameter('docId', 0) as string; + const tableId = this.getNodeParameter('tableId', 0) as string; + const rowId = this.getNodeParameter('rowId', 0) as string; + const filters = this.getNodeParameter('filters', 0) as IDataObject; + const endpoint = `/docs/${docId}/tables/${tableId}/rows/${rowId}`; + if (filters.useColumnNames) { + qs.useColumnNames = filters.useColumnNames as boolean; + } + if (filters.valueFormat) { + qs.valueFormat = filters.valueFormat as string; + } + try { + responseData = await codaApiRequest.call(this, 'GET', endpoint, {}, qs); + } catch (err) { + throw new Error(`Coda Error: ${err.message}`); + } + } + //https://coda.io/developers/apis/v1beta1#operation/listRows + if (operation === 'getAll') { + const docId = this.getNodeParameter('docId', 0) as string; + const returnAll = this.getNodeParameter('returnAll', 0) as boolean; + const tableId = this.getNodeParameter('tableId', 0) as string; + const filters = this.getNodeParameter('filters', 0) as IDataObject; + const endpoint = `/docs/${docId}/tables/${tableId}/rows`; + if (filters.useColumnNames) { + qs.useColumnNames = filters.useColumnNames as boolean; + } + if (filters.valueFormat) { + qs.valueFormat = filters.valueFormat as string; + } + if (filters.sortBy) { + qs.sortBy = filters.sortBy as string; + } + if (filters.visibleOnly) { + qs.visibleOnly = filters.visibleOnly as boolean; + } + try { + if (returnAll === true) { + responseData = await codaApiRequestAllItems.call(this, 'items', 'GET', endpoint, {}, qs); + } else { + qs.limit = this.getNodeParameter('limit', 0) as number; + responseData = await codaApiRequest.call(this, 'GET', endpoint, {}, qs); + responseData = responseData.items; + } + } catch (err) { + throw new Error(`Flow Error: ${err.message}`); + } + } + //https://coda.io/developers/apis/v1beta1#operation/deleteRows + if (operation === 'delete') { + const docId = this.getNodeParameter('docId', 0) as string; + const tableId = this.getNodeParameter('tableId', 0) as string; + const body = {}; + let rowIds = ''; + const endpoint = `/docs/${docId}/tables/${tableId}/rows`; + for (let i = 0; i < items.length; i++) { + const rowId = this.getNodeParameter('rowId', i) as string; + rowIds += rowId; + } + // @ts-ignore + body['rowIds'] = rowIds.split(',') as string[]; + try { + // @ts-ignore + responseData = await codaApiRequest.call(this, 'DELETE', endpoint, body, qs); + } catch (err) { + throw new Error(`Coda Error: ${err.message}`); + } + } + if (Array.isArray(responseData)) { + returnData.push.apply(returnData, responseData as IDataObject[]); + } else { + returnData.push(responseData as IDataObject); + } + } + return [this.helpers.returnJsonArray(responseData)]; + } +} diff --git a/packages/nodes-base/nodes/Coda/GenericFunctions.ts b/packages/nodes-base/nodes/Coda/GenericFunctions.ts new file mode 100644 index 0000000000..2d26d593a7 --- /dev/null +++ b/packages/nodes-base/nodes/Coda/GenericFunctions.ts @@ -0,0 +1,64 @@ +import { OptionsWithUri } from 'request'; +import { + IExecuteFunctions, + ILoadOptionsFunctions, + IExecuteSingleFunctions, +} from 'n8n-core'; +import { IDataObject } from 'n8n-workflow'; + +export async function codaApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any + const credentials = this.getCredentials('codaApi'); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + let options: OptionsWithUri = { + headers: { 'Authorization': `Bearer ${credentials.accessToken}`}, + method, + qs, + body, + uri: uri ||`https://coda.io/apis/v1beta1${resource}`, + json: true + }; + options = Object.assign({}, options, option); + if (Object.keys(options.body).length === 0) { + delete options.body; + } + try { + return await this.helpers.request!(options); + } catch (error) { + let errorMessage = error.message; + if (error.response.body) { + errorMessage = error.response.body.message || error.response.body.Message || error.message; + } + + throw new Error(errorMessage); + } +} + +/** + * Make an API request to paginated coda endpoint + * and return all results + */ +export async function codaApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise { // tslint:disable-line:no-any + + const returnData: IDataObject[] = []; + + let responseData; + + query.limit = 100; + + let uri: string | undefined; + + do { + responseData = await codaApiRequest.call(this, method, resource, body, query, uri); + uri = responseData.nextPageLink; + // @ts-ignore + returnData.push.apply(returnData, responseData[propertyName]); + } while ( + responseData.nextPageLink !== undefined && + responseData.nextPageLink !== '' + ); + + return returnData; +} diff --git a/packages/nodes-base/nodes/Coda/RowDescription.ts b/packages/nodes-base/nodes/Coda/RowDescription.ts new file mode 100644 index 0000000000..70529412ea --- /dev/null +++ b/packages/nodes-base/nodes/Coda/RowDescription.ts @@ -0,0 +1,458 @@ +import { INodeProperties } from "n8n-workflow"; + +export const rowOpeations = [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'row', + ], + }, + }, + options: [ + { + name: 'Create', + value: 'create', + description: 'Create/Upsert a row', + }, + { + name: 'Get', + value: 'get', + description: 'Get row', + }, + { + name: 'Get All', + value: 'getAll', + description: 'Get all the rows', + }, + { + name: 'Delete', + value: 'delete', + description: 'Delete one or multiple rows', + }, + ], + default: 'create', + description: 'The operation to perform.', + }, +] as INodeProperties[]; + +export const rowFields = [ + +/* -------------------------------------------------------------------------- */ +/* row:create */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Doc', + name: 'docId', + type: 'options', + required: true, + typeOptions: { + loadOptionsMethod: 'getDocs', + }, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'create' + ] + }, + }, + description: 'ID of the doc.', + }, + { + displayName: 'Table ID', + name: 'tableId', + type: 'string', + required: true, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'create' + ] + }, + }, + description: `ID or name of the table. Names are discouraged because
+ they're easily prone to being changed by users.
+ If you're using a name, be sure to URI-encode it.`, + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'create', + ], + }, + }, + options: [ + { + displayName: 'Key Columns', + name: 'keyColumns', + type: 'string', + default: '', + description: `Optional column IDs, URLs, or names (fragile and discouraged), + specifying columns to be used as upsert keys. If more than one separate by ,`, + }, + { + displayName: 'Disable Parsing', + name: 'disableParsing', + type: 'boolean', + default: false, + description: `If true, the API will not attempt to parse the data in any way.`, + }, + ] + }, + +/* -------------------------------------------------------------------------- */ +/* row:get */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Doc', + name: 'docId', + type: 'options', + required: true, + typeOptions: { + loadOptionsMethod: 'getDocs', + }, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'get' + ] + }, + }, + description: 'ID of the doc.', + }, + { + displayName: 'Table ID', + name: 'tableId', + type: 'string', + required: true, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'get' + ] + }, + }, + description: `ID or name of the table. Names are discouraged because
+ they're easily prone to being changed by users.
+ If you're using a name, be sure to URI-encode it.`, + }, + { + displayName: 'Row ID', + name: 'rowId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'get' + ] + }, + }, + description: `ID or name of the row. Names are discouraged because they're easily prone to being changed by users. + If you're using a name, be sure to URI-encode it. + If there are multiple rows with the same value in the identifying column, an arbitrary one will be selected`, + }, + { + displayName: 'Filters', + name: 'filters', + type: 'collection', + placeholder: 'Add Filter', + default: {}, + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'get', + ], + }, + }, + options: [ + { + displayName: 'Use Column Names', + name: 'useColumnNames', + type: 'boolean', + default: false, + description: `Use column names instead of column IDs in the returned output.
+ This is generally discouraged as it is fragile. If columns are renamed,
+ code using original names may throw errors.`, + }, + { + displayName: 'ValueFormat', + name: 'valueFormat', + type: 'options', + default: [], + options: [ + { + name: 'Simple', + value: 'simple', + }, + { + name: 'Simple With Arrays', + value: 'simpleWithArrays', + }, + { + name: 'Rich', + value: 'rich', + }, + ], + description: `The format that cell values are returned as.`, + }, + ] + }, +/* -------------------------------------------------------------------------- */ +/* get:all */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Doc', + name: 'docId', + type: 'options', + required: true, + typeOptions: { + loadOptionsMethod: 'getDocs', + }, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'getAll' + ] + }, + }, + description: 'ID of the doc.', + }, + { + displayName: 'Table ID', + name: 'tableId', + type: 'string', + required: true, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'getAll' + ] + }, + }, + description: `ID or name of the table. Names are discouraged because
+ they're easily prone to being changed by users.
+ If you're using a name, be sure to URI-encode it.`, + }, + { + displayName: 'Return All', + name: 'returnAll', + type: 'boolean', + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'getAll' + ] + }, + }, + default: false, + description: 'If all results should be returned or only up to a given limit.', + }, + { + displayName: 'Limit', + name: 'limit', + type: 'number', + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'getAll' + ], + returnAll: [ + false, + ], + }, + }, + typeOptions: { + minValue: 1, + maxValue: 100, + }, + default: 50, + description: 'How many results to return.', + }, + { + displayName: 'Filters', + name: 'filters', + type: 'collection', + placeholder: 'Add Filter', + default: {}, + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'getAll', + ], + }, + }, + options: [ + { + displayName: 'Use Column Names', + name: 'useColumnNames', + type: 'boolean', + default: false, + description: `Use column names instead of column IDs in the returned output.
+ This is generally discouraged as it is fragile. If columns are renamed,
+ code using original names may throw errors.`, + }, + { + displayName: 'ValueFormat', + name: 'valueFormat', + type: 'options', + default: [], + options: [ + { + name: 'Simple', + value: 'simple', + }, + { + name: 'Simple With Arrays', + value: 'simpleWithArrays', + }, + { + name: 'Rich', + value: 'rich', + }, + ], + description: `The format that cell values are returned as.`, + }, + { + displayName: 'Sort By', + name: 'sortBy', + type: 'options', + default: [], + options: [ + { + name: 'Created At', + value: 'createdAt', + }, + { + name: 'Natural', + value: 'natural', + }, + ], + description: `Specifies the sort order of the rows returned. + If left unspecified, rows are returned by creation time ascending.`, + }, + { + displayName: 'Visible Only', + name: 'visibleOnly', + type: 'boolean', + default: false, + description: `If true, returns only visible rows and columns for the table.`, + }, + ] + }, +/* -------------------------------------------------------------------------- */ +/* row:delete */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Doc', + name: 'docId', + type: 'options', + required: true, + typeOptions: { + loadOptionsMethod: 'getDocs', + }, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'delete' + ] + }, + }, + description: 'ID of the doc.', + }, + { + displayName: 'Table ID', + name: 'tableId', + type: 'string', + required: true, + default: [], + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'delete' + ] + }, + }, + description: `ID or name of the table. Names are discouraged because
+ they're easily prone to being changed by users.
+ If you're using a name, be sure to URI-encode it.`, + }, + { + displayName: 'Row ID', + name: 'rowId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'row', + ], + operation: [ + 'delete' + ] + }, + }, + description: `Row IDs to delete separated by ,.`, + }, + +] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Coda/coda.png b/packages/nodes-base/nodes/Coda/coda.png new file mode 100644 index 0000000000..cd74b6330f Binary files /dev/null and b/packages/nodes-base/nodes/Coda/coda.png differ diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 9790b8279b..5afa6b431c 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -32,6 +32,7 @@ "dist/credentials/AsanaApi.credentials.js", "dist/credentials/Aws.credentials.js", "dist/credentials/ChargebeeApi.credentials.js", + "dist/credentials/CodaApi.credentials.js", "dist/credentials/DropboxApi.credentials.js", "dist/credentials/FreshdeskApi.credentials.js", "dist/credentials/FileMaker.credentials.js", @@ -85,6 +86,7 @@ "dist/nodes/Chargebee/Chargebee.node.js", "dist/nodes/Chargebee/ChargebeeTrigger.node.js", "dist/nodes/Cron.node.js", + "dist/nodes/Coda/Coda.node.js", "dist/nodes/Dropbox/Dropbox.node.js", "dist/nodes/Discord/Discord.node.js", "dist/nodes/EditImage.node.js",