mindee 1.3.0 → 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.
- package/CHANGELOG.md +37 -32
- package/dist/api/financialDocument.js +51 -0
- package/dist/api/index.js +13 -0
- package/dist/api/invoice.js +59 -0
- package/dist/api/object.js +80 -0
- package/dist/api/receipt.js +45 -0
- package/dist/api/request.js +90 -0
- package/dist/api/response.js +91 -0
- package/dist/documents/document.js +80 -0
- package/dist/documents/fields/amount.js +21 -0
- package/dist/documents/fields/date.js +17 -0
- package/dist/documents/fields/field.js +85 -0
- package/dist/documents/fields/index.js +17 -0
- package/dist/documents/fields/locale.js +25 -0
- package/dist/documents/fields/orientation.js +22 -0
- package/dist/documents/fields/paymentDetails.js +50 -0
- package/dist/documents/fields/tax.js +42 -0
- package/dist/documents/financialDocument.js +216 -0
- package/dist/documents/index.js +13 -0
- package/dist/documents/invoice.js +361 -0
- package/dist/documents/receipt.js +231 -0
- package/dist/errors/handler.js +17 -0
- package/dist/index.js +23 -0
- package/dist/inputs.js +208 -0
- package/dist/logger.js +34 -0
- package/lib/index.js +29 -30
- package/mindee/api/request.js +1 -1
- package/package.json +1 -1
package/dist/inputs.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.Input = void 0;
|
|
39
|
+
// const fs = require("fs").promises;
|
|
40
|
+
// const errorHandler = require("./errors/handler");
|
|
41
|
+
// const path = require("path");
|
|
42
|
+
// const { PDFDocument } = require("pdf-lib");
|
|
43
|
+
// const concat = require("concat-stream");
|
|
44
|
+
// const { Base64Encode } = require("base64-stream");
|
|
45
|
+
// const fileType = require("file-type");
|
|
46
|
+
// const ArrayBufferEncode = require("base64-arraybuffer");
|
|
47
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
48
|
+
const path = __importStar(require("path"));
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
const concat = __importStar(require("concat-stream"));
|
|
52
|
+
const base64_stream_1 = require("base64-stream");
|
|
53
|
+
const fileType = __importStar(require("file-type"));
|
|
54
|
+
const ArrayBufferEncode = __importStar(require("base64-arraybuffer"));
|
|
55
|
+
const handler_1 = require("@errors/handler");
|
|
56
|
+
const pdf_lib_1 = require("pdf-lib");
|
|
57
|
+
class Input {
|
|
58
|
+
/**
|
|
59
|
+
* @param {(String | Buffer)} file - the file that will be read. Either path or base64 string, or a steam
|
|
60
|
+
* @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
|
|
61
|
+
* NB: dummy is only used for tests purposes
|
|
62
|
+
* @param {String} filename - File name of the input
|
|
63
|
+
* @param allowCutPdf
|
|
64
|
+
* NB: Because of async calls, init() should be called after creating the object
|
|
65
|
+
*/
|
|
66
|
+
constructor(file, inputType, allowCutPdf = true, filename) {
|
|
67
|
+
this.file = file;
|
|
68
|
+
this.inputType = inputType;
|
|
69
|
+
this.allowCutPdf = allowCutPdf;
|
|
70
|
+
this.filename = filename;
|
|
71
|
+
this.MIMETYPES = {
|
|
72
|
+
png: "image/png",
|
|
73
|
+
jpg: "image/jpg",
|
|
74
|
+
jpeg: "image/jpeg",
|
|
75
|
+
webp: "image/webp",
|
|
76
|
+
pdf: "application/pdf",
|
|
77
|
+
};
|
|
78
|
+
this.ALLOWED_INPUT_TYPE = ["base64", "path", "stream", "dummy"];
|
|
79
|
+
this.CUT_PDF_SIZE = 5;
|
|
80
|
+
// Check if inputType is valid
|
|
81
|
+
if (!this.ALLOWED_INPUT_TYPE.includes(inputType)) {
|
|
82
|
+
handler_1.errorHandler.throw(new Error(`The input type is invalid. It should be \
|
|
83
|
+
${this.ALLOWED_INPUT_TYPE.toString()}`));
|
|
84
|
+
}
|
|
85
|
+
this.file = file;
|
|
86
|
+
this.filename = filename;
|
|
87
|
+
this.inputType = inputType;
|
|
88
|
+
this.allowCutPdf = allowCutPdf;
|
|
89
|
+
}
|
|
90
|
+
init() {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
if (this.inputType === "base64")
|
|
93
|
+
yield this.initBase64();
|
|
94
|
+
else if (this.inputType === "path")
|
|
95
|
+
yield this.initFile();
|
|
96
|
+
else if (this.inputType === "stream")
|
|
97
|
+
yield this.initStream();
|
|
98
|
+
else
|
|
99
|
+
this.initDummy();
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
initBase64() {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
this.fileObject = this.file;
|
|
105
|
+
this.filepath = undefined;
|
|
106
|
+
if (this.filename === undefined) {
|
|
107
|
+
const mimeType = yield fileType.fromBuffer(Buffer.from(this.fileObject, "base64"));
|
|
108
|
+
if (mimeType !== undefined) {
|
|
109
|
+
this.fileExtension = mimeType.mime;
|
|
110
|
+
this.filename = `from_${this.inputType}.${mimeType.ext}`;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
throw "Could not determine the MIME type. Please specify the 'filename' option.";
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const filetype = this.filename.split(".").pop();
|
|
118
|
+
this.fileExtension = this.MIMETYPES[filetype];
|
|
119
|
+
}
|
|
120
|
+
if (this.fileExtension === "application/pdf" && this.allowCutPdf == true) {
|
|
121
|
+
yield this.cutPdf();
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
initFile() {
|
|
126
|
+
var _a;
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
this.fileObject = yield promises_1.default.readFile(this.file);
|
|
129
|
+
this.filepath = this.file;
|
|
130
|
+
if (typeof this.file === "string") {
|
|
131
|
+
this.filename = this.filename || path.basename(this.file);
|
|
132
|
+
}
|
|
133
|
+
// Check if file type is valid
|
|
134
|
+
const filetype = (_a = this.filename) === null || _a === void 0 ? void 0 : _a.split(".").pop();
|
|
135
|
+
if (!filetype || !(filetype in this.MIMETYPES)) {
|
|
136
|
+
handler_1.errorHandler.throw(new Error(`File type is not allowed. It must be ${Object.keys(this.MIMETYPES).toString()}`));
|
|
137
|
+
}
|
|
138
|
+
this.fileExtension = filetype ? this.MIMETYPES[filetype] : "";
|
|
139
|
+
if (this.fileExtension === "application/pdf" && this.allowCutPdf) {
|
|
140
|
+
yield this.cutPdf();
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
initStream() {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
this.file = (yield this.streamToBase64(this.file));
|
|
147
|
+
this.inputType = "base64";
|
|
148
|
+
yield this.initBase64();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
initDummy() {
|
|
152
|
+
this.fileObject = "";
|
|
153
|
+
this.filename = "";
|
|
154
|
+
this.filepath = "";
|
|
155
|
+
this.fileExtension = "";
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Convert ReadableStream to Base64 encoded String
|
|
159
|
+
*
|
|
160
|
+
* @param {*} stream ReadableStream to encode
|
|
161
|
+
* @returns Base64 encoded String
|
|
162
|
+
*/
|
|
163
|
+
streamToBase64(stream) {
|
|
164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
const base64 = new base64_stream_1.Base64Encode();
|
|
167
|
+
const cbConcat = (base64) => {
|
|
168
|
+
resolve(base64);
|
|
169
|
+
};
|
|
170
|
+
stream
|
|
171
|
+
.pipe(base64)
|
|
172
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
173
|
+
// @ts-ignore TO DO : FIX
|
|
174
|
+
.pipe(concat(cbConcat))
|
|
175
|
+
.on("error", (error) => {
|
|
176
|
+
reject(error);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/** Cut PDF if pages > 5 */
|
|
182
|
+
cutPdf() {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
// convert document to PDFDocument & cut CUT_PDF_SIZE - 1 first pages and last page
|
|
185
|
+
const pdfDocument = yield pdf_lib_1.PDFDocument.load(this.fileObject, {
|
|
186
|
+
ignoreEncryption: true,
|
|
187
|
+
});
|
|
188
|
+
const splitedPdfDocument = yield pdf_lib_1.PDFDocument.create();
|
|
189
|
+
const pdfLength = pdfDocument.getPageCount();
|
|
190
|
+
if (pdfLength <= this.CUT_PDF_SIZE)
|
|
191
|
+
return;
|
|
192
|
+
const pagesNumbers = [
|
|
193
|
+
...Array(this.CUT_PDF_SIZE - 1).keys(),
|
|
194
|
+
pdfLength - 1,
|
|
195
|
+
];
|
|
196
|
+
const pages = yield splitedPdfDocument.copyPages(pdfDocument, pagesNumbers);
|
|
197
|
+
pages.forEach((page) => splitedPdfDocument.addPage(page));
|
|
198
|
+
const data = yield splitedPdfDocument.save();
|
|
199
|
+
if (this.inputType === "path") {
|
|
200
|
+
this.fileObject = Buffer.from(data);
|
|
201
|
+
}
|
|
202
|
+
else if (this.inputType === "base64") {
|
|
203
|
+
this.fileObject = ArrayBufferEncode.encode(data);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.Input = Input;
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
const LOGGER_LEVELS = {
|
|
5
|
+
debug: 0,
|
|
6
|
+
info: 1,
|
|
7
|
+
warn: 2,
|
|
8
|
+
error: 3,
|
|
9
|
+
};
|
|
10
|
+
class Logger {
|
|
11
|
+
constructor(level = "debug") {
|
|
12
|
+
this.level = level;
|
|
13
|
+
if (!(level in LOGGER_LEVELS))
|
|
14
|
+
level = "debug";
|
|
15
|
+
this.level = LOGGER_LEVELS[level];
|
|
16
|
+
}
|
|
17
|
+
debug(...args) {
|
|
18
|
+
if (this.level <= LOGGER_LEVELS["debug"])
|
|
19
|
+
console.debug(args);
|
|
20
|
+
}
|
|
21
|
+
info(...args) {
|
|
22
|
+
if (this.level <= LOGGER_LEVELS["info"])
|
|
23
|
+
console.info(args);
|
|
24
|
+
}
|
|
25
|
+
warn(...args) {
|
|
26
|
+
if (this.level <= LOGGER_LEVELS["warn"])
|
|
27
|
+
console.warn(args);
|
|
28
|
+
}
|
|
29
|
+
error(...args) {
|
|
30
|
+
if (this.level <= LOGGER_LEVELS["error"])
|
|
31
|
+
console.error(args);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.logger = new Logger();
|
package/lib/index.js
CHANGED
|
@@ -1,39 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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/mindee/api/request.js
CHANGED