2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions, IPollFunctions } from 'n8n-core';
|
2019-07-07 10:00:05 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-11-24 01:53:39 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
2022-07-10 03:32:19 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-11-24 01:53:39 -08:00
|
|
|
INodeExecutionData,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
2020-11-24 01:53:39 -08:00
|
|
|
} from 'n8n-workflow';
|
2019-07-07 10:00:05 -07:00
|
|
|
|
2020-11-24 01:53:39 -08:00
|
|
|
interface IAttachment {
|
|
|
|
url: string;
|
|
|
|
filename: string;
|
|
|
|
type: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IRecord {
|
|
|
|
fields: {
|
2022-08-01 13:47:55 -07:00
|
|
|
[key: string]: string | IAttachment[];
|
2020-11-24 01:53:39 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-07 10:00:05 -07:00
|
|
|
/**
|
|
|
|
* Make an API request to Airtable
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function apiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object,
|
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('airtableApi');
|
2019-07-07 10:00:05 -07:00
|
|
|
|
|
|
|
query = query || {};
|
|
|
|
|
|
|
|
// For some reason for some endpoints the bearer auth does not work
|
|
|
|
// and it returns 404 like for the /meta request. So we always send
|
|
|
|
// it as query string.
|
2022-07-10 03:32:19 -07:00
|
|
|
// query.api_key = credentials.apiKey;
|
2019-07-07 10:00:05 -07:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
2022-08-01 13:47:55 -07:00
|
|
|
headers: {},
|
2019-07-07 10:00:05 -07:00
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
2020-11-24 01:53:39 -08:00
|
|
|
uri: uri || `https://api.airtable.com/v0/${endpoint}`,
|
2021-03-12 01:23:35 -08:00
|
|
|
useQuerystring: false,
|
2019-07-07 10:00:05 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2020-11-24 01:53:39 -08:00
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2019-07-07 10:00:05 -07:00
|
|
|
try {
|
2022-07-10 03:32:19 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'airtableApi', options);
|
2019-07-07 10:00:05 -07:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-07-07 10:00:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to paginated Airtable endpoint
|
|
|
|
* and return all results
|
|
|
|
*
|
|
|
|
* @export
|
2022-07-10 03:32:19 -07:00
|
|
|
* @param {(IExecuteFunctions | IExecuteFunctions)} this
|
2019-07-07 10:00:05 -07:00
|
|
|
* @param {string} method
|
|
|
|
* @param {string} endpoint
|
|
|
|
* @param {IDataObject} body
|
|
|
|
* @param {IDataObject} [query]
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function apiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
query?: IDataObject,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2019-07-07 10:00:05 -07:00
|
|
|
if (query === undefined) {
|
|
|
|
query = {};
|
|
|
|
}
|
|
|
|
query.pageSize = 100;
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await apiRequest.call(this, method, endpoint, body, query);
|
|
|
|
returnData.push.apply(returnData, responseData.records);
|
|
|
|
|
|
|
|
query.offset = responseData.offset;
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (responseData.offset !== undefined);
|
2019-07-07 10:00:05 -07:00
|
|
|
|
|
|
|
return {
|
2020-10-22 06:46:03 -07:00
|
|
|
records: returnData,
|
2019-07-07 10:00:05 -07:00
|
|
|
};
|
|
|
|
}
|
2020-11-24 01:53:39 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function downloadRecordAttachments(
|
|
|
|
this: IExecuteFunctions | IPollFunctions,
|
|
|
|
records: IRecord[],
|
|
|
|
fieldNames: string[],
|
|
|
|
): Promise<INodeExecutionData[]> {
|
2020-11-24 01:53:39 -08:00
|
|
|
const elements: INodeExecutionData[] = [];
|
|
|
|
for (const record of records) {
|
|
|
|
const element: INodeExecutionData = { json: {}, binary: {} };
|
|
|
|
element.json = record as unknown as IDataObject;
|
|
|
|
for (const fieldName of fieldNames) {
|
|
|
|
if (record.fields[fieldName] !== undefined) {
|
|
|
|
for (const [index, attachment] of (record.fields[fieldName] as IAttachment[]).entries()) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const file = await apiRequest.call(this, 'GET', '', {}, {}, attachment.url, {
|
|
|
|
json: false,
|
|
|
|
encoding: null,
|
|
|
|
});
|
|
|
|
element.binary![`${fieldName}_${index}`] = await this.helpers.prepareBinaryData(
|
|
|
|
Buffer.from(file),
|
|
|
|
attachment.filename,
|
|
|
|
attachment.type,
|
|
|
|
);
|
2020-11-24 01:53:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Object.keys(element.binary as IBinaryKeyData).length === 0) {
|
|
|
|
delete element.binary;
|
|
|
|
}
|
|
|
|
elements.push(element);
|
|
|
|
}
|
|
|
|
return elements;
|
|
|
|
}
|