mindee 1.1.2 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +47 -26
  2. package/LICENSE +21 -202
  3. package/README.md +15 -2
  4. package/coverage.lcov +0 -0
  5. package/dist/api/financialDocument.js +51 -0
  6. package/dist/api/index.js +13 -0
  7. package/dist/api/invoice.js +59 -0
  8. package/dist/api/object.js +80 -0
  9. package/dist/api/receipt.js +45 -0
  10. package/dist/api/request.js +90 -0
  11. package/dist/api/response.js +91 -0
  12. package/dist/documents/document.js +80 -0
  13. package/dist/documents/fields/amount.js +21 -0
  14. package/dist/documents/fields/date.js +17 -0
  15. package/dist/documents/fields/field.js +85 -0
  16. package/dist/documents/fields/index.js +17 -0
  17. package/dist/documents/fields/locale.js +25 -0
  18. package/dist/documents/fields/orientation.js +22 -0
  19. package/dist/documents/fields/paymentDetails.js +50 -0
  20. package/dist/documents/fields/tax.js +42 -0
  21. package/dist/documents/financialDocument.js +216 -0
  22. package/dist/documents/index.js +13 -0
  23. package/dist/documents/invoice.js +361 -0
  24. package/dist/documents/receipt.js +231 -0
  25. package/dist/errors/handler.js +17 -0
  26. package/dist/index.js +23 -0
  27. package/dist/inputs.js +208 -0
  28. package/dist/logger.js +34 -0
  29. package/lib/api/financialDocument.js +1 -1
  30. package/lib/api/invoice.js +1 -1
  31. package/lib/api/response.js +1 -1
  32. package/lib/documents/fields/field.js +4 -2
  33. package/lib/documents/financialDocument.js +8 -51
  34. package/lib/documents/invoice.js +9 -48
  35. package/lib/documents/receipt.js +5 -5
  36. package/lib/index.js +29 -30
  37. package/lib/inputs.js +2 -0
  38. package/mindee/api/financialDocument.js +3 -3
  39. package/mindee/api/object.js +1 -1
  40. package/mindee/api/request.js +17 -15
  41. package/mindee/api/response.js +19 -3
  42. package/mindee/documents/financialDocument.js +7 -2
  43. package/mindee/documents/invoice.js +4 -5
  44. package/mindee/documents/receipt.js +4 -4
  45. package/mindee/inputs.js +14 -8
  46. package/package.json +5 -5
@@ -45,17 +45,13 @@ class FinancialDocument extends Document {
45
45
  * @param {Object} Date - date value for creating FinancialDocument object from scratch
46
46
  * @param {Object} InvoiceNumber - Invoice number value for creating FinancialDocument object from scratch
47
47
  * @param {Object} taxes - taxes value for creating FinancialDocument object from scratch
48
- * @param {Object} supplier - supplier value for creating FinancialDocument object from scratch
49
- * @param {Object} supplierAddress - supplier address value for creating FinancialDocument object from scratch
48
+ * @param {Object} merchantName - merchant name value for creating FinancialDocument object from scratch
50
49
  * @param {Object} paymentDetails - payment details value for creating FinancialDocument object from scratch
51
50
  * @param {Object} companyNumber - company number value for creating FinancialDocument object from scratch
52
51
  * @param {Object} vatNumber - vat number value for creating FinancialDocument object from scratch
53
52
  * @param {Object} orientation - orientation value for creating FinancialDocument object from scratch
54
53
  * @param {Object} totalTax - total tax value for creating FinancialDocument object from scratch
55
54
  * @param {Object} time - time value for creating FinancialDocument object from scratch
56
- * @param {Object} customerName - customer name value for creating FinancialDocument object from scratch
57
- * @param {Object} customerAddress - customer address value for creating FinancialDocument object from scratch
58
- * @param {Object} customerCompanyRegistration - customer company registration value for creating FinancialDocument object from scratch
59
55
  * @param {Object} pageNumber - pageNumber for multi pages pdf input
60
56
  * @param {String} level - specify whether object is built from "page" level or "document" level prediction
61
57
  */
@@ -71,16 +67,12 @@ class FinancialDocument extends Document {
71
67
  dueDate: _dueDate = undefined,
72
68
  taxes: _taxes = undefined,
73
69
  supplier: _supplier = undefined,
74
- supplierAddress: _supplierAddress = undefined,
75
70
  paymentDetails: _paymentDetails = undefined,
76
71
  companyNumber: _companyNumber = undefined,
77
72
  vatNumber: _vatNumber = undefined,
78
73
  orientation: _orientation = undefined,
79
74
  totalTax: _totalTax = undefined,
80
75
  time: _time = undefined,
81
- customerName: _customerName = undefined,
82
- customerAddress: _customerAddress = undefined,
83
- customerCompanyRegistration: _customerCompanyRegistration = undefined,
84
76
  pageNumber: _pageNumber = 0,
85
77
  level = "page"
86
78
  } = _ref;
@@ -106,17 +98,13 @@ class FinancialDocument extends Document {
106
98
  dueDate: _dueDate,
107
99
  taxes: _taxes,
108
100
  supplier: _supplier,
109
- supplierAddress: _supplierAddress,
110
101
  paymentDetails: _paymentDetails,
111
102
  companyNumber: _companyNumber,
112
103
  vatNumber: _vatNumber,
113
104
  orientation: _orientation,
114
105
  pageNumber: _pageNumber,
115
106
  totalTax: _totalTax,
116
- time: _time,
117
- customerName: _customerName,
118
- customerAddress: _customerAddress,
119
- customerCompanyRegistration: _customerCompanyRegistration
107
+ time: _time
120
108
  });
