2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2021-09-29 17:10:39 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-09-29 17:10:39 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2021-09-29 17:10:39 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
GristCredentials,
|
|
|
|
GristDefinedFields,
|
|
|
|
GristFilterProperties,
|
|
|
|
GristSortProperties,
|
|
|
|
} from './types';
|
|
|
|
|
|
|
|
export async function gristApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject | number[] = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const { apiKey, planType, customSubdomain, selfHostedUrl } = (await this.getCredentials(
|
|
|
|
'gristApi',
|
|
|
|
)) as GristCredentials;
|
2021-09-29 17:10:39 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const gristapiurl =
|
|
|
|
planType === 'free'
|
|
|
|
? `https://docs.getgrist.com/api${endpoint}`
|
|
|
|
: planType === 'paid'
|
|
|
|
? `https://${customSubdomain}.getgrist.com/api${endpoint}`
|
|
|
|
: `${selfHostedUrl}/api${endpoint}`;
|
2021-09-29 17:10:39 -07:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${apiKey}`,
|
|
|
|
},
|
|
|
|
method,
|
2022-02-19 03:29:28 -08:00
|
|
|
uri: gristapiurl,
|
2021-09-29 17:10:39 -07:00
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseSortProperties(sortProperties: GristSortProperties) {
|
|
|
|
return sortProperties.reduce((acc, cur, curIdx) => {
|
|
|
|
if (cur.direction === 'desc') acc += '-';
|
|
|
|
acc += cur.field;
|
|
|
|
if (curIdx !== sortProperties.length - 1) acc += ',';
|
|
|
|
return acc;
|
|
|
|
}, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseFilterProperties(filterProperties: GristFilterProperties) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return filterProperties.reduce<{ [key: string]: Array<string | number> }>((acc, cur) => {
|
2021-09-29 17:10:39 -07:00
|
|
|
acc[cur.field] = acc[cur.field] ?? [];
|
|
|
|
const values = isNaN(Number(cur.values)) ? cur.values : Number(cur.values);
|
|
|
|
acc[cur.field].push(values);
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseDefinedFields(fieldsToSendProperties: GristDefinedFields) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return fieldsToSendProperties.reduce<{ [key: string]: string }>((acc, cur) => {
|
2021-09-29 17:10:39 -07:00
|
|
|
acc[cur.fieldId] = cur.fieldValue;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2022-12-02 06:25:21 -08:00
|
|
|
export function parseAutoMappedInputs(incomingKeys: string[], inputsToIgnore: string[], item: any) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return incomingKeys.reduce<{ [key: string]: any }>((acc, curKey) => {
|
2021-09-29 17:10:39 -07:00
|
|
|
if (inputsToIgnore.includes(curKey)) return acc;
|
|
|
|
acc = { ...acc, [curKey]: item[curKey] };
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function throwOnZeroDefinedFields(this: IExecuteFunctions, fields: GristDefinedFields) {
|
|
|
|
if (!fields?.length) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2022-08-17 08:50:24 -07:00
|
|
|
"No defined data found. Please specify the data to send in 'Fields to Send'.",
|
2021-09-29 17:10:39 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|