mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-11 15:14:05 -08:00
61e26804ba
* ⚡ 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>
84 lines
1.8 KiB
TypeScript
84 lines
1.8 KiB
TypeScript
import { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
import { IDataObject, IPollFunctions, NodeApiError } from 'n8n-workflow';
|
|
|
|
/**
|
|
* Make an API request to Airtable
|
|
*
|
|
*/
|
|
export async function apiRequest(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: IDataObject,
|
|
query?: IDataObject,
|
|
uri?: string,
|
|
option: IDataObject = {},
|
|
): Promise<any> {
|
|
const credentials = await this.getCredentials('stackbyApi');
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {
|
|
'api-key': credentials.apiKey,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
method,
|
|
body,
|
|
qs: query,
|
|
uri: uri || `https://stackby.com/api/betav1${endpoint}`,
|
|
json: true,
|
|
};
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
Object.assign(options, option);
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
delete options.body;
|
|
}
|
|
|
|
try {
|
|
return this.helpers.request!(options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make an API request to paginated Airtable endpoint
|
|
* and return all results
|
|
*
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
*/
|
|
export async function apiRequestAllItems(
|
|
this: IHookFunctions | IExecuteFunctions | IPollFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: IDataObject = {},
|
|
query: IDataObject = {},
|
|
): Promise<any> {
|
|
query.maxrecord = 100;
|
|
|
|
query.offset = 0;
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
do {
|
|
responseData = await apiRequest.call(this, method, endpoint, body, query);
|
|
returnData.push.apply(returnData, responseData);
|
|
query.offset += query.maxrecord;
|
|
} while (responseData.length !== 0);
|
|
|
|
return returnData;
|
|
}
|
|
|
|
export interface IRecord {
|
|
field: {
|
|
[key: string]: string;
|
|
};
|
|
}
|