n8n/packages/nodes-base/nodes/Lemlist/GenericFunctions.ts
Iván Ovejero b03e358a12
refactor: Integrate consistent-type-imports in nodes-base (no-changelog) (#5267)
* 👕 Enable `consistent-type-imports` for nodes-base

* 👕 Apply to nodes-base

*  Undo unrelated changes

* 🚚 Move to `.eslintrc.js` in nodes-base

*  Revert "Enable `consistent-type-imports` for nodes-base"

This reverts commit 529ad72b05.

* 👕 Fix severity
2023-01-27 12:22:44 +01:00

88 lines
1.8 KiB
TypeScript

import type { IExecuteFunctions, IHookFunctions } from 'n8n-core';
import type { IDataObject, ILoadOptionsFunctions } from 'n8n-workflow';
import type { OptionsWithUri } from 'request';
import { capitalCase } from 'change-case';
/**
* Make an authenticated API request to Lemlist.
*/
export async function lemlistApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
option: IDataObject = {},
) {
const options: OptionsWithUri = {
headers: {},
method,
uri: `https://api.lemlist.com/api${endpoint}`,
qs,
body,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
if (Object.keys(option)) {
Object.assign(options, option);
}
return this.helpers.requestWithAuthentication.call(this, 'lemlistApi', options);
}
/**
* Make an authenticated API request to Lemlist and return all results.
*/
export async function lemlistApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
method: string,
endpoint: string,
) {
const returnData: IDataObject[] = [];
let responseData;
const qs: IDataObject = {};
qs.limit = 100;
qs.offset = 0;
do {
responseData = await lemlistApiRequest.call(this, method, endpoint, {}, qs);
returnData.push(...responseData);
qs.offset += qs.limit;
} while (responseData.length !== 0);
return returnData;
}
export function getEvents() {
const events = [
'*',
'emailsBounced',
'emailsClicked',
'emailsFailed',
'emailsInterested',
'emailsNotInterested',
'emailsOpened',
'emailsReplied',
'emailsSendFailed',
'emailsSent',
'emailsUnsubscribed',
];
return events.map((event: string) => ({
name: event === '*' ? '*' : capitalCase(event),
value: event,
}));
}