mindee 1.3.2 → 2.0.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 (161) hide show
  1. package/CHANGELOG.md +47 -18
  2. package/README.md +43 -127
  3. package/bin/mindee +3 -0
  4. package/dist/package.json +70 -0
  5. package/dist/src/api/endpoint.d.ts +29 -0
  6. package/dist/src/api/endpoint.js +148 -0
  7. package/dist/src/api/index.d.ts +2 -0
  8. package/dist/src/api/index.js +18 -0
  9. package/dist/src/api/response.d.ts +52 -0
  10. package/dist/src/api/response.js +107 -0
  11. package/dist/src/cli.d.ts +1 -0
  12. package/dist/src/cli.js +117 -0
  13. package/dist/src/client.d.ts +48 -0
  14. package/dist/src/client.js +134 -0
  15. package/dist/src/constants.d.ts +13 -0
  16. package/dist/src/constants.js +54 -0
  17. package/dist/src/documents/custom.d.ts +14 -0
  18. package/dist/src/documents/custom.js +48 -0
  19. package/dist/src/documents/document.d.ts +38 -0
  20. package/dist/{documents → src/documents}/document.js +15 -35
  21. package/dist/src/documents/documentConfig.d.ts +44 -0
  22. package/dist/src/documents/documentConfig.js +121 -0
  23. package/dist/src/documents/financialDocument.d.ts +32 -0
  24. package/dist/src/documents/financialDocument.js +130 -0
  25. package/dist/src/documents/index.d.ts +12 -0
  26. package/dist/src/documents/index.js +20 -0
  27. package/dist/src/documents/invoice.d.ts +37 -0
  28. package/dist/src/documents/invoice.js +303 -0
  29. package/dist/src/documents/passport.d.ts +36 -0
  30. package/dist/src/documents/passport.js +218 -0
  31. package/dist/src/documents/receipt.d.ts +24 -0
  32. package/dist/src/documents/receipt.js +157 -0
  33. package/dist/src/errors/handler.d.ts +7 -0
  34. package/dist/{errors → src/errors}/handler.js +5 -4
  35. package/dist/src/fields/amount.d.ts +13 -0
  36. package/dist/{documents → src}/fields/amount.js +19 -6
  37. package/dist/src/fields/companyRegistration.d.ts +6 -0
  38. package/dist/src/fields/companyRegistration.js +11 -0
  39. package/dist/src/fields/customDocs.d.ts +42 -0
  40. package/dist/src/fields/customDocs.js +61 -0
  41. package/dist/src/fields/date.d.ts +12 -0
  42. package/dist/src/fields/date.js +25 -0
  43. package/dist/src/fields/field.d.ts +74 -0
  44. package/dist/{documents → src}/fields/field.js +48 -27
  45. package/dist/src/fields/fullText.d.ts +24 -0
  46. package/dist/src/fields/fullText.js +89 -0
  47. package/dist/src/fields/index.d.ts +11 -0
  48. package/dist/src/fields/index.js +27 -0
  49. package/dist/src/fields/locale.d.ts +13 -0
  50. package/dist/{documents → src}/fields/locale.js +20 -5
  51. package/dist/src/fields/orientation.d.ts +10 -0
  52. package/dist/{documents → src}/fields/orientation.js +5 -5
  53. package/dist/src/fields/paymentDetails.d.ts +31 -0
  54. package/dist/{documents → src}/fields/paymentDetails.js +14 -6
  55. package/dist/src/fields/tax.d.ts +30 -0
  56. package/dist/src/fields/tax.js +51 -0
  57. package/dist/src/geometry.d.ts +59 -0
  58. package/dist/src/geometry.js +113 -0
  59. package/dist/src/index.d.ts +2 -0
  60. package/dist/src/index.js +12 -0
  61. package/dist/src/inputs.d.ts +69 -0
  62. package/dist/src/inputs.js +223 -0
  63. package/dist/src/logger.d.ts +20 -0
  64. package/dist/src/logger.js +35 -0
  65. package/package.json +45 -26
  66. package/.eslintrc +0 -24
  67. package/.mocharc.yaml +0 -2
  68. package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  69. package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  70. package/.nyc_output/processinfo/index.json +0 -1
  71. package/.prettierrc +0 -4
  72. package/.tool-versions +0 -1
  73. package/CODE_OF_CONDUCT.md +0 -63
  74. package/CONTRIBUTING.md +0 -84
  75. package/ISSUE_TEMPLATE.md +0 -45
  76. package/PULL_REQUEST_TEMPLATE.md +0 -45
  77. package/babel.config.json +0 -10
  78. package/coverage/coverage.json +0 -1
  79. package/coverage/lcov-report/base.css +0 -224
  80. package/coverage/lcov-report/block-navigation.js +0 -79
  81. package/coverage/lcov-report/favicon.png +0 -0
  82. package/coverage/lcov-report/index.html +0 -171
  83. package/coverage/lcov-report/prettify.css +0 -1
  84. package/coverage/lcov-report/prettify.js +0 -2
  85. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  86. package/coverage/lcov-report/sorter.js +0 -170
  87. package/coverage/lcov.info +0 -1595
  88. package/coverage.lcov +0 -1595
  89. package/dist/api/financialDocument.js +0 -51
  90. package/dist/api/index.js +0 -13
  91. package/dist/api/invoice.js +0 -59
  92. package/dist/api/object.js +0 -80
  93. package/dist/api/receipt.js +0 -45
  94. package/dist/api/request.js +0 -90
  95. package/dist/api/response.js +0 -91
  96. package/dist/documents/fields/date.js +0 -17
  97. package/dist/documents/fields/index.js +0 -17
  98. package/dist/documents/fields/tax.js +0 -42
  99. package/dist/documents/financialDocument.js +0 -216
  100. package/dist/documents/index.js +0 -13
  101. package/dist/documents/invoice.js +0 -361
  102. package/dist/documents/receipt.js +0 -231
  103. package/dist/index.js +0 -23
  104. package/dist/inputs.js +0 -208
  105. package/dist/logger.js +0 -34
  106. package/examples/.env.example +0 -2
  107. package/examples/documents/invoices/credit_note.pdf +0 -900
  108. package/examples/documents/invoices/invoice.pdf +0 -0
  109. package/examples/documents/invoices/invoice_6p.pdf +0 -0
  110. package/examples/documents/receipts/receipt.jpg +0 -0
  111. package/examples/financialDocument.js +0 -32
  112. package/examples/invoice.js +0 -74
  113. package/examples/receipt.js +0 -45
  114. package/lib/api/financialDocument.js +0 -59
  115. package/lib/api/index.js +0 -7
  116. package/lib/api/invoice.js +0 -73
  117. package/lib/api/object.js +0 -90
  118. package/lib/api/receipt.js +0 -58
  119. package/lib/api/request.js +0 -84
  120. package/lib/api/response.js +0 -92
  121. package/lib/documents/document.js +0 -97
  122. package/lib/documents/fields/amount.js +0 -35
  123. package/lib/documents/fields/date.js +0 -36
  124. package/lib/documents/fields/field.js +0 -102
  125. package/lib/documents/fields/index.js +0 -9
  126. package/lib/documents/fields/locale.js +0 -35
  127. package/lib/documents/fields/orientation.js +0 -33
  128. package/lib/documents/fields/paymentDetails.js +0 -71
  129. package/lib/documents/fields/tax.js +0 -57
  130. package/lib/documents/financialDocument.js +0 -280
  131. package/lib/documents/index.js +0 -7
  132. package/lib/documents/invoice.js +0 -451
  133. package/lib/documents/receipt.js +0 -332
  134. package/lib/errors/handler.js +0 -19
  135. package/lib/index.js +0 -40
  136. package/lib/inputs.js +0 -190
  137. package/lib/logger.js +0 -52
  138. package/mindee/api/financialDocument.js +0 -47
  139. package/mindee/api/index.js +0 -5
  140. package/mindee/api/invoice.js +0 -53
  141. package/mindee/api/object.js +0 -82
  142. package/mindee/api/receipt.js +0 -39
  143. package/mindee/api/request.js +0 -72
  144. package/mindee/api/response.js +0 -80
  145. package/mindee/documents/document.js +0 -69
  146. package/mindee/documents/fields/amount.js +0 -25
  147. package/mindee/documents/fields/date.js +0 -29
  148. package/mindee/documents/fields/field.js +0 -86
  149. package/mindee/documents/fields/index.js +0 -7
  150. package/mindee/documents/fields/locale.js +0 -30
  151. package/mindee/documents/fields/orientation.js +0 -24
  152. package/mindee/documents/fields/paymentDetails.js +0 -52
  153. package/mindee/documents/fields/tax.js +0 -47
  154. package/mindee/documents/financialDocument.js +0 -269
  155. package/mindee/documents/index.js +0 -5
  156. package/mindee/documents/invoice.js +0 -456
  157. package/mindee/documents/receipt.js +0 -287
  158. package/mindee/errors/handler.js +0 -16
  159. package/mindee/index.js +0 -36
  160. package/mindee/inputs.js +0 -158
  161. package/mindee/logger.js +0 -33
