🔀 Merge branch 'feature/coda-node' of https://github.com/RicardoE105/n8n into RicardoE105-feature/coda-node

This commit is contained in:
Jan Oberhauser 2019-12-16 12:01:59 -06:00
commit a565c69b5d
6 changed files with 736 additions and 0 deletions

View file

@ -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: '',
},
];
}

View file

@ -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<INodePropertyOptions[]> {
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<INodeExecutionData[][]> {
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)];
}
}

View file

@ -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<any> { // 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<any> { // 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;
}

View file

@ -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</br>
they're easily prone to being changed by users.</br>
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</br>
they're easily prone to being changed by users.</br>
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.</br>
This is generally discouraged as it is fragile. If columns are renamed,</br>
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</br>
they're easily prone to being changed by users.</br>
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.</br>
This is generally discouraged as it is fragile. If columns are renamed,</br>
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</br>
they're easily prone to being changed by users.</br>
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[];

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -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",