2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-workflow';
|
2021-03-04 01:25:47 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-03-04 01:25:47 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { capitalCase } from 'change-case';
|
2021-09-15 01:28:48 -07:00
|
|
|
|
2021-03-04 01:25:47 -08:00
|
|
|
/**
|
|
|
|
* 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 = {
|
2022-11-11 02:32:43 -08:00
|
|
|
headers: {},
|
2021-03-04 01:25:47 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, 'lemlistApi', options);
|
2021-03-04 01:25:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push(...(responseData as IDataObject[]));
|
2021-03-04 01:25:47 -08:00
|
|
|
qs.offset += qs.limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.length !== 0);
|
2021-03-04 01:25:47 -08:00
|
|
|
return returnData;
|
|
|
|
}
|
2021-09-15 01:28:48 -07:00
|
|
|
|
|
|
|
export function getEvents() {
|
|
|
|
const events = [
|
|
|
|
'*',
|
|
|
|
'emailsBounced',
|
|
|
|
'emailsClicked',
|
|
|
|
'emailsFailed',
|
|
|
|
'emailsInterested',
|
|
|
|
'emailsNotInterested',
|
|
|
|
'emailsOpened',
|
|
|
|
'emailsReplied',
|
|
|
|
'emailsSendFailed',
|
|
|
|
'emailsSent',
|
|
|
|
'emailsUnsubscribed',
|
|
|
|
];
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
return events.map((event: string) => ({
|
|
|
|
name: event === '*' ? '*' : capitalCase(event),
|
|
|
|
value: event,
|
|
|
|
}));
|
2021-09-15 01:28:48 -07:00
|
|
|
}
|