mindee 1.3.1 → 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.
Files changed (86) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/mindee/api/request.js +1 -1
  3. package/mindee/errors/handler.js +1 -1
  4. package/mindee/index.js +5 -2
  5. package/mindee/inputs.js +45 -25
  6. package/mindee/logger.js +8 -7
  7. package/package.json +13 -4
  8. package/.eslintrc +0 -24
  9. package/.mocharc.yaml +0 -2
  10. package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  11. package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  12. package/.nyc_output/processinfo/index.json +0 -1
  13. package/.prettierrc +0 -4
  14. package/.tool-versions +0 -1
  15. package/CODE_OF_CONDUCT.md +0 -63
  16. package/CONTRIBUTING.md +0 -84
  17. package/ISSUE_TEMPLATE.md +0 -45
  18. package/PULL_REQUEST_TEMPLATE.md +0 -45
  19. package/babel.config.json +0 -10
  20. package/coverage/coverage.json +0 -1
  21. package/coverage/lcov-report/base.css +0 -224
  22. package/coverage/lcov-report/block-navigation.js +0 -79
  23. package/coverage/lcov-report/favicon.png +0 -0
  24. package/coverage/lcov-report/index.html +0 -171
  25. package/coverage/lcov-report/prettify.css +0 -1
  26. package/coverage/lcov-report/prettify.js +0 -2
  27. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  28. package/coverage/lcov-report/sorter.js +0 -170
  29. package/coverage/lcov.info +0 -1595
  30. package/coverage.lcov +0 -1595
  31. package/dist/api/financialDocument.js +0 -51
  32. package/dist/api/index.js +0 -13
  33. package/dist/api/invoice.js +0 -59
  34. package/dist/api/object.js +0 -80
  35. package/dist/api/receipt.js +0 -45
  36. package/dist/api/request.js +0 -90
  37. package/dist/api/response.js +0 -91
  38. package/dist/documents/document.js +0 -80
  39. package/dist/documents/fields/amount.js +0 -21
  40. package/dist/documents/fields/date.js +0 -17
  41. package/dist/documents/fields/field.js +0 -85
  42. package/dist/documents/fields/index.js +0 -17
  43. package/dist/documents/fields/locale.js +0 -25
  44. package/dist/documents/fields/orientation.js +0 -22
  45. package/dist/documents/fields/paymentDetails.js +0 -50
  46. package/dist/documents/fields/tax.js +0 -42
  47. package/dist/documents/financialDocument.js +0 -216
  48. package/dist/documents/index.js +0 -13
  49. package/dist/documents/invoice.js +0 -361
  50. package/dist/documents/receipt.js +0 -231
  51. package/dist/errors/handler.js +0 -17
  52. package/dist/index.js +0 -23
  53. package/dist/inputs.js +0 -208
  54. package/dist/logger.js +0 -34
  55. package/examples/.env.example +0 -2
  56. package/examples/documents/invoices/credit_note.pdf +0 -900
  57. package/examples/documents/invoices/invoice.pdf +0 -0
  58. package/examples/documents/invoices/invoice_6p.pdf +0 -0
  59. package/examples/documents/receipts/receipt.jpg +0 -0
  60. package/examples/financialDocument.js +0 -32
  61. package/examples/invoice.js +0 -74
  62. package/examples/receipt.js +0 -45
  63. package/lib/api/financialDocument.js +0 -59
  64. package/lib/api/index.js +0 -7
  65. package/lib/api/invoice.js +0 -73
  66. package/lib/api/object.js +0 -90
  67. package/lib/api/receipt.js +0 -58
  68. package/lib/api/request.js +0 -84
  69. package/lib/api/response.js +0 -92
  70. package/lib/documents/document.js +0 -97
  71. package/lib/documents/fields/amount.js +0 -35
  72. package/lib/documents/fields/date.js +0 -36
  73. package/lib/documents/fields/field.js +0 -102
  74. package/lib/documents/fields/index.js +0 -9
  75. package/lib/documents/fields/locale.js +0 -35
  76. package/lib/documents/fields/orientation.js +0 -33
  77. package/lib/documents/fields/paymentDetails.js +0 -71
  78. package/lib/documents/fields/tax.js +0 -57
  79. package/lib/documents/financialDocument.js +0 -280
  80. package/lib/documents/index.js +0 -7
  81. package/lib/documents/invoice.js +0 -451
  82. package/lib/documents/receipt.js +0 -332
  83. package/lib/errors/handler.js +0 -19
  84. package/lib/index.js +0 -40
  85. package/lib/inputs.js +0 -190
  86. package/lib/logger.js +0 -52
package/dist/inputs.js DELETED
@@ -1,208 +0,0 @@
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 DELETED
@@ -1,34 +0,0 @@
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();
@@ -1,2 +0,0 @@
1
- MINDEE_RECEIPT_TOKEN=TOKEN
2
- MINDEE_INVOICE_TOKEN=TOKEN