mindee 2.0.0 → 2.0.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.
Files changed (69) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/README.md +15 -11
  3. package/bin/mindee.d.ts +2 -0
  4. package/bin/mindee.js +5 -0
  5. package/package.json +68 -68
  6. package/{dist/src → src}/api/endpoint.d.ts +0 -0
  7. package/{dist/src → src}/api/endpoint.js +5 -2
  8. package/{dist/src → src}/api/index.d.ts +0 -0
  9. package/{dist/src → src}/api/index.js +0 -0
  10. package/{dist/src → src}/api/response.d.ts +0 -0
  11. package/{dist/src → src}/api/response.js +0 -0
  12. package/{dist/src → src}/cli.d.ts +0 -0
  13. package/src/cli.js +114 -0
  14. package/{dist/src → src}/client.d.ts +0 -0
  15. package/{dist/src → src}/client.js +35 -46
  16. package/{dist/src → src}/constants.d.ts +0 -0
  17. package/{dist/src → src}/constants.js +0 -0
  18. package/{dist/src → src}/documents/custom.d.ts +0 -0
  19. package/{dist/src → src}/documents/custom.js +0 -0
  20. package/{dist/src → src}/documents/document.d.ts +0 -0
  21. package/{dist/src → src}/documents/document.js +5 -6
  22. package/{dist/src → src}/documents/documentConfig.d.ts +0 -0
  23. package/{dist/src → src}/documents/documentConfig.js +21 -38
  24. package/{dist/src → src}/documents/financialDocument.d.ts +0 -0
  25. package/{dist/src → src}/documents/financialDocument.js +4 -1
  26. package/{dist/src → src}/documents/index.d.ts +0 -0
  27. package/{dist/src → src}/documents/index.js +0 -0
  28. package/{dist/src → src}/documents/invoice.d.ts +0 -0
  29. package/{dist/src → src}/documents/invoice.js +0 -0
  30. package/{dist/src → src}/documents/passport.d.ts +0 -0
  31. package/{dist/src → src}/documents/passport.js +0 -0
  32. package/{dist/src → src}/documents/receipt.d.ts +0 -0
  33. package/{dist/src → src}/documents/receipt.js +0 -0
  34. package/{dist/src → src}/errors/handler.d.ts +0 -0
  35. package/{dist/src → src}/errors/handler.js +0 -0
  36. package/{dist/src → src}/fields/amount.d.ts +0 -0
  37. package/{dist/src → src}/fields/amount.js +0 -0
  38. package/{dist/src → src}/fields/companyRegistration.d.ts +0 -0
  39. package/{dist/src → src}/fields/companyRegistration.js +0 -0
  40. package/{dist/src → src}/fields/customDocs.d.ts +2 -2
  41. package/{dist/src → src}/fields/customDocs.js +4 -4
  42. package/{dist/src → src}/fields/date.d.ts +0 -0
  43. package/{dist/src → src}/fields/date.js +0 -0
  44. package/{dist/src → src}/fields/field.d.ts +0 -0
  45. package/{dist/src → src}/fields/field.js +0 -0
  46. package/{dist/src → src}/fields/fullText.d.ts +0 -0
  47. package/{dist/src → src}/fields/fullText.js +0 -0
  48. package/{dist/src → src}/fields/index.d.ts +1 -1
  49. package/{dist/src → src}/fields/index.js +2 -2
  50. package/{dist/src → src}/fields/locale.d.ts +0 -0
  51. package/{dist/src → src}/fields/locale.js +0 -0
  52. package/{dist/src → src}/fields/orientation.d.ts +0 -0
  53. package/{dist/src → src}/fields/orientation.js +0 -0
  54. package/{dist/src → src}/fields/paymentDetails.d.ts +0 -0
  55. package/{dist/src → src}/fields/paymentDetails.js +0 -0
  56. package/{dist/src → src}/fields/tax.d.ts +0 -0
  57. package/{dist/src → src}/fields/tax.js +1 -2
  58. package/{dist/src → src}/geometry.d.ts +0 -0
  59. package/{dist/src → src}/geometry.js +0 -0
  60. package/{dist/src → src}/index.d.ts +0 -0
  61. package/{dist/src → src}/index.js +0 -0
  62. package/{dist/src → src}/inputs.d.ts +0 -0
  63. package/src/inputs.js +196 -0
  64. package/{dist/src → src}/logger.d.ts +0 -0
  65. package/{dist/src → src}/logger.js +0 -0
  66. package/bin/mindee +0 -3
  67. package/dist/package.json +0 -70
  68. package/dist/src/cli.js +0 -117
  69. package/dist/src/inputs.js +0 -223
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -8,7 +8,7 @@ export declare class ClassificationField {
8
8
  });
