mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
d5d4dd3845
* Notion cred updated * Airtable new cred * revamped twilio cred * urlscanlo revamp cred * Wordpress revamp cred with testing * SendGrid cred revamped * 🐛 Fix imports * 🐛 Fixes imports in urlscanio * Fix airtable cred injection * Fixes notion request, changes way of cred injection * Change auth type from method to generic * Fix minor issues * Fix lint issue Co-authored-by: Omar Ajoue <krynble@gmail.com>
134 lines
3.4 KiB
TypeScript
134 lines
3.4 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
IPollFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IBinaryKeyData,
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
NodeApiError,
|
|
} 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
|
|
*
|
|
* @param {IHookFunctions} this
|
|
* @param {string} method
|
|
* @param {string} url
|
|
* @param {object} body
|
|
* @returns {Promise<any>}
|
|
*/
|
|
export async function apiRequest(this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: object, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
const credentials = await this.getCredentials('airtableApi');
|
|
|
|
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,
|
|
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;
|
|
}
|
|
|
|
try {
|
|
return await this.helpers.requestWithAuthentication.call(this, 'airtableApi', options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Make an API request to paginated Airtable endpoint
|
|
* and return all results
|
|
*
|
|
* @export
|
|
* @param {(IExecuteFunctions | IExecuteFunctions)} this
|
|
* @param {string} method
|
|
* @param {string} endpoint
|
|
* @param {IDataObject} body
|
|
* @param {IDataObject} [query]
|
|
* @returns {Promise<any>}
|
|
*/
|
|
export async function apiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-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);
|
|
|
|
query.offset = responseData.offset;
|
|
} while (
|
|
responseData.offset !== undefined
|
|
);
|
|
|
|
return {
|
|
records: returnData,
|
|
};
|
|
}
|
|
|
|
export async function downloadRecordAttachments(this: IExecuteFunctions | IPollFunctions, records: IRecord[], fieldNames: string[]): Promise<INodeExecutionData[]> {
|
|
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 });
|
|
element.binary![`${fieldName}_${index}`] = await this.helpers.prepareBinaryData(Buffer.from(file), attachment.filename, attachment.type);
|
|
}
|
|
}
|
|
}
|
|
if (Object.keys(element.binary as IBinaryKeyData).length === 0) {
|
|
delete element.binary;
|
|
}
|
|
elements.push(element);
|
|
}
|
|
return elements;
|
|
}
|