2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2021-03-04 01:25:47 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, ILoadOptionsFunctions, NodeApiError } from 'n8n-workflow';
|
2021-03-04 01:25:47 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { 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 = {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-08-24 01:26:48 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this,'lemlistApi',options);
|
2021-03-04 01:25:47 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
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);
|
|
|
|
returnData.push(...responseData);
|
|
|
|
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
|
|
|
}
|