mindee 1.4.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 (89) hide show
  1. package/CHANGELOG.md +37 -19
  2. package/README.md +46 -126
  3. package/bin/mindee.d.ts +2 -0
  4. package/bin/mindee.js +5 -0
  5. package/package.json +68 -58
  6. package/src/api/endpoint.d.ts +29 -0
  7. package/src/api/endpoint.js +151 -0
  8. package/src/api/index.d.ts +2 -0
  9. package/src/api/index.js +18 -0
  10. package/src/api/response.d.ts +52 -0
  11. package/src/api/response.js +107 -0
  12. package/src/cli.d.ts +1 -0
  13. package/src/cli.js +114 -0
  14. package/src/client.d.ts +48 -0
  15. package/src/client.js +123 -0
  16. package/src/constants.d.ts +13 -0
  17. package/src/constants.js +54 -0
  18. package/src/documents/custom.d.ts +14 -0
  19. package/src/documents/custom.js +48 -0
  20. package/src/documents/document.d.ts +38 -0
  21. package/src/documents/document.js +59 -0
  22. package/src/documents/documentConfig.d.ts +44 -0
  23. package/src/documents/documentConfig.js +104 -0
  24. package/src/documents/financialDocument.d.ts +32 -0
  25. package/src/documents/financialDocument.js +133 -0
  26. package/src/documents/index.d.ts +12 -0
  27. package/src/documents/index.js +20 -0
  28. package/src/documents/invoice.d.ts +37 -0
  29. package/src/documents/invoice.js +303 -0
  30. package/src/documents/passport.d.ts +36 -0
  31. package/src/documents/passport.js +218 -0
  32. package/src/documents/receipt.d.ts +24 -0
  33. package/src/documents/receipt.js +157 -0
  34. package/src/errors/handler.d.ts +7 -0
  35. package/src/errors/handler.js +18 -0
  36. package/src/fields/amount.d.ts +13 -0
  37. package/src/fields/amount.js +34 -0
  38. package/src/fields/companyRegistration.d.ts +6 -0
  39. package/src/fields/companyRegistration.js +11 -0
  40. package/src/fields/customDocs.d.ts +42 -0
  41. package/src/fields/customDocs.js +61 -0
  42. package/src/fields/date.d.ts +12 -0
  43. package/src/fields/date.js +25 -0
  44. package/src/fields/field.d.ts +74 -0
  45. package/src/fields/field.js +106 -0
  46. package/src/fields/fullText.d.ts +24 -0
  47. package/src/fields/fullText.js +89 -0
  48. package/src/fields/index.d.ts +11 -0
  49. package/src/fields/index.js +27 -0
  50. package/src/fields/locale.d.ts +13 -0
  51. package/src/fields/locale.js +40 -0
  52. package/src/fields/orientation.d.ts +10 -0
  53. package/src/fields/orientation.js +22 -0
  54. package/src/fields/paymentDetails.d.ts +31 -0
  55. package/src/fields/paymentDetails.js +58 -0
  56. package/src/fields/tax.d.ts +30 -0
  57. package/src/fields/tax.js +50 -0
  58. package/src/geometry.d.ts +59 -0
  59. package/src/geometry.js +113 -0
  60. package/src/index.d.ts +2 -0
  61. package/src/index.js +12 -0
  62. package/src/inputs.d.ts +69 -0
  63. package/src/inputs.js +196 -0
  64. package/src/logger.d.ts +20 -0
  65. package/src/logger.js +35 -0
  66. package/mindee/api/financialDocument.js +0 -47
  67. package/mindee/api/index.js +0 -5
  68. package/mindee/api/invoice.js +0 -53
  69. package/mindee/api/object.js +0 -82
  70. package/mindee/api/receipt.js +0 -39
  71. package/mindee/api/request.js +0 -72
  72. package/mindee/api/response.js +0 -80
  73. package/mindee/documents/document.js +0 -69
  74. package/mindee/documents/fields/amount.js +0 -25
  75. package/mindee/documents/fields/date.js +0 -29
  76. package/mindee/documents/fields/field.js +0 -86
  77. package/mindee/documents/fields/index.js +0 -7
  78. package/mindee/documents/fields/locale.js +0 -30
  79. package/mindee/documents/fields/orientation.js +0 -24
  80. package/mindee/documents/fields/paymentDetails.js +0 -52
  81. package/mindee/documents/fields/tax.js +0 -47
  82. package/mindee/documents/financialDocument.js +0 -269
  83. package/mindee/documents/index.js +0 -5
  84. package/mindee/documents/invoice.js +0 -456
  85. package/mindee/documents/receipt.js +0 -287
  86. package/mindee/errors/handler.js +0 -16
  87. package/mindee/index.js +0 -39
  88. package/mindee/inputs.js +0 -181
  89. package/mindee/logger.js +0 -34
