n8n/packages/nodes-base/nodes/LingvaNex/GenericFunctions.ts
Ricardo Espinoza be828a3808
Add LignvaNex Node (#1194)
*  Add LignvaNex node

*  Small improvement to LingvaNext Node

Improvements to #1191

Co-authored-by: Tanay Pant <tanaypant@protonmail.com>
2020-11-24 23:30:43 +01:00

55 lines
1.4 KiB
TypeScript

import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function lingvaNexApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
try {
const credentials = this.getCredentials('lingvaNexApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
},
method,
qs,
body,
uri: uri || `https://api-b2b.backenster.com/b1/api/v3${resource}`,
json: true,
};
options = Object.assign({}, options, option);
const response = await this.helpers.request!(options);
if (response.err !== null) {
throw new Error(`LingvaNex error response [400]: ${response.err}`);
}
return response;
} catch (error) {
if (error.response && error.response.body && error.response.body.message) {
// Try to return the error prettier
const errorBody = error.response.body;
throw new Error(`LingvaNex error response [${error.statusCode}]: ${errorBody.message}`);
}
// Expected error data did not get returned so throw the actual error
throw error;
}
}