121
109
  } else {
122
110
  _classPrivateMethodGet(this, _initFromApiPrediction, _initFromApiPrediction2).call(this, _apiPrediction, _inputFile, _pageNumber);
@@ -147,11 +135,7 @@ var _initFromScratch2 = function _initFromScratch2(_ref2) {
147
135
  orientation,
148
136
  pageNumber,
149
137
  supplier,
150
- supplierAddress,
151
- time,
152
- customerName,
153
- customerAddress,
154
- customerCompanyRegistration
138
+ time
155
139
  } = _ref2;
156
140
 
157
141
  var constructPrediction = function constructPrediction(item) {
@@ -171,16 +155,12 @@ var _initFromScratch2 = function _initFromScratch2(_ref2) {
171
155
  this.date = new Date(constructPrediction(date));
172
156
  this.dueDate = new Date(constructPrediction(dueDate));
173
157
  this.supplier = new Field(constructPrediction(supplier));
174
- this.supplierAddress = new Field(this.constructPrediction(supplierAddress));
175
158
  this.time = new Field(constructPrediction(time));
176
159
  this.orientation = new Orientation(constructPrediction(orientation));
177
160
  this.invoiceNumber = new Field(constructPrediction(invoiceNumber));
178
161
  this.paymentDetails = new Field(constructPrediction(paymentDetails));
179
162
  this.companyNumber = new Field(constructPrediction(companyNumber));
180
163
  this.vatNumber = new Field(constructPrediction(vatNumber));
181
- this.customerName = new Field(this.constructPrediction(customerName));
182
- this.customerAddress = new Field(this.constructPrediction(customerAddress));
183
- this.customerCompanyRegistration = new Field(this.constructPrediction(customerCompanyRegistration));
184
164
 
185
165
  if (taxes !== undefined) {
186
166
  this.taxes = [];
@@ -215,7 +195,6 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, in
215
195
  this.dueDate = invoice.dueDate;
216
196
  this.taxes = invoice.taxes;
217
197
  this.supplier = invoice.supplier;
218
- this.supplierAddress = invoice.supplierAddress;
219
198
  this.paymentDetails = invoice.paymentDetails;
220
199
  this.companyNumber = invoice.companyNumber;
221
200
  this.orientation = invoice.orientation;
@@ -223,12 +202,9 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, in
223
202
  this.time = new Field({
224
203
  prediction: {
225
204
  value: undefined,
226
- confidence: 0.0
205
+ probability: 0.0
227
206
  }
228
207
  });
229
- this.customerName = invoice.customerName;
230
- this.customerAddress = invoice.customerAddress;
231
- this.customerCompanyRegistration = invoice.customerCompanyRegistration;
232
208
  } else {
233
209
  var receipt = new Receipt({
234
210
  apiPrediction,
@@ -244,45 +220,26 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, in
244
220
  this.totalIncl = receipt.totalIncl;
245
221
  this.totalExcl = receipt.totalExcl;
246
222
  this.supplier = receipt.merchantName;
247
- this.supplierAddress = new Field({
248
- prediction: {
249
- value: undefined,
250
- confidence: 0.0
251
- }
252
- });
253
223
  this.time = receipt.time;
254
224
  this.totalTax = receipt.totalTax;
255
225
  this.invoiceNumber = new Field({
256
226
  prediction: {
257
227
  value: undefined,
258
- confidence: 0.0
228
+ probability: 0.0
259
229
  }
260
230
  });
261
231
  this.paymentDetails = new Field({
262
232
  prediction: {
263
233
  value: undefined,
264
- confidence: 0.0
234
+ probability: 0.0
265
235
  }
266
236
  });
267
237
  this.companyNumber = new Field({
268
238
  prediction: {
269
239
  value: undefined,
270
- confidence: 0.0
271
- }
272
- });
273
- this.customerName = new Field({
274
- prediction: {
275
- value: undefined,
276
- confidence: 0.0
277
- }
278
- });
279
- this.customerAddress = new Field({
280
- prediction: {
281
- value: undefined,
282
- confidence: 0.0
240
+ probability: 0.0
283
241
  }
284
242
  });
285
- this.customerCompanyRegistration = [];
286
243
  }
287
244
  };
288
245
 
@@ -310,7 +267,7 @@ var _taxesMatchTotalIncl2 = function _taxesMatchTotalIncl2() {
310
267
 
311
268
  if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal && reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
312
269
  this.taxes = this.taxes.map(tax => _objectSpread(_objectSpread({}, tax), {}, {
313
- confidence: 1.0
270
+ probability: 1.0
314
271
  }));
315
272
  this.totalTax.probability = 1.0;
316
273
  this.totalIncl.probability = 1.0;
@@ -57,15 +57,11 @@ class Invoice extends Document {
57
57
  * @param {Object} invoiceNumber - invoice number value for creating Invoice object from scratch
58
58
  * @param {Object} taxes - taxes value for creating Invoice object from scratch
59
59
  * @param {Object} supplier - supplier value for creating Invoice object from scratch
60
- * @param {Object} supplierAddress - supplier address value for creating Invoice object from scratch
61
60
  * @param {Object} paymentDetails - payment details value for creating Invoice object from scratch
62
61
  * @param {Object} companyNumber - company number value for creating Invoice object from scratch
63
62
  * @param {Object} vatNumber - vat number value for creating Invoice object from scratch
64
63
  * @param {Object} orientation - orientation value for creating Invoice object from scratch
65
64
  * @param {Object} totalTax - total tax value for creating Invoice object from scratch
66
- * @param {Object} customerName - customer name value for creating Invoice object from scratch
67
- * @param {Object} customerAddress - customer address value for creating Invoice object from scratch
68
- * @param {Object} customerCompanyRegistration - customer company registration value for creating Invoice object from scratch
69
65
  * @param {Number} pageNumber - pageNumber for multi pages pdf input
70
66
  * @param {String} level - specify whether object is built from "page" level or "document" level prediction
71
67
  */
@@ -81,15 +77,11 @@ class Invoice extends Document {
81
77
  dueDate: _dueDate = undefined,
82
78
  taxes: _taxes = undefined,
83
79
  supplier: _supplier = undefined,
84
- supplierAddress: _supplierAddress = undefined,
85
80
  paymentDetails: _paymentDetails = undefined,
86
81
  companyNumber: _companyNumber = undefined,
87
82
  vatNumber: _vatNumber = undefined,
88
83
  orientation: _orientation = undefined,
89
84
  totalTax: _totalTax = undefined,
90
- customerName: _customerName = undefined,
91
- customerAddress: _customerAddress = undefined,
92
- customerCompanyRegistration: _customerCompanyRegistration = undefined,
93
85
  pageNumber: _pageNumber = 0,
94
86
  level = "page"
95
87
  } = _ref;
@@ -139,16 +131,12 @@ class Invoice extends Document {
139
131
  dueDate: _dueDate,
140
132
  taxes: _taxes,
141
133
  supplier: _supplier,
142
- supplierAddress: _supplierAddress,
143
134
  paymentDetails: _paymentDetails,
144
135
  companyNumber: _companyNumber,
145
136
  vatNumber: _vatNumber,
146
137
  orientation: _orientation,
147
138
  pageNumber: _pageNumber,
148
- totalTax: _totalTax,
149
- customerName: _customerName,
150
- customerAddress: _customerAddress,
151
- customerCompanyRegistration: _customerCompanyRegistration
139
+ totalTax: _totalTax
152
140
  });
153
141
  } else {
154
142
  _classPrivateMethodGet(this, _initFromApiPrediction, _initFromApiPrediction2).call(this, _apiPrediction, _pageNumber);
@@ -160,7 +148,7 @@ class Invoice extends Document {
160
148
  }
161
149
 
162
150
  toString() {
163
- return "\n -----Invoice data-----\n Filename: ".concat(this.filename, "\n Invoice number: ").concat(this.invoiceNumber.value, "\n Total amount including taxes: ").concat(this.totalIncl.value, "\n Total amount excluding taxes: ").concat(this.totalExcl.value, "\n Invoice Date: ").concat(this.invoiceDate.value, "\n Supplier name: ").concat(this.supplier.value, "\n Supplier address: ").concat(this.supplierAddress.value, "\n Customer name: ").concat(this.customerName.value, "\n Customer address: ").concat(this.customerAddress.value, "\n Taxes: ").concat(this.taxes.map(tax => tax.toString()).join(" - "), "\n Total taxes: ").concat(this.totalTax.value, "\n ");
151
+ return "\n -----Invoice data-----\n Filename: ".concat(this.filename, "\n Invoice number: ").concat(this.invoiceNumber.value, "\n Total amount including taxes: ").concat(this.totalIncl.value, "\n Total amount excluding taxes: ").concat(this.totalExcl.value, "\n Invoice Date: ").concat(this.invoiceDate.value, "\n Supplier name: ").concat(this.supplier.value, "\n Taxes: ").concat(this.taxes.map(tax => tax.toString()).join(" - "), "\n Total taxes: ").concat(this.totalTax.value, "\n ");
164
152
  }
165
153
 
166
154
  }
@@ -176,15 +164,11 @@ var _initFromScratch2 = function _initFromScratch2(_ref2) {
176
164
  dueDate,
177
165
  taxes,
178
166
  supplier,
179
- supplierAddress,
180
167
  paymentDetails,
181
168
  companyNumber,
182
169
  vatNumber,
183
170
  orientation,
184
- pageNumber,
185
- customerName,
186
- customerAddress,
187
- customerCompanyRegistration
171
+ pageNumber
188
172
  } = _ref2;
189
173
  this.locale = new Locale(this.constructPrediction(locale));
190
174
  this.totalIncl = new Amount(this.constructPrediction(totalIncl));
@@ -194,14 +178,10 @@ var _initFromScratch2 = function _initFromScratch2(_ref2) {
194
178
  this.invoiceDate = new Date(this.constructPrediction(invoiceDate));
195
179
  this.dueDate = new Date(this.constructPrediction(dueDate));
196
180
  this.supplier = new Field(this.constructPrediction(supplier));
197
- this.supplierAddress = new Field(this.constructPrediction(supplierAddress));
198
181
  this.invoiceNumber = new Field(this.constructPrediction(invoiceNumber));
199
182
  this.paymentDetails = new Field(this.constructPrediction(paymentDetails));
200
183
  this.companyNumber = new Field(this.constructPrediction(companyNumber));
201
184
  this.vatNumber = new Field(this.constructPrediction(vatNumber));
202
- this.customerName = new Field(this.constructPrediction(customerName));
203
- this.customerAddress = new Field(this.constructPrediction(customerAddress));
204
- this.customerCompanyRegistration = new Field(this.constructPrediction(customerCompanyRegistration));
205
185
 
206
186
  if (taxes !== undefined) {
207
187
  this.taxes = [];
@@ -238,7 +218,7 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, pa
238
218
  this.totalTax = new Amount({
239
219
  prediction: {
240
220
  value: undefined,
241
- confidence: 0.0
221
+ probability: 0.0
242
222
  },
243
223
  valueKey: "value",
244
224
  pageNumber
@@ -287,25 +267,6 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, pa
287
267
  prediction: apiPrediction.supplier,
288
268
  pageNumber
289
269
  });
290
- this.supplierAddress = new Field({
291
- prediction: apiPrediction.supplier_address,
292
- pageNumber
293
- });
294
- this.customerName = new Field({
295
- prediction: apiPrediction.customer,
296
- pageNumber
297
- });
298
- this.customerAddress = new Field({
299
- prediction: apiPrediction.customer_address,
300
- pageNumber
301
- });
302
- this.customerCompanyRegistration = apiPrediction.customer_company_registration.map(function (customerCompanyRegistration) {
303
- return new Field({
304
- prediction: customerCompanyRegistration,
305
- pageNumber,
306
- extraFields: ["type"]
307
- });
308
- });
309
270
  this.paymentDetails = apiPrediction.payment_details.map(function (paymentDetail) {
310
271
  return new PaymentDetails({
311
272
  prediction: paymentDetail,
@@ -322,7 +283,7 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, pa
322
283
  this.orientation = new Orientation(this.constructPrediction({
323
284
  prediction: {
324
285
  value: undefined,
325
- confidence: 0.0,
286
+ probability: 0.0,
326
287
  degrees: undefined
327
288
  }
328
289
  }));
@@ -431,7 +392,7 @@ var _reconstructTotalTax2 = function _reconstructTotalTax2() {
431
392
  value: this.taxes.reduce((acc, tax) => {
432
393
  return tax.value !== undefined ? acc + tax.value : acc;
433
394
  }, 0),
434
- confidence: Field.arrayProbability(this.taxes)
395
+ probability: Field.arrayProbability(this.taxes)
435
396
  };
436
397
  if (totalTax.value > 0) this.totalTax = new Amount({
437
398
  prediction: totalTax,
@@ -445,7 +406,7 @@ var _reconstructTotalTaxFromTotals2 = function _reconstructTotalTaxFromTotals2()
445
406
  if (this.totalTax.value === undefined && this.totalIncl.value > 0 && this.totalExcl.value > 0 && this.totalExcl.value <= this.totalIncl.value) {
446
407
  var totalTax = {
447
408
  value: this.totalIncl.value - this.totalExcl.value,
448
- confidence: this.totalIncl.probability * this.totalExcl.probability
409
+ probability: this.totalIncl.probability * this.totalExcl.probability
449
410
  };
450
411
  if (totalTax.value > 0) this.totalTax = new Amount({
451
412
  prediction: totalTax,
@@ -461,7 +422,7 @@ var _reconstructTotalExcl2 = function _reconstructTotalExcl2() {
461
422
  value: this.totalIncl.value - this.taxes.reduce((acc, tax) => {
462
423
  return tax.value !== undefined ? acc + tax.value : acc;
463
424
  }, 0),
464
- confidence: Field.arrayProbability(this.taxes) * this.totalIncl.probability
425
+ probability: Field.arrayProbability(this.taxes) * this.totalIncl.probability
465
426
  };
466
427
  this.totalExcl = new Amount({
467
428
  prediction: totalExcl,
@@ -477,7 +438,7 @@ var _reconstructTotalIncl2 = function _reconstructTotalIncl2() {
477
438
  value: this.totalExcl.value + this.taxes.reduce((acc, tax) => {
478
439
  return tax.value ? acc + tax.value : acc;
479
440
  }, 0.0),
480
- confidence: Field.arrayProbability(this.taxes) * this.totalExcl.probability
441
+ probability: Field.arrayProbability(this.taxes) * this.totalExcl.probability
481
442
  };
482
443
  this.totalIncl = new Amount({
483
444
  prediction: totalIncl,
@@ -229,7 +229,7 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, pa
229
229
  this.totalTax = new Amount({
230
230
  prediction: {
231
231
  value: undefined,
232
- confidence: 0
232
+ probability: 0
233
233
  },
234
234
  valueKey: "value",
235
235
  pageNumber
@@ -237,7 +237,7 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, pa
237
237
  this.totalExcl = new Amount({
238
238
  prediction: {
239
239
  value: undefined,
240
- confidence: 0
240
+ probability: 0
241
241
  },
242
242
  valueKey: "value",
243
243
  pageNumber
@@ -252,7 +252,7 @@ var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, pa
252
252
  this.orientation = new Orientation(this.constructPrediction({
253
253
  prediction: {
254
254
  value: undefined,
255
- confidence: 0.0,
255
+ probability: 0.0,
256
256
  degrees: undefined
257
257
  }
258
258
  }));
@@ -305,7 +305,7 @@ var _reconstructTotalExclFromTCCAndTaxes2 = function _reconstructTotalExclFromTC
305
305
  if (this.taxes.length && this.totalIncl.value != null) {
306
306
  var totalExcl = {
307
307
  value: this.totalIncl.value - Field.arraySum(this.taxes),
308
- confidence: Field.arrayProbability(this.taxes) * this.totalIncl.probability
308
+ probability: Field.arrayProbability(this.taxes) * this.totalIncl.probability
309
309
  };
310
310
  this.totalExcl = new Amount({
311
311
  prediction: totalExcl,
@@ -319,7 +319,7 @@ var _reconstructTotalTax2 = function _reconstructTotalTax2() {
319
319
  if (this.taxes.length && this.totalTax.value == null) {
320
320
  var totalTax = {
321
321
  value: this.taxes.map(tax => tax.value || 0).reduce((a, b) => a + b, 0),
322
- confidence: Field.arrayProbability(this.taxes)
322
+ probability: Field.arrayProbability(this.taxes)
323
323
  };
324
324
  if (totalTax.value > 0) this.totalTax = new Amount({
325
325
  prediction: totalTax,
package/lib/index.js CHANGED
@@ -1,39 +1,38 @@
1
1
  "use strict";
2
2
 
3
- var errorHandler = require("./errors/handler");
4
-
5
- var logger = require("./logger");
6
-
7
- var APIReceipt = require("./api/receipt");
8
-
9
- var APIInvoice = require("./api/invoice");
10
-
11
- var APIFinancialDocument = require("./api/financialDocument");
12
-
13
- class Client {
14
- /**
15
- * @param {string} receiptToken - Receipt Expense Token from Mindee dashboard
16
- * @param {string} invoiceToken - Invoice Token from Mindee dashboard
17
- * @param {boolean} throwOnError - Throw if an error is send from the API / SDK (true by default)
18
- * @param {boolean} debug - Enable debug logging (disable by default)
19
- */
20
- constructor() {
21
- var {
22
- receiptToken = undefined,
23
- invoiceToken = undefined,
24
- throwOnError = true,
25
- debug = undefined
26
- } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
3
+ exports.__esModule = true;
4
+ exports.Client = void 0;
5
+
6
+ var handler_1 = require("@errors/handler");
7
+
8
+ var logger_1 = require("@mindee/logger");
9
+
10
+ var receipt_1 = require("@mindee/api/receipt");
11
+
12
+ var invoice_1 = require("@mindee/api/invoice");
13
+
14
+ var financialDocument_1 = require("@mindee/api/financialDocument");
15
+
16
+ var Client =
17
+ /** @class */
18
+ function () {
19
+ function Client(_a) {
20
+ var receiptToken = _a.receiptToken,
21
+ invoiceToken = _a.invoiceToken,
22
+ _b = _a.throwOnError,
23
+ throwOnError = _b === void 0 ? true : _b,
24
+ debug = _a.debug;
27
25
  this.receiptToken = receiptToken || process.env.MINDEE_RECEIPT_TOKEN;
28
26
  this.invoiceToken = invoiceToken || process.env.MINDEE_INVOICE_TOKEN;
29
- errorHandler.throwOnError = throwOnError;
30
- logger.level = (debug !== null && debug !== void 0 ? debug : process.env.MINDEE_DEBUG) ? "debug" : "warn";
31
- this.receipt = new APIReceipt(this.receiptToken);
32
- this.invoice = new APIInvoice(this.invoiceToken);
33
- this.financialDocument = new APIFinancialDocument(this.invoiceToken, this.receiptToken);
27
+ handler_1.errorHandler.throwOnError = throwOnError;
28
+ logger_1.logger.level = (debug !== null && debug !== void 0 ? debug : process.env.MINDEE_DEBUG) ? "debug" : "warn";
29
+ this.receipt = new receipt_1.APIReceipt(this.receiptToken);
30
+ this.invoice = new invoice_1.APIInvoice(this.invoiceToken);
31
+ this.financialDocument = new financialDocument_1.APIFinancialDocument(this.invoiceToken, this.receiptToken);
34
32
  }
35
33
 
36
- }
34
+ return Client;
35
+ }();
37
36
 
38
37
  exports.Client = Client;
39
38
  exports.documents = require("./documents");
package/lib/inputs.js CHANGED
@@ -104,6 +104,8 @@ class Input {
104
104
 
105
105
  var filetype = _this3.filename.split(".").pop();
106
106
 
107
+ console.log(_this3.filename.split("."));
108
+
107
109
  if (!(filetype in _this3.MIMETYPES)) {
108
110
  errorHandler.throw(new Error("File type is not allowed. It must be ".concat(Object.keys(_this3.MIMETYPES).toString())));
109
111
  }
@@ -37,10 +37,10 @@ class APIFinancialDocument extends APIObject {
37
37
  : this.receiptToken;
38
38
  const url =
39
39
  inputFile.fileExtension === "application/pdf"
40
- ? "/invoices/v3/predict"
41
- : "/expense_receipts/v3/predict";
40
+ ? `/invoices/v${version}/predict`
41
+ : `/expense_receipts/v${version}/predict`;
42
42
  super.parse();
43
- return await super._request(url, inputFile, version, includeWords);
43
+ return super._request(url, inputFile, includeWords);
44
44
  }
45
45
  }
46
46
 
@@ -10,7 +10,7 @@ const request = require("./request");
10
10
  class APIObject {
11
11
  constructor(apiToken = undefined, apiName = "") {
12
12
  this.apiToken = apiToken;
13
- this.baseUrl = "https://api.mindee.net/v1/products/mindee/";
13
+ this.baseUrl = "https://api.mindee.net/v1/products/mindee";
14
14
  this.apiName = apiName;
15
15
  }
16
16
 
@@ -6,24 +6,27 @@ const os = require("os");
6
6
 
7
7
  const request = (url, method, headers, input, includeWords = false) => {
8
8
  return new Promise(function (resolve, reject) {
9
- const form = new FormData();
10
- let body;
9
+ let form = new FormData();
11
10
 
12
11
  headers["User-Agent"] = `mindee-api-nodejs@v${sdkVersion} nodejs-${
13
12
  process.version
14
13
  } ${os.type().toLowerCase()}`;
15
- if (["path", "stream"].includes(input.inputType)) {
16
- const fileParams = { filename: input.filename };
14
+
15
+ const fileParams = { filename: input.filename };
16
+
17
+ if (input.inputType === "base64") {
18
+ form.append(
19
+ "document",
20
+ Buffer.from(input.fileObject, "base64"),
21
+ fileParams
22
+ );
23
+ } else {
17
24
  form.append("document", input.fileObject, fileParams);
18
- if (includeWords) form.append("include_mvision", "true");
19
- headers = { ...headers, ...form.getHeaders() };
20
- } else if (input.inputType === "base64") {
21
- let body_obj = { document: input.fileObject };
22
- if (includeWords) body_obj["include_mvision"] = "true";
23
- body = JSON.stringify(body_obj);
24
- headers["Content-Type"] = "application/json";
25
- headers["Content-Length"] = body.length;
26
25
  }
26
+ if (includeWords) {
27
+ form.append("include_mvision", "true");
28
+ }
29
+ headers = { ...headers, ...form.getHeaders() };
27
30
 
28
31
  const uri = new URL(url);
29
32
  const options = {
@@ -47,7 +50,7 @@ const request = (url, method, headers, input, includeWords = false) => {
47
50
  data: JSON.parse(responseBody),
48
51
  });
49
52
  } catch (error) {
50
- console.log(responseBody, error);
53
+ console.error(responseBody, error);
51
54
  }
52
55
  });
53
56
  });
@@ -61,8 +64,7 @@ const request = (url, method, headers, input, includeWords = false) => {
61
64
  }
62
65
 
63
66
  if (input.inputType === "base64") {
64
- req.write(body);
65
- req.end();
67
+ form.pipe(req);
66
68
  }
67
69
  });
68
70
  };
@@ -32,31 +32,47 @@ class Response {
32
32
  }
33
33
 
34
34
  formatResponse() {
35
+ let http_data_document = this.httpResponse.data.document;
35
36
  const constructors = {
36
37
  receipt: (params) => new Receipt(params),
37
38
  invoice: (params) => new Invoice(params),
38
39
  financialDocument: (params) => new FinancialDocument(params),
39
40
  };
40
- const predictions = this.httpResponse.data.document.inference.pages.entries();
41
+
42
+ const predictions = http_data_document.inference.pages.entries();
41
43
  this[`${this.documentType}s`] = [];
44
+ let document_words_content = [];
42
45
 
43
46
  // Create a list of Document (Receipt, Invoice...) for each page of the input document
44
47
  for (const [pageNumber, prediction] of predictions) {
48
+ let page_words_content = [];
49
+ if (
50
+ "ocr" in http_data_document &&
51
+ Object.keys(http_data_document.ocr).length > 0
52
+ ) {
53
+ page_words_content =
54
+ http_data_document.ocr["mvision-v1"].pages[pageNumber].all_words;
55
+ document_words_content.push(
56
+ ...http_data_document.ocr["mvision-v1"].pages[pageNumber].all_words
57
+ );
58
+ }
45
59
  this[`${this.documentType}s`].push(
46
60
  constructors[this.documentType]({
47
61
  apiPrediction: prediction.prediction,
48
62
  inputFile: this.input,
49
63
  pageNumber: pageNumber,
64
+ words: page_words_content,
50
65
  })
51
66
  );
52
67
  }
53
68
 
54
69
  // Merge the list of Document into a unique Document
55
70
  this[this.documentType] = constructors[this.documentType]({
56
- apiPrediction: this.httpResponse.data.document.inference.prediction,
71
+ apiPrediction: http_data_document.inference.prediction,
57
72
  inputFile: this.input,
58
- pageNumber: this.httpResponse.data.document.n_pages,
73
+ pageNumber: http_data_document.n_pages,
59
74
  level: "document",
75
+ words: document_words_content,
60
76
  });
61
77
  }
62
78
  }
@@ -54,6 +54,7 @@ class FinancialDocument extends Document {
54
54
  customerName = undefined,
55
55
  customerAddress = undefined,
56
56
  customerCompanyRegistration = undefined,
57
+ words = undefined,
57
58
  pageNumber = 0,
58
59
  level = "page",
59
60
  }) {
@@ -82,7 +83,7 @@ class FinancialDocument extends Document {
82
83
  customerCompanyRegistration,
83
84
  });
84
85
  } else {
85
- this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber);
86
+ this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber, words);
86
87
  }
87
88
  this.#checklist();
88
89
  }
@@ -145,13 +146,14 @@ class FinancialDocument extends Document {
145
146
  }
146
147
  }
147
148
 
148
- #initFromApiPrediction(apiPrediction, inputFile, pageNumber) {
149
+ #initFromApiPrediction(apiPrediction, inputFile, pageNumber, words) {
149
150
  if (Object.keys(apiPrediction).includes("invoice_number")) {
150
151
  const invoice = new Invoice({
151
152
  apiPrediction,
152
153
  inputFile,
153
154
  pageNumber,
154
155
  level: this.level,
156
+ words: words,
155
157
  });
156
158
  this.locale = invoice.locale;
157
159
  this.totalIncl = invoice.totalIncl;
@@ -166,6 +168,7 @@ class FinancialDocument extends Document {
166
168
  this.companyNumber = invoice.companyNumber;
167
169
  this.orientation = invoice.orientation;
168
170
  this.totalTax = invoice.totalTax;
171
+ if (invoice.words) this.words = invoice.words;
169
172
  this.time = new Field({
170
173
  prediction: { value: undefined, confidence: 0.0 },
171
174
  });
@@ -178,6 +181,7 @@ class FinancialDocument extends Document {
178
181
  inputFile,
179
182
  pageNumber,
180
183
  level: this.level,
184
+ words: words,
181
185
  });
182
186
  this.orientation = receipt.orientation;
183
187
  this.date = receipt.date;
@@ -208,6 +212,7 @@ class FinancialDocument extends Document {
208
212
  prediction: { value: undefined, confidence: 0.0 },
209
213
  });
210
214
  this.customerCompanyRegistration = [];
215
+ if (receipt.words) this.words = receipt.words;
211
216
  }
212
217
  }
213
218