mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -08:00
d3fecb9f6d
* Centralized error throwing for encryption key * Unifying the error message used by cli and core packages * Improvements to error messages to make it more DRY * Removed unnecessary throw * Throwing error when credential does not exist to simplify node behavior (#3112) Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IDataObject, 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): Promise<any> { // tslint:disable-line:no-any
|
|
const credentials = await this.getCredentials('customerIoApi');
|
|
|
|
query = query || {};
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
method,
|
|
body,
|
|
uri: '',
|
|
json: true,
|
|
};
|
|
|
|
if (baseApi === 'tracking') {
|
|
options.uri = `https://track.customer.io/api/v1${endpoint}`;
|
|
const basicAuthKey = Buffer.from(`${credentials.trackingSiteId}:${credentials.trackingApiKey}`).toString('base64');
|
|
Object.assign(options.headers, { 'Authorization': `Basic ${basicAuthKey}` });
|
|
} else if (baseApi === 'api') {
|
|
options.uri = `https://api.customer.io/v1/api${endpoint}`;
|
|
const basicAuthKey = Buffer.from(`${credentials.trackingSiteId}:${credentials.trackingApiKey}`).toString('base64');
|
|
Object.assign(options.headers, { 'Authorization': `Basic ${basicAuthKey}` });
|
|
} else if (baseApi === 'beta') {
|
|
options.uri = `https://beta-api.customer.io/v1/api${endpoint}`;
|
|
Object.assign(options.headers, { 'Authorization': `Bearer ${credentials.appApiKey as string}` });
|
|
}
|
|
|
|
try {
|
|
return await this.helpers.request!(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;
|
|
}
|
|
|
|
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;
|
|
}
|