n8n/packages/nodes-base/nodes/ActiveCampaign/GenericFunctions.ts
agobrech 683d2dfc98
feat: Add more credentials tests (#3668)
*   Add injection to notion,
Add test to notion in cred

* 🔥 Remove unuse method

* 🎨  Move testing from node file to cred file

*  Add injection and testing in facebook graph

* Add cred injec with testing

* Add Cred injection and cred test

* Add cred injection, and cred testing for typeform, fix issue in clickup

* Add cred injection, move testing inside creds

* Add cred injection and cred testing to SendGrid

* Add cred injection and cred testing to woocommerce

* Add cred injection, add cred test to gitlab

* 🔥 Fix duplicated imports in Mautic cred

* 🔥 removed unused credentials testing in node

* Add cred injection, cred testing, handles slash trailing for Grafana node

* Add cred injection,  cred testing to shopify

* Add cred injection , add cred testing to stripe

* changed cred injection, add testing to cred for mattermost

* add cred injection and testing for dropbox

* Add cred injection, cred testing to webflow

*  Add cred injection and cred test to nocodb

*  Add cred injection, cred testing to mailchimp

* 🐛 fix a bug In credentials testing

*  Add cred injection, cred testing to sms77

*  Add cred injection, cred testing to ActiveCampaign

* Add cred injection, cred testing to TheHive

*  Add cred injection, add cred testing to ApiTemplateio

*  Add cred injection, add cred testing for zoom

*  Add cred injection, cred testing to rocketchat

*  Add cred injection, add cred test to getResponse

* 🔥 Remove useless authentcate creds and testing from facebookGraphApp

* 🔥 Remove useless imports in FacebookGrappApp credentials file

* 🔥 Removed useless imports and if statement

* 🐛 Add version to header when testing cred

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
2022-07-15 16:20:41 +02:00

180 lines
3.9 KiB
TypeScript

import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
IDataObject, ILoadOptionsFunctions, INodeProperties, NodeApiError, NodeOperationError,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
export interface IProduct {
fields: {
item?: object[];
};
}
/**
* Make an API request to ActiveCampaign
*
* @param {IHookFunctions} this
* @param {string} method
* @param {string} url
* @param {object} body
* @returns {Promise<any>}
*/
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
const credentials = await this.getCredentials('activeCampaignApi');
if (query === undefined) {
query = {};
}
const options: OptionsWithUri = {
headers: {
},
method,
qs: query,
uri: `${credentials.apiUrl}${endpoint}`,
json: true,
};
if (Object.keys(body).length !== 0) {
options.body = body;
}
try {
const responseData = await this.helpers.requestWithAuthentication.call(this, 'activeCampaignApi',options);
if (responseData.success === false) {
throw new NodeApiError(this.getNode(), responseData);
}
if (dataKey === undefined) {
return responseData;
} else {
return responseData[dataKey] as IDataObject;
}
} catch (error) {
throw new NodeApiError(this.getNode(), 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>}
*/
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
if (query === undefined) {
query = {};
}
query.limit = 100;
query.offset = 0;
const returnData: IDataObject[] = [];
let responseData;
let itemsReceived = 0;
do {
responseData = await activeCampaignApiRequest.call(this, method, endpoint, body, query);
if (dataKey === undefined) {
returnData.push.apply(returnData, responseData);
if (returnData !== undefined) {
itemsReceived += returnData.length;
}
} else {
returnData.push.apply(returnData, responseData[dataKey]);
if (responseData[dataKey] !== undefined) {
itemsReceived += responseData[dataKey].length;
}
}
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: 'Whether to return all results 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: 'Max number of results to return',
},
{
displayName: 'Simplify',
name: 'simple',
type: 'boolean',
displayOptions: {
show: {
operation: [
operation,
],
resource: [
resource,
],
},
},
default: true,
description: 'Whether to return a simplified version of the response instead of the raw data',
},
];
}