Add LignvaNex Node (#1194)

*  Add LignvaNex node

*  Small improvement to LingvaNext Node

Improvements to #1191

Co-authored-by: Tanay Pant <tanaypant@protonmail.com>
This commit is contained in:
Ricardo Espinoza
2020-11-24 17:30:43 -05:00
committed by GitHub
parent 991379066b
commit be828a3808
6 changed files with 505 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
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;
}
}