2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-11-25 04:08:59 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function quickbaseApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('quickbaseApi');
|
2020-11-25 04:09:58 -08:00
|
|
|
|
|
|
|
if (!credentials.hostname) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Hostname must be defined');
|
2020-11-25 04:08:59 -08:00
|
|
|
}
|
|
|
|
|
2020-11-25 04:09:58 -08:00
|
|
|
if (!credentials.userToken) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'User Token must be defined');
|
2020-11-25 04:08:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'QB-Realm-Hostname': credentials.hostname,
|
|
|
|
'User-Agent': 'n8n',
|
2022-08-17 08:50:24 -07:00
|
|
|
Authorization: `QB-USER-TOKEN ${credentials.userToken}`,
|
2020-11-25 04:08:59 -08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `https://api.quickbase.com/v1${resource}`,
|
2021-04-30 19:49:15 -07:00
|
|
|
json: true,
|
2020-11-25 04:08:59 -08:00
|
|
|
};
|
2021-04-30 19:48:40 -07:00
|
|
|
|
2020-11-25 04:08:59 -08:00
|
|
|
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) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-11-25 04:08:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//@ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
// prettier-ignore
|
2022-12-02 06:25:21 -08:00
|
|
|
export async function getFieldsObject(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, tableId: string): any {
|
2020-11-25 04:08:59 -08:00
|
|
|
const fieldsLabelKey: { [key: string]: number } = {};
|
|
|
|
const fieldsIdKey: { [key: number]: string } = {};
|
2020-11-25 04:09:58 -08:00
|
|
|
const data = await quickbaseApiRequest.call(this, 'GET', '/fields', {}, { tableId });
|
2020-11-25 04:08:59 -08:00
|
|
|
for (const field of data) {
|
|
|
|
fieldsLabelKey[field.label] = field.id;
|
|
|
|
fieldsIdKey[field.id] = field.label;
|
|
|
|
}
|
|
|
|
return { fieldsLabelKey, fieldsIdKey };
|
|
|
|
}
|
2020-11-25 04:09:58 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function quickbaseApiRequestAllItems(
|
|
|
|
this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-11-25 04:08:59 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData = [];
|
|
|
|
|
|
|
|
if (method === 'POST') {
|
|
|
|
body.options = {
|
|
|
|
skip: 0,
|
|
|
|
top: 100,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
query.skip = 0;
|
|
|
|
query.top = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
let metadata;
|
|
|
|
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
data,
|
|
|
|
fields,
|
|
|
|
metadata: meta,
|
|
|
|
} = await quickbaseApiRequest.call(this, method, resource, body, query);
|
2020-11-25 04:08:59 -08:00
|
|
|
|
|
|
|
metadata = meta;
|
|
|
|
|
|
|
|
const fieldsIdKey: { [key: string]: string } = {};
|
|
|
|
|
|
|
|
for (const field of fields) {
|
|
|
|
fieldsIdKey[field.id] = field.label;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const record of data) {
|
2022-12-02 12:54:28 -08:00
|
|
|
const recordData: IDataObject = {};
|
2020-11-25 04:08:59 -08:00
|
|
|
for (const [key, value] of Object.entries(record)) {
|
2022-12-02 12:54:28 -08:00
|
|
|
recordData[fieldsIdKey[key]] = (value as IDataObject).value;
|
2020-11-25 04:08:59 -08:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData.push(recordData);
|
2020-11-25 04:08:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (method === 'POST') {
|
|
|
|
body.options.skip += body.options.top;
|
|
|
|
} else {
|
|
|
|
//@ts-ignore
|
|
|
|
query.skip += query.top;
|
|
|
|
}
|
|
|
|
returnData.push.apply(returnData, responseData);
|
|
|
|
responseData = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (returnData.length < metadata.totalRecords);
|
2020-11-25 04:08:59 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|