2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, ILoadOptionsFunctions, IPollFunctions, NodeApiError } from 'n8n-workflow';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
TDtableMetadataColumns,
|
|
|
|
TDtableViewColumns,
|
|
|
|
TEndpointResolvedExpr,
|
|
|
|
TEndpointVariableName,
|
|
|
|
} from './types';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { schema } from './Schema';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
ICredential,
|
|
|
|
ICtx,
|
|
|
|
IDtableMetadataColumn,
|
|
|
|
IEndpointVariables,
|
|
|
|
IName,
|
|
|
|
IRow,
|
|
|
|
IRowObject,
|
|
|
|
} from './Interfaces';
|
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import _ from 'lodash';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function seaTableApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
|
|
|
ctx: ICtx,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
url: string | undefined = undefined,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-09-29 16:28:27 -07:00
|
|
|
const credentials = await this.getCredentials('seaTableApi');
|
|
|
|
|
|
|
|
ctx.credentials = credentials as unknown as ICredential;
|
|
|
|
|
|
|
|
await getBaseAccessToken.call(this, ctx);
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Token ${ctx?.base?.access_token}`,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
uri: url || `${resolveBaseUri(ctx)}${endpointCtxExpr(ctx, endpoint)}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function setableApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | IPollFunctions,
|
|
|
|
ctx: ICtx,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
query?: IDataObject,
|
|
|
|
): Promise<any> {
|
2021-09-29 16:28:27 -07:00
|
|
|
if (query === undefined) {
|
|
|
|
query = {};
|
|
|
|
}
|
|
|
|
const segment = schema.rowFetchSegmentLimit;
|
|
|
|
query.start = 0;
|
|
|
|
query.limit = segment;
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = (await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
method,
|
|
|
|
endpoint,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
)) as unknown as IRow[];
|
2021-09-29 16:28:27 -07:00
|
|
|
//@ts-ignore
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
query.start = +query.start + segment;
|
|
|
|
} while (responseData && responseData.length > segment - 1);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getTableColumns(
|
|
|
|
this: ILoadOptionsFunctions | IExecuteFunctions | IPollFunctions,
|
|
|
|
tableName: string,
|
|
|
|
ctx: ICtx = {},
|
|
|
|
): Promise<TDtableMetadataColumns> {
|
|
|
|
const {
|
|
|
|
metadata: { tables },
|
|
|
|
} = await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'GET',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/metadata`,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
for (const table of tables) {
|
|
|
|
if (table.name === tableName) {
|
|
|
|
return table.columns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getTableViews(
|
|
|
|
this: ILoadOptionsFunctions | IExecuteFunctions,
|
|
|
|
tableName: string,
|
|
|
|
ctx: ICtx = {},
|
|
|
|
): Promise<TDtableViewColumns> {
|
|
|
|
const { views } = await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'GET',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/views`,
|
|
|
|
{},
|
|
|
|
{ table_name: tableName },
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
return views;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getBaseAccessToken(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
|
|
|
ctx: ICtx,
|
|
|
|
) {
|
2021-09-29 16:28:27 -07:00
|
|
|
if (ctx?.base?.access_token !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Token ${ctx?.credentials?.token}`,
|
|
|
|
},
|
|
|
|
uri: `${resolveBaseUri(ctx)}/api/v2.1/dtable/app-access-token/`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.base = await this.helpers.request!(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveBaseUri(ctx: ICtx) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return ctx?.credentials?.environment === 'cloudHosted'
|
|
|
|
? 'https://cloud.seatable.io'
|
|
|
|
: userBaseUri(ctx?.credentials?.domain);
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function simplify(data: { results: IRow[] }, metadata: IDataObject) {
|
|
|
|
return data.results.map((row: IDataObject) => {
|
|
|
|
for (const key of Object.keys(row)) {
|
|
|
|
if (!key.startsWith('_')) {
|
|
|
|
row[metadata[key] as string] = row[key];
|
|
|
|
delete row[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function getColumns(data: { metadata: [{ key: string; name: string }] }) {
|
|
|
|
return data.metadata.reduce(
|
|
|
|
(obj, value) => Object.assign(obj, { [`${value.key}`]: value.name }),
|
|
|
|
{},
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function getDownloadableColumns(data: {
|
|
|
|
metadata: [{ key: string; name: string; type: string }];
|
|
|
|
}) {
|
|
|
|
return data.metadata.filter((row) => ['image', 'file'].includes(row.type)).map((row) => row.name);
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const uniquePredicate = (current: string, index: number, all: string[]) =>
|
|
|
|
all.indexOf(current) === index;
|
2021-09-29 16:28:27 -07:00
|
|
|
const nonInternalPredicate = (name: string) => !Object.keys(schema.internalNames).includes(name);
|
|
|
|
const namePredicate = (name: string) => (named: IName) => named.name === name;
|
2022-08-17 08:50:24 -07:00
|
|
|
export const nameOfPredicate = (names: ReadonlyArray<IName>) => (name: string) =>
|
|
|
|
names.find(namePredicate(name));
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
export function columnNamesToArray(columnNames: string): string[] {
|
2022-08-17 08:50:24 -07:00
|
|
|
return columnNames ? split(columnNames).filter(nonInternalPredicate).filter(uniquePredicate) : [];
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function columnNamesGlob(
|
|
|
|
columnNames: string[],
|
|
|
|
dtableColumns: TDtableMetadataColumns,
|
|
|
|
): string[] {
|
2021-09-29 16:28:27 -07:00
|
|
|
const buffer: string[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const names: string[] = dtableColumns.map((c) => c.name).filter(nonInternalPredicate);
|
|
|
|
columnNames.forEach((columnName) => {
|
2021-09-29 16:28:27 -07:00
|
|
|
if (columnName !== '*') {
|
|
|
|
buffer.push(columnName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer.push(...names);
|
|
|
|
});
|
|
|
|
return buffer.filter(uniquePredicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sequence rows on _seq
|
|
|
|
*/
|
|
|
|
export function rowsSequence(rows: IRow[]) {
|
|
|
|
const l = rows.length;
|
|
|
|
if (l) {
|
|
|
|
const [first] = rows;
|
|
|
|
if (first && first._seq !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
for (let i = 0; i < l; ) {
|
2021-09-29 16:28:27 -07:00
|
|
|
rows[i]._seq = ++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rowDeleteInternalColumns(row: IRow): IRow {
|
2022-08-17 08:50:24 -07:00
|
|
|
Object.keys(schema.internalNames).forEach((columnName) => delete row[columnName]);
|
2021-09-29 16:28:27 -07:00
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
function rowFormatColumn(input: unknown): boolean | number | string | string[] | null {
|
|
|
|
if (null === input || undefined === input) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof input === 'boolean' || typeof input === 'number' || typeof input === 'string') {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (Array.isArray(input) && input.every((i) => typeof i === 'string')) {
|
2021-09-29 16:28:27 -07:00
|
|
|
return input;
|
2022-10-11 00:51:43 -07:00
|
|
|
} else if (Array.isArray(input) && input.every((i) => typeof i === 'object')) {
|
|
|
|
const returnItems = [] as string[];
|
|
|
|
input.every((i) => returnItems.push(i.display_value));
|
|
|
|
return returnItems;
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rowFormatColumns(row: IRow, columnNames: string[]): IRow {
|
|
|
|
const outRow = {} as IRow;
|
|
|
|
columnNames.forEach((c) => (outRow[c] = rowFormatColumn(row[c])));
|
|
|
|
return outRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rowsFormatColumns(rows: IRow[], columnNames: string[]) {
|
|
|
|
rows = rows.map((row) => rowFormatColumns(row, columnNames));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rowMapKeyToName(row: IRow, columns: TDtableMetadataColumns): IRow {
|
|
|
|
const mappedRow = {} as IRow;
|
|
|
|
|
|
|
|
// move internal columns first
|
|
|
|
Object.keys(schema.internalNames).forEach((key) => {
|
|
|
|
if (row[key]) {
|
|
|
|
mappedRow[key] = row[key];
|
|
|
|
delete row[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// pick each by its key for name
|
2022-08-17 08:50:24 -07:00
|
|
|
Object.keys(row).forEach((key) => {
|
|
|
|
const column = columns.find((c) => c.key === key);
|
2021-09-29 16:28:27 -07:00
|
|
|
if (column) {
|
|
|
|
mappedRow[column.name] = row[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return mappedRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rowExport(row: IRowObject, columns: TDtableMetadataColumns): IRowObject {
|
|
|
|
for (const columnName of Object.keys(columns)) {
|
|
|
|
if (!columns.find(namePredicate(columnName))) {
|
|
|
|
delete row[columnName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const dtableSchemaIsColumn = (column: IDtableMetadataColumn): boolean =>
|
|
|
|
!!schema.columnTypes[column.type];
|
|
|
|
|
|
|
|
const dtableSchemaIsUpdateAbleColumn = (column: IDtableMetadataColumn): boolean =>
|
|
|
|
!!schema.columnTypes[column.type] && !schema.nonUpdateAbleColumnTypes[column.type];
|
|
|
|
|
|
|
|
export const dtableSchemaColumns = (columns: TDtableMetadataColumns): TDtableMetadataColumns =>
|
|
|
|
columns.filter(dtableSchemaIsColumn);
|
|
|
|
|
|
|
|
export const updateAble = (columns: TDtableMetadataColumns): TDtableMetadataColumns =>
|
|
|
|
columns.filter(dtableSchemaIsUpdateAbleColumn);
|
|
|
|
|
|
|
|
function endpointCtxExpr(this: void, ctx: ICtx, endpoint: string): string {
|
|
|
|
const endpointVariables: IEndpointVariables = {};
|
|
|
|
endpointVariables.access_token = ctx?.base?.access_token;
|
|
|
|
endpointVariables.dtable_uuid = ctx?.base?.dtable_uuid;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
return endpoint.replace(
|
|
|
|
/({{ *(access_token|dtable_uuid|server) *}})/g,
|
|
|
|
(match: string, expr: string, name: TEndpointVariableName) => {
|
|
|
|
return endpointVariables[name] || match;
|
|
|
|
},
|
|
|
|
) as TEndpointResolvedExpr;
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const normalize = (subject: string): string => (subject ? subject.normalize() : '');
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
export const split = (subject: string): string[] =>
|
|
|
|
normalize(subject)
|
|
|
|
.split(/\s*((?:[^\\,]*?(?:\\[\s\S])*)*?)\s*(?:,|$)/)
|
2022-08-17 08:50:24 -07:00
|
|
|
.filter((s) => s.length)
|
|
|
|
.map((s) => s.replace(/\\([\s\S])/gm, ($0, $1) => $1));
|
2021-10-04 04:49:16 -07:00
|
|
|
|
2021-10-07 15:12:06 -07:00
|
|
|
const userBaseUri = (uri?: string) => {
|
|
|
|
if (uri === undefined) {
|
|
|
|
return uri;
|
2021-10-04 04:49:16 -07:00
|
|
|
}
|
2021-10-07 15:12:06 -07:00
|
|
|
|
|
|
|
if (uri.endsWith('/')) {
|
|
|
|
return uri.slice(0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri;
|
2021-10-04 04:49:16 -07:00
|
|
|
};
|