package/CHANGELOG.md CHANGED
@@ -1,21 +1,50 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v2.0.0 (2022-08-26)
4
+ ### Note
5
+ This is a complete rewrite in TypeScript!
6
+
7
+ It's **not** backwards compatible with previous versions.
8
+
9
+ ### Changes
10
+ * :sparkles: Add Passport API support
11
+ * :sparkles: Add custom document support (API builder)
12
+ * :sparkles: Add basic geometry functions
13
+ * :label: everything is typed, in particular response objects.
14
+ Allows for much easier auto-completion in your IDE.
15
+ * :arrow_up: all major dependencies updated
16
+
17
+ ## v1.4.0
18
+ ### Fixes
19
+ * :bug: cut/merge pages of an encrypted PDF leads to unexpected results
20
+ * :bug: logger not activating in some cases
21
+ * :bug: align PDF cut/merge with other client libs
22
+
23
+ ### Changes
24
+ * :sparkles: Add TIFF and HEIC support
25
+ * :white_check_mark: fully test PDF cut function
26
+
27
+ ## v1.3.3
28
+
29
+ ### Fixes
30
+ * :bug: reject errors instead of printing them only to the console
31
+
3
32
  ## v1.3.2
4
33
 
5
34
  ### Fixes
6
- * 🐛 use PDF length instead of cut PDF size limit
35
+ * :bug: use PDF length instead of cut PDF size limit
7
36
 
