mindee 2.1.1 → 3.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 (66) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +30 -9
  3. package/package.json +3 -2
  4. package/src/api/endpoint.d.ts +19 -17
  5. package/src/api/endpoint.js +27 -39
  6. package/src/api/index.d.ts +2 -2
  7. package/src/api/index.js +2 -11
  8. package/src/api/response.d.ts +8 -41
  9. package/src/api/response.js +18 -89
  10. package/src/cli.js +46 -12
  11. package/src/client.d.ts +47 -13
  12. package/src/client.js +73 -27
  13. package/src/documents/cropper/cropperV1.d.ts +7 -0
  14. package/src/documents/{cropper.js → cropper/cropperV1.js} +12 -8
  15. package/src/documents/custom.d.ts +2 -6
  16. package/src/documents/custom.js +12 -7
  17. package/src/documents/document.d.ts +8 -3
  18. package/src/documents/document.js +11 -5
  19. package/src/documents/documentConfig.d.ts +20 -34
  20. package/src/documents/documentConfig.js +33 -57
  21. package/src/documents/{financialDocument.d.ts → financialDocument/financialDocumentV1.d.ts} +4 -4
  22. package/src/documents/{financialDocument.js → financialDocument/financialDocumentV1.js} +28 -22
  23. package/src/documents/fr/idCard/idCardV1.d.ts +3 -0
  24. package/src/documents/fr/idCard/idCardV1.js +7 -0
  25. package/src/documents/index.d.ts +15 -13
  26. package/src/documents/index.js +21 -18
  27. package/src/documents/{invoice.d.ts → invoice/invoiceV3.d.ts} +4 -4
  28. package/src/documents/{invoice.js → invoice/invoiceV3.js} +30 -24
  29. package/src/documents/{passport.d.ts → passport/passportV1.d.ts} +4 -4
  30. package/src/documents/{passport.js → passport/passportV1.js} +19 -14
  31. package/src/documents/receipt/receiptV3.d.ts +17 -0
  32. package/src/documents/{receipt.js → receipt/receiptV3.js} +28 -22
  33. package/src/documents/receipt/receiptV4.d.ts +27 -0
  34. package/src/documents/receipt/receiptV4.js +96 -0
  35. package/src/documents/receipt/tax.d.ts +25 -0
  36. package/src/documents/receipt/tax.js +40 -0
  37. package/src/documents/us/bankCheck/bankCheckV1.d.ts +3 -0
  38. package/src/documents/us/bankCheck/bankCheckV1.js +7 -0
  39. package/src/errors/MindeeError.d.ts +3 -0
  40. package/src/errors/MindeeError.js +10 -0
  41. package/src/errors/index.d.ts +1 -0
  42. package/src/errors/index.js +5 -0
  43. package/src/fields/{customDocs.d.ts → apiBuilder.d.ts} +5 -1
  44. package/src/fields/{customDocs.js → apiBuilder.js} +0 -0
  45. package/src/fields/index.d.ts +1 -1
  46. package/src/fields/index.js +4 -4
  47. package/src/index.d.ts +2 -1
  48. package/src/index.js +11 -9
  49. package/src/inputs/base.d.ts +33 -0
  50. package/src/inputs/base.js +116 -0
  51. package/src/inputs/index.d.ts +3 -0
  52. package/src/inputs/index.js +18 -0
  53. package/src/inputs/pageOptions.d.ts +20 -0
  54. package/src/inputs/pageOptions.js +8 -0
  55. package/src/inputs/sources.d.ts +55 -0
  56. package/src/inputs/sources.js +121 -0
  57. package/src/pdf/index.d.ts +1 -0
  58. package/src/pdf/index.js +6 -0
  59. package/src/pdf/pdfOperation.d.ts +14 -0
  60. package/src/pdf/pdfOperation.js +69 -0
  61. package/src/constants.d.ts +0 -13
  62. package/src/constants.js +0 -60
  63. package/src/documents/cropper.d.ts +0 -7
  64. package/src/documents/receipt.d.ts +0 -17
  65. package/src/inputs.d.ts +0 -69
  66. package/src/inputs.js +0 -196
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v3.0.1 - 2022-11-02
4
+ ### Fixes
5
+ * :bug: Fix for `supplier` property name in `ReceiptV4` document.
6
+
7
+ ## v3.0.0 - 2022-10-31
8
+ ### ¡Breaking Changes!
9
+ * :sparkles: New PDF cut system, which allows specifying exactly which pages to keep or remove.
10
+ * :recycle: PDF documents are no longer cut by default, use the `pageOptions` parameter in the `parse` method.
11
+ * :sparkles: Document (endpoints) are now versioned, providing better backward-compatible support.
12
+ * :recycle: Pass the document class instead of the response class to the `parse` method.
13
+ * :recycle: Some methods and parameters renamed for better clarity.
14
+
15
+ ### Changes
16
+ * :sparkles: New URL input source, `docFromUrl`.
17
+ * :sparkles: Add support for expense receipts V4
18
+
3
19
  ## v2.1.1 - 2022-10-28
