2020-06-30 13:35:31 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-08-03 14:11:57 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2020-08-03 14:11:57 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
get,
|
|
|
|
} from 'lodash';
|
2020-06-30 13:35:31 -07:00
|
|
|
|
2020-09-02 03:32:12 -07:00
|
|
|
export async function customerIoApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, baseApi?: string, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
2020-06-30 13:35:31 -07:00
|
|
|
const credentials = this.getCredentials('customerIoApi');
|
|
|
|
|
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2020-06-30 13:35:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
query = query || {};
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
2020-09-02 03:25:11 -07:00
|
|
|
uri: '',
|
2020-06-30 13:35:31 -07:00
|
|
|
json: true,
|
|
|
|
};
|
2020-09-02 03:25:11 -07:00
|
|
|
|
|
|
|
if (baseApi === 'tracking') {
|
|
|
|
options.uri = `https://track.customer.io/api/v1${endpoint}`;
|
|
|
|
const basicAuthKey = Buffer.from(`${credentials.trackingSiteId}:${credentials.trackingApiKey}`).toString('base64');
|
2020-09-02 03:32:12 -07:00
|
|
|
Object.assign(options.headers, { 'Authorization': `Basic ${basicAuthKey}` });
|
2020-09-02 03:25:11 -07:00
|
|
|
} else if (baseApi === 'api') {
|
|
|
|
options.uri = `https://api.customer.io/v1/api${endpoint}`;
|
|
|
|
const basicAuthKey = Buffer.from(`${credentials.trackingSiteId}:${credentials.trackingApiKey}`).toString('base64');
|
2020-09-02 03:32:12 -07:00
|
|
|
Object.assign(options.headers, { 'Authorization': `Basic ${basicAuthKey}` });
|
2020-09-02 03:25:11 -07:00
|
|
|
} else if (baseApi === 'beta') {
|
|
|
|
options.uri = `https://beta-api.customer.io/v1/api${endpoint}`;
|
2020-09-02 03:32:12 -07:00
|
|
|
Object.assign(options.headers, { 'Authorization': `Bearer ${credentials.appApiKey as string}` });
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2020-06-30 13:35:31 -07:00
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
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) {
|
2020-08-04 01:32:51 -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
|
|
|
|
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|