n8n/packages/nodes-base/nodes/QuickBase/GenericFunctions.ts
Michael Kret 61e26804ba
refactor(core): Remove linting exceptions in nodes-base (#4794)
*  enabled array-type

*  await-thenable on

*  ban-types on

*  default-param-last on

*  dot-notation on

*  member-delimiter-style on

*  no-duplicate-imports on

*  no-empty-interface on

*  no-floating-promises on

*  no-for-in-array on

*  no-invalid-void-type on

*  no-loop-func on

*  no-shadow on

*  ban-ts-comment re enabled

*  @typescript-eslint/lines-between-class-members on

* address my own comment

* @typescript-eslint/return-await on

* @typescript-eslint/promise-function-async on

* @typescript-eslint/no-unnecessary-boolean-literal-compare on

* @typescript-eslint/no-unnecessary-type-assertion on

* prefer-const on

* @typescript-eslint/prefer-optional-chain on

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2022-12-02 21:54:28 +01:00

136 lines
3.1 KiB
TypeScript

import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
export async function quickbaseApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
method: string,
resource: string,
body: any = {},
qs: IDataObject = {},
option: IDataObject = {},
): Promise<any> {
const credentials = await this.getCredentials('quickbaseApi');
if (!credentials.hostname) {
throw new NodeOperationError(this.getNode(), 'Hostname must be defined');
}
if (!credentials.userToken) {
throw new NodeOperationError(this.getNode(), 'User Token must be defined');
}
try {
const options: OptionsWithUri = {
headers: {
'QB-Realm-Hostname': credentials.hostname,
'User-Agent': 'n8n',
Authorization: `QB-USER-TOKEN ${credentials.userToken}`,
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: `https://api.quickbase.com/v1${resource}`,
json: true,
};
if (Object.keys(body).length === 0) {
delete options.body;
}
if (Object.keys(qs).length === 0) {
delete options.qs;
}
if (Object.keys(option).length !== 0) {
Object.assign(options, option);
}
//@ts-ignore
return await this.helpers?.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
//@ts-ignore
// prettier-ignore
export async function getFieldsObject(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, tableId: string): any {
const fieldsLabelKey: { [key: string]: number } = {};
const fieldsIdKey: { [key: number]: string } = {};
const data = await quickbaseApiRequest.call(this, 'GET', '/fields', {}, { tableId });
for (const field of data) {
fieldsLabelKey[field.label] = field.id;
fieldsIdKey[field.id] = field.label;
}
return { fieldsLabelKey, fieldsIdKey };
}
export async function quickbaseApiRequestAllItems(
this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions,
method: string,
resource: string,
body: any = {},
query: IDataObject = {},
): Promise<any> {
const returnData: IDataObject[] = [];
let responseData = [];
if (method === 'POST') {
body.options = {
skip: 0,
top: 100,
};
} else {
query.skip = 0;
query.top = 100;
}
let metadata;
do {
const {
data,
fields,
metadata: meta,
} = await quickbaseApiRequest.call(this, method, resource, body, query);
metadata = meta;
const fieldsIdKey: { [key: string]: string } = {};
for (const field of fields) {
fieldsIdKey[field.id] = field.label;
}
for (const record of data) {
const recordData: IDataObject = {};
for (const [key, value] of Object.entries(record)) {
recordData[fieldsIdKey[key]] = (value as IDataObject).value;
}
responseData.push(recordData);
}
if (method === 'POST') {
body.options.skip += body.options.top;
} else {
//@ts-ignore
query.skip += query.top;
}
returnData.push.apply(returnData, responseData);
responseData = [];
} while (returnData.length < metadata.totalRecords);
return returnData;
}