2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-11-24 01:53:39 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2020-11-24 01:53:39 -08:00
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IPollFunctions,
|
2022-07-10 03:32:19 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-11-24 01:53:39 -08:00
|
|
|
INodeExecutionData,
|
2024-01-19 03:47:25 -08:00
|
|
|
IPairedItemData,
|
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
|
|
|
|
*
|
|
|
|
*/
|
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 = {},
|
|
|
|
): Promise<any> {
|
2023-01-19 04:37:19 -08:00
|
|
|
query = query || {};
|
2019-07-07 10:00:05 -07:00
|
|
|
|
|
|
|
// 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,
|
2023-01-19 04:37:19 -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;
|
|
|
|
}
|
2023-05-04 03:17:22 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, authenticationMethod, options);
|
2019-07-07 10:00:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to paginated Airtable endpoint
|
|
|
|
* and return all results
|
|
|
|
*
|
2022-07-10 03:32:19 -07:00
|
|
|
* @param {(IExecuteFunctions | IExecuteFunctions)} this
|
2019-07-07 10:00:05 -07:00
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function apiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
query?: IDataObject,
|
|
|
|
): 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);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData.records as IDataObject[]);
|
2019-07-07 10:00:05 -07:00
|
|
|
|
|
|
|
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[],
|
2024-01-19 03:47:25 -08:00
|
|
|
pairedItem?: IPairedItemData[],
|
2022-08-01 13:47:55 -07:00
|
|
|
): Promise<INodeExecutionData[]> {
|
2020-11-24 01:53:39 -08:00
|
|
|
const elements: INodeExecutionData[] = [];
|
|
|
|
for (const record of records) {
|
|
|
|
const element: INodeExecutionData = { json: {}, binary: {} };
|
2024-01-19 03:47:25 -08:00
|
|
|
if (pairedItem) {
|
|
|
|
element.pairedItem = pairedItem;
|
|
|
|
}
|
2020-11-24 01:53:39 -08:00
|
|
|
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(
|
2023-02-27 19:39:43 -08:00
|
|
|
Buffer.from(file as string),
|
2022-08-01 13:47:55 -07:00
|
|
|
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;
|
|
|
|
}
|