2021-02-07 08:38:37 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
IPollFunctions,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
2021-02-07 08:38:37 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Airtable
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-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,
|
|
|
|
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 await this.helpers.request!(options);
|
|
|
|
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-02-07 08:38:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to paginated Airtable endpoint
|
|
|
|
* and return all results
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} endpoint
|
|
|
|
* @param {IDataObject} body
|
|
|
|
* @param {IDataObject} [query]
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
export async function apiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|