4
20
  ### Fixes
5
21
  * :bug: fix for sending base64 documents.
package/README.md CHANGED
@@ -24,12 +24,25 @@ const mindee = require("mindee");
24
24
  const mindeeClient = new mindee.Client({apiKey: "my-api-key"});
25
25
 
26
26
  // Load a file from disk and parse it
27
- const invoiceResponse = mindeeClient.docFromPath("/path/to/the/invoice.pdf")
28
- .parse(mindee.InvoiceResponse);
27
+ const invoiceResponse = mindeeClient
28
+ .docFromPath("/path/to/the/invoice.pdf")
29
+ .parse(mindee.InvoiceV3);
29
30
 
30
31
  // Print a brief summary of the parsed data
31
32
  invoiceResponse.then((resp) => {
33
+
34
+ // The document property can be undefined:
35
+ // * TypeScript will throw an error without this guard clause
36
+ // (or consider using the '?' notation)
37
+ // * JavaScript will be very happy to produce subtle bugs
38
+ // without this guard clause
39
+ if (resp.document === undefined) return;
40
+
41
+ // full object
32
42
  console.log(resp.document);
43
+
44
+ // string summary
45
+ console.log(resp.document.toString());
33
46
  });
34
47
  ```
35
48
 
@@ -42,18 +55,26 @@ const mindee = require("mindee");
42
55
 
43
56
  // Init a new client and add your document endpoint
44
57
  const mindeeClient = new mindee.Client({apiKey: "my-api-key"})
45
- .addEndpoint({
46
- accountName: "john",
47
- documentType: "wsnine",
48
- });
58
+ .addEndpoint({
59
+ accountName: "john",
60
+ endpointName: "wsnine",
61
+ });
49
62
 
50
63
  // Load a file from disk and parse it
51
- const customResponse = mindeeClient.docFromPath("/path/to/the/wsnine.jpg")
52
- .parse(mindee.CustomResponse, {docType: "wsnine"});
64
+ const customResponse = mindeeClient
65
+ .docFromPath("/path/to/the/wsnine.jpg")
66
+ .parse(mindee.CustomV1, { endpointName: "wsnine" });
53
67
 
54
68
  // Print a brief summary of the parsed data
55
69
  customResponse.then((resp) => {
56
- console.log(resp.document);
70
+
71
+ if (resp.document === undefined) return;
72
+
73
+ // full object
74
+ console.log(resp.document);
75
+
76
+ // string summary
77
+ console.log(resp.document.toString());
57
78
  });
58
79
  ```
59
80
 
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "mindee",
3
- "version": "2.1.1",
3
+ "version": "3.0.1",
4
4
  "description": "Mindee Client Library for Node.js",
5
5
  "main": "src/index.js",
6
6
  "bin": "bin/mindee.js",
7
7
  "license": "MIT",
