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.
Files changed (85) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/mindee/errors/handler.js +1 -1
  3. package/mindee/index.js +5 -2
  4. package/mindee/inputs.js +45 -22
  5. package/mindee/logger.js +8 -7
  6. package/package.json +13 -4
  7. package/.eslintrc +0 -24
  8. package/.mocharc.yaml +0 -2
  9. package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  10. package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  11. package/.nyc_output/processinfo/index.json +0 -1
  12. package/.prettierrc +0 -4
  13. package/.tool-versions +0 -1
  14. package/CODE_OF_CONDUCT.md +0 -63
  15. package/CONTRIBUTING.md +0 -84
  16. package/ISSUE_TEMPLATE.md +0 -45
  17. package/PULL_REQUEST_TEMPLATE.md +0 -45
  18. package/babel.config.json +0 -10
  19. package/coverage/coverage.json +0 -1
  20. package/coverage/lcov-report/base.css +0 -224
  21. package/coverage/lcov-report/block-navigation.js +0 -79
  22. package/coverage/lcov-report/favicon.png +0 -0
  23. package/coverage/lcov-report/index.html +0 -171
  24. package/coverage/lcov-report/prettify.css +0 -1
  25. package/coverage/lcov-report/prettify.js +0 -2
  26. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  27. package/coverage/lcov-report/sorter.js +0 -170
  28. package/coverage/lcov.info +0 -1595
  29. package/coverage.lcov +0 -1595
  30. package/dist/api/financialDocument.js +0 -51
  31. package/dist/api/index.js +0 -13
  32. package/dist/api/invoice.js +0 -59
  33. package/dist/api/object.js +0 -80
  34. package/dist/api/receipt.js +0 -45
  35. package/dist/api/request.js +0 -90
  36. package/dist/api/response.js +0 -91
  37. package/dist/documents/document.js +0 -80
  38. package/dist/documents/fields/amount.js +0 -21
  39. package/dist/documents/fields/date.js +0 -17
  40. package/dist/documents/fields/field.js +0 -85
  41. package/dist/documents/fields/index.js +0 -17
  42. package/dist/documents/fields/locale.js +0 -25
  43. package/dist/documents/fields/orientation.js +0 -22
  44. package/dist/documents/fields/paymentDetails.js +0 -50
  45. package/dist/documents/fields/tax.js +0 -42
  46. package/dist/documents/financialDocument.js +0 -216
  47. package/dist/documents/index.js +0 -13
  48. package/dist/documents/invoice.js +0 -361
  49. package/dist/documents/receipt.js +0 -231
  50. package/dist/errors/handler.js +0 -17
  51. package/dist/index.js +0 -23
  52. package/dist/inputs.js +0 -208
  53. package/dist/logger.js +0 -34
  54. package/examples/.env.example +0 -2
  55. package/examples/documents/invoices/credit_note.pdf +0 -900
  56. package/examples/documents/invoices/invoice.pdf +0 -0
  57. package/examples/documents/invoices/invoice_6p.pdf +0 -0
  58. package/examples/documents/receipts/receipt.jpg +0 -0
  59. package/examples/financialDocument.js +0 -32
  60. package/examples/invoice.js +0 -74
  61. package/examples/receipt.js +0 -45
  62. package/lib/api/financialDocument.js +0 -59
  63. package/lib/api/index.js +0 -7
  64. package/lib/api/invoice.js +0 -73
  65. package/lib/api/object.js +0 -90
  66. package/lib/api/receipt.js +0 -58
  67. package/lib/api/request.js +0 -84
  68. package/lib/api/response.js +0 -92
  69. package/lib/documents/document.js +0 -97
  70. package/lib/documents/fields/amount.js +0 -35
  71. package/lib/documents/fields/date.js +0 -36
  72. package/lib/documents/fields/field.js +0 -102
  73. package/lib/documents/fields/index.js +0 -9
  74. package/lib/documents/fields/locale.js +0 -35
  75. package/lib/documents/fields/orientation.js +0 -33
  76. package/lib/documents/fields/paymentDetails.js +0 -71
  77. package/lib/documents/fields/tax.js +0 -57
  78. package/lib/documents/financialDocument.js +0 -280
  79. package/lib/documents/index.js +0 -7
  80. package/lib/documents/invoice.js +0 -451
  81. package/lib/documents/receipt.js +0 -332
  82. package/lib/errors/handler.js +0 -19
  83. package/lib/index.js +0 -40
  84. package/lib/inputs.js +0 -190
  85. package/lib/logger.js +0 -52
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v1.4.0
4
+ ### Fixes
5
+ * :bug: cut/merge pages of an encrypted PDF leads to unexpected results
6
+ * :bug: logger not activating in some cases
7
+ * :bug: align PDF cut/merge with other client libs
8
+
9
+ ### Changes
10
+ * :sparkles: Add TIFF and HEIC support
11
+ * :white_check_mark: fully test PDF cut function
12
+
3
13
  ## v1.3.3
