first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
IHttpRequestMethods,
|
||||
ILoadOptionsFunctions,
|
||||
IRequestOptions,
|
||||
JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import type {
|
||||
GristCredentials,
|
||||
GristDefinedFields,
|
||||
GristFilterProperties,
|
||||
GristSortProperties,
|
||||
} from './types';
|
||||
|
||||
export async function gristApiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
endpoint: string,
|
||||
body: IDataObject | number[] = {},
|
||||
qs: IDataObject = {},
|
||||
) {
|
||||
const { apiKey, planType, customSubdomain, selfHostedUrl } =
|
||||
await this.getCredentials<GristCredentials>('gristApi');
|
||||
|
||||
const gristapiurl =
|
||||
planType === 'free'
|
||||
? `https://docs.getgrist.com/api${endpoint}`
|
||||
: planType === 'paid'
|
||||
? `https://${customSubdomain}.getgrist.com/api${endpoint}`
|
||||
: `${selfHostedUrl}/api${endpoint}`;
|
||||
|
||||
const options: IRequestOptions = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
},
|
||||
method,
|
||||
uri: gristapiurl,
|
||||
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 as JsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
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 isSafeInteger(val: number) {
|
||||
//used MIN_SAFE_INTEGER and MAX_SAFE_INTEGER instead of MIN_VALUE and MAX_VALUE to avoid edge cases
|
||||
return !isNaN(val) && val > Number.MIN_SAFE_INTEGER && val < Number.MAX_SAFE_INTEGER;
|
||||
}
|
||||
|
||||
export function parseFilterProperties(filterProperties: GristFilterProperties) {
|
||||
return filterProperties.reduce<{ [key: string]: Array<string | number> }>((acc, cur) => {
|
||||
acc[cur.field] = acc[cur.field] ?? [];
|
||||
const values = isSafeInteger(Number(cur.values)) ? Number(cur.values) : cur.values;
|
||||
acc[cur.field].push(values);
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
export function parseDefinedFields(fieldsToSendProperties: GristDefinedFields) {
|
||||
return fieldsToSendProperties.reduce<{ [key: string]: string }>((acc, cur) => {
|
||||
acc[cur.fieldId] = cur.fieldValue;
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
export function parseAutoMappedInputs(incomingKeys: string[], inputsToIgnore: string[], item: any) {
|
||||
return incomingKeys.reduce<{ [key: string]: any }>((acc, curKey) => {
|
||||
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(),
|
||||
"No defined data found. Please specify the data to send in 'Fields to Send'.",
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user