8
8
  "scripts": {
9
- "build": "tsc --build && cp LICENSE README.md CHANGELOG.md ./dist",
9
+ "build": "tsc --build",
10
+ "build-for-publish": "tsc --build && cp LICENSE README.md CHANGELOG.md ./dist",
10
11
  "clean": "rm -rf ./dist",
11
12
  "test": "mocha 'tests/**/*.spec.ts'",
12
13
  "lint-fix": "eslint '**/*.ts' --fix",
@@ -1,32 +1,34 @@
1
- import { Input } from "../inputs";
1
+ /// <reference types="node" />
2
+ import { InputSource } from "../inputs";
3
+ import { IncomingMessage } from "http";
2
4
  export declare const STANDARD_API_OWNER = "mindee";
3
5
  export declare const API_KEY_ENVVAR_NAME = "MINDEE_API_KEY";
6
+ export interface predictResponse {
7
+ messageObj: IncomingMessage;
8
+ data: {
9
+ [key: string]: any;
10
+ };
11
+ }
4
12
  export declare class Endpoint {
5
13
  apiKey: string;
6
14
  urlName: string;
7
- keyName: string;
8
15
  owner: string;
9
16
  version: string;
10
17
  urlRoot: string;
11
- constructor(owner: string, urlName: string, version: string, apiKey: string, keyName?: string);
18
+ private readonly baseHeaders;
19
+ constructor(owner: string, urlName: string, version: string, apiKey: string);
12
20
  /**
13
- * Make a prediction request.
21
+ * Make a request to POST a document for prediction.
22
+ * @param input
23
+ * @param includeWords
24
+ * @param cropper
14
25
  */
15
- predictRequest(input: Input, includeWords?: boolean, cropper?: boolean): Promise<unknown>;
26
+ predictReqPost(input: InputSource, includeWords?: boolean, cropper?: boolean): Promise<predictResponse>;
16
27
  protected apiKeyFromEnv(): string;
17
28
  }
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 CropperEndpoint extends Endpoint {
28
- constructor(apiKey: string);
29
+ export declare class StandardEndpoint extends Endpoint {
30
+ constructor(endpointName: string, version: string, apiKey: string);
29
31
  }
30
32
  export declare class CustomEndpoint extends Endpoint {
31
- constructor(documentType: string, accountName: string, version: string, apiKey: string);
33
+ constructor(endpointName: string, accountName: string, version: string, apiKey: string);
32
34
  }
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.CustomEndpoint = exports.CropperEndpoint = exports.PassportEndpoint = exports.ReceiptEndpoint = exports.InvoiceEndpoint = exports.Endpoint = exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = void 0;
29
+ exports.CustomEndpoint = exports.StandardEndpoint = exports.Endpoint = exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = void 0;
30
30
  const https = __importStar(require("https"));
31
31
  const os = __importStar(require("os"));
32
32
  const package_json_1 = require("../../package.json");
@@ -38,35 +38,41 @@ const USER_AGENT = `mindee-api-nodejs@v${package_json_1.version} nodejs-${proces
38
38
  exports.STANDARD_API_OWNER = "mindee";
39
39
  exports.API_KEY_ENVVAR_NAME = "MINDEE_API_KEY";
40
40
  class Endpoint {
41
- constructor(owner, urlName, version, apiKey, keyName) {
41
+ constructor(owner, urlName, version, apiKey) {
42
42
  this.owner = owner;
43
43
  this.urlName = urlName;
44
44
  this.version = version;
45
- this.keyName = keyName || urlName;
46
45
  this.apiKey = apiKey || this.apiKeyFromEnv();
47
46
  this.urlRoot = `${MINDEE_API_URL}/products/${owner}/${urlName}/v${version}`;
47
+ this.baseHeaders = {
48
+ "User-Agent": USER_AGENT,
49
+ Authorization: `Token ${this.apiKey}`,
50
+ };
48
51
  }
49
52
  /**
50
- * Make a prediction request.
53
+ * Make a request to POST a document for prediction.
54
+ * @param input
55
+ * @param includeWords
56
+ * @param cropper
51
57
  */
52
- predictRequest(input, includeWords = false, cropper = false) {
58
+ predictReqPost(input, includeWords = false, cropper = false) {
53
59
  return new Promise((resolve, reject) => {
54
- let headers = {
55
- "User-Agent": USER_AGENT,
56
- Authorization: `Token ${this.apiKey}`,
57
- };
58
60
  const uri = new url_1.URL(`${this.urlRoot}/predict`);
59
61
  if (cropper) {
60
62
  uri.searchParams.append("cropper", "true");
61
63
  }
62
- logger_1.logger.debug(`Prediction request: ${uri}`);
64
+ logger_1.logger.debug(`Request URI: ${uri}`);
63
65
  const form = new form_data_1.default();
64
- const fileParams = { filename: input.filename };
65
- form.append("document", input.fileObject, fileParams);
66
+ if (input.fileObject instanceof Buffer) {
67
+ form.append("document", input.fileObject, { filename: input.filename });
68
+ }
69
+ else {
70
+ form.append("document", input.fileObject);
71
+ }
66
72
  if (includeWords) {
67
73
  form.append("include_mvision", "true");
68
74
  }
69
- headers = { ...headers, ...form.getHeaders() };
75
+ const headers = { ...this.baseHeaders, ...form.getHeaders() };
70
76
  const options = {
71
77
  method: "POST",
72
78
  headers: headers,
@@ -83,7 +89,7 @@ class Endpoint {
83
89
  res.on("end", function () {
84
90
  try {
85
91
  resolve({
86
- ...res,
92
+ messageObj: res,
87
93
  data: JSON.parse(responseBody),
88
94
  });
89
95
  }
@@ -101,40 +107,22 @@ class Endpoint {
101
107
  apiKeyFromEnv() {
102
108
  const envVarValue = process.env[exports.API_KEY_ENVVAR_NAME];
103
109
  if (envVarValue) {
104
- logger_1.logger.debug(`Set '${this.keyName}' API key from environment: ${exports.API_KEY_ENVVAR_NAME}`);
110
+ logger_1.logger.debug(`Set ${this.urlName} v${this.version} API key from environment: ${exports.API_KEY_ENVVAR_NAME}`);
105
111
  return envVarValue;
106
112
  }
107
113
  return "";
108
114
  }
109
115
  }
110
116
  exports.Endpoint = Endpoint;
111
- class InvoiceEndpoint extends Endpoint {
112
- constructor(apiKey) {
113
- super(exports.STANDARD_API_OWNER, "invoices", "3", apiKey, "invoice");
114
- }
115
- }
116
- exports.InvoiceEndpoint = InvoiceEndpoint;
117
- class ReceiptEndpoint extends Endpoint {
118
- constructor(apiKey) {
119
- super(exports.STANDARD_API_OWNER, "expense_receipts", "3", apiKey, "receipt");
120
- }
121
- }
122
- exports.ReceiptEndpoint = ReceiptEndpoint;
123
- class PassportEndpoint extends Endpoint {
124
- constructor(apiKey) {
125
- super(exports.STANDARD_API_OWNER, "passport", "1", apiKey);
126
- }
127
- }
128
- exports.PassportEndpoint = PassportEndpoint;
129
- class CropperEndpoint extends Endpoint {
130
- constructor(apiKey) {
131
- super(exports.STANDARD_API_OWNER, "cropper", "1", apiKey);
117
+ class StandardEndpoint extends Endpoint {
118
+ constructor(endpointName, version, apiKey) {
119
+ super(exports.STANDARD_API_OWNER, endpointName, version, apiKey);
132
120
  }
133
121
  }
134
- exports.CropperEndpoint = CropperEndpoint;
122
+ exports.StandardEndpoint = StandardEndpoint;
135
123
  class CustomEndpoint extends Endpoint {
136
- constructor(documentType, accountName, version, apiKey) {
137
- super(accountName, documentType, version, apiKey);
124
+ constructor(endpointName, accountName, version, apiKey) {
125
+ super(accountName, endpointName, version, apiKey);
138
126
  }
139
127
  }
140
128
  exports.CustomEndpoint = CustomEndpoint;
@@ -1,2 +1,2 @@
1
- export { Response, InvoiceResponse, ReceiptResponse, FinancialDocResponse, PassportResponse, CustomResponse, CropperResponse, } from "./response";
2
- export { Endpoint, InvoiceEndpoint, ReceiptEndpoint, PassportEndpoint, CustomEndpoint, CropperEndpoint, STANDARD_API_OWNER, API_KEY_ENVVAR_NAME, } from "./endpoint";
1
+ export { Response } from "./response";
2
+ export { Endpoint, CustomEndpoint, StandardEndpoint, predictResponse, STANDARD_API_OWNER, API_KEY_ENVVAR_NAME, } from "./endpoint";
package/src/api/index.js CHANGED
@@ -1,20 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = exports.CropperEndpoint = exports.CustomEndpoint = exports.PassportEndpoint = exports.ReceiptEndpoint = exports.InvoiceEndpoint = exports.Endpoint = exports.CropperResponse = exports.CustomResponse = exports.PassportResponse = exports.FinancialDocResponse = exports.ReceiptResponse = exports.InvoiceResponse = exports.Response = void 0;
3
+ exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = exports.StandardEndpoint = exports.CustomEndpoint = exports.Endpoint = exports.Response = void 0;
4
4
  var response_1 = require("./response");
5
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
- Object.defineProperty(exports, "CropperResponse", { enumerable: true, get: function () { return response_1.CropperResponse; } });
12
6
  var endpoint_1 = require("./endpoint");
13
7
  Object.defineProperty(exports, "Endpoint", { enumerable: true, get: function () { return endpoint_1.Endpoint; } });
14
- Object.defineProperty(exports, "InvoiceEndpoint", { enumerable: true, get: function () { return endpoint_1.InvoiceEndpoint; } });
15
- Object.defineProperty(exports, "ReceiptEndpoint", { enumerable: true, get: function () { return endpoint_1.ReceiptEndpoint; } });
16
- Object.defineProperty(exports, "PassportEndpoint", { enumerable: true, get: function () { return endpoint_1.PassportEndpoint; } });
17
8
  Object.defineProperty(exports, "CustomEndpoint", { enumerable: true, get: function () { return endpoint_1.CustomEndpoint; } });
18
- Object.defineProperty(exports, "CropperEndpoint", { enumerable: true, get: function () { return endpoint_1.CropperEndpoint; } });
9
+ Object.defineProperty(exports, "StandardEndpoint", { enumerable: true, get: function () { return endpoint_1.StandardEndpoint; } });
19
10
  Object.defineProperty(exports, "STANDARD_API_OWNER", { enumerable: true, get: function () { return endpoint_1.STANDARD_API_OWNER; } });
20
11
  Object.defineProperty(exports, "API_KEY_ENVVAR_NAME", { enumerable: true, get: function () { return endpoint_1.API_KEY_ENVVAR_NAME; } });
@@ -1,10 +1,10 @@
1
- import { Document, Passport, Receipt, Invoice, FinancialDocument, CustomDocument, DocumentConstructorProps, Cropper } from "../documents";
1
+ import { Document, DocumentSig } from "../documents";
2
2
  import { FullText } from "../fields";
3
- import { Input } from "../inputs";
3
+ import { InputSource } from "../inputs";
4
4
  export interface ResponseProps {
5
5
  httpResponse: any;
6
- documentType: string;
7
- input: Input;
6
+ documentType?: string;
7
+ input: InputSource;
8
8
  error: boolean;
9
9
  }
10
10
  /**
@@ -12,44 +12,11 @@ export interface ResponseProps {
12
12
  */
13
13
  export declare class Response<DocType extends Document> {
14
14
  httpResponse: any;
15
- readonly documentType: string;
16
- inputFile: Input;
15
+ inputFile: InputSource;
17
16
  document?: DocType;
18
17
  pages: Array<DocType>;
19
- constructor(params: ResponseProps);
18
+ readonly documentClass: DocumentSig<DocType>;
19
+ constructor(documentClass: DocumentSig<DocType>, params: ResponseProps);
20
+ protected formatResponse(documentType?: string): void;
20
21
  protected getPageText(httpDataDocument: any, pageId: number): FullText;
21
22
  }
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 ({ prediction, 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 declare class CropperResponse extends StandardProductResponse<Cropper> {
53
- constructor(params: ResponseProps);
54
- }
55
- export {};
@@ -1,79 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CropperResponse = exports.PassportResponse = exports.FinancialDocResponse = exports.ReceiptResponse = exports.InvoiceResponse = exports.StandardProductResponse = exports.CustomResponse = exports.Response = void 0;
4
- const documents_1 = require("../documents");
3
+ exports.Response = void 0;
5
4
  const fields_1 = require("../fields");
6
5
  /**
7
6
  * Base class for all responses.
8
7
  */
9
8
  class Response {
10
- constructor(params) {
9
+ constructor(documentClass, params) {
11
10
  this.pages = [];
11
+ this.documentClass = documentClass;
12
12
  this.httpResponse = params.httpResponse;
13
- this.documentType = params.documentType;
14
13
  this.inputFile = params.input;
15
- }
16
- getPageText(httpDataDocument, pageId) {
17
- const pageText = new fields_1.FullText();
18
- if ("ocr" in httpDataDocument &&
19
- Object.keys(httpDataDocument.ocr).length > 0) {
20
- pageText.words =
21
- httpDataDocument.ocr["mvision-v1"].pages[pageId].all_words;
22
- }
23
- return pageText;
24
- }
25
- }
26
- exports.Response = Response;
27
- /**
28
- * Class for all constructed (API Builder) endpoint responses.
29
- */
30
- class CustomResponse extends Response {
31
- constructor(params) {
32
- super(params);
33
14
  if (!params.error) {
34
- this.formatResponse();
15
+ this.formatResponse(params.documentType);
35
16
  }
36
17
  }
37
- formatResponse() {
38
- const httpDataDocument = this.httpResponse.data.document;
39
- httpDataDocument.inference.pages.forEach((apiPage) => {
40
- this.pages.push(new documents_1.CustomDocument({
41
- prediction: apiPage.prediction,
42
- orientation: apiPage.orientation,
43
- extras: apiPage.extras,
44
- inputFile: this.inputFile,
45
- pageId: apiPage.id,
46
- documentType: this.documentType,
47
- }));
48
- });
49
- this.document = new documents_1.CustomDocument({
50
- prediction: httpDataDocument.inference.prediction,
51
- inputFile: this.inputFile,
52
- documentType: this.documentType,
53
- orientation: {},
54
- extras: {},
55
- });
56
- }
57
- }
58
- exports.CustomResponse = CustomResponse;
59
- /**
60
- * Generic class for all standard (Off-the-Shelf) endpoint responses.
61
- */
62
- class StandardProductResponse extends Response {
63
- constructor(documentClass, params) {
64
- super(params);
65
- this.documentClass = documentClass;
66
- if (!params.error) {
67
- this.formatResponse();
68
- }
69
- }
70
- formatResponse() {
18
+ formatResponse(documentType) {
71
19
  const httpDataDocument = this.httpResponse.data.document;
72
20
  httpDataDocument.inference.pages.forEach((apiPage) => {
73
21
  const pageText = this.getPageText(httpDataDocument, apiPage.id);
74
22
  this.pages.push(new this.documentClass({
23
+ documentType: documentType,
75
24
  prediction: apiPage.prediction,
76
- inputFile: this.inputFile,
25
+ inputSource: this.inputFile,
77
26
  pageId: apiPage.id,
78
27
  orientation: apiPage.orientation,
79
28
  extras: apiPage.extras,
@@ -81,41 +30,21 @@ class StandardProductResponse extends Response {
81
30
  }));
82
31
  });
83
32
  this.document = new this.documentClass({
33
+ documentType: documentType,
84
34
  prediction: httpDataDocument.inference.prediction,
85
- inputFile: this.inputFile,
35
+ inputSource: this.inputFile,
86
36
  orientation: {},
87
37
  extras: {},
88
38
  });
89
39
  }
90
- }
91
- exports.StandardProductResponse = StandardProductResponse;
92
- class InvoiceResponse extends StandardProductResponse {
93
- constructor(params) {
94
- super(documents_1.Invoice, params);
95
- }
96
- }
97
- exports.InvoiceResponse = InvoiceResponse;
98
- class ReceiptResponse extends StandardProductResponse {
99
- constructor(params) {
100
- super(documents_1.Receipt, params);
101
- }
102
- }
103
- exports.ReceiptResponse = ReceiptResponse;
104
- class FinancialDocResponse extends StandardProductResponse {
105
- constructor(params) {
106
- super(documents_1.FinancialDocument, params);
107
- }
108
- }
109
- exports.FinancialDocResponse = FinancialDocResponse;
110
- class PassportResponse extends StandardProductResponse {
111
- constructor(params) {
112
- super(documents_1.Passport, params);
113
- }
114
- }
115
- exports.PassportResponse = PassportResponse;
116
- class CropperResponse extends StandardProductResponse {
117
- constructor(params) {
118
- super(documents_1.Cropper, params);
40
+ getPageText(httpDataDocument, pageId) {
41
+ const pageText = new fields_1.FullText();
42
+ if ("ocr" in httpDataDocument &&
43
+ Object.keys(httpDataDocument.ocr).length > 0) {
44
+ pageText.words =
45
+ httpDataDocument.ocr["mvision-v1"].pages[pageId].all_words;
46
+ }
47
+ return pageText;
119
48
  }
120
49
  }
121
- exports.CropperResponse = CropperResponse;
50
+ exports.Response = Response;
package/src/cli.js CHANGED
@@ -5,7 +5,6 @@ const commander_1 = require("commander");
5
5
  const documents_1 = require("./documents");
6
6
  const api_1 = require("./api");
7
7
  const client_1 = require("./client");
8
- const constants_1 = require("./constants");
9
8
  const program = new commander_1.Command();
10
9
  const COMMAND_INVOICE = "invoice";
11
10
  const COMMAND_RECEIPT = "receipt";
@@ -13,11 +12,46 @@ const COMMAND_PASSPORT = "passport";
13
12
  const COMMAND_FINANCIAL = "financial";
14
13
  const COMMAND_CUSTOM = "custom";
15
14
  const CLI_COMMAND_CONFIG = new Map([
16
- [COMMAND_INVOICE, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_INVOICE)],
17
- [COMMAND_RECEIPT, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_RECEIPT)],
18
- [COMMAND_PASSPORT, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_PASSPORT)],
19
- [COMMAND_FINANCIAL, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_FINANCIAL)],
20
- [COMMAND_CUSTOM, constants_1.ProductConfigs.getByDocType(documents_1.DOC_TYPE_CUSTOM)],
15
+ [
16
+ COMMAND_INVOICE,
17
+ {
18
+ description: "Invoice V3",
19
+ docType: documents_1.DOC_TYPE_INVOICE_V3,
20
+ fullText: true,
21
+ },
22
+ ],
23
+ [
24
+ COMMAND_RECEIPT,
25
+ {
26
+ description: "Expense Receipt V4",
27
+ docType: documents_1.DOC_TYPE_RECEIPT_V4,
28
+ fullText: true,
29
+ },
30
+ ],
31
+ [
32
+ COMMAND_PASSPORT,
33
+ {
34
+ description: "Passport V1",
35
+ docType: documents_1.DOC_TYPE_PASSPORT_V1,
36
+ fullText: false,
37
+ },
38
+ ],
39
+ [
40
+ COMMAND_FINANCIAL,
41
+ {
42
+ description: "Financial Document V1 (receipt or invoice)",
43
+ docType: documents_1.DOC_TYPE_FINANCIAL_V1,
44
+ fullText: true,
45
+ },
46
+ ],
47
+ [
48
+ COMMAND_CUSTOM,
49
+ {
50
+ description: "A custom document",
51
+ docType: documents_1.DOC_TYPE_CUSTOM,
52
+ fullText: false,
53
+ },
54
+ ],
21
55
  ]);
22
56
  async function predictCall(command, inputPath, options) {
23
57
  const conf = CLI_COMMAND_CONFIG.get(command);
@@ -31,7 +65,7 @@ async function predictCall(command, inputPath, options) {
31
65
  if (command === COMMAND_CUSTOM) {
32
66
  mindeeClient.addEndpoint({
33
67
  accountName: options.user,
34
- documentType: options.documentType,
68
+ endpointName: options.documentType,
35
69
  });
36
70
  }
37
71
  const doc = mindeeClient.docFromPath(inputPath);
@@ -50,19 +84,19 @@ async function predictCall(command, inputPath, options) {
50
84
  let response;
51
85
  switch (command) {
52
86
  case COMMAND_INVOICE:
53
- response = await doc.parse(api_1.InvoiceResponse, predictParams);
87
+ response = await doc.parse(documents_1.InvoiceV3, predictParams);
54
88
  break;
55
89
  case COMMAND_RECEIPT:
56
- response = await doc.parse(api_1.ReceiptResponse, predictParams);
90
+ response = await doc.parse(documents_1.ReceiptV4, predictParams);
57
91
  break;
58
92
  case COMMAND_FINANCIAL:
59
- response = await doc.parse(api_1.FinancialDocResponse, predictParams);
93
+ response = await doc.parse(documents_1.FinancialDocumentV1, predictParams);
60
94
  break;
61
95
  case COMMAND_PASSPORT:
62
- response = await doc.parse(api_1.PassportResponse, predictParams);
96
+ response = await doc.parse(documents_1.PassportV1, predictParams);
63
97
  break;
64
98
  case COMMAND_CUSTOM:
65
- response = await doc.parse(api_1.CustomResponse, predictParams);
99
+ response = await doc.parse(documents_1.CustomV1, predictParams);
66
100
  break;
67
101
  default:
68
102
  throw `Unhandled command: ${command}`;