4
14
 
5
15
  ### Fixes
@@ -1,4 +1,4 @@
1
- const logger = require("../logger");
1
+ const { logger } = require("../logger");
2
2
 
3
3
  class ErrorHandler {
4
4
  constructor(throwOnError = true) {
package/mindee/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const errorHandler = require("./errors/handler");
2
- const logger = require("./logger");
2
+ const { logger, LOG_LEVELS } = require("./logger");
3
3
  const APIReceipt = require("./api/receipt");
4
4
  const APIInvoice = require("./api/invoice");
5
5
  const APIFinancialDocument = require("./api/financialDocument");
@@ -20,7 +20,10 @@ class Client {
20
20
  this.receiptToken = receiptToken || process.env.MINDEE_RECEIPT_TOKEN;
21
21
  this.invoiceToken = invoiceToken || process.env.MINDEE_INVOICE_TOKEN;
22
22
  errorHandler.throwOnError = throwOnError;
23
- logger.level = debug ?? process.env.MINDEE_DEBUG ? "debug" : "warn";
23
+ logger.level =
24
+ debug ?? process.env.MINDEE_DEBUG
25
+ ? LOG_LEVELS["debug"]
26
+ : LOG_LEVELS["warn"];
24
27
  this.receipt = new APIReceipt(this.receiptToken);
25
28
  this.invoice = new APIInvoice(this.invoiceToken);
26
29
  this.financialDocument = new APIFinancialDocument(
package/mindee/inputs.js CHANGED
@@ -6,17 +6,21 @@ const concat = require("concat-stream");
6
6
  const { Base64Encode } = require("base64-stream");
7
7
  const fileType = require("file-type");
8
8
  const ArrayBufferEncode = require("base64-arraybuffer");
9
+ const { logger } = require("./logger");
9
10
 
10
11
  class Input {
11
12
  MIMETYPES = {
12
- png: "image/png",
13
- jpg: "image/jpg",
13
+ pdf: "application/pdf",
14
+ heic: "image/heic",
15
+ jpg: "image/jpeg",
14
16
  jpeg: "image/jpeg",
17
+ png: "image/png",
18
+ tif: "image/tiff",
19
+ tiff: "image/tiff",
15
20
  webp: "image/webp",
16
- pdf: "application/pdf",
17
21
  };
18
22
  ALLOWED_INPUT_TYPE = ["base64", "path", "stream", "dummy"];
19
- CUT_PDF_SIZE = 5;
23
+ MAX_DOC_PAGES = 3;
20
24
 
21
25
  /**
22
26
  * @param {(String | Buffer)} file - the file that will be read. Either path or base64 string, or a steam
@@ -47,6 +51,10 @@ class Input {
47
51
  else if (this.inputType === "path") await this.initFile();
48
52
  else if (this.inputType === "stream") await this.initStream();
49
53
  else this.initDummy();
54
+
55
+ if (this.fileExtension === "application/pdf" && this.allowCutPdf === true) {
56
+ await this.cutPdf();
57
+ }
50
58
  }
51
59
 
52
60
  async initBase64() {
@@ -67,10 +75,6 @@ class Input {
67
75
  const filetype = this.filename.split(".").pop();
68
76
  this.fileExtension = this.MIMETYPES[filetype];
69
77
  }
70
-
71
- if (this.fileExtension === "application/pdf" && this.allowCutPdf == true) {
72
- await this.cutPdf();
73
- }
74
78
  }
75
79
 
76
80
  async initFile() {
@@ -90,10 +94,6 @@ class Input {
90
94
  );
91
95
  }
92
96
  this.fileExtension = this.MIMETYPES[filetype];
93
-
94
- if (this.fileExtension === "application/pdf" && this.allowCutPdf == true) {
95
- await this.cutPdf();
96
- }
97
97
  }
98
98
 
99
99
  async initStream() {
@@ -134,19 +134,42 @@ class Input {
134
134
  });
135
135
  }
136
136
 
137
- /** Cut PDF if pages > 5 */
138
- async cutPdf() {
139
- // convert document to PDFDocument & cut CUT_PDF_SIZE - 1 first pages and last page
137
+ async countPages() {
140
138
  let pdfDocument = await PDFDocument.load(this.fileObject, {
141
139
  ignoreEncryption: true,
142
140
  });
143
- const splitedPdfDocument = await PDFDocument.create();
144
- const pdfLength = pdfDocument.getPageCount();
145
- if (pdfLength <= this.CUT_PDF_SIZE) return;
146
- const pagesNumbers = [...Array(pdfLength - 1).keys(), pdfLength - 1];
147
- const pages = await splitedPdfDocument.copyPages(pdfDocument, pagesNumbers);
148
- pages.forEach((page) => splitedPdfDocument.addPage(page));
149
- const data = await splitedPdfDocument.save();
141
+ return pdfDocument.getPageCount();
142
+ }
143
+
144
+ /** Merge PDF pages */
145
+ async cutPdf() {
146
+ let currentPdf = await PDFDocument.load(this.fileObject, {
147
+ ignoreEncryption: true,
148
+ });
149
+
150
+ const pdfLength = currentPdf.getPageCount();
151
+ if (pdfLength <= this.MAX_DOC_PAGES) {
152
+ return;
153
+ }
154
+
155
+ if (currentPdf.isEncrypted) {
156
+ logger.warn(
157
+ "PDF of %d pages is encrypted, not possible to cut pages.",
158
+ pdfLength
159
+ );
160
+ return;
161
+ }
162
+
163
+ const newPdf = await PDFDocument.create();
164
+ const pagesNumbers = [0, pdfLength - 2, pdfLength - 1].slice(
165
+ 0,
166
+ this.MAX_DOC_PAGES
167
+ );
168
+
169
+ const pages = await newPdf.copyPages(currentPdf, pagesNumbers);
170
+ pages.forEach((page) => newPdf.addPage(page));
171
+ const data = await newPdf.save();
172
+
150
173
  if (this.inputType === "path") {
151
174
  this.fileObject = Buffer.from(data);
152
175
  } else if (this.inputType === "base64") {
package/mindee/logger.js CHANGED
@@ -12,22 +12,23 @@ class Logger {
12
12
  }
13
13
 
14
14
  debug(...args) {
15
- if (this.level <= LOGGER_LEVELS["debug"]) console.debug(args);
15
+ if (this.level <= LOGGER_LEVELS["debug"]) console.debug(...args);
16
16
  }
17
17
 
18
18
  info(...args) {
19
- if (this.level <= LOGGER_LEVELS["info"]) console.info(args);
19
+ if (this.level <= LOGGER_LEVELS["info"]) console.info(...args);
20
20
  }
21
21
 
22
22
  warn(...args) {
23
- if (this.level <= LOGGER_LEVELS["warn"]) console.warn(args);
23
+ if (this.level <= LOGGER_LEVELS["warn"]) console.warn(...args);
24
24
  }
25
25
 
26
26
  error(...args) {
27
- if (this.level <= LOGGER_LEVELS["error"]) console.error(args);
27
+ if (this.level <= LOGGER_LEVELS["error"]) console.error(...args);
28
28
  }
29
29
  }
30
30
 
31
- const logger = new Logger();
32
-
33
- module.exports = logger;
31
+ module.exports = {
32
+ logger: new Logger(),
33
+ LOG_LEVELS: LOGGER_LEVELS,
34
+ };
package/package.json CHANGED
@@ -1,15 +1,24 @@
1
1
  {
2
2
  "name": "mindee",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "description": "Mindee Client Library for Node.js",
5
5
  "main": "mindee/index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
8
8
  "build": "babel mindee -d lib",
9
- "test": "npm run build && mocha tests/**/*.js",
9
+ "test": "npm run build && mocha tests/*.js tests/**/*.js",
10
10
  "lint": "eslint 'mindee/**/*.js' 'tests/*.js' --max-warnings=0 --report-unused-disable-directives && echo '✔ Your .js files look good.'",
11
11
  "documentation": "jsdoc -r mindee -d docs"
12
12
  },
13
+ "files": [
14
+ "mindee/",
15
+ "LICENSE",
16
+ "README.md",
17
+ "CHANGELOG.md"
18
+ ],
19
+ "engines": {
20
+ "node": ">= 12.0.0"
21
+ },
13
22
  "repository": {
14
23
  "type": "git",
15
24
  "url": "git+https://github.com/mindee/mindee-api-nodejs.git"
@@ -20,8 +29,8 @@
20
29
  },
21
30
  "homepage": "https://github.com/mindee/mindee-api-nodejs/#readme",
22
31
  "devDependencies": {
23
- "@babel/cli": "^7.12.8",
24
- "@babel/core": "^7.12.9",
32
+ "@babel/cli": "^7.17.10",
33
+ "@babel/core": "^7.17.12",
25
34
  "@babel/eslint-parser": "^7.12.1",
26
35
  "@babel/preset-env": "^7.12.7",
27
36
  "@babel/register": "^7.12.1",
package/.eslintrc DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "env": {
3
- "node": true,
4
- "es6": true,
5
- "mocha": true
6
- },
7
- "parser": "@babel/eslint-parser",
8
- "extends": ["eslint:recommended", "plugin:prettier/recommended"],
9
- "globals": {
10
- "Promise": true
11
- },
12
- "rules": {
13
- "quotes": [
14
- "error",
15
- "double",
16
- {
17
- "avoidEscape": true,
18
- "allowTemplateLiterals": false
19
- }
20
- ],
21
- "comma-dangle": [2, "always-multiline"],
22
- "no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
23
- }
24
- }
package/.mocharc.yaml DELETED
@@ -1,2 +0,0 @@
1
- require:
2
- - '@babel/register'