2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2021-02-07 08:38:37 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-02-07 08:38:37 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IDataObject, IPollFunctions, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-02-07 08:38:37 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Airtable
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function apiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('stackbyApi');
|
2021-02-07 08:38:37 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'api-key': credentials.apiKey,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://stackby.com/api/betav1${endpoint}`,
|
2021-02-07 08:38:37 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2021-02-07 08:38:37 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-02-07 08:38:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to paginated Airtable endpoint
|
|
|
|
* and return all results
|
|
|
|
*
|
|
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function apiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IPollFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
query: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2021-02-07 08:38:37 -08:00
|
|
|
query.maxrecord = 100;
|
|
|
|
|
|
|
|
query.offset = 0;
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await apiRequest.call(this, method, endpoint, body, query);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
2021-02-07 08:38:37 -08:00
|
|
|
query.offset += query.maxrecord;
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.length !== 0);
|
2021-02-07 08:38:37 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IRecord {
|
|
|
|
field: {
|
2022-08-17 08:50:24 -07:00
|
|
|
[key: string]: string;
|
2021-02-07 08:38:37 -08:00
|
|
|
};
|
|
|
|
}
|