9
9
  toString(): string;
10
10
  }
11
- export declare class ListFieldItem {
11
+ export declare class ListFieldValue {
12
12
  content: any;
13
13
  /**
14
14
  * The confidence score of the prediction.
@@ -29,7 +29,7 @@ export declare class ListFieldItem {
29
29
  toString(): string;
30
30
  }
31
31
  export declare class ListField {
32
- values: ListFieldItem[];
32
+ readonly values: ListFieldValue[];
33
33
  confidence: number;
34
34
  /** True if the field was reconstructed or computed using other fields. */
35
35
  reconstructed: boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ListField = exports.ListFieldItem = exports.ClassificationField = void 0;
3
+ exports.ListField = exports.ListFieldValue = exports.ClassificationField = void 0;
4
4
  const geometry_1 = require("../geometry");
5
5
  class ClassificationField {
6
6
  constructor({ prediction }) {
@@ -12,7 +12,7 @@ class ClassificationField {
12
12
  }
13
13
  }
14
14
  exports.ClassificationField = ClassificationField;
15
- class ListFieldItem {
15
+ class ListFieldValue {
16
16
  constructor(prediction) {
17
17
  /**
18
18
  * Contains exactly 4 relative vertices coordinates (points) of a right
@@ -35,7 +35,7 @@ class ListFieldItem {
35
35
  return `${this.content}`;
36
36
  }
37
37
  }
38
- exports.ListFieldItem = ListFieldItem;
38
+ exports.ListFieldValue = ListFieldValue;
39
39
  class ListField {
40
40
  constructor({ prediction, reconstructed = false, pageId }) {
41
41
  this.values = [];
@@ -44,7 +44,7 @@ class ListField {
44
44
  this.pageId = pageId !== undefined ? pageId : prediction["page_id"];
45
45
  if (Object.prototype.hasOwnProperty.call(prediction, "values")) {
46
46
  prediction["values"].forEach((field) => {
47
- this.values.push(new ListFieldItem(field));
47
+ this.values.push(new ListFieldValue(field));
48
48
  });
49
49
  }
50
50
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -6,6 +6,6 @@ export { Amount } from "./amount";
6
6
  export { DateField } from "./date";
7
7
  export { BaseField } from "./field";
8
8
  export { Field } from "./field";
9
- export { ListField, ListFieldItem, ClassificationField } from "./customDocs";
9
+ export { ListField, ListFieldValue, ClassificationField } from "./customDocs";
10
10
  export { FullText } from "./fullText";
11
11
  export { CompanyRegistration } from "./companyRegistration";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CompanyRegistration = exports.FullText = exports.ClassificationField = exports.ListFieldItem = exports.ListField = exports.Field = exports.BaseField = exports.DateField = exports.Amount = exports.Locale = exports.Orientation = exports.PaymentDetails = exports.TaxField = void 0;
3
+ exports.CompanyRegistration = exports.FullText = exports.ClassificationField = exports.ListFieldValue = exports.ListField = exports.Field = exports.BaseField = exports.DateField = exports.Amount = exports.Locale = exports.Orientation = exports.PaymentDetails = exports.TaxField = void 0;
4
4
  var tax_1 = require("./tax");
5
5
  Object.defineProperty(exports, "TaxField", { enumerable: true, get: function () { return tax_1.TaxField; } });
6
6
  var paymentDetails_1 = require("./paymentDetails");
@@ -19,7 +19,7 @@ var field_2 = require("./field");
19
19
  Object.defineProperty(exports, "Field", { enumerable: true, get: function () { return field_2.Field; } });
20
20
  var customDocs_1 = require("./customDocs");
21
21
  Object.defineProperty(exports, "ListField", { enumerable: true, get: function () { return customDocs_1.ListField; } });
22
- Object.defineProperty(exports, "ListFieldItem", { enumerable: true, get: function () { return customDocs_1.ListFieldItem; } });
22
+ Object.defineProperty(exports, "ListFieldValue", { enumerable: true, get: function () { return customDocs_1.ListFieldValue; } });
23
23
  Object.defineProperty(exports, "ClassificationField", { enumerable: true, get: function () { return customDocs_1.ClassificationField; } });
24
24
  var fullText_1 = require("./fullText");
25
25
  Object.defineProperty(exports, "FullText", { enumerable: true, get: function () { return fullText_1.FullText; } });
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -13,7 +13,6 @@ class TaxField extends field_1.Field {
13
13
  * @param {Integer} pageNumber - Page ID for multi-page document
14
14
  */
15
15
  constructor({ prediction, valueKey = "value", rateKey = "rate", codeKey = "code", reconstructed = false, pageId = undefined, }) {
16
- var _a;
17
16
  super({ prediction, valueKey, reconstructed, pageId });
18
17
  /** The tax amount. */
19
18
  this.value = undefined;
@@ -24,7 +23,7 @@ class TaxField extends field_1.Field {
24
23
  this.rate = +parseFloat(prediction[rateKey]);
25
24
  if (isNaN(this.rate))
26
25
  this.rate = undefined;
27
- this.code = (_a = prediction[codeKey]) === null || _a === void 0 ? void 0 : _a.toString();
26
+ this.code = prediction[codeKey]?.toString();
28
27
  if (this.code === "N/A" || this.code === "None") {
29
28
  this.code = undefined;
30
29
  }
File without changes
File without changes
File without changes
File without changes
File without changes
package/src/inputs.js ADDED
@@ -0,0 +1,196 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.BytesInput = exports.StreamInput = exports.Base64Input = exports.PathInput = exports.Input = exports.INPUT_TYPE_PATH = exports.INPUT_TYPE_BYTES = exports.INPUT_TYPE_BASE64 = exports.INPUT_TYPE_STREAM = void 0;
27
+ const fs_1 = require("fs");
28
+ const path = __importStar(require("path"));
29
+ const fileType = __importStar(require("file-type"));
30
+ const handler_1 = require("./errors/handler");
31
+ const pdf_lib_1 = require("pdf-lib");
32
+ const node_buffer_1 = require("node:buffer");
33
+ const logger_1 = require("./logger");
34
+ exports.INPUT_TYPE_STREAM = "stream";
35
+ exports.INPUT_TYPE_BASE64 = "base64";
36
+ exports.INPUT_TYPE_BYTES = "bytes";
37
+ exports.INPUT_TYPE_PATH = "path";
38
+ const MIMETYPES = new Map([
39
+ [".pdf", "application/pdf"],
40
+ [".heic", "image/heic"],
41
+ [".jpg", "image/jpeg"],
42
+ [".jpeg", "image/jpeg"],
43
+ [".png", "image/png"],
44
+ [".tif", "image/tiff"],
45
+ [".tiff", "image/tiff"],
46
+ [".webp", "image/webp"],
47
+ ]);
48
+ const ALLOWED_INPUT_TYPES = [
49
+ exports.INPUT_TYPE_STREAM,
50
+ exports.INPUT_TYPE_BASE64,
51
+ exports.INPUT_TYPE_BYTES,
52
+ exports.INPUT_TYPE_PATH,
53
+ ];
54
+ class Input {
55
+ /**
56
+ * @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
57
+ * NB: dummy is only used for tests purposes
58
+ * @param {Boolean} cutPages
59
+ * NB: Because of async calls, init() should be called after creating the object
60
+ */
61
+ constructor({ inputType }) {
62
+ this.MAX_DOC_PAGES = 3;
63
+ this.filename = "";
64
+ this.mimeType = "";
65
+ this.fileObject = node_buffer_1.Buffer.alloc(0);
66
+ // Check if inputType is valid
67
+ if (!ALLOWED_INPUT_TYPES.includes(inputType)) {
68
+ const allowed = Array.from(MIMETYPES.keys()).join(", ");
69
+ handler_1.errorHandler.throw(new Error(`Invalid input type, must be one of ${allowed}.`));
70
+ }
71
+ this.inputType = inputType;
72
+ logger_1.logger.debug(`Loading file from: ${inputType}`);
73
+ }
74
+ async init() {
75
+ throw new Error("not Implemented");
76
+ }
77
+ isPdf() {
78
+ return this.mimeType === "application/pdf";
79
+ }
80
+ async checkMimetype() {
81
+ let mimeType;
82
+ const fileExt = path.extname(this.filename);
83
+ if (fileExt) {
84
+ mimeType = MIMETYPES.get(fileExt.toLowerCase()) || "";
85
+ }
86
+ else {
87
+ const guess = await fileType.fromBuffer(this.fileObject);
88
+ if (guess !== undefined) {
89
+ mimeType = guess.mime;
90
+ }
91
+ else {
92
+ throw "Could not determine the MIME type of the file";
93
+ }
94
+ }
95
+ if (!mimeType) {
96
+ const allowed = Array.from(MIMETYPES.keys()).join(", ");
97
+ const err = new Error(`Invalid file type, must be one of ${allowed}.`);
98
+ handler_1.errorHandler.throw(err);
99
+ }
100
+ logger_1.logger.debug(`File is of type: ${mimeType}`);
101
+ return mimeType;
102
+ }
103
+ async countPages() {
104
+ const pdfDocument = await pdf_lib_1.PDFDocument.load(this.fileObject, {
105
+ ignoreEncryption: true,
106
+ });
107
+ return pdfDocument.getPageCount();
108
+ }
109
+ /** Merge PDF pages */
110
+ async cutPdf() {
111
+ const currentPdf = await pdf_lib_1.PDFDocument.load(this.fileObject, {
112
+ ignoreEncryption: true,
113
+ });
114
+ const pdfLength = currentPdf.getPageCount();
115
+ if (pdfLength <= this.MAX_DOC_PAGES) {
116
+ return;
117
+ }
118
+ const newPdf = await pdf_lib_1.PDFDocument.create();
119
+ const pagesNumbers = [0, pdfLength - 2, pdfLength - 1].slice(0, this.MAX_DOC_PAGES);
120
+ logger_1.logger.debug(`Cutting PDF, keeping pages: ${pagesNumbers}`);
121
+ const pages = await newPdf.copyPages(currentPdf, pagesNumbers);
122
+ pages.forEach((page) => newPdf.addPage(page));
123
+ const data = await newPdf.save();
124
+ this.fileObject = node_buffer_1.Buffer.from(data);
125
+ }
126
+ }
127
+ exports.Input = Input;
128
+ class PathInput extends Input {
129
+ constructor({ inputPath }) {
130
+ super({
131
+ inputType: exports.INPUT_TYPE_PATH,
132
+ });
133
+ this.inputPath = inputPath;
134
+ this.filename = path.basename(this.inputPath);
135
+ }
136
+ async init() {
137
+ logger_1.logger.debug(`Loading from: ${this.inputPath}`);
138
+ this.fileObject = node_buffer_1.Buffer.from(await fs_1.promises.readFile(this.inputPath));
139
+ this.mimeType = await this.checkMimetype();
140
+ }
141
+ }
142
+ exports.PathInput = PathInput;
143
+ class Base64Input extends Input {
144
+ constructor({ inputString, filename }) {
145
+ super({
146
+ inputType: exports.INPUT_TYPE_BASE64,
147
+ });
148
+ this.filename = filename;
149
+ this.inputString = inputString;
150
+ }
151
+ async init() {
152
+ this.fileObject = node_buffer_1.Buffer.from(this.inputString, "base64");
153
+ this.mimeType = await this.checkMimetype();
154
+ // clear out the string
155
+ this.inputString = "";
156
+ }
157
+ }
158
+ exports.Base64Input = Base64Input;
159
+ class StreamInput extends Input {
160
+ constructor({ inputStream, filename }) {
161
+ super({
162
+ inputType: exports.INPUT_TYPE_STREAM,
163
+ });
164
+ this.filename = filename;
165
+ this.inputStream = inputStream;
166
+ }
167
+ async init() {
168
+ this.fileObject = await this.stream2buffer(this.inputStream);
169
+ this.mimeType = await this.checkMimetype();
170
+ }
171
+ async stream2buffer(stream) {
172
+ return new Promise((resolve, reject) => {
173
+ const _buf = Array();
174
+ stream.on("data", (chunk) => _buf.push(chunk));
175
+ stream.on("end", () => resolve(node_buffer_1.Buffer.concat(_buf)));
176
+ stream.on("error", (err) => reject(`Error converting stream - ${err}`));
177
+ });
178
+ }
179
+ }
180
+ exports.StreamInput = StreamInput;
181
+ class BytesInput extends Input {
182
+ constructor({ inputBytes, filename }) {
183
+ super({
184
+ inputType: exports.INPUT_TYPE_BYTES,
185
+ });
186
+ this.filename = filename;
187
+ this.inputBytes = inputBytes;
188
+ }
189
+ async init() {
190
+ this.fileObject = node_buffer_1.Buffer.from(this.inputBytes, "hex");
191
+ this.mimeType = await this.checkMimetype();
192
+ // clear out the string
193
+ this.inputBytes = "";
194
+ }
195
+ }
196
+ exports.BytesInput = BytesInput;
File without changes
File without changes
package/bin/mindee DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- require('../dist/src/cli').cli();
package/dist/package.json DELETED
@@ -1,70 +0,0 @@
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
- }
package/dist/src/cli.js DELETED
@@ -1,117 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.cli = void 0;
13
- const commander_1 = require("commander");
14
- const documents_1 = require("./documents");
15
- const api_1 = require("./api");
16
- const client_1 = require("./client");
17
- const constants_1 = require("./constants");
18
- const program = new commander_1.Command();
19
- const COMMAND_INVOICE = "invoice";
20
- const COMMAND_RECEIPT = "receipt";
21
- const COMMAND_PASSPORT = "passport";
22
- const COMMAND_FINANCIAL = "financial";
23
- const COMMAND_CUSTOM = "custom";
24
- const CLI_COMMAND_CONFIG = new Map([
25
- [COMMAND_INVOICE, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_INVOICE)],
26
- [COMMAND_RECEIPT, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_RECEIPT)],
27
- [COMMAND_PASSPORT, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_PASSPORT)],
28
- [COMMAND_FINANCIAL, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_FINANCIAL)],
29
- [COMMAND_CUSTOM, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_CUSTOM)],
30
- ]);
31
- function predictCall(command, inputPath, options) {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- const conf = CLI_COMMAND_CONFIG.get(command);
34
- if (conf === undefined) {
35
- throw new Error(`Invalid document type ${command}`);
36
- }
37
- const mindeeClient = new client_1.Client({
38
- apiKey: options.apiKey,
39
- debug: options.debug,
40
- });
41
- if (command === COMMAND_CUSTOM) {
42
- mindeeClient.addEndpoint({
43
- accountName: options.user,
44
- documentType: options.documentType,
45
- });
46
- }
47
- const doc = mindeeClient.docFromPath(inputPath);
48
- const params = {
49
- docType: command === COMMAND_CUSTOM ? options.documentType : conf.docType,
50
- username: command === COMMAND_CUSTOM ? options.user : api_1.STANDARD_API_OWNER,
51
- cutPages: options.cutPages,
52
- fullText: options.fullText,
53
- };
54
- // Tried setting the response by using the responseClass property in constants.PRODUCTS_CONFIG
55
- // This compiled, but threw an exception:
56
- // TypeError: responseType is not a constructor
57
- //
58
- // So using a switch to explicitly set the response class parameter. Ugly, but works.
59
- // Improvements welcome!
60
- let response;
61
- switch (command) {
62
- case COMMAND_INVOICE:
63
- response = yield doc.parse(api_1.InvoiceResponse, params);
64
- break;
65
- case COMMAND_RECEIPT:
66
- response = yield doc.parse(api_1.ReceiptResponse, params);
67
- break;
68
- case COMMAND_FINANCIAL:
69
- response = yield doc.parse(api_1.FinancialDocResponse, params);
70
- break;
71
- case COMMAND_PASSPORT:
72
- response = yield doc.parse(api_1.PassportResponse, params);
73
- break;
74
- case COMMAND_CUSTOM:
75
- response = yield doc.parse(api_1.CustomResponse, params);
76
- break;
77
- default:
78
- throw `Unhandled command: ${command}`;
79
- }
80
- if (response.document) {
81
- console.log(`\n${response.document}`);
82
- }
83
- });
84
- }
85
- function cli() {
86
- program.name("mindee");
87
- program.option("-d, --debug", "high verbosity mode");
88
- CLI_COMMAND_CONFIG.forEach((info, name) => {
89
- const prog = program.command(name);
90
- prog.description(info.description);
91
- prog.option("-k, --api-key <api_key>", "API key for document endpoint");
92
- prog.option("-C, --no-cut-pages", "Don't cut document pages");
93
- if (info.fullText) {
94
- prog.option("-t, --full-text", "Include full document text in response");
95
- }
96
- if (name === COMMAND_CUSTOM) {
97
- prog.requiredOption("-u, --user <username>", "API account name for the endpoint");
98
- prog.option("-v, --version <version>", "API account name for the endpoint");
99
- prog.argument("<endpoint_name>", "API endpoint name");
100
- }
101
- prog.argument("<input_path>", "Full path to the file");
102
- if (name === COMMAND_CUSTOM) {
103
- prog.action((endpointName, inputPath, options, command) => {
104
- const allOptions = Object.assign(Object.assign(Object.assign({}, program.opts()), options), { documentType: endpointName });
105
- predictCall(command.name(), inputPath, allOptions);
106
- });
107
- }
108
- else {
109
- prog.action((inputPath, options, command) => {
110
- const allOptions = Object.assign(Object.assign(Object.assign({}, program.opts()), options), { endpointName: undefined });
111
- predictCall(command.name(), inputPath, allOptions);
112
- });
113
- }
114
- });
115
- program.parse(process.argv);
116
- }
117
- exports.cli = cli;