first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
import type {
|
||||
IBinaryKeyData,
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
IPollFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
IPairedItemData,
|
||||
IRequestOptions,
|
||||
IHttpRequestMethods,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
interface IAttachment {
|
||||
url: string;
|
||||
filename: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface IRecord {
|
||||
fields: {
|
||||
[key: string]: string | IAttachment[];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an API request to Airtable
|
||||
*
|
||||
*/
|
||||
export async function apiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
endpoint: string,
|
||||
body: object,
|
||||
query?: IDataObject,
|
||||
uri?: string,
|
||||
option: IDataObject = {},
|
||||
): Promise<any> {
|
||||
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.
|
||||
// query.api_key = credentials.apiKey;
|
||||
|
||||
const options: IRequestOptions = {
|
||||
headers: {},
|
||||
method,
|
||||
body,
|
||||
qs: query,
|
||||
uri: uri || `https://api.airtable.com/v0/${endpoint}`,
|
||||
useQuerystring: false,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(option).length !== 0) {
|
||||
Object.assign(options, option);
|
||||
}
|
||||
|
||||
if (Object.keys(body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
|
||||
return await this.helpers.requestWithAuthentication.call(this, authenticationMethod, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an API request to paginated Airtable endpoint
|
||||
* and return all results
|
||||
*
|
||||
* @param {(IExecuteFunctions | IExecuteFunctions)} this
|
||||
*/
|
||||
export async function apiRequestAllItems(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
endpoint: string,
|
||||
body: IDataObject,
|
||||
query?: IDataObject,
|
||||
): Promise<any> {
|
||||
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 as IDataObject[]);
|
||||
|
||||
query.offset = responseData.offset;
|
||||
} while (responseData.offset !== undefined);
|
||||
|
||||
return {
|
||||
records: returnData,
|
||||
};
|
||||
}
|
||||
|
||||
export async function downloadRecordAttachments(
|
||||
this: IExecuteFunctions | IPollFunctions,
|
||||
records: IRecord[],
|
||||
fieldNames: string[],
|
||||
pairedItem?: IPairedItemData[],
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const elements: INodeExecutionData[] = [];
|
||||
for (const record of records) {
|
||||
const element: INodeExecutionData = { json: {}, binary: {} };
|
||||
if (pairedItem) {
|
||||
element.pairedItem = pairedItem;
|
||||
}
|
||||
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()) {
|
||||
const file = await apiRequest.call(this, 'GET', '', {}, {}, attachment.url, {
|
||||
json: false,
|
||||
encoding: null,
|
||||
});
|
||||
element.binary![`${fieldName}_${index}`] = await this.helpers.prepareBinaryData(
|
||||
Buffer.from(file as string),
|
||||
attachment.filename,
|
||||
attachment.type,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Object.keys(element.binary as IBinaryKeyData).length === 0) {
|
||||
delete element.binary;
|
||||
}
|
||||
elements.push(element);
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
Reference in New Issue
Block a user