Some changes that Code-Node works identical to other similar nodes

This commit is contained in:
Jan Oberhauser 2019-12-16 17:52:15 -06:00
parent a565c69b5d
commit a26ff6362b
2 changed files with 250 additions and 154 deletions

View file

@ -14,9 +14,9 @@ import {
codaApiRequestAllItems, codaApiRequestAllItems,
} from './GenericFunctions'; } from './GenericFunctions';
import { import {
rowOpeations, tableFields,
rowFields tableOperations,
} from './RowDescription'; } from './TableDescription';
export class Coda implements INodeType { export class Coda implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -46,18 +46,16 @@ export class Coda implements INodeType {
type: 'options', type: 'options',
options: [ options: [
{ {
name: 'Rows', name: 'Table',
value: 'row', value: 'table',
description: `You'll likely use this part of the API the most. description: `Access data of tables in documents.`,
These endpoints let you retrieve row data from tables in Coda as well
as create, upsert, update, and delete them.`,
}, },
], ],
default: 'row', default: 'table',
description: 'Resource to consume.', description: 'Resource to consume.',
}, },
...rowOpeations, ...tableOperations,
...rowFields, ...tableFields,
], ],
}; };
@ -84,6 +82,29 @@ export class Coda implements INodeType {
} }
return returnData; return returnData;
}, },
// Get all the available tables to display them to user so that he can
// select them easily
async getTables(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let tables;
const docId = this.getCurrentNodeParameter('docId');
try {
tables = await codaApiRequestAllItems.call(this, 'items', 'GET', `/docs/${docId}/tables`, {});
} catch (err) {
throw new Error(`Coda Error: ${err}`);
}
for (const table of tables) {
const tableName = table.name;
const tableId = table.id;
returnData.push({
name: tableName,
value: tableId,
});
}
return returnData;
},
}, },
}; };
@ -91,66 +112,110 @@ export class Coda implements INodeType {
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const items = this.getInputData(); const items = this.getInputData();
let responseData; let responseData;
const qs: IDataObject = {};
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
if (resource === 'row') {
//https://coda.io/developers/apis/v1beta1#operation/upsertRows let qs: IDataObject = {};
if (operation === 'create') {
const docId = this.getNodeParameter('docId', 0) as string; if (resource === 'table') {
const tableId = this.getNodeParameter('tableId', 0) as string; // https://coda.io/developers/apis/v1beta1#operation/upsertRows
const additionalFields = this.getNodeParameter('additionalFields', 0) as IDataObject; if (operation === 'createRow') {
const endpoint = `/docs/${docId}/tables/${tableId}/rows`; const sendData = {} as IDataObject;
if (additionalFields.keyColumns) { for (let i = 0; i < items.length; i++) {
// @ts-ignore qs = {};
items[0].json['keyColumns'] = additionalFields.keyColumns.split(',') as string[]; const docId = this.getNodeParameter('docId', i) as string;
const tableId = this.getNodeParameter('tableId', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const endpoint = `/docs/${docId}/tables/${tableId}/rows`;
if (additionalFields.keyColumns) {
// @ts-ignore
items[i].json['keyColumns'] = additionalFields.keyColumns.split(',') as string[];
}
if (additionalFields.disableParsing) {
qs.disableParsing = additionalFields.disableParsing as boolean;
}
const cells = [];
cells.length = 0;
for (const key of Object.keys(items[i].json)) {
cells.push({
column: key,
value: items[i].json[key],
});
}
// Collect all the data for the different docs/tables
if (sendData[endpoint] === undefined) {
sendData[endpoint] = {
rows: [],
// TODO: This is not perfect as it ignores if qs changes between
// different items but should be OK for now
qs,
};
}
((sendData[endpoint]! as IDataObject).rows! as IDataObject[]).push({ cells });
} }
if (additionalFields.disableParsing) {
qs.disableParsing = additionalFields.disableParsing as boolean; // Now that all data got collected make all the requests
} for (const endpoint of Object.keys(sendData)) {
try { await codaApiRequest.call(this, 'POST', endpoint, sendData[endpoint], (sendData[endpoint]! as IDataObject).qs! as IDataObject);
responseData = await codaApiRequest.call(this, 'POST', endpoint, items[0].json, qs);
} catch (err) {
throw new Error(`Coda Error: ${err.message}`);
} }
// Return the incoming data
return [items];
} }
//https://coda.io/developers/apis/v1beta1#operation/getRow // https://coda.io/developers/apis/v1beta1#operation/getRow
if (operation === 'get') { if (operation === 'getRow') {
const docId = this.getNodeParameter('docId', 0) as string; for (let i = 0; i < items.length; i++) {
const tableId = this.getNodeParameter('tableId', 0) as string; const docId = this.getNodeParameter('docId', i) as string;
const rowId = this.getNodeParameter('rowId', 0) as string; const tableId = this.getNodeParameter('tableId', i) as string;
const filters = this.getNodeParameter('filters', 0) as IDataObject; const rowId = this.getNodeParameter('rowId', i) as string;
const endpoint = `/docs/${docId}/tables/${tableId}/rows/${rowId}`; const options = this.getNodeParameter('options', i) as IDataObject;
if (filters.useColumnNames) {
qs.useColumnNames = filters.useColumnNames as boolean; const endpoint = `/docs/${docId}/tables/${tableId}/rows/${rowId}`;
} if (options.useColumnNames === false) {
if (filters.valueFormat) { qs.useColumnNames = options.useColumnNames as boolean;
qs.valueFormat = filters.valueFormat as string; } else {
} qs.useColumnNames = true;
try { }
if (options.valueFormat) {
qs.valueFormat = options.valueFormat as string;
}
responseData = await codaApiRequest.call(this, 'GET', endpoint, {}, qs); responseData = await codaApiRequest.call(this, 'GET', endpoint, {}, qs);
} catch (err) { if (options.rawData === true) {
throw new Error(`Coda Error: ${err.message}`); returnData.push(responseData);
} else {
returnData.push({
id: responseData.id,
...responseData.values
});
}
} }
return [this.helpers.returnJsonArray(returnData)];
} }
//https://coda.io/developers/apis/v1beta1#operation/listRows // https://coda.io/developers/apis/v1beta1#operation/listRows
if (operation === 'getAll') { if (operation === 'getAllRows') {
const docId = this.getNodeParameter('docId', 0) as string; const docId = this.getNodeParameter('docId', 0) as string;
const returnAll = this.getNodeParameter('returnAll', 0) as boolean; const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
const tableId = this.getNodeParameter('tableId', 0) as string; const tableId = this.getNodeParameter('tableId', 0) as string;
const filters = this.getNodeParameter('filters', 0) as IDataObject; const options = this.getNodeParameter('options', 0) as IDataObject;
const endpoint = `/docs/${docId}/tables/${tableId}/rows`; const endpoint = `/docs/${docId}/tables/${tableId}/rows`;
if (filters.useColumnNames) { if (options.useColumnNames === false) {
qs.useColumnNames = filters.useColumnNames as boolean; qs.useColumnNames = options.useColumnNames as boolean;
} else {
qs.useColumnNames = true;
} }
if (filters.valueFormat) { if (options.valueFormat) {
qs.valueFormat = filters.valueFormat as string; qs.valueFormat = options.valueFormat as string;
} }
if (filters.sortBy) { if (options.sortBy) {
qs.sortBy = filters.sortBy as string; qs.sortBy = options.sortBy as string;
} }
if (filters.visibleOnly) { if (options.visibleOnly) {
qs.visibleOnly = filters.visibleOnly as boolean; qs.visibleOnly = options.visibleOnly as boolean;
} }
try { try {
if (returnAll === true) { if (returnAll === true) {
@ -163,33 +228,46 @@ export class Coda implements INodeType {
} catch (err) { } catch (err) {
throw new Error(`Flow Error: ${err.message}`); throw new Error(`Flow Error: ${err.message}`);
} }
if (options.rawData === true) {
return [this.helpers.returnJsonArray(responseData)];
} else {
for (const item of responseData) {
returnData.push({
id: item.id,
...item.values
});
}
return [this.helpers.returnJsonArray(returnData)];
}
} }
//https://coda.io/developers/apis/v1beta1#operation/deleteRows // https://coda.io/developers/apis/v1beta1#operation/deleteRows
if (operation === 'delete') { if (operation === 'deleteRow') {
const docId = this.getNodeParameter('docId', 0) as string; const sendData = {} as IDataObject;
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++) { for (let i = 0; i < items.length; i++) {
const docId = this.getNodeParameter('docId', i) as string;
const tableId = this.getNodeParameter('tableId', i) as string;
const rowId = this.getNodeParameter('rowId', i) as string; const rowId = this.getNodeParameter('rowId', i) as string;
rowIds += rowId; const endpoint = `/docs/${docId}/tables/${tableId}/rows`;
// Collect all the data for the different docs/tables
if (sendData[endpoint] === undefined) {
sendData[endpoint] = [];
}
(sendData[endpoint] as string[]).push(rowId);
} }
// @ts-ignore
body['rowIds'] = rowIds.split(',') as string[]; // Now that all data got collected make all the requests
try { for (const endpoint of Object.keys(sendData)) {
// @ts-ignore await codaApiRequest.call(this, 'DELETE', endpoint, { rowIds: sendData[endpoint]}, qs);
responseData = await codaApiRequest.call(this, 'DELETE', endpoint, body, qs);
} catch (err) {
throw new Error(`Coda Error: ${err.message}`);
} }
}
if (Array.isArray(responseData)) { // Return the incoming data
returnData.push.apply(returnData, responseData as IDataObject[]); return [items];
} else {
returnData.push(responseData as IDataObject);
} }
} }
return [this.helpers.returnJsonArray(responseData)];
return [];
} }
} }

