mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 09:04:07 -08:00
0712334679
* ✨ Autopilot Node & Trigger * 🎨 Replace PNG with SVG icon * 🚚 Rename description file * 🔨 Fix contact operations default * ✏️ Edit contact property descriptions * ✏️ Edit journey property descriptions * ✏️ Edit contact list property descriptions * ✏️ Edit list property descriptions * 🐛 Fix issue with a wrong named resource * ⚡ Fix Trigger-Node name and minor improvements * 🔨 Remove 404 from contactList:exist Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
73 lines
2 KiB
TypeScript
73 lines
2 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
IHookFunctions,
|
|
IWebhookFunctions,
|
|
} from 'n8n-workflow';
|
|
|
|
export async function autopilotApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const credentials = this.getCredentials('autopilotApi') as IDataObject;
|
|
|
|
const apiKey = `${credentials.apiKey}`;
|
|
|
|
const endpoint = 'https://api2.autopilothq.com/v1';
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
autopilotapikey: apiKey,
|
|
},
|
|
method,
|
|
body,
|
|
qs: query,
|
|
uri: uri || `${endpoint}${resource}`,
|
|
json: true,
|
|
};
|
|
if (!Object.keys(body).length) {
|
|
delete options.body;
|
|
}
|
|
if (!Object.keys(query).length) {
|
|
delete options.qs;
|
|
}
|
|
|
|
try {
|
|
return await this.helpers.request!(options);
|
|
} catch (error) {
|
|
if (error.response) {
|
|
const errorMessage = error.response.body.message || error.response.body.description || error.message;
|
|
throw new Error(`Autopilot error response [${error.statusCode}]: ${errorMessage}`);
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function autopilotApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const returnData: IDataObject[] = [];
|
|
const returnAll = this.getNodeParameter('returnAll', 0, false) as boolean;
|
|
|
|
const base = endpoint;
|
|
|
|
let responseData;
|
|
do {
|
|
responseData = await autopilotApiRequest.call(this, method, endpoint, body, query);
|
|
endpoint = `${base}/${responseData.bookmark}`;
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
if (query.limit && returnData.length >= query.limit && returnAll === false) {
|
|
return returnData;
|
|
}
|
|
} while (
|
|
responseData.bookmark !== undefined
|
|
);
|
|
return returnData;
|
|
}
|