n8n/packages/nodes-base/nodes/CustomerIo/GenericFunctions.ts

58 lines
1.5 KiB
TypeScript
Raw Normal View History

import { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
2020-06-30 13:35:31 -07:00
import { IDataObject, IHttpRequestMethods, IHttpRequestOptions } from 'n8n-workflow';
2020-08-03 14:11:57 -07:00
import { get } from 'lodash';
2020-06-30 13:35:31 -07:00
export async function customerIoApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: object,
baseApi?: string,
_query?: IDataObject,
) {
const credentials = await this.getCredentials('customerIoApi');
const options: IHttpRequestOptions = {
2020-06-30 13:35:31 -07:00
headers: {
'Content-Type': 'application/json',
},
method: method as IHttpRequestMethods,
2020-06-30 13:35:31 -07:00
body,
url: '',
2020-06-30 13:35:31 -07:00
json: true,
};
if (baseApi === 'tracking') {
const region = credentials.region;
options.url = `https://${region}/api/v1${endpoint}`;
} else if (baseApi === 'api') {
options.url = `https://api.customer.io/v1/api${endpoint}`;
} else if (baseApi === 'beta') {
options.url = `https://beta-api.customer.io/v1/api${endpoint}`;
}
return 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) {
if (
get(webhookEvents, `${currentEvent.split('.')[0]}.${currentEvent.split('.')[1]}`) !== true
) {
2020-08-03 14:11:57 -07:00
return false;
}
}
return true;
}
export function validateJSON(json: string | undefined): any {
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = undefined;
}
return result;
}