2019-07-07 10:00:05 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-11-24 01:53:39 -08:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
2020-11-24 09:35:59 -08:00
|
|
|
IPollFunctions,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
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: {
|
|
|
|
[key: string]: string | IAttachment[],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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>}
|
|
|
|
*/
|
2020-11-24 09:35:59 -08:00
|
|
|
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: object, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('airtableApi');
|
2019-07-07 10:00:05 -07:00
|
|
|
|
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
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.
|
|
|
|
query.api_key = credentials.apiKey;
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} 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
|
|
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} endpoint
|
|
|
|
* @param {IDataObject} body
|
|
|
|
* @param {IDataObject} [query]
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2020-11-06 17:18:10 -08:00
|
|
|
export async function apiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-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;
|
|
|
|
} while (
|
|
|
|
responseData.offset !== undefined
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-02-04 06:43:48 -08: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()) {
|
|
|
|
const file = await apiRequest.call(this, 'GET', '', {}, {}, attachment.url, { json: false, encoding: null });
|
2022-01-03 13:42:42 -08:00
|
|
|
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;
|
|
|
|
}
|