8
37
  ## v1.3.1
9
38
 
10
39
  ### Fixes
11
- * 🐛 forward api error to stderr instead of stdout
40
+ * :bug: forward api error to stderr instead of stdout
12
41
 
13
42
  ## v1.3.0
14
43
 
15
44
  ### Changes
16
45
 
17
46
  * :sparkles: allow sending the filename for streams and base64
18
- * fixes :bug: not able to specify MIME type
47
+ * :bug: not able to specify MIME type
19
48
  * :page_facing_up: change license to MIT
20
49
  * :white_check_mark: add node18 to tests
21
50
  * various package updates
@@ -24,43 +53,43 @@
24
53
 
25
54
  ### Fixes
26
55
 
27
- * fix: 🐛 Get `words` values when `includeWords` parameter is `true`
56
+ * :bug: Get `words` values when `includeWords` parameter is `true`
28
57
 
29
58
  ## v1.1.2
30
59
 
31
60
  ### Fixes
32
61
 
33
- * fix: 🐛 Fix returned probability value
34
- * fix: 🐛 Fix returned bbox value
62
+ * :bug: Fix returned probability value
63
+ * :bug: Fix returned bbox value
35
64
 
36
65
  ### new
37
66
 
38
- * new: New Mindee Invoice API V3 support
39
- * new: Add new supplier and customer fields
67
+ * :sparkles: New Mindee Invoice API V3 support
68
+ * :sparkles: Add new supplier and customer fields
40
69
 
41
70
  ## v1.1.0 (2022-01-03)
42
71
 
43
72
  ### new
44
73
 
45
- * chg: :sparkles: New Mindee API V2 support
46
- * chg: :zap: Server side invoice reconstruction from multi-page pdfs
47
- * new: :sparkles: Added OS in User-Agent headers
74
+ * :sparkles: New Mindee API V2 support
75
+ * :zap: Server side invoice reconstruction from multi-page pdfs
76
+ * :sparkles: Added OS in User-Agent headers
48
77
 
49
78
 
50
79
  ## v1.0.9 (2021-12-09)
51
80
 
52
81
  ### Fixes
53
82
 
54
- * fix: 🐛 handle attribute of type array when merging pages
83
+ * :bug: handle attribute of type array when merging pages
55
84
 
56
85
  ## v1.0.8 (2021-11-29)
57
86
 
58
87
  ### Fixes
59
88
 
60
- * 🐛 fix cutPDF for ReadableStream + add it for base64 file string
61
- * 🐛 Updated api Input initialization to specifically declare parameters
62
- * 🐛 prevent error when the mime type isn't detectable
63
- * 🐛 raise proper error when the APi doesn't return a valid JSON
89
+ * :bug: fix cutPDF for ReadableStream + add it for base64 file string
90
+ * :bug: Updated api Input initialization to specifically declare parameters
91
+ * :bug: prevent error when the mime type isn't detectable
92
+ * :bug: raise proper error when the APi doesn't return a valid JSON
64
93
 
