mindee 1.3.3 → 1.4.0
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.
- package/CHANGELOG.md +10 -0
- package/mindee/errors/handler.js +1 -1
- package/mindee/index.js +5 -2
- package/mindee/inputs.js +45 -22
- package/mindee/logger.js +8 -7
- package/package.json +13 -4
- package/.eslintrc +0 -24
- package/.mocharc.yaml +0 -2
- package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
- package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/.prettierrc +0 -4
- package/.tool-versions +0 -1
- package/CODE_OF_CONDUCT.md +0 -63
- package/CONTRIBUTING.md +0 -84
- package/ISSUE_TEMPLATE.md +0 -45
- package/PULL_REQUEST_TEMPLATE.md +0 -45
- package/babel.config.json +0 -10
- package/coverage/coverage.json +0 -1
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -171
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov.info +0 -1595
- package/coverage.lcov +0 -1595
- package/dist/api/financialDocument.js +0 -51
- package/dist/api/index.js +0 -13
- package/dist/api/invoice.js +0 -59
- package/dist/api/object.js +0 -80
- package/dist/api/receipt.js +0 -45
- package/dist/api/request.js +0 -90
- package/dist/api/response.js +0 -91
- package/dist/documents/document.js +0 -80
- package/dist/documents/fields/amount.js +0 -21
- package/dist/documents/fields/date.js +0 -17
- package/dist/documents/fields/field.js +0 -85
- package/dist/documents/fields/index.js +0 -17
- package/dist/documents/fields/locale.js +0 -25
- package/dist/documents/fields/orientation.js +0 -22
- package/dist/documents/fields/paymentDetails.js +0 -50
- package/dist/documents/fields/tax.js +0 -42
- package/dist/documents/financialDocument.js +0 -216
- package/dist/documents/index.js +0 -13
- package/dist/documents/invoice.js +0 -361
- package/dist/documents/receipt.js +0 -231
- package/dist/errors/handler.js +0 -17
- package/dist/index.js +0 -23
- package/dist/inputs.js +0 -208
- package/dist/logger.js +0 -34
- package/examples/.env.example +0 -2
- package/examples/documents/invoices/credit_note.pdf +0 -900
- package/examples/documents/invoices/invoice.pdf +0 -0
- package/examples/documents/invoices/invoice_6p.pdf +0 -0
- package/examples/documents/receipts/receipt.jpg +0 -0
- package/examples/financialDocument.js +0 -32
- package/examples/invoice.js +0 -74
- package/examples/receipt.js +0 -45
- package/lib/api/financialDocument.js +0 -59
- package/lib/api/index.js +0 -7
- package/lib/api/invoice.js +0 -73
- package/lib/api/object.js +0 -90
- package/lib/api/receipt.js +0 -58
- package/lib/api/request.js +0 -84
- package/lib/api/response.js +0 -92
- package/lib/documents/document.js +0 -97
- package/lib/documents/fields/amount.js +0 -35
- package/lib/documents/fields/date.js +0 -36
- package/lib/documents/fields/field.js +0 -102
- package/lib/documents/fields/index.js +0 -9
- package/lib/documents/fields/locale.js +0 -35
- package/lib/documents/fields/orientation.js +0 -33
- package/lib/documents/fields/paymentDetails.js +0 -71
- package/lib/documents/fields/tax.js +0 -57
- package/lib/documents/financialDocument.js +0 -280
- package/lib/documents/index.js +0 -7
- package/lib/documents/invoice.js +0 -451
- package/lib/documents/receipt.js +0 -332
- package/lib/errors/handler.js +0 -19
- package/lib/index.js +0 -40
- package/lib/inputs.js +0 -190
- package/lib/logger.js +0 -52
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const { Client } = require("mindee");
|
|
2
|
-
|
|
3
|
-
// Receipt token can be set by Env (MINDEE_RECEIPT_TOKEN) or via params (Client({receiptToken: "token"}))
|
|
4
|
-
const mindeeClient = new Client();
|
|
5
|
-
|
|
6
|
-
// parsing receipt from picture
|
|
7
|
-
mindeeClient.financialDocument
|
|
8
|
-
.parse({ input: "./documents/receipts/receipt.jpg" })
|
|
9
|
-
.then((res) => {
|
|
10
|
-
console.log("Success !");
|
|
11
|
-
console.log(res.financialDocuments);
|
|
12
|
-
console.log(res.financialDocument);
|
|
13
|
-
})
|
|
14
|
-
.catch((err) => {
|
|
15
|
-
console.error(err);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
// parsing receipt from base64 picture
|
|
19
|
-
const fs = require("fs");
|
|
20
|
-
const base64 = fs.readFileSync("./documents/receipts/receipt.jpg", {
|
|
21
|
-
encoding: "base64",
|
|
22
|
-
});
|
|
23
|
-
mindeeClient.financialDocument
|
|
24
|
-
.parse({ input: base64, inputType: "base64" })
|
|
25
|
-
.then((res) => {
|
|
26
|
-
console.log("Success !");
|
|
27
|
-
console.log(res.financialDocuments);
|
|
28
|
-
console.log(res.financialDocument);
|
|
29
|
-
})
|
|
30
|
-
.catch((err) => {
|
|
31
|
-
console.error(err);
|
|
32
|
-
});
|
package/examples/invoice.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
const { Client } = require("mindee");
|
|
2
|
-
const fs = require("fs");
|
|
3
|
-
|
|
4
|
-
// Invoice token can be set by Env (MINDEE_INVOICE_TOKEN) or via params (Client({invoiceToken: "token"}))
|
|
5
|
-
const mindeeClient = new Client();
|
|
6
|
-
|
|
7
|
-
// parsing invoice from pdf
|
|
8
|
-
mindeeClient.invoice
|
|
9
|
-
.parse({ input: "./documents/invoices/invoice.pdf" })
|
|
10
|
-
.then((res) => {
|
|
11
|
-
console.log("Success !");
|
|
12
|
-
console.log(res.invoices);
|
|
13
|
-
console.log(res.invoice);
|
|
14
|
-
console.log(res.documentType);
|
|
15
|
-
})
|
|
16
|
-
.catch((err) => {
|
|
17
|
-
console.error(err);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// parsing credit note from pdf
|
|
21
|
-
mindeeClient.invoice
|
|
22
|
-
.parse({ input: "./documents/invoices/credit_note.pdf" })
|
|
23
|
-
.then((res) => {
|
|
24
|
-
console.log("Success !");
|
|
25
|
-
console.log(res.invoices);
|
|
26
|
-
console.log(res.invoice);
|
|
27
|
-
console.log(res.documentType);
|
|
28
|
-
})
|
|
29
|
-
.catch((err) => {
|
|
30
|
-
console.error(err);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// // parsing invoice from multiple page pdf
|
|
34
|
-
mindeeClient.invoice
|
|
35
|
-
.parse({ input: "./documents/invoices/invoice_6p.pdf" })
|
|
36
|
-
.then((res) => {
|
|
37
|
-
console.log("Success !");
|
|
38
|
-
console.log(res.documentType);
|
|
39
|
-
console.log(res.invoices);
|
|
40
|
-
console.log(res.invoice);
|
|
41
|
-
})
|
|
42
|
-
.catch((err) => {
|
|
43
|
-
console.error(err);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// parsing invoice from base64 file
|
|
47
|
-
const base64 = fs.readFileSync("./documents/invoices/invoice.pdf", {
|
|
48
|
-
encoding: "base64",
|
|
49
|
-
});
|
|
50
|
-
mindeeClient.invoice
|
|
51
|
-
.parse({ input: base64, inputType: "base64" })
|
|
52
|
-
.then((res) => {
|
|
53
|
-
console.log("Success !");
|
|
54
|
-
console.log(res.documentType);
|
|
55
|
-
console.log(res.invoices);
|
|
56
|
-
console.log(res.invoice);
|
|
57
|
-
})
|
|
58
|
-
.catch((err) => {
|
|
59
|
-
console.error(err);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// parsing invoice from stream
|
|
63
|
-
const stream = fs.createReadStream("./documents/invoices/invoice.pdf");
|
|
64
|
-
mindeeClient.invoice
|
|
65
|
-
.parse({ input: stream, inputType: "stream" })
|
|
66
|
-
.then((res) => {
|
|
67
|
-
console.log("Success !");
|
|
68
|
-
console.log(res.documentType);
|
|
69
|
-
console.log(res.invoices);
|
|
70
|
-
console.log(res.invoice);
|
|
71
|
-
})
|
|
72
|
-
.catch((err) => {
|
|
73
|
-
console.error(err);
|
|
74
|
-
});
|
package/examples/receipt.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const { Client } = require("mindee");
|
|
2
|
-
const fs = require("fs");
|
|
3
|
-
|
|
4
|
-
// Receipt token can be set by Env (MINDEE_RECEIPT_TOKEN) or via params (Client({receiptToken: "token"}))
|
|
5
|
-
const mindeeClient = new Client();
|
|
6
|
-
|
|
7
|
-
// parsing receipt from picture
|
|
8
|
-
mindeeClient.receipt
|
|
9
|
-
.parse({ input: "./documents/receipts/receipt.jpg" })
|
|
10
|
-
.then((res) => {
|
|
11
|
-
console.log("Success !");
|
|
12
|
-
console.log(res.receipts);
|
|
13
|
-
console.log(res.receipt);
|
|
14
|
-
})
|
|
15
|
-
.catch((err) => {
|
|
16
|
-
console.error(err);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// parsing receipt from base64 picture
|
|
20
|
-
const base64 = fs.readFileSync("./documents/receipts/receipt.jpg", {
|
|
21
|
-
encoding: "base64",
|
|
22
|
-
});
|
|
23
|
-
mindeeClient.receipt
|
|
24
|
-
.parse({ input: base64, inputType: "base64" })
|
|
25
|
-
.then((res) => {
|
|
26
|
-
console.log("Success !");
|
|
27
|
-
console.log(res.receipts);
|
|
28
|
-
console.log(res.receipt);
|
|
29
|
-
})
|
|
30
|
-
.catch((err) => {
|
|
31
|
-
console.error(err);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
// parsing receipt from stream
|
|
35
|
-
const stream = fs.createReadStream("./documents/receipts/receipt.jpg");
|
|
36
|
-
mindeeClient.receipt
|
|
37
|
-
.parse({ input: stream, inputType: "stream" })
|
|
38
|
-
.then((res) => {
|
|
39
|
-
console.log("Success !");
|
|
40
|
-
console.log(res.receipts);
|
|
41
|
-
console.log(res.receipt);
|
|
42
|
-
})
|
|
43
|
-
.catch((err) => {
|
|
44
|
-
console.error(err);
|
|
45
|
-
});
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
|
|
5
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
6
|
-
|
|
7
|
-
var APIObject = require("./object");
|
|
8
|
-
|
|
9
|
-
var Input = require("../inputs");
|
|
10
|
-
|
|
11
|
-
class APIFinancialDocument extends APIObject {
|
|
12
|
-
constructor(invoiceToken, receiptToken) {
|
|
13
|
-
super(undefined, "financialDocument");
|
|
14
|
-
this.invoiceToken = invoiceToken;
|
|
15
|
-
this.receiptToken = receiptToken;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* @param {String} file: Receipt filepath (allowed jpg, png, tiff, pdf)
|
|
19
|
-
* @param {String} inputType: String in {'path', 'stream', 'base64'}
|
|
20
|
-
* @param {Boolean} includeWords: extract all words into http_response
|
|
21
|
-
* @param {String} version: expense_receipt api version
|
|
22
|
-
* @param {Boolean} cutPdf: Automatically reconstruct pdf with more than 4 pages
|
|
23
|
-
* @returns {Response} Wrapped response with Receipts objects parsed
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
parse(_ref) {
|
|
28
|
-
var _superprop_getParse = () => super.parse,
|
|
29
|
-
_superprop_get_request = () => super._request,
|
|
30
|
-
_this = this;
|
|
31
|
-
|
|
32
|
-
return _asyncToGenerator(function* () {
|
|
33
|
-
var {
|
|
34
|
-
input,
|
|
35
|
-
inputType = "path",
|
|
36
|
-
filename = undefined,
|
|
37
|
-
version = "3",
|
|
38
|
-
cutPdf = true,
|
|
39
|
-
includeWords = false
|
|
40
|
-
} = _ref;
|
|
41
|
-
var inputFile = new Input({
|
|
42
|
-
file: input,
|
|
43
|
-
inputType: inputType,
|
|
44
|
-
filename: filename,
|
|
45
|
-
allowCutPdf: cutPdf
|
|
46
|
-
});
|
|
47
|
-
yield inputFile.init();
|
|
48
|
-
_this.apiToken = inputFile.fileExtension === "application/pdf" ? _this.invoiceToken : _this.receiptToken;
|
|
49
|
-
var url = inputFile.fileExtension === "application/pdf" ? "/invoices/v2/predict" : "/expense_receipts/v3/predict";
|
|
50
|
-
|
|
51
|
-
_superprop_getParse().call(_this);
|
|
52
|
-
|
|
53
|
-
return yield _superprop_get_request().call(_this, url, inputFile, version, includeWords);
|
|
54
|
-
})();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
module.exports = APIFinancialDocument;
|
package/lib/api/index.js
DELETED
package/lib/api/invoice.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
|
|
5
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
6
|
-
|
|
7
|
-
var APIObject = require("./object");
|
|
8
|
-
|
|
9
|
-
var Input = require("../inputs");
|
|
10
|
-
|
|
11
|
-
class APIInvoice extends APIObject {
|
|
12
|
-
constructor() {
|
|
13
|
-
var apiToken = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
|
|
14
|
-
super(apiToken, "invoice");
|
|
15
|
-
this.baseUrl = "".concat(this.baseUrl, "/invoices/");
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* @param {Boolean} includeWords: , extract all words into http_response
|
|
19
|
-
* @param {Boolean} cutPdf: Automatically reconstruct pdf with more than 4 pages
|
|
20
|
-
* @param {String} inputType: String in {'path', 'stream', 'base64'}
|
|
21
|
-
* @param {String} file: Receipt filepath (allowed jpg, png, tiff, pdf)
|
|
22
|
-
* @param {String} version: expense_receipt api version
|
|
23
|
-
* @returns {Response} Wrapped response with Receipts objects parsed
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
parse(_ref) {
|
|
28
|
-
var _superprop_getParse = () => super.parse,
|
|
29
|
-
_superprop_get_request = () => super._request,
|
|
30
|
-
_this = this;
|
|
31
|
-
|
|
32
|
-
return _asyncToGenerator(function* () {
|
|
33
|
-
var {
|
|
34
|
-
input,
|
|
35
|
-
inputType = "path",
|
|
36
|
-
filename = undefined,
|
|
37
|
-
version = "2",
|
|
38
|
-
cutPdf = true,
|
|
39
|
-
includeWords = false
|
|
40
|
-
} = _ref;
|
|
41
|
-
|
|
42
|
-
_superprop_getParse().call(_this);
|
|
43
|
-
|
|
44
|
-
var inputFile = new Input({
|
|
45
|
-
file: input,
|
|
46
|
-
inputType: inputType,
|
|
47
|
-
filename: filename,
|
|
48
|
-
allowCutPdf: cutPdf
|
|
49
|
-
});
|
|
50
|
-
yield inputFile.init();
|
|
51
|
-
var url = "v".concat(version, "/predict");
|
|
52
|
-
return yield _superprop_get_request().call(_this, url, inputFile, includeWords);
|
|
53
|
-
})();
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
@param {String} inputFile - Input object
|
|
57
|
-
@param {} response - HTTP response
|
|
58
|
-
@param {Document} documentType - Document class in {"Receipt", "Invoice", "Financial_document"}
|
|
59
|
-
@returns {Response}
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
wrapResponse(inputFile, response, documentType) {
|
|
64
|
-
var _result$httpResponse$, _result$httpResponse$2, _result$httpResponse$3, _result$httpResponse$4, _result$httpResponse$5;
|
|
65
|
-
|
|
66
|
-
var result = super.wrapResponse(inputFile, response, documentType);
|
|
67
|
-
result.documentType = ((_result$httpResponse$ = result.httpResponse.data) === null || _result$httpResponse$ === void 0 ? void 0 : (_result$httpResponse$2 = _result$httpResponse$.predictions) === null || _result$httpResponse$2 === void 0 ? void 0 : (_result$httpResponse$3 = _result$httpResponse$2[0]) === null || _result$httpResponse$3 === void 0 ? void 0 : (_result$httpResponse$4 = _result$httpResponse$3.document_type) === null || _result$httpResponse$4 === void 0 ? void 0 : (_result$httpResponse$5 = _result$httpResponse$4.value) === null || _result$httpResponse$5 === void 0 ? void 0 : _result$httpResponse$5.toLowerCase()) || result.documentType;
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
module.exports = APIInvoice;
|
package/lib/api/object.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
|
|
5
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
6
|
-
|
|
7
|
-
var Response = require("./response");
|
|
8
|
-
|
|
9
|
-
var errorHandler = require("../errors/handler");
|
|
10
|
-
|
|
11
|
-
var request = require("./request");
|
|
12
|
-
/**
|
|
13
|
-
* Base class for APIs (APIReceipt & APIInvoice)
|
|
14
|
-
* @param {String} apiToken - Token of the API used for parsing document
|
|
15
|
-
* @param {String} apiName - Name of the API used for parsing document
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class APIObject {
|
|
20
|
-
constructor() {
|
|
21
|
-
var apiToken = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
|
|
22
|
-
var apiName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
|
23
|
-
this.apiToken = apiToken;
|
|
24
|
-
this.baseUrl = "https://api.mindee.net/v1/products/mindee/";
|
|
25
|
-
this.apiName = apiName;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
parse() {
|
|
32
|
-
if (!this.apiToken) {
|
|
33
|
-
errorHandler.throw({
|
|
34
|
-
error: new Error("Missing API token for ".concat(this.apiName, ". Have you create a mindee Client with a ").concat(this.apiName, "Token in parameters ?"))
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
@param {String} url - API url for request
|
|
40
|
-
@param {Input} inputFile - input file for API
|
|
41
|
-
@param {Boolean} includeWords - Include Mindee vision words in Response
|
|
42
|
-
@returns {Response}
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
_request(url, inputFile) {
|
|
47
|
-
var _arguments = arguments,
|
|
48
|
-
_this = this;
|
|
49
|
-
|
|
50
|
-
return _asyncToGenerator(function* () {
|
|
51
|
-
var includeWords = _arguments.length > 2 && _arguments[2] !== undefined ? _arguments[2] : false;
|
|
52
|
-
var headers = {
|
|
53
|
-
"X-Inferuser-Token": _this.apiToken
|
|
54
|
-
};
|
|
55
|
-
var response = yield request("".concat(_this.baseUrl).concat(url), "POST", headers, inputFile, includeWords);
|
|
56
|
-
return _this.wrapResponse(inputFile, response, _this.apiName);
|
|
57
|
-
})();
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
@param {String} inputFile - Input object
|
|
61
|
-
@param {} response - HTTP response
|
|
62
|
-
@param {Document} documentType - Document class in {"Receipt", "Invoice", "Financial_document"}
|
|
63
|
-
@returns {Response}
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
wrapResponse(inputFile, response, documentType) {
|
|
68
|
-
if (response.statusCode > 201) {
|
|
69
|
-
var errorMessage = JSON.stringify(response.data, null, 4);
|
|
70
|
-
errorHandler.throw(new Error("".concat(this.apiName, " API ").concat(response.statusCode, " HTTP error: ").concat(errorMessage)), false);
|
|
71
|
-
return new Response({
|
|
72
|
-
httpResponse: response,
|
|
73
|
-
documentType: documentType,
|
|
74
|
-
document: undefined,
|
|
75
|
-
input: inputFile,
|
|
76
|
-
error: true
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return new Response({
|
|
81
|
-
httpResponse: response,
|
|
82
|
-
documentType: documentType,
|
|
83
|
-
document: inputFile,
|
|
84
|
-
input: inputFile
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
module.exports = APIObject;
|
package/lib/api/receipt.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
|
|
5
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
6
|
-
|
|
7
|
-
var APIObject = require("./object");
|
|
8
|
-
|
|
9
|
-
var Input = require("../inputs");
|
|
10
|
-
|
|
11
|
-
class APIReceipt extends APIObject {
|
|
12
|
-
constructor() {
|
|
13
|
-
var apiToken = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
|
|
14
|
-
super(apiToken, "receipt");
|
|
15
|
-
this.baseUrl = "".concat(this.baseUrl, "/expense_receipts/");
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* @param {String} file: Receipt filepath (allowed jpg, png, tiff, pdf)
|
|
19
|
-
* @param {String} inputType: String in {'path', 'stream', 'base64'}
|
|
20
|
-
* @param {Boolean} includeWords: extract all words into http_response
|
|
21
|
-
* @param {String} version: expense_receipt api version
|
|
22
|
-
* @param {Boolean} cutPdf: Automatically reconstruct pdf with more than 4 pages
|
|
23
|
-
* @returns {Response} Wrapped response with Receipts objects parsed
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
parse(_ref) {
|
|
28
|
-
var _superprop_getParse = () => super.parse,
|
|
29
|
-
_superprop_get_request = () => super._request,
|
|
30
|
-
_this = this;
|
|
31
|
-
|
|
32
|
-
return _asyncToGenerator(function* () {
|
|
33
|
-
var {
|
|
34
|
-
input,
|
|
35
|
-
inputType = "path",
|
|
36
|
-
filename = undefined,
|
|
37
|
-
version = "3",
|
|
38
|
-
cutPdf = true,
|
|
39
|
-
includeWords = false
|
|
40
|
-
} = _ref;
|
|
41
|
-
|
|
42
|
-
_superprop_getParse().call(_this);
|
|
43
|
-
|
|
44
|
-
var inputFile = new Input({
|
|
45
|
-
file: input,
|
|
46
|
-
inputType: inputType,
|
|
47
|
-
filename: filename,
|
|
48
|
-
allowCutPdf: cutPdf
|
|
49
|
-
});
|
|
50
|
-
yield inputFile.init();
|
|
51
|
-
var url = "v".concat(version, "/predict");
|
|
52
|
-
return yield _superprop_get_request().call(_this, url, inputFile, includeWords);
|
|
53
|
-
})();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
module.exports = APIReceipt;
|
package/lib/api/request.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
4
|
-
|
|
5
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
6
|
-
|
|
7
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
-
|
|
9
|
-
var https = require("https");
|
|
10
|
-
|
|
11
|
-
var {
|
|
12
|
-
version: sdkVersion
|
|
13
|
-
} = require("../../package.json");
|
|
14
|
-
|
|
15
|
-
var {
|
|
16
|
-
URL
|
|
17
|
-
} = require("url");
|
|
18
|
-
|
|
19
|
-
var FormData = require("form-data");
|
|
20
|
-
|
|
21
|
-
var os = require("os");
|
|
22
|
-
|
|
23
|
-
var request = function request(url, method, headers, input) {
|
|
24
|
-
var includeWords = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
25
|
-
return new Promise(function (resolve, reject) {
|
|
26
|
-
var form = new FormData();
|
|
27
|
-
var body;
|
|
28
|
-
headers["User-Agent"] = "mindee-api-nodejs@v".concat(sdkVersion, " nodejs-").concat(process.version, " ").concat(os.type().toLowerCase());
|
|
29
|
-
|
|
30
|
-
if (["path", "stream"].includes(input.inputType)) {
|
|
31
|
-
var fileParams = {
|
|
32
|
-
filename: input.filename
|
|
33
|
-
};
|
|
34
|
-
form.append("document", input.fileObject, fileParams);
|
|
35
|
-
if (includeWords) form.append("include_mvision", "true");
|
|
36
|
-
headers = _objectSpread(_objectSpread({}, headers), form.getHeaders());
|
|
37
|
-
} else if (input.inputType === "base64") {
|
|
38
|
-
var body_obj = {
|
|
39
|
-
document: input.fileObject
|
|
40
|
-
};
|
|
41
|
-
if (includeWords) body_obj["include_mvision"] = "true";
|
|
42
|
-
body = JSON.stringify(body_obj);
|
|
43
|
-
headers["Content-Type"] = "application/json";
|
|
44
|
-
headers["Content-Length"] = body.length;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
var uri = new URL(url);
|
|
48
|
-
var options = {
|
|
49
|
-
method: method,
|
|
50
|
-
headers: headers,
|
|
51
|
-
hostname: uri.hostname,
|
|
52
|
-
path: "".concat(uri.pathname).concat(uri.search)
|
|
53
|
-
};
|
|
54
|
-
var req = https.request(options, function (res) {
|
|
55
|
-
var responseBody = [];
|
|
56
|
-
res.on("data", function (chunk) {
|
|
57
|
-
responseBody += chunk;
|
|
58
|
-
});
|
|
59
|
-
res.on("end", function () {
|
|
60
|
-
try {
|
|
61
|
-
resolve(_objectSpread(_objectSpread({}, res), {}, {
|
|
62
|
-
data: JSON.parse(responseBody)
|
|
63
|
-
}));
|
|
64
|
-
} catch (error) {
|
|
65
|
-
console.log(responseBody, error);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
req.on("error", err => {
|
|
70
|
-
reject(err);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
if (["path", "stream"].includes(input.inputType)) {
|
|
74
|
-
form.pipe(req);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (input.inputType === "base64") {
|
|
78
|
-
req.write(body);
|
|
79
|
-
req.end();
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
module.exports = request;
|
package/lib/api/response.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
4
|
-
|
|
5
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
6
|
-
|
|
7
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
-
|
|
9
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
-
|
|
11
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
12
|
-
|
|
13
|
-
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
14
|
-
|
|
15
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
16
|
-
|
|
17
|
-
var Receipt = require("../documents").receipt;
|
|
18
|
-
|
|
19
|
-
var Invoice = require("../documents").invoice;
|
|
20
|
-
|
|
21
|
-
var FinancialDocument = require("../documents").financialDocument;
|
|
22
|
-
|
|
23
|
-
var fs = require("fs").promises;
|
|
24
|
-
|
|
25
|
-
class Response {
|
|
26
|
-
constructor(_ref) {
|
|
27
|
-
var {
|
|
28
|
-
httpResponse,
|
|
29
|
-
documentType,
|
|
30
|
-
input,
|
|
31
|
-
error,
|
|
32
|
-
reconsctruted = false
|
|
33
|
-
} = _ref,
|
|
34
|
-
args = _objectWithoutProperties(_ref, ["httpResponse", "documentType", "input", "error", "reconsctruted"]);
|
|
35
|
-
|
|
36
|
-
this.httpResponse = httpResponse;
|
|
37
|
-
this.documentType = documentType;
|
|
38
|
-
this.input = input;
|
|
39
|
-
if (!error && !reconsctruted) this.formatResponse();
|
|
40
|
-
|
|
41
|
-
if (reconsctruted === true) {
|
|
42
|
-
Object.assign(this, args);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
dump(path) {
|
|
47
|
-
var _this = this;
|
|
48
|
-
|
|
49
|
-
return _asyncToGenerator(function* () {
|
|
50
|
-
return yield fs.writeFile(path, JSON.stringify(Object.entries(_this)));
|
|
51
|
-
})();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
static load(path) {
|
|
55
|
-
return _asyncToGenerator(function* () {
|
|
56
|
-
var file = fs.readFile(path);
|
|
57
|
-
var args = JSON.parse(file);
|
|
58
|
-
return new Response(_objectSpread({
|
|
59
|
-
reconsctruted: true
|
|
60
|
-
}, args));
|
|
61
|
-
})();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
formatResponse() {
|
|
65
|
-
var constructors = {
|
|
66
|
-
receipt: params => new Receipt(params),
|
|
67
|
-
invoice: params => new Invoice(params),
|
|
68
|
-
financialDocument: params => new FinancialDocument(params)
|
|
69
|
-
};
|
|
70
|
-
var predictions = this.httpResponse.data.document.inference.pages.entries();
|
|
71
|
-
this["".concat(this.documentType, "s")] = []; // Create a list of Document (Receipt, Invoice...) for each page of the input document
|
|
72
|
-
|
|
73
|
-
for (var [pageNumber, prediction] of predictions) {
|
|
74
|
-
this["".concat(this.documentType, "s")].push(constructors[this.documentType]({
|
|
75
|
-
apiPrediction: prediction.prediction,
|
|
76
|
-
inputFile: this.input,
|
|
77
|
-
pageNumber: pageNumber
|
|
78
|
-
}));
|
|
79
|
-
} // Merge the list of Document into a unique Document
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
this[this.documentType] = constructors[this.documentType]({
|
|
83
|
-
apiPrediction: this.httpResponse.data.document.inference.prediction,
|
|
84
|
-
inputFile: this.input,
|
|
85
|
-
pageNumber: -1,
|
|
86
|
-
level: "document"
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
module.exports = Response;
|