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>
80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IExecuteSingleFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject, NodeApiError,
|
|
} from 'n8n-workflow';
|
|
|
|
export async function mindeeApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, path: string, body: any = {}, qs: IDataObject = {}, option = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
let credentials;
|
|
|
|
if (resource === 'receipt') {
|
|
credentials = await this.getCredentials('mindeeReceiptApi');
|
|
} else {
|
|
credentials = await this.getCredentials('mindeeInvoiceApi');
|
|
}
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {
|
|
'X-Inferuser-Token': credentials.apiKey,
|
|
},
|
|
method,
|
|
body,
|
|
qs,
|
|
uri: `https://api.mindee.net/products${path}`,
|
|
json: true,
|
|
};
|
|
try {
|
|
if (Object.keys(body).length === 0) {
|
|
delete options.body;
|
|
}
|
|
if (Object.keys(qs).length === 0) {
|
|
delete options.qs;
|
|
}
|
|
if (Object.keys(option).length !== 0) {
|
|
Object.assign(options, option);
|
|
}
|
|
//@ts-ignore
|
|
return await this.helpers.request.call(this, options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export function cleanData(predictions: IDataObject[]) {
|
|
|
|
const newData: IDataObject = {};
|
|
|
|
for (const key of Object.keys(predictions[0])) {
|
|
|
|
const data = predictions[0][key] as IDataObject | IDataObject[];
|
|
|
|
if (key === 'taxes' && data.length) {
|
|
newData[key] = {
|
|
amount: (data as IDataObject[])[0].amount,
|
|
rate: (data as IDataObject[])[0].rate,
|
|
};
|
|
} else if (key === 'locale') {
|
|
//@ts-ignore
|
|
newData['currency'] = data.currency;
|
|
//@ts-ignore
|
|
newData['locale'] = data.value;
|
|
} else {
|
|
//@ts-ignore
|
|
newData[key] = data.value || data.name || data.raw || data.degrees || data.amount || data.iban;
|
|
}
|
|
}
|
|
|
|
return newData;
|
|
}
|