65
94
  ### new
66
95
 
@@ -70,7 +99,7 @@
70
99
 
71
100
  ### New
72
101
 
73
- * Added pdf page number parameter for multi-pages pdfs with file
102
+ * :sparkles: Added pdf page number parameter for multi-pages pdfs with file
74
103
 
75
104
  ### Changes
76
105
 
@@ -109,4 +138,4 @@
109
138
 
110
139
  ## v1.0.1 (2021-01-11)
111
140
 
112
- * 🎉 First release
141
+ * :tada: First release
package/README.md CHANGED
@@ -1,150 +1,66 @@
1
- # Mindee Client Library for Node.js
1
+ # Mindee API Helper Library for Node.js
2
+ Quickly and easily connect to Mindee's API services using Node.js.
2
3
 
3
- # Installation
4
+ ## Quick Start
5
+ Here's the TL;DR of getting started.
4
6
 
5
- ## Requirements
7
+ First, get an [API Key](https://developers.mindee.com/docs/create-api-key)
6
8
 
7
- The SDK is officially supported on Node.js LTS _active_ and _maintenance_
8
- [versions](https://nodejs.org/en/about/releases/).
9
-
10
- ## From source code
11
-
12
- Download and decompress or clone the source code from GitHub and then do in your Node.js project:
13
-
14
- ```sh
9
+ Then, install this library:
10
+ ```shell
15
11
  npm install mindee
16
12
  ```
17
13
 
18
- # Usage
19
-
20
- You can use the SDK easily by creating a Client like this:
21
-
22
- ```js
23
- const { Client } = require("mindee");
14
+ Finally, Node.js away!
24
15
 
25
- const mindeeClient = new Client({
26
- invoiceToken: "invoiceApiToken",
27
- receiptToken: "receiptExpenseApiToken",
28
- });
29
-
30
- mindeeClient.invoice.parse({ input: "path/to/file", inputType: "file" });
31
- mindeeClient.receipt.parse({ input: base64String, inputType: "base64" });
32
- mindeeClient.financialDocument.parse({
33
- input: base64String,
34
- inputType: "base64",
35
- });
36
- ```
16
+ ### Off-the-Shelf Document
37
17
 
38
- Three APIs are actually supported: invoice (`ìnvoice`), receipt (`receipt`) and financial document (`financialDocument`)
18
+ ```ts
19
+ import { Client, InvoiceResponse } from "mindee";
39
20
 
40
- You can find more examples on how to use the SDK into the folder `examples`.
21
+ // Init a new client
22
+ const mindeeClient = new Client({apiKey: "my-api-key"});
41
23
 
42
- ## MIME Types
43
- When using `stream` or `base64` inputs, the MIME type is guessed using magic numbers.
44
- In some cases, for example with non-standard files, it will not be possible to detect the MIME type in this way.
24
+ // Load a file from disk and parse it
25
+ const invoiceResponse = mindeeClient.docFromPath("/path/to/the/invoice.pdf")
26
+ .parse(InvoiceResponse);
45
27
 
46
- If you are getting MIME type errors, you will need to specify a file name having the correct extension, for example:
47
- ```js
48
- mindeeClient.invoice.parse({
49
- input: base64String,
50
- inputType: "base64",
51
- filename: "myfile.pdf",
28
+ // Print a brief summary of the parsed data
29
+ invoiceResponse.then((resp) => {
30
+ console.log(resp.document);
52
31
  });
53
32
  ```
54
33
 
55
- ## Client
56
-
57
- The Mindee client can take multiple parameters to be initialize.
58
- Some this parameters can also be set with env variables.
59
- If the env variable and the parameter are both set, the parameter will be the one chosen by the client.
60
- This is a list of them:
61
-
62
- - invoiceToken (env variable : `MINDEE_INVOICE_TOKEN`) The expense api token
63
- - receiptToken (env variable : `MINDEE_RECEIPT_TOKEN`) The invoice api token
64
- - throwOnError (`true` by default) If `true`, the SDK will throw on error
65
- - debug (env variable : `MINDEE_DEBUG`, `false` by default) If `true`, logging will be in debug mode
66
-
67
- Depending on what type of document you want to parse, you need to add specifics auth token for each endpoint.
68
- We suggest storing your credentials as environment variables for security reasons.
69
-
70
- ## Parsing Invoices
71
-
72
- ```js
73
- const { Client } = require("mindee");
74
- const fs = require("fs");
75
-
76
- // Invoice token can be set by Env (MINDEE_INVOICE_TOKEN) or via params (Client({invoiceToken: "token"}))
77
- const mindeeClient = new Client();
78
-
79
- // parsing invoice from pdf
80
- mindeeClient.invoice
81
- .parse({ input: "./documents/invoices/invoice.pdf" }) // see examples for more input types
82
- .then((res) => {
83
- console.log("Success !");
84
- console.log(res.invoices);
85
- console.log(res.invoice);
86
- })
87
- .catch((err) => {
88
- console.error(err);
89
- });
90
- ```
91
-
92
- ## Parsing receipts
93
-
94
- ```js
95
- const { Client } = require("mindee");
96
- const fs = require("fs");
97
-
98
- // Receipt token can be set by Env (MINDEE_RECEIPT_TOKEN) or via params (Client({receiptToken: "token"}))
99
- const mindeeClient = new Client();
100
-
101
- // parsing receipt from picture
102
- mindeeClient.receipt
103
- .parse({ input: "./documents/receipts/receipt.jpg" }) // see examples for more input types
104
- .then((res) => {
105
- console.log("Success !");
106
- console.log(res.receipts);
107
- console.log(res.receipt);
108
- })
109
- .catch((err) => {
110
- console.error(err);
111
- });
112
- ```
113
-
114
- ## Parsing a mix of invoices and receipts: Financial document
34
+ ### Custom Document (API Builder)
115
35
 
116
- The Financial document API optimizes the parsing results when you don't know if your file is a receipt or an invoice.
36
+ ```ts
37
+ import { Client, CustomResponse } from "mindee";
117
38
 
118
- ```js
119
- const { Client } = require("mindee");
120
-
121
- // Invoice token and Receipt token must be set
122
- // Receipt token can be set by Env (MINDEE_RECEIPT_TOKEN) or via params (Client({receiptToken: "token"}))
123
- // Invoice token can be set by Env (MINDEE_INVOICE_TOKEN) or via params (Client({invoiceToken: "token"}))
39
+ // Init a new client and add your document endpoint
40
+ const mindeeClient = new Client({apiKey: "my-api-key"})
41
+ .addEndpoint({
42
+ accountName: "john",
43
+ documentType: "wsnine",
44
+ });
124
45
 
125
- const mindeeClient = new Client();
46
+ // Load a file from disk and parse it
47
+ const customResponse = mindeeClient.docFromPath("/path/to/the/wsnine.jpg")
48
+ .parse(CustomResponse, {docType: "wsnine"});
126
49
 
127
- // parsing receipt from picture
128
- mindeeClient.financialDocument
129
- .parse({ input: "./documents/receipts/receipt.jpg" })
130
- .then((res) => {
131
- console.log("Success !");
132
- console.log(res.financialDocuments);
133
- console.log(res.financialDocument);
134
- })
135
- .catch((err) => {
136
- console.error(err);
137
- });
50
+ // Print a brief summary of the parsed data
51
+ customResponse.then((resp) => {
52
+ console.log(resp.document);
53
+ });
138
54
  ```
139
55
 
140
- # Tests
141
-
142
- To run the tests, use the command `npm run test`
56
+ ## Further Reading
57
+ There's more to it than that for those that need more features, or want to
58
+ customize the experience.
143
59
 
144
- # Documentation
60
+ All the juicy details are described in the
61
+ **[Official Documentation](https://developers.mindee.com/docs/nodejs-sdk)**.
145
62
 
146
- A basic documentation of the sdk has been done with the help of JSDoc.
147
- If you want to generate it, you can use the command `npm run documentation`.
148
- A folder `docs` will be created with generated documentation.
149
- Open the `index.html` in your browser to read the documentation.
63
+ ## License
64
+ Copyright © Mindee
150
65
 
66
+ Available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
package/bin/mindee ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../dist/src/cli').cli();
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "mindee",
3
+ "version": "2.0.0",
4
+ "description": "Mindee Client Library for Node.js",
5
+ "main": "src/index.js",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "build": "tsc --build",
9
+ "clean": "rm -rf ./dist",
10
+ "test": "mocha 'tests/**/*.spec.ts'",
11
+ "lint-fix": "eslint 'src/**/*.ts' --fix",
12
+ "lint": "eslint 'src/**/*.ts' --report-unused-disable-directives && echo 'Your .ts files look good.'",
13
+ "docs": "jsdoc -r src -d docs"
14
+ },
15
+ "files": [
16
+ "dist/",
17
+ "bin/",
18
+ "LICENSE",
19
+ "README.md",
20
+ "CHANGELOG.md"
21
+ ],
22
+ "engines": {
23
+ "node": ">= 14.0.0"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/mindee/mindee-api-nodejs.git"
28
+ },
29
+ "author": {
30
+ "name": "Mindee",
31
+ "email": "devrel@mindee.com",
32
+ "url": "https://mindee.com/"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/mindee/mindee-api-nodejs/issues"
36
+ },
37
+ "homepage": "https://mindee.com/",
38
+ "devDependencies": {
39
+ "@tsconfig/node16": "^1.0.3",
40
+ "@types/chai": "^4.3.1",
41
+ "@types/mocha": "^9.1.1",
42
+ "@types/node": "^17.0.38",
43
+ "@typescript-eslint/eslint-plugin": "^5.30.0",
44
+ "@typescript-eslint/parser": "^5.30.0",
45
+ "chai": "^4.3.6",
46
+ "eslint": "^8.18.0",
47
+ "eslint-config-prettier": "^8.5.0",
48
+ "eslint-plugin-prettier": "^4.1.0",
49
+ "jsdoc": "^3.6.10",
50
+ "mocha": "^10.0.0",
51
+ "prettier": "^2.7.1",
52
+ "ts-node": "^10.8.1",
53
+ "typescript": "^4.7.4"
54
+ },
55
+ "dependencies": {
56
+ "commander": "^9.4.0",
57
+ "file-type": "^16.5.4",
58
+ "form-data": "^3.0.1",
59
+ "mrz": "^3.1.4",
60
+ "pdf-lib": "^1.17.1"
61
+ },
62
+ "keywords": [
63
+ "typescript",
64
+ "mindee",
65
+ "api",
66
+ "client",
67
+ "client library",
68
+ "nodejs"
69
+ ]
70
+ }
@@ -0,0 +1,29 @@
1
+ import { Input } from "../inputs";
2
+ export declare const STANDARD_API_OWNER = "mindee";
3
+ export declare const API_KEY_ENVVAR_NAME = "MINDEE_API_KEY";
4
+ export declare class Endpoint {
5
+ apiKey: string;
6
+ urlName: string;
7
+ keyName: string;
8
+ owner: string;
9
+ version: string;
10
+ urlRoot: string;
11
+ constructor(owner: string, urlName: string, version: string, apiKey: string, keyName?: string);
12
+ /**
13
+ * Make a prediction request.
14
+ */
15
+ predictRequest(input: Input, includeWords?: boolean): Promise<unknown>;
16
+ protected apiKeyFromEnv(): string;
17
+ }
18
+ export declare class InvoiceEndpoint extends Endpoint {
19
+ constructor(apiKey: string);
20
+ }
21
+ export declare class ReceiptEndpoint extends Endpoint {
22
+ constructor(apiKey: string);
23
+ }
24
+ export declare class PassportEndpoint extends Endpoint {
25
+ constructor(apiKey: string);
26
+ }
27
+ export declare class CustomEndpoint extends Endpoint {
28
+ constructor(documentType: string, accountName: string, version: string, apiKey: string);
29
+ }
@@ -0,0 +1,148 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.CustomEndpoint = exports.PassportEndpoint = exports.ReceiptEndpoint = exports.InvoiceEndpoint = exports.Endpoint = exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = void 0;
30
+ const https = __importStar(require("https"));
31
+ const os = __importStar(require("os"));
32
+ const package_json_1 = require("../../package.json");
33
+ const url_1 = require("url");
34
+ const form_data_1 = __importDefault(require("form-data"));
35
+ const logger_1 = require("../logger");
36
+ const MINDEE_API_URL = "https://api.mindee.net/v1";
37
+ const USER_AGENT = `mindee-api-nodejs@v${package_json_1.version} nodejs-${process.version} ${os.type().toLowerCase()}`;
38
+ exports.STANDARD_API_OWNER = "mindee";
39
+ exports.API_KEY_ENVVAR_NAME = "MINDEE_API_KEY";
40
+ class Endpoint {
41
+ constructor(owner, urlName, version, apiKey, keyName) {
42
+ this.owner = owner;
43
+ this.urlName = urlName;
44
+ this.version = version;
45
+ this.keyName = keyName || urlName;
46
+ this.apiKey = apiKey || this.apiKeyFromEnv();
47
+ this.urlRoot = `${MINDEE_API_URL}/products/${owner}/${urlName}/v${version}`;
48
+ }
49
+ /**
50
+ * Make a prediction request.
51
+ */
52
+ predictRequest(input, includeWords = false) {
53
+ return new Promise((resolve, reject) => {
54
+ const form = new form_data_1.default();
55
+ let body;
56
+ let headers = {
57
+ "User-Agent": USER_AGENT,
58
+ Authorization: `Token ${this.apiKey}`,
59
+ };
60
+ // TODO: redo this section given there should only
61
+ // be a single way of reading the input doc
62
+ if (["path", "stream"].includes(input.inputType)) {
63
+ const fileParams = { filename: input.filename };
64
+ form.append("document", input.fileObject, fileParams);
65
+ if (includeWords) {
66
+ form.append("include_mvision", "true");
67
+ }
68
+ headers = Object.assign(Object.assign({}, headers), form.getHeaders());
69
+ }
70
+ else if (input.inputType === "base64") {
71
+ const bodyObj = { document: input.fileObject };
72
+ if (includeWords) {
73
+ bodyObj["include_mvision"] = "true";
74
+ }
75
+ body = JSON.stringify(bodyObj);
76
+ headers["Content-Type"] = "application/json";
77
+ headers["Content-Length"] = body.length;
78
+ }
79
+ const uri = new url_1.URL(`${this.urlRoot}/predict`);
80
+ const options = {
81
+ method: "POST",
82
+ headers: headers,
83
+ hostname: uri.hostname,
84
+ path: `${uri.pathname}${uri.search}`,
85
+ };
86
+ logger_1.logger.debug(`Prediction request: ${uri}`);
87
+ const req = https.request(options, function (res) {
88
+ // when the encoding is set, data chunks will be strings
89
+ res.setEncoding("utf-8");
90
+ let responseBody = "";
91
+ res.on("data", function (chunk) {
92
+ responseBody += chunk;
93
+ });
94
+ res.on("end", function () {
95
+ try {
96
+ resolve(Object.assign(Object.assign({}, res), { data: JSON.parse(responseBody) }));
97
+ }
98
+ catch (error) {
99
+ reject(error);
100
+ }
101
+ });
102
+ });
103
+ req.on("error", (err) => {
104
+ reject(err);
105
+ });
106
+ if (["path", "stream"].includes(input.inputType)) {
107
+ form.pipe(req);
108
+ }
109
+ if (input.inputType === "base64") {
110
+ req.write(body);
111
+ req.end();
112
+ }
113
+ });
114
+ }
115
+ apiKeyFromEnv() {
116
+ const envVarValue = process.env[exports.API_KEY_ENVVAR_NAME];
117
+ if (envVarValue) {
118
+ logger_1.logger.debug(`Set '${this.keyName}' API key from environment: ${exports.API_KEY_ENVVAR_NAME}`);
119
+ return envVarValue;
120
+ }
121
+ return "";
122
+ }
123
+ }
124
+ exports.Endpoint = Endpoint;
125
+ class InvoiceEndpoint extends Endpoint {
126
+ constructor(apiKey) {
127
+ super(exports.STANDARD_API_OWNER, "invoices", "3", apiKey, "invoice");
128
+ }
129
+ }
130
+ exports.InvoiceEndpoint = InvoiceEndpoint;
131
+ class ReceiptEndpoint extends Endpoint {
132
+ constructor(apiKey) {
133
+ super(exports.STANDARD_API_OWNER, "expense_receipts", "3", apiKey, "receipt");
134
+ }
135
+ }
136
+ exports.ReceiptEndpoint = ReceiptEndpoint;
137
+ class PassportEndpoint extends Endpoint {
138
+ constructor(apiKey) {
139
+ super(exports.STANDARD_API_OWNER, "passport", "1", apiKey);
140
+ }
141
+ }
142
+ exports.PassportEndpoint = PassportEndpoint;
143
+ class CustomEndpoint extends Endpoint {
144
+ constructor(documentType, accountName, version, apiKey) {
145
+ super(accountName, documentType, version, apiKey);
146
+ }
147
+ }
148
+ exports.CustomEndpoint = CustomEndpoint;
@@ -0,0 +1,2 @@
1
+ export { Response, InvoiceResponse, ReceiptResponse, FinancialDocResponse, PassportResponse, CustomResponse, } from "./response";
2
+ export { Endpoint, InvoiceEndpoint, ReceiptEndpoint, PassportEndpoint, CustomEndpoint, STANDARD_API_OWNER, API_KEY_ENVVAR_NAME, } from "./endpoint";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = exports.CustomEndpoint = exports.PassportEndpoint = exports.ReceiptEndpoint = exports.InvoiceEndpoint = exports.Endpoint = exports.CustomResponse = exports.PassportResponse = exports.FinancialDocResponse = exports.ReceiptResponse = exports.InvoiceResponse = exports.Response = void 0;
4
+ var response_1 = require("./response");
5
+ Object.defineProperty(exports, "Response", { enumerable: true, get: function () { return response_1.Response; } });
6
+ Object.defineProperty(exports, "InvoiceResponse", { enumerable: true, get: function () { return response_1.InvoiceResponse; } });
7
+ Object.defineProperty(exports, "ReceiptResponse", { enumerable: true, get: function () { return response_1.ReceiptResponse; } });
8
+ Object.defineProperty(exports, "FinancialDocResponse", { enumerable: true, get: function () { return response_1.FinancialDocResponse; } });
9
+ Object.defineProperty(exports, "PassportResponse", { enumerable: true, get: function () { return response_1.PassportResponse; } });
10
+ Object.defineProperty(exports, "CustomResponse", { enumerable: true, get: function () { return response_1.CustomResponse; } });
11
+ var endpoint_1 = require("./endpoint");
12
+ Object.defineProperty(exports, "Endpoint", { enumerable: true, get: function () { return endpoint_1.Endpoint; } });
13
+ Object.defineProperty(exports, "InvoiceEndpoint", { enumerable: true, get: function () { return endpoint_1.InvoiceEndpoint; } });
14
+ Object.defineProperty(exports, "ReceiptEndpoint", { enumerable: true, get: function () { return endpoint_1.ReceiptEndpoint; } });
15
+ Object.defineProperty(exports, "PassportEndpoint", { enumerable: true, get: function () { return endpoint_1.PassportEndpoint; } });
16
+ Object.defineProperty(exports, "CustomEndpoint", { enumerable: true, get: function () { return endpoint_1.CustomEndpoint; } });
17
+ Object.defineProperty(exports, "STANDARD_API_OWNER", { enumerable: true, get: function () { return endpoint_1.STANDARD_API_OWNER; } });
18
+ Object.defineProperty(exports, "API_KEY_ENVVAR_NAME", { enumerable: true, get: function () { return endpoint_1.API_KEY_ENVVAR_NAME; } });
@@ -0,0 +1,52 @@
1
+ import { Document, Passport, Receipt, Invoice, FinancialDocument, CustomDocument, DocumentConstructorProps } from "../documents";
2
+ import { FullText } from "../fields";
3
+ import { Input } from "../inputs";
4
+ export interface ResponseProps {
5
+ httpResponse: any;
6
+ documentType: string;
7
+ input: Input;
8
+ error: boolean;
9
+ }
10
+ /**
11
+ * Base class for all responses.
12
+ */
13
+ export declare class Response<DocType extends Document> {
14
+ httpResponse: any;
15
+ readonly documentType: string;
16
+ inputFile: Input;
17
+ document?: DocType;
18
+ pages: Array<DocType>;
19
+ constructor(params: ResponseProps);
20
+ protected getPageText(httpDataDocument: any, pageId: number): FullText;
21
+ }
22
+ /**
23
+ * Class for all constructed (API Builder) endpoint responses.
24
+ */
25
+ export declare class CustomResponse extends Response<CustomDocument> {
26
+ constructor(params: ResponseProps);
27
+ protected formatResponse(): void;
28
+ }
29
+ declare type StandardDocumentSig<DocType extends Document> = {
30
+ new ({ apiPrediction, inputFile, pageId, fullText, }: DocumentConstructorProps): DocType;
31
+ };
32
+ /**
33
+ * Generic class for all standard (Off-the-Shelf) endpoint responses.
34
+ */
35
+ export declare class StandardProductResponse<DocType extends Document> extends Response<DocType> {
36
+ documentClass: StandardDocumentSig<DocType>;
37
+ constructor(documentClass: StandardDocumentSig<DocType>, params: ResponseProps);
38
+ protected formatResponse(): void;
39
+ }
40
+ export declare class InvoiceResponse extends StandardProductResponse<Invoice> {
41
+ constructor(params: ResponseProps);
42
+ }
43
+ export declare class ReceiptResponse extends StandardProductResponse<Receipt> {
44
+ constructor(params: ResponseProps);
45
+ }
46
+ export declare class FinancialDocResponse extends StandardProductResponse<FinancialDocument> {
47
+ constructor(params: ResponseProps);
48
+ }
49
+ export declare class PassportResponse extends StandardProductResponse<Passport> {
50
+ constructor(params: ResponseProps);
51
+ }
52
+ export {};