2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
|
|
|
IHttpRequestMethods,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function autopilotApiRequest(
|
|
|
|
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
resource: string,
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
2022-11-08 06:28:21 -08:00
|
|
|
_option: IDataObject = {},
|
2022-08-01 13:47:55 -07:00
|
|
|
): Promise<any> {
|
2024-08-27 06:23:58 -07:00
|
|
|
const credentials = await this.getCredentials<{ apiKey: string }>('autopilotApi');
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
const endpoint = 'https://api2.autopilothq.com/v1';
|
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2021-03-11 04:36:55 -08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2024-08-27 06:23:58 -07:00
|
|
|
autopilotapikey: credentials.apiKey,
|
2021-03-11 04:36:55 -08:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `${endpoint}${resource}`,
|
2021-03-11 04:36:55 -08:00
|
|
|
json: true,
|
|
|
|
};
|
2023-02-27 19:39:43 -08:00
|
|
|
if (!Object.keys(body as IDataObject).length) {
|
2021-03-11 04:36:55 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
if (!Object.keys(query).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2021-03-11 04:36:55 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function autopilotApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-03-11 04:36:55 -08:00
|
|
|
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}`;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2023-03-30 04:59:59 -07:00
|
|
|
const limit = query.limit as number | undefined;
|
|
|
|
if (limit && returnData.length >= limit && !returnAll) {
|
2021-03-11 04:36:55 -08:00
|
|
|
return returnData;
|
|
|
|
}
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (responseData.bookmark !== undefined);
|
2021-03-11 04:36:55 -08:00
|
|
|
return returnData;
|
|
|
|
}
|