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,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
2020-11-24 01:53:39 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-12-05 02:17:08 -08:00
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
2023-07-17 09:42:30 -07:00
|
|
|
import type { IAttachment, IRecord } from '../helpers/interfaces';
|
|
|
|
import { flattenOutput } from '../helpers/utils';
|
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,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
endpoint: string,
|
2023-07-17 09:42:30 -07:00
|
|
|
body: IDataObject = {},
|
2022-08-01 13:47:55 -07:00
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
2023-07-17 09:42:30 -07:00
|
|
|
) {
|
2023-01-19 04:37:19 -08:00
|
|
|
query = query || {};
|
2019-07-07 10:00:05 -07:00
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
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-07-17 09:42:30 -07:00
|
|
|
|
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,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
endpoint: string,
|
2023-07-17 09:42:30 -07:00
|
|
|
body?: IDataObject,
|
2022-08-01 13:47:55 -07:00
|
|
|
query?: IDataObject,
|
2023-07-17 09:42:30 -07:00
|
|
|
) {
|
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[],
|
2023-07-17 09:42:30 -07:00
|
|
|
fieldNames: string | string[],
|
2024-01-19 03:47:25 -08:00
|
|
|
pairedItem?: IPairedItemData[],
|
2022-08-01 13:47:55 -07:00
|
|
|
): Promise<INodeExecutionData[]> {
|
2023-07-17 09:42:30 -07:00
|
|
|
if (typeof fieldNames === 'string') {
|
|
|
|
fieldNames = fieldNames.split(',').map((item) => item.trim());
|
|
|
|
}
|
|
|
|
if (!fieldNames.length) {
|
2023-12-05 02:17:08 -08:00
|
|
|
throw new ApplicationError("Specify field to download in 'Download Attachments' option", {
|
|
|
|
level: 'warning',
|
|
|
|
});
|
2023-07-17 09:42:30 -07:00
|
|
|
}
|
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;
|
|
|
|
}
|
2023-07-17 09:42:30 -07:00
|
|
|
element.json = flattenOutput(record as unknown as IDataObject);
|
2020-11-24 01:53:39 -08:00
|
|
|
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;
|
|
|
|
}
|
2023-07-17 09:42:30 -07:00
|
|
|
|
|
|
|
export async function batchUpdate(
|
|
|
|
this: IExecuteFunctions | IPollFunctions,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
updateRecords: IDataObject[],
|
|
|
|
) {
|
|
|
|
if (!updateRecords.length) {
|
|
|
|
return { records: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
let responseData: IDataObject;
|
|
|
|
|
|
|
|
if (updateRecords.length && updateRecords.length <= 10) {
|
|
|
|
const updateBody = {
|
|
|
|
...body,
|
|
|
|
records: updateRecords,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await apiRequest.call(this, 'PATCH', endpoint, updateBody);
|
|
|
|
return responseData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const batchSize = 10;
|
|
|
|
const batches = Math.ceil(updateRecords.length / batchSize);
|
|
|
|
const updatedRecords: IDataObject[] = [];
|
|
|
|
|
|
|
|
for (let j = 0; j < batches; j++) {
|
|
|
|
const batch = updateRecords.slice(j * batchSize, (j + 1) * batchSize);
|
|
|
|
|
|
|
|
const updateBody = {
|
|
|
|
...body,
|
|
|
|
records: batch,
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateResponse = await apiRequest.call(this, 'PATCH', endpoint, updateBody);
|
|
|
|
updatedRecords.push(...((updateResponse.records as IDataObject[]) || []));
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = { records: updatedRecords };
|
|
|
|
|
|
|
|
return responseData;
|
|
|
|
}
|