n8n/packages/nodes-base/nodes/Mindee/GenericFunctions.ts
Michael Kret 61e26804ba
refactor(core): Remove linting exceptions in nodes-base (#4794)
*  enabled array-type

*  await-thenable on

*  ban-types on

*  default-param-last on

*  dot-notation on

*  member-delimiter-style on

*  no-duplicate-imports on

*  no-empty-interface on

*  no-floating-promises on

*  no-for-in-array on

*  no-invalid-void-type on

*  no-loop-func on

*  no-shadow on

*  ban-ts-comment re enabled

*  @typescript-eslint/lines-between-class-members on

* address my own comment

* @typescript-eslint/return-await on

* @typescript-eslint/promise-function-async on

* @typescript-eslint/no-unnecessary-boolean-literal-compare on

* @typescript-eslint/no-unnecessary-type-assertion on

* prefer-const on

* @typescript-eslint/prefer-optional-chain on

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2022-12-02 21:54:28 +01:00

112 lines
2.8 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> {
const resource = this.getNodeParameter('resource', 0);
let service;
if (resource === 'receipt') {
service = 'mindeeReceiptApi';
} else {
service = 'mindeeInvoiceApi';
}
const version = this.getNodeParameter('apiVersion', 0) as number;
// V1 of mindee is deprecated, we are keeping it for now but now V3 is active
const url =
version === 1
? `https://api.mindee.net/products${path}`
: `https://api.mindee.net/v1/products/mindee${path}`;
const options: OptionsWithUri = {
headers: {},
method,
body,
qs,
uri: url,
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);
}
return await this.helpers.requestWithAuthentication.call(this, service, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export function cleanDataPreviousApiVersions(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 {
newData[key] =
//@ts-ignore
data.value || data.name || data.raw || data.degrees || data.amount || data.iban;
}
}
return newData;
}
export function cleanData(document: IDataObject) {
// @ts-ignore
const prediction = document.inference.prediction as IDataObject;
const newData: IDataObject = {};
newData.id = document.id;
newData.name = document.name;
newData.number_of_pages = document.n_pages;
for (const key of Object.keys(prediction)) {
const data = prediction[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 {
newData[key] =
//@ts-ignore
data.value || data.name || data.raw || data.degrees || data.amount || data.iban;
}
}
return newData;
}