@@ -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 {};
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PassportResponse = exports.FinancialDocResponse = exports.ReceiptResponse = exports.InvoiceResponse = exports.StandardProductResponse = exports.CustomResponse = exports.Response = void 0;
4
+ const documents_1 = require("../documents");
5
+ const fields_1 = require("../fields");
6
+ /**
7
+ * Base class for all responses.
8
+ */
9
+ class Response {
10
+ constructor(params) {
11
+ this.pages = [];
12
+ this.httpResponse = params.httpResponse;
13
+ this.documentType = params.documentType;
14
+ 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
+ if (!params.error) {
34
+ this.formatResponse();
35
+ }
36
+ }
37
+ formatResponse() {
38
+ const httpDataDocument = this.httpResponse.data.document;
39
+ httpDataDocument.inference.pages.forEach((apiPage) => {
40
+ this.pages.push(new documents_1.CustomDocument({
41
+ apiPrediction: apiPage.prediction,
42
+ inputFile: this.inputFile,
43
+ pageId: apiPage.id,
44
+ documentType: this.documentType,
45
+ }));
46
+ });
47
+ this.document = new documents_1.CustomDocument({
48
+ apiPrediction: httpDataDocument.inference.prediction,
49
+ inputFile: this.inputFile,
50
+ documentType: this.documentType,
51
+ });
52
+ }
53
+ }
54
+ exports.CustomResponse = CustomResponse;
55
+ /**
56
+ * Generic class for all standard (Off-the-Shelf) endpoint responses.
57
+ */
58
+ class StandardProductResponse extends Response {
59
+ constructor(documentClass, params) {
60
+ super(params);
61
+ this.documentClass = documentClass;
62
+ if (!params.error) {
63
+ this.formatResponse();
64
+ }
65
+ }
66
+ formatResponse() {
67
+ const httpDataDocument = this.httpResponse.data.document;
68
+ httpDataDocument.inference.pages.forEach((apiPage) => {
69
+ const pageText = this.getPageText(httpDataDocument, apiPage.id);
70
+ this.pages.push(new this.documentClass({
71
+ apiPrediction: apiPage.prediction,
72
+ inputFile: this.inputFile,
73
+ pageId: apiPage.id,
74
+ fullText: pageText,
75
+ }));
76
+ });
77
+ this.document = new this.documentClass({
78
+ apiPrediction: httpDataDocument.inference.prediction,
79
+ inputFile: this.inputFile,
80
+ });
81
+ }
82
+ }
83
+ exports.StandardProductResponse = StandardProductResponse;
84
+ class InvoiceResponse extends StandardProductResponse {
85
+ constructor(params) {
86
+ super(documents_1.Invoice, params);
87
+ }
88
+ }
89
+ exports.InvoiceResponse = InvoiceResponse;
90
+ class ReceiptResponse extends StandardProductResponse {
91
+ constructor(params) {
92
+ super(documents_1.Receipt, params);
93
+ }
94
+ }
95
+ exports.ReceiptResponse = ReceiptResponse;
96
+ class FinancialDocResponse extends StandardProductResponse {
97
+ constructor(params) {
98
+ super(documents_1.FinancialDocument, params);
99
+ }
100
+ }
101
+ exports.FinancialDocResponse = FinancialDocResponse;
102
+ class PassportResponse extends StandardProductResponse {
103
+ constructor(params) {
104
+ super(documents_1.Passport, params);
105
+ }
106
+ }
107
+ exports.PassportResponse = PassportResponse;
package/src/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function cli(): void;
package/src/cli.js ADDED
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cli = void 0;
4
+ const commander_1 = require("commander");
5
+ const documents_1 = require("./documents");
6
+ const api_1 = require("./api");
7
+ const client_1 = require("./client");
8
+ const constants_1 = require("./constants");
9
+ const program = new commander_1.Command();
10
+ const COMMAND_INVOICE = "invoice";
11
+ const COMMAND_RECEIPT = "receipt";
12
+ const COMMAND_PASSPORT = "passport";
13
+ const COMMAND_FINANCIAL = "financial";
14
+ const COMMAND_CUSTOM = "custom";
15
+ 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)],
21
+ ]);
22
+ async function predictCall(command, inputPath, options) {
23
+ const conf = CLI_COMMAND_CONFIG.get(command);
24
+ if (conf === undefined) {
25
+ throw new Error(`Invalid document type ${command}`);
26
+ }
27
+ const mindeeClient = new client_1.Client({
28
+ apiKey: options.apiKey,
29
+ debug: options.debug,
30
+ });
31
+ if (command === COMMAND_CUSTOM) {
32
+ mindeeClient.addEndpoint({
33
+ accountName: options.user,
34
+ documentType: options.documentType,
35
+ });
36
+ }
37
+ const doc = mindeeClient.docFromPath(inputPath);
38
+ const params = {
39
+ docType: command === COMMAND_CUSTOM ? options.documentType : conf.docType,
40
+ username: command === COMMAND_CUSTOM ? options.user : api_1.STANDARD_API_OWNER,
41
+ cutPages: options.cutPages,
42
+ fullText: options.fullText,
43
+ };
44
+ // Tried setting the response by using the responseClass property in constants.PRODUCTS_CONFIG
45
+ // This compiled, but threw an exception:
46
+ // TypeError: responseType is not a constructor
47
+ //
48
+ // So using a switch to explicitly set the response class parameter. Ugly, but works.
49
+ // Improvements welcome!
50
+ let response;
51
+ switch (command) {
52
+ case COMMAND_INVOICE:
53
+ response = await doc.parse(api_1.InvoiceResponse, params);
54
+ break;
55
+ case COMMAND_RECEIPT:
56
+ response = await doc.parse(api_1.ReceiptResponse, params);
57
+ break;
58
+ case COMMAND_FINANCIAL:
59
+ response = await doc.parse(api_1.FinancialDocResponse, params);
60
+ break;
61
+ case COMMAND_PASSPORT:
62
+ response = await doc.parse(api_1.PassportResponse, params);
63
+ break;
64
+ case COMMAND_CUSTOM:
65
+ response = await doc.parse(api_1.CustomResponse, params);
66
+ break;
67
+ default:
68
+ throw `Unhandled command: ${command}`;
69
+ }
70
+ if (response.document) {
71
+ console.log(`\n${response.document}`);
72
+ }
73
+ }
74
+ function cli() {
75
+ program.name("mindee");
76
+ program.option("-d, --debug", "high verbosity mode");
77
+ CLI_COMMAND_CONFIG.forEach((info, name) => {
78
+ const prog = program.command(name);
79
+ prog.description(info.description);
80
+ prog.option("-k, --api-key <api_key>", "API key for document endpoint");
81
+ prog.option("-C, --no-cut-pages", "Don't cut document pages");
82
+ if (info.fullText) {
83
+ prog.option("-t, --full-text", "Include full document text in response");
84
+ }
85
+ if (name === COMMAND_CUSTOM) {
86
+ prog.requiredOption("-u, --user <username>", "API account name for the endpoint");
87
+ prog.option("-v, --version <version>", "API account name for the endpoint");
88
+ prog.argument("<endpoint_name>", "API endpoint name");
89
+ }
90
+ prog.argument("<input_path>", "Full path to the file");
91
+ if (name === COMMAND_CUSTOM) {
92
+ prog.action((endpointName, inputPath, options, command) => {
93
+ const allOptions = {
94
+ ...program.opts(),
95
+ ...options,
96
+ documentType: endpointName,
97
+ };
98
+ predictCall(command.name(), inputPath, allOptions);
99
+ });
100
+ }
101
+ else {
102
+ prog.action((inputPath, options, command) => {
103
+ const allOptions = {
104
+ ...program.opts(),
105
+ ...options,
106
+ endpointName: undefined,
107
+ };
108
+ predictCall(command.name(), inputPath, allOptions);
109
+ });
110
+ }
111
+ });
112
+ program.parse(process.argv);
113
+ }
114
+ exports.cli = cli;
@@ -0,0 +1,48 @@
1
+ /// <reference types="node" />
2
+ import { Input } from "./inputs";
3
+ import { Response } from "./api";
4
+ import { Document } from "./documents";
5
+ import { DocumentConfig, responseSig } from "./documents/documentConfig";
6
+ import { ReadStream } from "fs";
7
+ declare type DocConfigs = Map<string[], DocumentConfig>;
8
+ interface PredictOptions {
9
+ docType?: string;
10
+ username?: string;
11
+ cutPages?: boolean;
12
+ fullText?: boolean;
13
+ }
14
+ declare class DocumentClient {
15
+ inputDoc: Input;
16
+ docConfigs: DocConfigs;
17
+ constructor(inputDoc: Input, docConfigs: DocConfigs);
18
+ parse<RespType extends Response<Document>>(responseClass: responseSig<RespType>, params?: PredictOptions): Promise<RespType>;
19
+ protected getDocType(responseClass: string): string;
20
+ }
21
+ interface customConfigParams {
22
+ accountName: string;
23
+ documentType: string;
24
+ version?: string;
25
+ }
26
+ interface ClientOptions {
27
+ apiKey?: string;
28
+ throwOnError?: boolean;
29
+ debug?: boolean;
30
+ }
31
+ /**
32
+ * Mindee Client
33
+ */
34
+ export declare class Client {
35
+ readonly docConfigs: DocConfigs;
36
+ protected apiKey: string;
37
+ /**
38
+ * @param options
39
+ */
40
+ constructor(options?: ClientOptions);
41
+ protected addStandardEndpoints(): void;
42
+ addEndpoint({ accountName, documentType, version, }: customConfigParams): this;
43
+ docFromPath(inputPath: string): DocumentClient;
44
+ docFromBase64(inputString: string, filename: string): DocumentClient;
45
+ docFromStream(inputStream: ReadStream, filename: string): DocumentClient;
46
+ docFromBytes(inputBytes: string, filename: string): DocumentClient;
47
+ }
48
+ export {};
package/src/client.js ADDED
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Client = void 0;
4
+ const inputs_1 = require("./inputs");
5
+ const api_1 = require("./api");
6
+ const documents_1 = require("./documents");
7
+ const documentConfig_1 = require("./documents/documentConfig");
8
+ const handler_1 = require("./errors/handler");
9
+ const logger_1 = require("./logger");
10
+ const constants_1 = require("./constants");
11
+ class DocumentClient {
12
+ constructor(inputDoc, docConfigs) {
13
+ this.inputDoc = inputDoc;
14
+ this.docConfigs = docConfigs;
15
+ }
16
+ async parse(responseClass, params = {
17
+ docType: "",
18
+ username: "",
19
+ cutPages: true,
20
+ fullText: false,
21
+ }) {
22
+ // seems like there should be a better way of doing this
23
+ const fullText = params?.fullText !== undefined ? params.fullText : false;
24
+ const cutPages = params?.cutPages !== undefined ? params.cutPages : true;
25
+ const docType = params.docType === undefined || params.docType === ""
26
+ ? this.getDocType(responseClass.name)
27
+ : params.docType;
28
+ const found = [];
29
+ this.docConfigs.forEach((config, configKey) => {
30
+ if (configKey[1] === docType) {
31
+ found.push(configKey);
32
+ }
33
+ });
34
+ if (found.length === 0) {
35
+ throw `Document type not configured: '${docType}'`;
36
+ }
37
+ let configKey = [];
38
+ if (found.length === 1) {
39
+ configKey = found[0];
40
+ }
41
+ else if (params.username) {
42
+ configKey = [params.username, docType];
43
+ }
44
+ const docConfig = this.docConfigs.get(configKey);
45
+ if (docConfig === undefined) {
46
+ // TODO: raise error printing all usernames
47
+ throw `Couldn't find the config '${configKey}'`;
48
+ }
49
+ return await docConfig.predict(responseClass, {
50
+ inputDoc: this.inputDoc,
51
+ includeWords: fullText,
52
+ cutPages: cutPages,
53
+ });
54
+ }
55
+ getDocType(responseClass) {
56
+ return constants_1.ProductConfigs.getByResponseClassName(responseClass).docType;
57
+ }
58
+ }
59
+ /**
60
+ * Mindee Client
61
+ */
62
+ class Client {
63
+ /**
64
+ * @param options
65
+ */
66
+ constructor(options) {
67
+ const throwOnError = options?.throwOnError === undefined ? true : options.throwOnError;
68
+ const debug = options?.debug === undefined ? false : options.debug;
69
+ this.apiKey = options?.apiKey === undefined ? "" : options.apiKey;
70
+ this.docConfigs = new Map();
71
+ handler_1.errorHandler.throwOnError = throwOnError;
72
+ logger_1.logger.level =
73
+ debug ?? process.env.MINDEE_DEBUG
74
+ ? logger_1.LOG_LEVELS["debug"]
75
+ : logger_1.LOG_LEVELS["warn"];
76
+ logger_1.logger.debug("Client initialized");
77
+ this.addStandardEndpoints();
78
+ }
79
+ // TODO: init only those endpoints we actually need.
80
+ addStandardEndpoints() {
81
+ this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_INVOICE], new documentConfig_1.InvoiceConfig(this.apiKey));
82
+ this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_RECEIPT], new documentConfig_1.ReceiptConfig(this.apiKey));
83
+ this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_FINANCIAL], new documentConfig_1.FinancialDocConfig(this.apiKey));
84
+ this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_PASSPORT], new documentConfig_1.PassportConfig(this.apiKey));
85
+ }
86
+ addEndpoint({ accountName, documentType, version = "1", }) {
87
+ this.docConfigs.set([accountName, documentType], new documentConfig_1.CustomDocConfig({
88
+ accountName: accountName,
89
+ documentType: documentType,
90
+ version: version,
91
+ apiKey: this.apiKey,
92
+ }));
93
+ return this;
94
+ }
95
+ docFromPath(inputPath) {
96
+ const doc = new inputs_1.PathInput({
97
+ inputPath: inputPath,
98
+ });
99
+ return new DocumentClient(doc, this.docConfigs);
100
+ }
101
+ docFromBase64(inputString, filename) {
102
+ const doc = new inputs_1.Base64Input({
103
+ inputString: inputString,
104
+ filename: filename,
105
+ });
106
+ return new DocumentClient(doc, this.docConfigs);
107
+ }
108
+ docFromStream(inputStream, filename) {
109
+ const doc = new inputs_1.StreamInput({
110
+ inputStream: inputStream,
111
+ filename: filename,
112
+ });
113
+ return new DocumentClient(doc, this.docConfigs);
114
+ }
115
+ docFromBytes(inputBytes, filename) {
116
+ const doc = new inputs_1.BytesInput({
117
+ inputBytes: inputBytes,
118
+ filename: filename,
119
+ });
120
+ return new DocumentClient(doc, this.docConfigs);
121
+ }
122
+ }
123
+ exports.Client = Client;
@@ -0,0 +1,13 @@
1
+ import { Response } from "./api";
2
+ import { Document } from "./documents";
3
+ export interface ProductConfig {
4
+ description: string;
5
+ docType: string;
6
+ responseClass: typeof Response<Document>;
7
+ fullText: boolean;
8
+ }
9
+ export declare class ProductConfigs {
10
+ private static configs;
11
+ static getByResponseClassName(responseClass: string): ProductConfig;
12
+ static getByDocType(docType: string): ProductConfig;
13
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProductConfigs = void 0;
4
+ const api_1 = require("./api");
5
+ const documents_1 = require("./documents");
6
+ class ProductConfigs {
7
+ static getByResponseClassName(responseClass) {
8
+ const config = this.configs.find((element) => element.responseClass.name === responseClass);
9
+ if (config === undefined) {
10
+ throw `Unknown response class: ${responseClass}`;
11
+ }
12
+ return config;
13
+ }
14
+ static getByDocType(docType) {
15
+ const config = this.configs.find((element) => element.docType === docType);
16
+ if (config === undefined) {
17
+ throw `Unknown document type: ${docType}`;
18
+ }
19
+ return config;
20
+ }
21
+ }
22
+ exports.ProductConfigs = ProductConfigs;
23
+ ProductConfigs.configs = [
24
+ {
25
+ description: "Invoice",
26
+ docType: documents_1.DOC_TYPE_INVOICE,
27
+ responseClass: api_1.InvoiceResponse,
28
+ fullText: true,
29
+ },
30
+ {
31
+ description: "Expense Receipt",
32
+ docType: documents_1.DOC_TYPE_RECEIPT,
33
+ responseClass: api_1.ReceiptResponse,
34
+ fullText: true,
35
+ },
36
+ {
37
+ description: "Passport",
38
+ docType: documents_1.DOC_TYPE_PASSPORT,
39
+ responseClass: api_1.PassportResponse,
40
+ fullText: false,
41
+ },
42
+ {
43
+ description: "Financial Document (receipt or invoice)",
44
+ docType: documents_1.DOC_TYPE_FINANCIAL,
45
+ responseClass: api_1.FinancialDocResponse,
46
+ fullText: true,
47
+ },
48
+ {
49
+ description: "A custom document",
50
+ docType: documents_1.DOC_TYPE_CUSTOM,
51
+ responseClass: api_1.CustomResponse,
52
+ fullText: false,
53
+ },
54
+ ];
@@ -0,0 +1,14 @@
1
+ import { Document, DocumentConstructorProps } from "./document";
2
+ import { ClassificationField, ListField } from "../fields";
3
+ import { stringDict } from "../fields/field";
4
+ export interface CustomDocConstructorProps extends DocumentConstructorProps {
5
+ documentType: string;
6
+ }
7
+ export declare class CustomDocument extends Document {
8
+ fields: Map<string, ListField>;
9
+ classifications: Map<string, ClassificationField>;
10
+ readonly documentType: string;
11
+ constructor({ inputFile, apiPrediction, pageId, documentType, }: CustomDocConstructorProps);
12
+ protected setField(fieldName: string, apiPrediction: stringDict, pageId: number | undefined): void;
13
+ toString(): string;
14
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomDocument = void 0;
4
+ const document_1 = require("./document");
5
+ const fields_1 = require("../fields");
6
+ class CustomDocument extends document_1.Document {
7
+ constructor({ inputFile, apiPrediction, pageId, documentType, }) {
8
+ super(inputFile, pageId);
9
+ this.fields = new Map();
10
+ this.classifications = new Map();
11
+ this.documentType = documentType;
12
+ Object.keys(apiPrediction).forEach((fieldName) => {
13
+ this.setField(fieldName, apiPrediction, pageId);
14
+ });
15
+ }
16
+ setField(fieldName, apiPrediction, pageId) {
17
+ // Currently, two types of fields possible in a custom API response:
18
+ // fields having a list of values, and classification fields.
19
+ const fieldPrediction = apiPrediction[fieldName];
20
+ if (fieldPrediction["values"] !== undefined) {
21
+ // Only value lists have the 'values' attribute.
22
+ this.fields.set(fieldName, new fields_1.ListField({
23
+ prediction: fieldPrediction,
24
+ pageId: pageId,
25
+ }));
26
+ }
27
+ else if (fieldPrediction["value"] !== undefined) {
28
+ // Only classifications have the 'value' attribute.
29
+ this.classifications.set(fieldName, new fields_1.ClassificationField({ prediction: fieldPrediction }));
30
+ }
31
+ else {
32
+ throw "Unknown API field type";
33
+ }
34
+ }
35
+ toString() {
36
+ let outStr = `----- ${this.documentType} -----`;
37
+ outStr += `\nFilename: ${this.filename}`.trimEnd();
38
+ this.classifications.forEach((fieldData, name) => {
39
+ outStr += `\n${name}: ${fieldData}`.trimEnd();
40
+ });
41
+ this.fields.forEach((fieldData, name) => {
42
+ outStr += `\n${name}: ${fieldData}`.trimEnd();
43
+ });
44
+ outStr += "\n----------------------\n";
45
+ return outStr;
46
+ }
47
+ }
48
+ exports.CustomDocument = CustomDocument;
@@ -0,0 +1,38 @@
1
+ import { Input } from "../inputs";
2
+ import { FullText } from "../fields";
3
+ export interface DocumentConstructorProps {
4
+ apiPrediction: {
5
+ [index: string]: any;
6
+ };
7
+ inputFile?: Input;
8
+ pageId?: number;
9
+ fullText?: FullText;
10
+ }
11
+ export declare class Document {
12
+ checklist: {
13
+ [index: string]: boolean;
14
+ };
15
+ mimeType: string | undefined;
16
+ filename: string;
17
+ filepath: string | undefined;
18
+ fullText?: FullText;
19
+ pageId?: number | undefined;
20
+ /**
21
+ * Takes a list of Documents and return one Document where
22
+ * each field is set with the maximum probability field
23
+ * @param {Input} inputFile - input file given to parse the document
24
+ * @param {number} pageId - Page ID for multi-page document
25
+ * @param {FullText} fullText - full OCR extracted text
26
+ */
27
+ constructor(inputFile?: Input, pageId?: number, fullText?: FullText);
28
+ clone(): any;
29
+ /** return true if all checklist of the document if true */
30
+ checkAll(): boolean;
31
+ /**
32
+ * Takes a list of Documents and return one Document where
33
+ * each field is set with the maximum probability field
34
+ * @param {Array<Document>} documents - A list of Documents
35
+ */
36
+ static mergePages(documents: any): any;
37
+ static cleanOutString(outStr: string): string;
38
+ }