n8n/packages/nodes-base/nodes/ActiveCampaign/GenericFunctions.ts

190 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-08-20 23:45:03 -07:00
import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
IDataObject, ILoadOptionsFunctions, INodeProperties,
2019-08-20 23:45:03 -07:00
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
2019-11-02 15:11:55 -07:00
export interface IProduct {
fields: {
item?: object[];
};
}
2019-08-20 23:45:03 -07:00
/**
* Make an API request to ActiveCampaign
*
* @param {IHookFunctions} this
* @param {string} method
* @param {string} url
* @param {object} body
* @returns {Promise<any>}
*/
2020-01-14 08:13:17 -08:00
export async function activeCampaignApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, dataKey?: string): Promise<any> { // tslint:disable-line:no-any
2019-08-20 23:45:03 -07:00
const credentials = this.getCredentials('activeCampaignApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
if (query === undefined) {
query = {};
}
const options: OptionsWithUri = {
2019-09-11 12:56:45 -07:00
headers: {
'Api-Token': credentials.apiKey,
},
2019-08-20 23:45:03 -07:00
method,
qs: query,
uri: `${credentials.apiUrl}${endpoint}`,
json: true
};
if (Object.keys(body).length !== 0) {
2019-09-11 12:56:45 -07:00
options.body = body;
2019-08-20 23:45:03 -07:00
}
try {
2020-01-14 08:13:17 -08:00
const responseData = await this.helpers.request!(options);
2019-08-20 23:45:03 -07:00
2019-09-11 12:56:45 -07:00
if (responseData.success === false) {
throw new Error(`ActiveCampaign error response: ${responseData.error} (${responseData.error_info})`);
2019-08-20 23:45:03 -07:00
}
2019-09-11 12:56:45 -07:00
if (dataKey === undefined) {
2019-08-20 23:45:03 -07:00
return responseData;
2019-09-11 12:56:45 -07:00
} else {
return responseData[dataKey] as IDataObject;
2019-08-20 23:45:03 -07:00
}
} catch (error) {
if (error.statusCode === 403) {
// Return a clear error
throw new Error('The ActiveCampaign credentials are not valid!');
}
// If that data does not exist for some reason return the actual error
throw error;
}
}
/**
* Make an API request to paginated ActiveCampaign 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-01-14 08:13:17 -08:00
export async function activeCampaignApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, dataKey?: string): Promise<any> { // tslint:disable-line:no-any
2019-08-20 23:45:03 -07:00
if (query === undefined) {
query = {};
}
query.limit = 100;
query.offset = 0;
const returnData: IDataObject[] = [];
let responseData;
let itemsReceived = 0;
do {
2019-09-11 12:56:45 -07:00
responseData = await activeCampaignApiRequest.call(this, method, endpoint, body, query);
if (dataKey === undefined) {
returnData.push.apply(returnData, responseData);
2019-11-03 12:49:49 -08:00
if (returnData !== undefined) {
itemsReceived += returnData.length;
}
2019-09-11 12:56:45 -07:00
} else {
returnData.push.apply(returnData, responseData[dataKey]);
2019-11-03 12:49:49 -08:00
if (responseData[dataKey] !== undefined) {
itemsReceived += responseData[dataKey].length;
}
2019-09-11 12:56:45 -07:00
}
2019-08-20 23:45:03 -07:00
query.offset = itemsReceived;
} while (
responseData.meta !== undefined &&
responseData.meta.total !== undefined &&
responseData.meta.total > itemsReceived
);
return returnData;
}
export function activeCampaignDefaultGetAllProperties(resource: string, operation: string): INodeProperties[] {
return [
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: [
operation,
],
resource: [
resource,
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: [
operation,
],
resource: [
resource,
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 500,
},
default: 100,
description: 'How many results to return.',
},
{
displayName: 'Simple',
name: 'simple',
type: 'boolean',
displayOptions: {
show: {
operation: [
operation,
],
resource: [
resource,
],
},
},
default: true,
description: 'When set to true a simplify version of the response will be used else the raw data.',
},
];
}