2020-10-03 05:08:50 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
2020-10-03 05:09:37 -07:00
|
|
|
} from 'request';
|
2020-10-03 05:08:50 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError,
|
2020-10-03 05:08:50 -07:00
|
|
|
} 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 = this.getCredentials('mindeeReceiptApi') as IDataObject;
|
|
|
|
} else {
|
|
|
|
credentials = this.getCredentials('mindeeInvoiceApi') as IDataObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-10-03 05:08:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
2020-10-10 09:41:23 -07:00
|
|
|
} else if (key === 'locale') {
|
|
|
|
//@ts-ignore
|
|
|
|
newData['currency'] = data.currency;
|
|
|
|
//@ts-ignore
|
|
|
|
newData['locale'] = data.value;
|
2020-10-03 05:08:50 -07:00
|
|
|
} else {
|
|
|
|
//@ts-ignore
|
|
|
|
newData[key] = data.value || data.name || data.raw || data.degrees || data.amount || data.iban;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newData;
|
|
|
|
}
|