2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
IHttpRequestMethods,
|
|
|
|
IHttpRequestOptions,
|
|
|
|
} from 'n8n-workflow';
|
2020-08-03 14:11:57 -07:00
|
|
|
|
2023-06-16 07:26:35 -07:00
|
|
|
import get from 'lodash/get';
|
2020-06-30 13:35:31 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function customerIoApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object,
|
|
|
|
baseApi?: string,
|
2022-11-08 06:28:21 -08:00
|
|
|
_query?: IDataObject,
|
|
|
|
) {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('customerIoApi');
|
2022-07-10 02:06:20 -07:00
|
|
|
const options: IHttpRequestOptions = {
|
2020-06-30 13:35:31 -07:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2022-07-10 02:06:20 -07:00
|
|
|
method: method as IHttpRequestMethods,
|
2020-06-30 13:35:31 -07:00
|
|
|
body,
|
2022-07-10 02:06:20 -07:00
|
|
|
url: '',
|
2020-06-30 13:35:31 -07:00
|
|
|
json: true,
|
|
|
|
};
|
2020-09-02 03:25:11 -07:00
|
|
|
|
|
|
|
if (baseApi === 'tracking') {
|
2022-07-10 02:06:20 -07:00
|
|
|
const region = credentials.region;
|
|
|
|
options.url = `https://${region}/api/v1${endpoint}`;
|
2020-09-02 03:25:11 -07:00
|
|
|
} else if (baseApi === 'api') {
|
2023-10-23 02:01:48 -07:00
|
|
|
const region = credentials.region;
|
|
|
|
// Special handling for EU region
|
|
|
|
if (region === 'track-eu.customer.io') {
|
|
|
|
options.url = `https://api-eu.customer.io/v1/api${endpoint}`;
|
|
|
|
} else {
|
|
|
|
options.url = `https://api.customer.io/v1/api${endpoint}`;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
} else if (baseApi === 'beta') {
|
2022-07-10 02:06:20 -07:00
|
|
|
options.url = `https://beta-api.customer.io/v1/api${endpoint}`;
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'customerIoApi', options);
|
2020-06-30 13:35:31 -07:00
|
|
|
}
|
2020-08-03 14:11:57 -07:00
|
|
|
|
2020-08-04 01:32:51 -07:00
|
|
|
export function eventExists(currentEvents: string[], webhookEvents: IDataObject) {
|
2020-08-03 14:11:57 -07:00
|
|
|
for (const currentEvent of currentEvents) {
|
2022-08-01 13:47:55 -07:00
|
|
|
if (
|
|
|
|
get(webhookEvents, `${currentEvent.split('.')[0]}.${currentEvent.split('.')[1]}`) !== true
|
|
|
|
) {
|
2020-08-03 14:11:57 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2020-09-02 03:25:11 -07:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|