n8n/packages/nodes-base/nodes/CustomerIo/GenericFunctions.ts
Michael Kret 0ecbb4a19d
refactor: Format nodes-base package (A-F) (#3800)
* 🔨 prettier formated nodes - A

* 🔨 prettier formated nodes - B

*  prettier formated nodes - C

*  prettier formated nodes - D

*  prettier formated nodes - E-F

* 🎨 Adjust nodes-base formatting command (#3805)

* Format additional files in nodes A-F (#3811)

*  fixes

* 🎨 Add Mindee to ignored dirs

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
2022-08-01 22:47:55 +02:00

73 lines
1.7 KiB
TypeScript

import { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
import { OptionsWithUri } from 'request';
import {
IDataObject,
IHttpRequestMethods,
IHttpRequestOptions,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import { get } from 'lodash';
export async function customerIoApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: object,
baseApi?: string,
query?: IDataObject,
// tslint:disable-next-line:no-any
): Promise<any> {
const credentials = await this.getCredentials('customerIoApi');
query = query || {};
const options: IHttpRequestOptions = {
headers: {
'Content-Type': 'application/json',
},
method: method as IHttpRequestMethods,
body,
url: '',
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}`;
}
try {
return await this.helpers.requestWithAuthentication.call(this, 'customerIoApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export function eventExists(currentEvents: string[], webhookEvents: IDataObject) {
for (const currentEvent of currentEvents) {
if (
get(webhookEvents, `${currentEvent.split('.')[0]}.${currentEvent.split('.')[1]}`) !== true
) {
return false;
}
}
return true;
}
// tslint:disable-next-line:no-any
export function validateJSON(json: string | undefined): any {
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = undefined;
}
return result;
}