View file

@ -1,6 +1,6 @@
import { INodeProperties } from "n8n-workflow"; import { INodeProperties } from 'n8n-workflow';
export const rowOpeations = [ export const tableOperations = [
{ {
displayName: 'Operation', displayName: 'Operation',
name: 'operation', name: 'operation',
@ -8,41 +8,41 @@ export const rowOpeations = [
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
}, },
}, },
options: [ options: [
{ {
name: 'Create', name: 'Create Row',
value: 'create', value: 'createRow',
description: 'Create/Upsert a row', description: 'Create/Upsert a row',
}, },
{ {
name: 'Get', name: 'Get Row',
value: 'get', value: 'getRow',
description: 'Get row', description: 'Get row',
}, },
{ {
name: 'Get All', name: 'Get All Rows',
value: 'getAll', value: 'getAllRows',
description: 'Get all the rows', description: 'Get all the rows',
}, },
{ {
name: 'Delete', name: 'Delete Row',
value: 'delete', value: 'deleteRow',
description: 'Delete one or multiple rows', description: 'Delete one or multiple rows',
}, },
], ],
default: 'create', default: 'createRow',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
] as INodeProperties[]; ] as INodeProperties[];
export const rowFields = [ export const tableFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* row:create */ /* table:createRow */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'Doc', displayName: 'Doc',
@ -52,38 +52,39 @@ export const rowFields = [
typeOptions: { typeOptions: {
loadOptionsMethod: 'getDocs', loadOptionsMethod: 'getDocs',
}, },
default: [], default: '',
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'create' 'createRow',
] ]
}, },
}, },
description: 'ID of the doc.', description: 'ID of the doc.',
}, },
{ {
displayName: 'Table ID', displayName: 'Table',
name: 'tableId', name: 'tableId',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getTables',
},
required: true, required: true,
default: [], default: [],
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'create' 'createRow',
] ]
}, },
}, },
description: `ID or name of the table. Names are discouraged because</br> description: 'The table to create the row in.',
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', displayName: 'Additional Fields',
@ -94,10 +95,10 @@ export const rowFields = [
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'create', 'createRow',
], ],
}, },
}, },
@ -121,7 +122,7 @@ export const rowFields = [
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* row:get */ /* table:get */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'Doc', displayName: 'Doc',
@ -131,38 +132,39 @@ export const rowFields = [
typeOptions: { typeOptions: {
loadOptionsMethod: 'getDocs', loadOptionsMethod: 'getDocs',
}, },
default: [], default: '',
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'get' 'getRow',
] ]
}, },
}, },
description: 'ID of the doc.', description: 'ID of the doc.',
}, },
{ {
displayName: 'Table ID', displayName: 'Table',
name: 'tableId', name: 'tableId',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getTables',
},
required: true, required: true,
default: [], default: [],
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'get' 'getRow',
] ]
}, },
}, },
description: `ID or name of the table. Names are discouraged because</br> description: 'The table to get the row from.',
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', displayName: 'Row ID',
@ -173,10 +175,10 @@ export const rowFields = [
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'get' 'getRow',
] ]
}, },
}, },
@ -185,18 +187,18 @@ export const rowFields = [
If there are multiple rows with the same value in the identifying column, an arbitrary one will be selected`, If there are multiple rows with the same value in the identifying column, an arbitrary one will be selected`,
}, },
{ {
displayName: 'Filters', displayName: 'Options',
name: 'filters', name: 'options',
type: 'collection', type: 'collection',
placeholder: 'Add Filter', placeholder: 'Add Option',
default: {}, default: {},
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'get', 'getRow',
], ],
}, },
}, },
@ -210,6 +212,13 @@ export const rowFields = [
This is generally discouraged as it is fragile. If columns are renamed,</br> This is generally discouraged as it is fragile. If columns are renamed,</br>
code using original names may throw errors.`, code using original names may throw errors.`,
}, },
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: `Returns the data exactly in the way it got received from the API.`,
},
{ {
displayName: 'ValueFormat', displayName: 'ValueFormat',
name: 'valueFormat', name: 'valueFormat',
@ -234,7 +243,7 @@ export const rowFields = [
] ]
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* get:all */ /* table:getAll */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'Doc', displayName: 'Doc',
@ -244,38 +253,39 @@ export const rowFields = [
typeOptions: { typeOptions: {
loadOptionsMethod: 'getDocs', loadOptionsMethod: 'getDocs',
}, },
default: [], default: '',
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'getAll' 'getAllRows',
] ]
}, },
}, },
description: 'ID of the doc.', description: 'ID of the doc.',
}, },
{ {
displayName: 'Table ID', displayName: 'Table',
name: 'tableId', name: 'tableId',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getTables',
},
required: true, required: true,
default: [], default: [],
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'getAll' 'getAllRows',
] ]
}, },
}, },
description: `ID or name of the table. Names are discouraged because</br> description: 'The table to get the rows from.',
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', displayName: 'Return All',
@ -284,10 +294,10 @@ export const rowFields = [
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'getAll' 'getAllRows',
] ]
}, },
}, },
@ -301,10 +311,10 @@ export const rowFields = [
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'getAll' 'getAllRows',
], ],
returnAll: [ returnAll: [
false, false,
@ -319,18 +329,18 @@ export const rowFields = [
description: 'How many results to return.', description: 'How many results to return.',
}, },
{ {
displayName: 'Filters', displayName: 'Options',
name: 'filters', name: 'options',
type: 'collection', type: 'collection',
placeholder: 'Add Filter', placeholder: 'Add Option',
default: {}, default: {},
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'getAll', 'getAllRows',
], ],
}, },
}, },
@ -365,6 +375,13 @@ export const rowFields = [
], ],
description: `The format that cell values are returned as.`, description: `The format that cell values are returned as.`,
}, },
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: `Returns the data exactly in the way it got received from the API.`,
},
{ {
displayName: 'Sort By', displayName: 'Sort By',
name: 'sortBy', name: 'sortBy',
@ -403,38 +420,39 @@ export const rowFields = [
typeOptions: { typeOptions: {
loadOptionsMethod: 'getDocs', loadOptionsMethod: 'getDocs',
}, },
default: [], default: '',
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'delete' 'deleteRow',
] ]
}, },
}, },
description: 'ID of the doc.', description: 'ID of the doc.',
}, },
{ {
displayName: 'Table ID', displayName: 'Table',
name: 'tableId', name: 'tableId',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getTables',
},
required: true, required: true,
default: [], default: [],
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'delete' 'deleteRow',
] ]
}, },
}, },
description: `ID or name of the table. Names are discouraged because</br> description: 'The table to delete the row in.',
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', displayName: 'Row ID',
@ -445,14 +463,14 @@ export const rowFields = [
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'row', 'table',
], ],
operation: [ operation: [
'delete' 'deleteRow',
] ]
}, },
}, },
description: `Row IDs to delete separated by ,.`, description: 'Row IDs to delete.',
}, },
] as INodeProperties[]; ] as INodeProperties[];