mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-16 01:24:05 -08:00
7ce7285f7a
* Changes to types so that credentials can be always loaded from DB This first commit changes all return types from the execute functions and calls to get credentials to be async so we can use await. This is a first step as previously credentials were loaded in memory and always available. We will now be loading them from the DB which requires turning the whole call chain async. * Fix updated files * Removed unnecessary credential loading to improve performance * Fix typo * ⚡ Fix issue * Updated new nodes to load credentials async * ⚡ Remove not needed comment Co-authored-by: Jan Oberhauser <jan.oberhauser@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') as IDataObject;
|
|
} else {
|
|
credentials = await 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) {
|
|
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;
|
|
}
|