2019-12-13 13:50:59 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeTypeDescription,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodePropertyOptions,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
|
|
codaApiRequest,
|
|
|
|
codaApiRequestAllItems,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
2019-12-16 15:52:15 -08:00
|
|
|
tableFields,
|
|
|
|
tableOperations,
|
|
|
|
} from './TableDescription';
|
2019-12-13 13:50:59 -08:00
|
|
|
|
|
|
|
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: [
|
|
|
|
{
|
2019-12-16 15:52:15 -08:00
|
|
|
name: 'Table',
|
|
|
|
value: 'table',
|
|
|
|
description: `Access data of tables in documents.`,
|
2019-12-13 13:50:59 -08:00
|
|
|
},
|
|
|
|
],
|
2019-12-16 15:52:15 -08:00
|
|
|
default: 'table',
|
2019-12-13 13:50:59 -08:00
|
|
|
description: 'Resource to consume.',
|
|
|
|
},
|
2019-12-16 15:52:15 -08:00
|
|
|
...tableOperations,
|
|
|
|
...tableFields,
|
2019-12-13 13:50:59 -08:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2019-12-16 08:40:44 -08:00
|
|
|
// Get all the available docs to display them to user so that he can
|
2019-12-13 13:50:59 -08:00
|
|
|
// select them easily
|
2019-12-16 08:40:44 -08:00
|
|
|
async getDocs(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2019-12-13 13:50:59 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {};
|
2019-12-16 08:40:44 -08:00
|
|
|
let docs;
|
2019-12-13 13:50:59 -08:00
|
|
|
try {
|
2019-12-16 08:40:44 -08:00
|
|
|
docs = await codaApiRequestAllItems.call(this,'items', 'GET', `/docs`, {}, qs);
|
2019-12-13 13:50:59 -08:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Coda Error: ${err}`);
|
|
|
|
}
|
2019-12-16 08:40:44 -08:00
|
|
|
for (const doc of docs) {
|
|
|
|
const docName = doc.name;
|
|
|
|
const docId = doc.id;
|
2019-12-13 13:50:59 -08:00
|
|
|
returnData.push({
|
2019-12-16 08:40:44 -08:00
|
|
|
name: docName,
|
|
|
|
value: docId,
|
2019-12-13 13:50:59 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2019-12-16 15:52:15 -08:00
|
|
|
// 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;
|
|
|
|
},
|
2019-12-13 13:50:59 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const returnData: IDataObject[] = [];
|
2019-12-14 08:42:32 -08:00
|
|
|
const items = this.getInputData();
|
2019-12-13 13:50:59 -08:00
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
let qs: IDataObject = {};
|
|
|
|
|
|
|
|
if (resource === 'table') {
|
|
|
|
// https://coda.io/developers/apis/v1beta1#operation/upsertRows
|
|
|
|
if (operation === 'createRow') {
|
|
|
|
const sendData = {} as IDataObject;
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
qs = {};
|
|
|
|
const docId = this.getNodeParameter('docId', i) as string;
|
|
|
|
const tableId = this.getNodeParameter('tableId', i) as string;
|
2019-12-16 18:43:46 -08:00
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
2019-12-16 15:52:15 -08:00
|
|
|
const endpoint = `/docs/${docId}/tables/${tableId}/rows`;
|
|
|
|
|
2019-12-16 18:43:46 -08:00
|
|
|
if (options.keyColumns) {
|
2019-12-16 15:52:15 -08:00
|
|
|
// @ts-ignore
|
2019-12-16 18:43:46 -08:00
|
|
|
items[i].json['keyColumns'] = options.keyColumns.split(',') as string[];
|
2019-12-16 15:52:15 -08:00
|
|
|
}
|
2019-12-16 18:43:46 -08:00
|
|
|
if (options.disableParsing) {
|
|
|
|
qs.disableParsing = options.disableParsing as boolean;
|
2019-12-16 15:52:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
// Now that all data got collected make all the requests
|
|
|
|
for (const endpoint of Object.keys(sendData)) {
|
|
|
|
await codaApiRequest.call(this, 'POST', endpoint, sendData[endpoint], (sendData[endpoint]! as IDataObject).qs! as IDataObject);
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
// Return the incoming data
|
|
|
|
return [items];
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
// https://coda.io/developers/apis/v1beta1#operation/getRow
|
|
|
|
if (operation === 'getRow') {
|
|
|
|
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 options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
|
|
|
|
const endpoint = `/docs/${docId}/tables/${tableId}/rows/${rowId}`;
|
|
|
|
if (options.useColumnNames === false) {
|
|
|
|
qs.useColumnNames = options.useColumnNames as boolean;
|
|
|
|
} else {
|
|
|
|
qs.useColumnNames = true;
|
|
|
|
}
|
|
|
|
if (options.valueFormat) {
|
|
|
|
qs.valueFormat = options.valueFormat as string;
|
|
|
|
}
|
|
|
|
|
2019-12-14 08:42:32 -08:00
|
|
|
responseData = await codaApiRequest.call(this, 'GET', endpoint, {}, qs);
|
2019-12-16 15:52:15 -08:00
|
|
|
if (options.rawData === true) {
|
|
|
|
returnData.push(responseData);
|
|
|
|
} else {
|
|
|
|
returnData.push({
|
|
|
|
id: responseData.id,
|
|
|
|
...responseData.values
|
|
|
|
});
|
|
|
|
}
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
// https://coda.io/developers/apis/v1beta1#operation/listRows
|
|
|
|
if (operation === 'getAllRows') {
|
2019-12-16 08:40:44 -08:00
|
|
|
const docId = this.getNodeParameter('docId', 0) as string;
|
2019-12-14 08:42:32 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
|
|
const tableId = this.getNodeParameter('tableId', 0) as string;
|
2019-12-16 15:52:15 -08:00
|
|
|
const options = this.getNodeParameter('options', 0) as IDataObject;
|
2019-12-14 08:42:32 -08:00
|
|
|
const endpoint = `/docs/${docId}/tables/${tableId}/rows`;
|
2019-12-16 15:52:15 -08:00
|
|
|
if (options.useColumnNames === false) {
|
|
|
|
qs.useColumnNames = options.useColumnNames as boolean;
|
|
|
|
} else {
|
|
|
|
qs.useColumnNames = true;
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
if (options.valueFormat) {
|
|
|
|
qs.valueFormat = options.valueFormat as string;
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
if (options.sortBy) {
|
|
|
|
qs.sortBy = options.sortBy as string;
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
if (options.visibleOnly) {
|
|
|
|
qs.visibleOnly = options.visibleOnly as boolean;
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
|
|
|
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}`);
|
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
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)];
|
|
|
|
}
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
// https://coda.io/developers/apis/v1beta1#operation/deleteRows
|
|
|
|
if (operation === 'deleteRow') {
|
|
|
|
const sendData = {} as IDataObject;
|
2019-12-16 08:40:44 -08:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
2019-12-16 15:52:15 -08:00
|
|
|
const docId = this.getNodeParameter('docId', i) as string;
|
|
|
|
const tableId = this.getNodeParameter('tableId', i) as string;
|
2019-12-16 08:40:44 -08:00
|
|
|
const rowId = this.getNodeParameter('rowId', i) as string;
|
2019-12-16 15:52:15 -08:00
|
|
|
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);
|
2019-12-16 08:40:44 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
// Now that all data got collected make all the requests
|
|
|
|
for (const endpoint of Object.keys(sendData)) {
|
|
|
|
await codaApiRequest.call(this, 'DELETE', endpoint, { rowIds: sendData[endpoint]}, qs);
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
// Return the incoming data
|
|
|
|
return [items];
|
2019-12-14 08:42:32 -08:00
|
|
|
}
|
2019-12-13 13:50:59 -08:00
|
|
|
}
|
2019-12-16 15:52:15 -08:00
|
|
|
|
|
|
|
return [];
|
2019-12-13 13:50:59 -08:00
|
|
|
}
|
|
|
|
}
|