mindee 4.0.1 → 4.1.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 (171) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/package.json +4 -3
  3. package/src/cli.js +36 -66
  4. package/src/client.d.ts +76 -32
  5. package/src/client.js +108 -21
  6. package/src/http/endpoint.d.ts +36 -0
  7. package/src/http/endpoint.js +54 -16
  8. package/src/http/error.d.ts +66 -1
  9. package/src/http/error.js +196 -4
  10. package/src/http/index.d.ts +1 -1
  11. package/src/http/index.js +10 -1
  12. package/src/index.d.ts +2 -1
  13. package/src/index.js +2 -1
  14. package/src/input/base.d.ts +1 -0
  15. package/src/input/base.js +3 -0
  16. package/src/input/pageOptions.d.ts +4 -0
  17. package/src/input/pageOptions.js +3 -0
  18. package/src/input/sources.d.ts +2 -3
  19. package/src/input/sources.js +2 -1
  20. package/src/internal.d.ts +5 -0
  21. package/src/internal.js +31 -0
  22. package/src/parsing/common/apiRequest.d.ts +9 -0
  23. package/src/parsing/common/apiRequest.js +5 -0
  24. package/src/parsing/common/apiResponse.d.ts +15 -0
  25. package/src/parsing/common/apiResponse.js +15 -0
  26. package/src/parsing/common/asyncPredictResponse.d.ts +24 -3
  27. package/src/parsing/common/asyncPredictResponse.js +15 -0
  28. package/src/parsing/common/document.d.ts +17 -0
  29. package/src/parsing/common/document.js +16 -4
  30. package/src/parsing/common/extras/cropperExtra.d.ts +3 -0
  31. package/src/parsing/common/extras/cropperExtra.js +3 -0
  32. package/src/parsing/common/extras/extras.d.ts +3 -0
  33. package/src/parsing/common/extras/extras.js +3 -0
  34. package/src/parsing/common/inference.d.ts +26 -0
  35. package/src/parsing/common/inference.js +23 -2
  36. package/src/parsing/common/mvisionV1.d.ts +3 -0
  37. package/src/parsing/common/mvisionV1.js +3 -0
  38. package/src/parsing/common/ocr.d.ts +3 -0
  39. package/src/parsing/common/ocr.js +3 -0
  40. package/src/parsing/common/ocrPage.d.ts +3 -0
  41. package/src/parsing/common/ocrPage.js +3 -0
  42. package/src/parsing/common/orientation.d.ts +3 -0
  43. package/src/parsing/common/orientation.js +3 -0
  44. package/src/parsing/common/page.d.ts +19 -0
  45. package/src/parsing/common/page.js +15 -0
  46. package/src/parsing/common/predictResponse.d.ts +11 -0
  47. package/src/parsing/common/predictResponse.js +10 -0
  48. package/src/parsing/custom/classificationField.d.ts +3 -0
  49. package/src/parsing/custom/classificationField.js +3 -0
  50. package/src/parsing/custom/listField.d.ts +6 -0
  51. package/src/parsing/custom/listField.js +6 -0
  52. package/src/parsing/standard/amount.d.ts +3 -0
  53. package/src/parsing/standard/amount.js +3 -0
  54. package/src/parsing/standard/base.d.ts +3 -0
  55. package/src/parsing/standard/base.js +3 -0
  56. package/src/parsing/standard/locale.d.ts +3 -0
  57. package/src/parsing/standard/locale.js +3 -0
  58. package/src/parsing/standard/paymentDetails.d.ts +3 -0
  59. package/src/parsing/standard/paymentDetails.js +3 -0
  60. package/src/parsing/standard/position.d.ts +3 -0
  61. package/src/parsing/standard/position.js +3 -0
  62. package/src/parsing/standard/tax.d.ts +3 -0
  63. package/src/parsing/standard/tax.js +3 -0
  64. package/src/product/cropper/cropperV1.d.ts +7 -0
  65. package/src/product/cropper/cropperV1.js +6 -0
  66. package/src/product/cropper/cropperV1Document.d.ts +6 -0
  67. package/src/product/cropper/cropperV1Document.js +6 -0
  68. package/src/product/cropper/internal.d.ts +2 -0
  69. package/src/product/cropper/internal.js +7 -0
  70. package/src/product/custom/customV1.d.ts +7 -0
  71. package/src/product/custom/customV1.js +6 -0
  72. package/src/product/custom/customV1Document.d.ts +16 -0
  73. package/src/product/custom/customV1Document.js +16 -2
  74. package/src/product/custom/customV1Page.d.ts +7 -0
  75. package/src/product/custom/customV1Page.js +7 -0
  76. package/src/product/custom/internal.d.ts +3 -0
  77. package/src/product/custom/internal.js +9 -0
  78. package/src/product/eu/internal.d.ts +1 -0
  79. package/src/product/eu/internal.js +27 -0
  80. package/src/product/eu/licensePlate/internal.d.ts +2 -0
  81. package/src/product/eu/licensePlate/internal.js +7 -0
  82. package/src/product/eu/licensePlate/licensePlateV1.d.ts +7 -0
  83. package/src/product/eu/licensePlate/licensePlateV1.js +6 -0
  84. package/src/product/eu/licensePlate/licensePlateV1Document.d.ts +3 -0
  85. package/src/product/eu/licensePlate/licensePlateV1Document.js +3 -0
  86. package/src/product/financialDocument/financialDocumentV1.d.ts +7 -0
  87. package/src/product/financialDocument/financialDocumentV1.js +6 -0
  88. package/src/product/financialDocument/financialDocumentV1Document.d.ts +3 -0
  89. package/src/product/financialDocument/financialDocumentV1Document.js +3 -0
  90. package/src/product/financialDocument/internal.d.ts +3 -0
  91. package/src/product/financialDocument/internal.js +9 -0
  92. package/src/product/fr/bankAccountDetails/bankAccountDetailsV1.d.ts +7 -0
  93. package/src/product/fr/bankAccountDetails/bankAccountDetailsV1.js +6 -0
  94. package/src/product/fr/bankAccountDetails/bankAccountDetailsV1Document.d.ts +3 -0
  95. package/src/product/fr/bankAccountDetails/bankAccountDetailsV1Document.js +3 -0
  96. package/src/product/fr/bankAccountDetails/bankAccountDetailsV2.d.ts +7 -0
  97. package/src/product/fr/bankAccountDetails/bankAccountDetailsV2.js +6 -0
  98. package/src/product/fr/bankAccountDetails/bankAccountDetailsV2Document.d.ts +3 -0
  99. package/src/product/fr/bankAccountDetails/bankAccountDetailsV2Document.js +3 -0
  100. package/src/product/fr/bankAccountDetails/internal.d.ts +5 -0
  101. package/src/product/fr/bankAccountDetails/internal.js +13 -0
  102. package/src/product/fr/carteVitale/carteVitaleV1.d.ts +7 -0
  103. package/src/product/fr/carteVitale/carteVitaleV1.js +6 -0
  104. package/src/product/fr/carteVitale/carteVitaleV1Document.d.ts +3 -0
  105. package/src/product/fr/carteVitale/carteVitaleV1Document.js +3 -0
  106. package/src/product/fr/carteVitale/internal.d.ts +2 -0
  107. package/src/product/fr/carteVitale/internal.js +7 -0
  108. package/src/product/fr/idCard/idCardV1.d.ts +7 -0
  109. package/src/product/fr/idCard/idCardV1.js +6 -0
  110. package/src/product/fr/idCard/idCardV1Document.d.ts +3 -0
  111. package/src/product/fr/idCard/idCardV1Document.js +3 -0
  112. package/src/product/fr/idCard/internal.d.ts +3 -0
  113. package/src/product/fr/idCard/internal.js +9 -0
  114. package/src/product/fr/internal.d.ts +3 -0
  115. package/src/product/fr/internal.js +29 -0
  116. package/src/product/index.d.ts +0 -1
  117. package/src/product/index.js +1 -3
  118. package/src/product/internal.d.ts +11 -0
  119. package/src/product/internal.js +37 -0
  120. package/src/product/invoice/internal.d.ts +3 -0
  121. package/src/product/invoice/internal.js +9 -0
  122. package/src/product/invoice/invoiceV4.d.ts +7 -0
  123. package/src/product/invoice/invoiceV4.js +6 -0
  124. package/src/product/invoice/invoiceV4Document.d.ts +3 -1
  125. package/src/product/invoice/invoiceV4Document.js +3 -1
  126. package/src/product/invoice/invoiceV4LineItem.d.ts +9 -0
  127. package/src/product/invoice/invoiceV4LineItem.js +9 -0
  128. package/src/product/invoiceSplitter/internal.d.ts +3 -0
  129. package/src/product/invoiceSplitter/internal.js +9 -0
  130. package/src/product/invoiceSplitter/invoiceSplitterV1.d.ts +7 -0
  131. package/src/product/invoiceSplitter/invoiceSplitterV1.js +6 -0
  132. package/src/product/invoiceSplitter/invoiceSplitterV1Document.d.ts +8 -2
  133. package/src/product/invoiceSplitter/invoiceSplitterV1Document.js +7 -1
  134. package/src/product/invoiceSplitter/invoiceSplitterV1PageGroup.d.ts +9 -1
  135. package/src/product/invoiceSplitter/invoiceSplitterV1PageGroup.js +10 -3
  136. package/src/product/passport/internal.d.ts +2 -0
  137. package/src/product/passport/internal.js +7 -0
  138. package/src/product/passport/passportV1.d.ts +7 -0
  139. package/src/product/passport/passportV1.js +6 -0
  140. package/src/product/passport/passportV1Document.d.ts +3 -0
  141. package/src/product/passport/passportV1Document.js +3 -0
  142. package/src/product/proofOfAddress/internal.d.ts +2 -0
  143. package/src/product/proofOfAddress/internal.js +7 -0
  144. package/src/product/proofOfAddress/proofOfAddressV1.d.ts +7 -0
  145. package/src/product/proofOfAddress/proofOfAddressV1.js +6 -0
  146. package/src/product/proofOfAddress/proofOfAddressV1Document.d.ts +3 -0
  147. package/src/product/proofOfAddress/proofOfAddressV1Document.js +3 -0
  148. package/src/product/receipt/internal.d.ts +5 -0
  149. package/src/product/receipt/internal.js +13 -0
  150. package/src/product/receipt/receiptV4.d.ts +7 -0
  151. package/src/product/receipt/receiptV4.js +5 -0
  152. package/src/product/receipt/receiptV4Document.d.ts +6 -0
  153. package/src/product/receipt/receiptV4Document.js +6 -0
  154. package/src/product/receipt/receiptV5.d.ts +7 -0
  155. package/src/product/receipt/receiptV5.js +6 -0
  156. package/src/product/receipt/receiptV5Document.d.ts +3 -0
  157. package/src/product/receipt/receiptV5Document.js +3 -0
  158. package/src/product/us/bankCheck/bankCheckV1.d.ts +7 -0
  159. package/src/product/us/bankCheck/bankCheckV1.js +6 -0
  160. package/src/product/us/bankCheck/bankCheckV1Document.d.ts +3 -0
  161. package/src/product/us/bankCheck/bankCheckV1Document.js +3 -0
  162. package/src/product/us/bankCheck/internal.d.ts +3 -0
  163. package/src/product/us/bankCheck/internal.js +9 -0
  164. package/src/product/us/driverLicense/driverLicenseV1.d.ts +7 -0
  165. package/src/product/us/driverLicense/driverLicenseV1.js +6 -0
  166. package/src/product/us/driverLicense/driverLicenseV1Document.d.ts +3 -0
  167. package/src/product/us/driverLicense/driverLicenseV1Document.js +3 -0
  168. package/src/product/us/driverLicense/internal.d.ts +3 -0
  169. package/src/product/us/driverLicense/internal.js +9 -0
  170. package/src/product/us/internal.d.ts +2 -0
  171. package/src/product/us/internal.js +28 -0
@@ -11,26 +11,55 @@ export interface EndpointResponse {
11
11
  [key: string]: any;
12
12
  };
13
13
  }
14
+ /**
15
+ * Endpoint object class wrapper.
16
+ */
14
17
  export declare class Endpoint {
15
18
  #private;
19
+ /** URL of a product. */
16
20
  urlName: string;
21
+ /** Account owning the product. */
17
22
  owner: string;
23
+ /** Product's version, as a string. */
18
24
  version: string;
25
+ /** Entire root of the URL for API calls. */
19
26
  urlRoot: string;
27
+ /** Settings relating to the API. */
20
28
  settings: MindeeApi;
21
29
  constructor(urlName: string, owner: string, version: string, settings: MindeeApi);
30
+ /**
31
+ * Sends a prediction to the API and parses out the result.
32
+ * Throws an error if the server's response contains one.
33
+ * @param params parameters relating to prediction options.
34
+ * @category Synchronous
35
+ * @returns a `Promise` containing parsing results.
36
+ */
22
37
  predict(params: {
23
38
  inputDoc: InputSource;
24
39
  includeWords: boolean;
25
40
  pageOptions?: PageOptions;
26
41
  cropper: boolean;
27
42
  }): Promise<EndpointResponse>;
43
+ /**
44
+ * Enqueues a prediction to the API.
45
+ * Throws an error if the server's response contains one.
46
+ * @param params parameters relating to prediction options.
47
+ * @category Asynchronous
48
+ * @returns a `Promise` containing queue data.
49
+ */
28
50
  predictAsync(params: {
29
51
  inputDoc: InputSource;
30
52
  includeWords: boolean;
31
53
  pageOptions?: PageOptions;
32
54
  cropper: boolean;
33
55
  }): Promise<EndpointResponse>;
56
+ /**
57
+ * Requests the results of a queued document from the API.
58
+ * Throws an error if the server's response contains one.
59
+ * @param params parameters relating to prediction options.
60
+ * @category Asynchronous
61
+ * @returns a `Promise` containing the parsed result.
62
+ */
34
63
  getQueuedDocument(queueId: string): Promise<EndpointResponse>;
35
64
  /**
36
65
  * Send a file to a prediction API.
@@ -40,5 +69,12 @@ export declare class Endpoint {
40
69
  * @param cropper
41
70
  */
42
71
  protected sendFileForPrediction(input: InputSource, predictUrl: string, includeWords?: boolean, cropper?: boolean): Promise<EndpointResponse>;
72
+ /**
73
+ * Reads a response from the API and processes it.
74
+ * @param options options related to the request itself.
75
+ * @param resolve the resolved response
76
+ * @param reject promise rejection reason.
77
+ * @returns the processed request.
78
+ */
43
79
  protected readResponse(options: RequestOptions, resolve: (value: EndpointResponse | PromiseLike<EndpointResponse>) => void, reject: (reason?: any) => void): ClientRequest;
44
80
  }
@@ -16,6 +16,9 @@ const form_data_1 = __importDefault(require("form-data"));
16
16
  const logger_1 = require("../logger");
17
17
  const base_1 = require("../input/base");
18
18
  const error_1 = require("./error");
19
+ /**
20
+ * Endpoint object class wrapper.
21
+ */
19
22
  class Endpoint {
20
23
  constructor(urlName, owner, version, settings) {
21
24
  _Endpoint_instances.add(this);
@@ -25,6 +28,13 @@ class Endpoint {
25
28
  this.settings = settings;
26
29
  this.urlRoot = `/v1/products/${owner}/${urlName}/v${version}`;
27
30
  }
31
+ /**
32
+ * Sends a prediction to the API and parses out the result.
33
+ * Throws an error if the server's response contains one.
34
+ * @param params parameters relating to prediction options.
35
+ * @category Synchronous
36
+ * @returns a `Promise` containing parsing results.
37
+ */
28
38
  async predict(params) {
29
39
  await params.inputDoc.init();
30
40
  if (params.pageOptions !== undefined) {
@@ -32,11 +42,18 @@ class Endpoint {
32
42
  }
33
43
  const response = await __classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_predictReqPost).call(this, params.inputDoc, params.includeWords, params.cropper);
34
44
  const statusCode = response.messageObj.statusCode;
35
- if (statusCode === undefined || statusCode > 400) {
36
- (0, error_1.handleError)(this.urlName, response, statusCode);
45
+ if (statusCode === undefined || statusCode >= 400) {
46
+ (0, error_1.handleError)(this.urlName, response, statusCode, response.messageObj?.statusMessage);
37
47
  }
38
48
  return response;
39
49
  }
50
+ /**
51
+ * Enqueues a prediction to the API.
52
+ * Throws an error if the server's response contains one.
53
+ * @param params parameters relating to prediction options.
54
+ * @category Asynchronous
55
+ * @returns a `Promise` containing queue data.
56
+ */
40
57
  async predictAsync(params) {
41
58
  await params.inputDoc.init();
42
59
  if (params.pageOptions !== undefined) {
@@ -44,18 +61,25 @@ class Endpoint {
44
61
  }
45
62
  const response = await __classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_predictAsyncReqPost).call(this, params.inputDoc, params.includeWords, params.cropper);
46
63
  const statusCode = response.messageObj.statusCode;
47
- if (statusCode === undefined || statusCode > 400) {
48
- (0, error_1.handleError)(this.urlName, response, statusCode);
64
+ if (statusCode === undefined || statusCode >= 400) {
65
+ (0, error_1.handleError)(this.urlName, response, statusCode, response.messageObj?.statusMessage);
49
66
  }
50
67
  return response;
51
68
  }
69
+ /**
70
+ * Requests the results of a queued document from the API.
71
+ * Throws an error if the server's response contains one.
72
+ * @param params parameters relating to prediction options.
73
+ * @category Asynchronous
74
+ * @returns a `Promise` containing the parsed result.
75
+ */
52
76
  async getQueuedDocument(queueId) {
53
77
  const queueResponse = await __classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_documentQueueReqGet).call(this, queueId);
54
78
  const queueStatusCode = queueResponse.messageObj.statusCode;
55
79
  if (queueStatusCode === undefined ||
56
80
  queueStatusCode < 200 ||
57
81
  queueStatusCode > 400) {
58
- (0, error_1.handleError)(this.urlName, queueResponse, queueStatusCode);
82
+ (0, error_1.handleError)(this.urlName, queueResponse, queueStatusCode, queueResponse.messageObj?.statusMessage);
59
83
  }
60
84
  if (queueStatusCode === 302 &&
61
85
  queueResponse.messageObj.headers.location !== undefined) {
@@ -80,15 +104,13 @@ class Endpoint {
80
104
  searchParams.append("cropper", "true");
81
105
  }
82
106
  const form = new form_data_1.default();
83
- if (input instanceof base_1.LocalInputSource) {
84
- if (input.fileObject instanceof Buffer) {
85
- form.append("document", input.fileObject, {
86
- filename: input.filename,
87
- });
88
- }
89
- else {
90
- form.append("document", input.fileObject);
91
- }
107
+ if (input instanceof base_1.LocalInputSource && input.fileObject instanceof Buffer) {
108
+ form.append("document", input.fileObject, {
109
+ filename: input.filename,
110
+ });
111
+ }
112
+ else {
113
+ form.append("document", input.fileObject);
92
114
  }
93
115
  if (includeWords) {
94
116
  form.append("include_mvision", "true");
@@ -110,6 +132,13 @@ class Endpoint {
110
132
  req.end();
111
133
  });
112
134
  }
135
+ /**
136
+ * Reads a response from the API and processes it.
137
+ * @param options options related to the request itself.
138
+ * @param resolve the resolved response
139
+ * @param reject promise rejection reason.
140
+ * @returns the processed request.
141
+ */
113
142
  readResponse(options, resolve, reject) {
114
143
  logger_1.logger.debug(`${options.method}: https://${options.hostname}${options.path}`);
115
144
  const req = (0, https_1.request)(options, function (res) {
@@ -142,7 +171,10 @@ class Endpoint {
142
171
  catch (error) {
143
172
  logger_1.logger.error("Could not parse the return as JSON.");
144
173
  logger_1.logger.debug(responseBody);
145
- reject(error);
174
+ resolve({
175
+ messageObj: res,
176
+ data: { reconstructedResponse: responseBody },
177
+ });
146
178
  }
147
179
  });
148
180
  });
@@ -153,7 +185,13 @@ class Endpoint {
153
185
  }
154
186
  }
155
187
  exports.Endpoint = Endpoint;
156
- _Endpoint_instances = new WeakSet(), _Endpoint_cutDocPages = async function _Endpoint_cutDocPages(inputDoc, pageOptions) {
188
+ _Endpoint_instances = new WeakSet(), _Endpoint_cutDocPages =
189
+ /**
190
+ * Cuts a document's pages according to the given options.
191
+ * @param inputDoc input document.
192
+ * @param pageOptions page cutting options.
193
+ */
194
+ async function _Endpoint_cutDocPages(inputDoc, pageOptions) {
157
195
  if (inputDoc instanceof base_1.LocalInputSource && inputDoc.isPdf()) {
158
196
  await inputDoc.cutPdf(pageOptions);
159
197
  }
@@ -1,2 +1,67 @@
1
+ import { MindeeError } from "../errors";
2
+ import { StringDict } from "../parsing/common";
1
3
  import { EndpointResponse } from "./endpoint";
2
- export declare function handleError(url: string, response: EndpointResponse, statusCode?: number): void;
4
+ export declare function handleError(url: string, response: EndpointResponse, code?: number, serverError?: string): void;
5
+ /**
6
+ * `Error` wrapper for server (HTTP) errors.
7
+ * Is used when an error is lacking a handled error code.
8
+ */
9
+ export declare class MindeeHttpError extends MindeeError {
10
+ /** Description of the error. */
11
+ message: string;
12
+ /** Additional details on the error. */
13
+ details: string | StringDict;
14
+ /** Standard HTTP error code. */
15
+ code?: number;
16
+ constructor(httpError: StringDict, url: string, code?: number);
17
+ }
18
+ /**
19
+ * Generic client errors.
20
+ * Can include errors like InvalidQuery.
21
+ */
22
+ export declare class MindeeHttp400Error extends MindeeHttpError {
23
+ constructor(httpError: StringDict, url: string, code?: number);
24
+ }
25
+ /**
26
+ * Can include errors like NoTokenSet or InvalidToken.
27
+ */
28
+ export declare class MindeeHttp401Error extends MindeeHttpError {
29
+ constructor(httpError: StringDict, url: string, code?: number);
30
+ }
31
+ /**
32
+ * Regular AccessForbidden error.
33
+ * Can also include errors like PlanLimitReached, AsyncRequestDisallowed or SyncRequestDisallowed.
34
+ */
35
+ export declare class MindeeHttp403Error extends MindeeHttpError {
36
+ constructor(httpError: StringDict, url: string, code?: number);
37
+ }
38
+ export declare class MindeeHttp404Error extends MindeeHttpError {
39
+ constructor(httpError: StringDict, url: string, code?: number);
40
+ }
41
+ /**
42
+ * Rare error.
43
+ * Can occasionally happen when unusually large documents are passed.
44
+ */
45
+ export declare class MindeeHttp413Error extends MindeeHttpError {
46
+ constructor(httpError: StringDict, url: string, code?: number);
47
+ }
48
+ /**
49
+ * Usually contains TooManyRequests errors.
50
+ * Arises whenever too many calls to the API are made in quick succession.
51
+ */
52
+ export declare class MindeeHttp429Error extends MindeeHttpError {
53
+ constructor(httpError: StringDict, url: string, code?: number);
54
+ }
55
+ /**
56
+ * Generic server errors.
57
+ */
58
+ export declare class MindeeHttp500Error extends MindeeHttpError {
59
+ constructor(httpError: StringDict, url: string, code?: number);
60
+ }
61
+ /**
62
+ * Miscellaneous server errors.
63
+ * Can include errors like RequestTimeout or GatewayTimeout.
64
+ */
65
+ export declare class MindeeHttp504Error extends MindeeHttpError {
66
+ constructor(httpError: StringDict, url: string, code?: number);
67
+ }
package/src/http/error.js CHANGED
@@ -1,9 +1,201 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleError = void 0;
3
+ exports.MindeeHttp504Error = exports.MindeeHttp500Error = exports.MindeeHttp429Error = exports.MindeeHttp413Error = exports.MindeeHttp404Error = exports.MindeeHttp403Error = exports.MindeeHttp401Error = exports.MindeeHttp400Error = exports.MindeeHttpError = exports.handleError = void 0;
4
+ const errors_1 = require("../errors");
4
5
  const handler_1 = require("../errors/handler");
5
- function handleError(url, response, statusCode) {
6
- const errorMessage = JSON.stringify(response.data, null, 2);
7
- handler_1.errorHandler.throw(new Error(`${url} API ${statusCode} HTTP error: ${errorMessage}`));
6
+ function handleError(url, response, code, serverError) {
7
+ if (code === undefined) {
8
+ throw new MindeeHttpError({ message: "Missing HTTP Error code.", details: response.data, undefined }, url);
9
+ }
10
+ let errorObj;
11
+ try {
12
+ //Regular instances where the returned error is in JSON format.
13
+ errorObj = response.data.api_request.error;
14
+ }
15
+ catch (err) {
16
+ //Rare instances where errors are returned as HTML instead of JSON.
17
+ if (!("reconstructedResponse" in response.data)) {
18
+ response.data.reconstructedResponse = "";
19
+ }
20
+ if (response.data.reconstructedResponse.includes("Maximum pdf pages")) {
21
+ errorObj = {
22
+ message: "TooManyPages",
23
+ details: "Maximum amount of pdf pages reached.",
24
+ };
25
+ }
26
+ else if (response.data.reconstructedResponse.includes("Max file size is")) {
27
+ errorObj = {
28
+ message: "FileTooLarge",
29
+ details: "Maximum file size reached.",
30
+ };
31
+ }
32
+ else if (response.data.reconstructedResponse.includes("Invalid file type")) {
33
+ errorObj = {
34
+ message: "InvalidFiletype",
35
+ details: "Invalid file type.",
36
+ };
37
+ }
38
+ else if (response.data.reconstructedResponse.includes("Gateway timeout")) {
39
+ errorObj = {
40
+ message: "RequestTimeout",
41
+ details: "Request timed out.",
42
+ };
43
+ }
44
+ else if (response.data.reconstructedResponse.includes("Bad gateway")) {
45
+ errorObj = {
46
+ message: "BadRequest",
47
+ details: "Bad Gateway",
48
+ };
49
+ }
50
+ else if (response.data.reconstructedResponse.includes("Too Many Requests")) {
51
+ errorObj = {
52
+ message: "TooManyRequests",
53
+ details: "Too Many Requests.",
54
+ };
55
+ }
56
+ else {
57
+ errorObj = {
58
+ message: "Unknown Server Error.",
59
+ details: response.data.reconstructedResponse,
60
+ };
61
+ }
62
+ }
63
+ if (serverError !== undefined &&
64
+ (!("message" in errorObj) ||
65
+ !errorObj.message ||
66
+ errorObj.message.length === 0)) {
67
+ errorObj.message = serverError;
68
+ }
69
+ let errorToThrow;
70
+ switch (code) {
71
+ case 400:
72
+ errorToThrow = new MindeeHttp400Error(errorObj, url, code);
73
+ break;
74
+ case 401:
75
+ errorToThrow = new MindeeHttp401Error(errorObj, url, code);
76
+ break;
77
+ case 403:
78
+ errorToThrow = new MindeeHttp403Error(errorObj, url, code);
79
+ break;
80
+ case 404:
81
+ errorToThrow = new MindeeHttp404Error(errorObj, url, code);
82
+ break;
83
+ case 413:
84
+ errorToThrow = new MindeeHttp413Error(errorObj, url, code);
85
+ break;
86
+ case 429:
87
+ errorToThrow = new MindeeHttp429Error(errorObj, url, code);
88
+ break;
89
+ case 500:
90
+ errorToThrow = new MindeeHttp500Error(errorObj, url, code);
91
+ break;
92
+ case 504:
93
+ errorToThrow = new MindeeHttp504Error(errorObj, url, code);
94
+ break;
95
+ default:
96
+ errorToThrow = new MindeeHttpError(errorObj, url, code);
97
+ break;
98
+ }
99
+ handler_1.errorHandler.throw(errorToThrow);
8
100
  }
9
101
  exports.handleError = handleError;
102
+ /**
103
+ * `Error` wrapper for server (HTTP) errors.
104
+ * Is used when an error is lacking a handled error code.
105
+ */
106
+ class MindeeHttpError extends errors_1.MindeeError {
107
+ constructor(httpError, url, code) {
108
+ super(`${url} API ${code} HTTP error: ${httpError.message}`);
109
+ /** Description of the error. */
110
+ this.message = "";
111
+ /** Additional details on the error. */
112
+ this.details = "";
113
+ this.details = httpError.details;
114
+ this.message = httpError.message;
115
+ this.code = code;
116
+ this.name = "MindeeHttpError";
117
+ }
118
+ }
119
+ exports.MindeeHttpError = MindeeHttpError;
120
+ /**
121
+ * Generic client errors.
122
+ * Can include errors like InvalidQuery.
123
+ */
124
+ class MindeeHttp400Error extends MindeeHttpError {
125
+ constructor(httpError, url, code) {
126
+ super(httpError, url, code);
127
+ this.name = "MindeeHttp400Error";
128
+ }
129
+ }
130
+ exports.MindeeHttp400Error = MindeeHttp400Error;
131
+ /**
132
+ * Can include errors like NoTokenSet or InvalidToken.
133
+ */
134
+ class MindeeHttp401Error extends MindeeHttpError {
135
+ constructor(httpError, url, code) {
136
+ super(httpError, url, code);
137
+ this.name = "MindeeHttp401Error";
138
+ }
139
+ }
140
+ exports.MindeeHttp401Error = MindeeHttp401Error;
141
+ /**
142
+ * Regular AccessForbidden error.
143
+ * Can also include errors like PlanLimitReached, AsyncRequestDisallowed or SyncRequestDisallowed.
144
+ */
145
+ class MindeeHttp403Error extends MindeeHttpError {
146
+ constructor(httpError, url, code) {
147
+ super(httpError, url, code);
148
+ this.name = "MindeeHttp403Error";
149
+ }
150
+ }
151
+ exports.MindeeHttp403Error = MindeeHttp403Error;
152
+ class MindeeHttp404Error extends MindeeHttpError {
153
+ constructor(httpError, url, code) {
154
+ super(httpError, url, code);
155
+ this.name = "MindeeHttp404Error";
156
+ }
157
+ }
158
+ exports.MindeeHttp404Error = MindeeHttp404Error;
159
+ /**
160
+ * Rare error.
161
+ * Can occasionally happen when unusually large documents are passed.
162
+ */
163
+ class MindeeHttp413Error extends MindeeHttpError {
164
+ constructor(httpError, url, code) {
165
+ super(httpError, url, code);
166
+ this.name = "MindeeHttp413Error";
167
+ }
168
+ }
169
+ exports.MindeeHttp413Error = MindeeHttp413Error;
170
+ /**
171
+ * Usually contains TooManyRequests errors.
172
+ * Arises whenever too many calls to the API are made in quick succession.
173
+ */
174
+ class MindeeHttp429Error extends MindeeHttpError {
175
+ constructor(httpError, url, code) {
176
+ super(httpError, url, code);
177
+ this.name = "MindeeHttp429Error";
178
+ }
179
+ }
180
+ exports.MindeeHttp429Error = MindeeHttp429Error;
181
+ /**
182
+ * Generic server errors.
183
+ */
184
+ class MindeeHttp500Error extends MindeeHttpError {
185
+ constructor(httpError, url, code) {
186
+ super(httpError, url, code);
187
+ this.name = "MindeeHttp500Error";
188
+ }
189
+ }
190
+ exports.MindeeHttp500Error = MindeeHttp500Error;
191
+ /**
192
+ * Miscellaneous server errors.
193
+ * Can include errors like RequestTimeout or GatewayTimeout.
194
+ */
195
+ class MindeeHttp504Error extends MindeeHttpError {
196
+ constructor(httpError, url, code) {
197
+ super(httpError, url, code);
198
+ this.name = "MindeeHttp500Error";
199
+ }
200
+ }
201
+ exports.MindeeHttp504Error = MindeeHttp504Error;
@@ -1,3 +1,3 @@
1
1
  export { Endpoint, EndpointResponse } from "./endpoint";
2
2
  export { STANDARD_API_OWNER, API_KEY_ENVVAR_NAME, MindeeApi, } from "./mindeeApi";
3
- export { handleError } from "./error";
3
+ export { MindeeHttpError, MindeeHttp400Error, MindeeHttp401Error, MindeeHttp403Error, MindeeHttp404Error, MindeeHttp413Error, MindeeHttp429Error, MindeeHttp500Error, MindeeHttp504Error, handleError, } from "./error";
package/src/http/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleError = exports.MindeeApi = exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = exports.Endpoint = void 0;
3
+ exports.handleError = exports.MindeeHttp504Error = exports.MindeeHttp500Error = exports.MindeeHttp429Error = exports.MindeeHttp413Error = exports.MindeeHttp404Error = exports.MindeeHttp403Error = exports.MindeeHttp401Error = exports.MindeeHttp400Error = exports.MindeeHttpError = exports.MindeeApi = exports.API_KEY_ENVVAR_NAME = exports.STANDARD_API_OWNER = exports.Endpoint = void 0;
4
4
  var endpoint_1 = require("./endpoint");
5
5
  Object.defineProperty(exports, "Endpoint", { enumerable: true, get: function () { return endpoint_1.Endpoint; } });
6
6
  var mindeeApi_1 = require("./mindeeApi");
@@ -8,4 +8,13 @@ Object.defineProperty(exports, "STANDARD_API_OWNER", { enumerable: true, get: fu
8
8
  Object.defineProperty(exports, "API_KEY_ENVVAR_NAME", { enumerable: true, get: function () { return mindeeApi_1.API_KEY_ENVVAR_NAME; } });
9
9
  Object.defineProperty(exports, "MindeeApi", { enumerable: true, get: function () { return mindeeApi_1.MindeeApi; } });
10
10
  var error_1 = require("./error");
11
+ Object.defineProperty(exports, "MindeeHttpError", { enumerable: true, get: function () { return error_1.MindeeHttpError; } });
12
+ Object.defineProperty(exports, "MindeeHttp400Error", { enumerable: true, get: function () { return error_1.MindeeHttp400Error; } });
13
+ Object.defineProperty(exports, "MindeeHttp401Error", { enumerable: true, get: function () { return error_1.MindeeHttp401Error; } });
14
+ Object.defineProperty(exports, "MindeeHttp403Error", { enumerable: true, get: function () { return error_1.MindeeHttp403Error; } });
15
+ Object.defineProperty(exports, "MindeeHttp404Error", { enumerable: true, get: function () { return error_1.MindeeHttp404Error; } });
16
+ Object.defineProperty(exports, "MindeeHttp413Error", { enumerable: true, get: function () { return error_1.MindeeHttp413Error; } });
17
+ Object.defineProperty(exports, "MindeeHttp429Error", { enumerable: true, get: function () { return error_1.MindeeHttp429Error; } });
18
+ Object.defineProperty(exports, "MindeeHttp500Error", { enumerable: true, get: function () { return error_1.MindeeHttp500Error; } });
19
+ Object.defineProperty(exports, "MindeeHttp504Error", { enumerable: true, get: function () { return error_1.MindeeHttp504Error; } });
11
20
  Object.defineProperty(exports, "handleError", { enumerable: true, get: function () { return error_1.handleError; } });
package/src/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * as product from "./product";
2
- export { Client, CustomConfigParams, PredictOptions } from "./client";
2
+ export { Client, PredictOptions } from "./client";
3
3
  export { AsyncPredictResponse, PredictResponse, Document, Page, } from "./parsing/common";
4
4
  export { InputSource, PageOptionsOperation } from "./input";
5
+ export * as internal from "./internal";
package/src/index.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.PageOptionsOperation = exports.InputSource = exports.Page = exports.Document = exports.PredictResponse = exports.AsyncPredictResponse = exports.Client = exports.product = void 0;
26
+ exports.internal = exports.PageOptionsOperation = exports.InputSource = exports.Page = exports.Document = exports.PredictResponse = exports.AsyncPredictResponse = exports.Client = exports.product = void 0;
27
27
  exports.product = __importStar(require("./product"));
28
28
  var client_1 = require("./client");
29
29
  Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
@@ -35,3 +35,4 @@ Object.defineProperty(exports, "Page", { enumerable: true, get: function () { re
35
35
  var input_1 = require("./input");
36
36
  Object.defineProperty(exports, "InputSource", { enumerable: true, get: function () { return input_1.InputSource; } });
37
37
  Object.defineProperty(exports, "PageOptionsOperation", { enumerable: true, get: function () { return input_1.PageOptionsOperation; } });
38
+ exports.internal = __importStar(require("./internal"));
@@ -9,6 +9,7 @@ export declare const INPUT_TYPE_BYTES = "bytes";
9
9
  export declare const INPUT_TYPE_PATH = "path";
10
10
  export declare const INPUT_TYPE_BUFFER = "buffer";
11
11
  export declare abstract class InputSource {
12
+ fileObject: Buffer | string;
12
13
  init(): Promise<void>;
13
14
  }
14
15
  export declare abstract class LocalInputSource extends InputSource {
package/src/input/base.js CHANGED
@@ -52,6 +52,9 @@ const ALLOWED_INPUT_TYPES = [
52
52
  exports.INPUT_TYPE_BUFFER,
53
53
  ];
54
54
  class InputSource {
55
+ constructor() {
56
+ this.fileObject = "";
57
+ }
55
58
  async init() {
56
59
  throw new Error("not Implemented");
57
60
  }
@@ -1,3 +1,4 @@
1
+ /** Options to pass to the `parse` method for cutting multi-page documents. */
1
2
  export interface PageOptions {
2
3
  /**
3
4
  * Zero-based list of page indexes.
@@ -14,7 +15,10 @@ export interface PageOptions {
14
15
  */
15
16
  onMinPages: number;
16
17
  }
18
+ /** Operation to apply on the document, given the page indexes specified. */
17
19
  export declare enum PageOptionsOperation {
20
+ /** Only keep pages matching the provided indexes. */
18
21
  KeepOnly = "KEEP_ONLY",
22
+ /** Remove pages matching the provided indexes. */
19
23
  Remove = "REMOVE"
20
24
  }
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PageOptionsOperation = void 0;
4
+ /** Operation to apply on the document, given the page indexes specified. */
4
5
  var PageOptionsOperation;
5
6
  (function (PageOptionsOperation) {
7
+ /** Only keep pages matching the provided indexes. */
6
8
  PageOptionsOperation["KeepOnly"] = "KEEP_ONLY";
9
+ /** Remove pages matching the provided indexes. */
7
10
  PageOptionsOperation["Remove"] = "REMOVE";
8
11
  })(PageOptionsOperation = exports.PageOptionsOperation || (exports.PageOptionsOperation = {}));
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" />
3
3
  import { Readable } from "stream";
4
4
  import { Buffer } from "buffer";
5
- import { LocalInputSource } from "./base";
5
+ import { LocalInputSource, InputSource } from "./base";
6
6
  interface PathInputProps {
7
7
  inputPath: string;
8
8
  }
@@ -43,9 +43,8 @@ export declare class BytesInput extends LocalInputSource {
43
43
  constructor({ inputBytes, filename }: BytesInputProps);
44
44
  init(): Promise<void>;
45
45
  }
46
- export declare class UrlInput {
46
+ export declare class UrlInput extends InputSource {
47
47
  private readonly url;
48
- fileObject: string;
49
48
  constructor({ url }: {
50
49
  url: string;
51
50
  });
@@ -106,8 +106,9 @@ exports.BytesInput = BytesInput;
106
106
  //
107
107
  // URL
108
108
  //
109
- class UrlInput {
109
+ class UrlInput extends base_1.InputSource {
110
110
  constructor({ url }) {
111
+ super();
111
112
  this.url = url;
112
113
  }
113
114
  async init() {
@@ -0,0 +1,5 @@
1
+ export * as product from "./product/internal";
2
+ export * as parsing from "./parsing";
3
+ export * as pdf from "./pdf";
4
+ export * as geometry from "./geometry";
5
+ export * as http from "./http";
@@ -0,0 +1,31 @@
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.http = exports.geometry = exports.pdf = exports.parsing = exports.product = void 0;
27
+ exports.product = __importStar(require("./product/internal"));
28
+ exports.parsing = __importStar(require("./parsing"));
29
+ exports.pdf = __importStar(require("./pdf"));
30
+ exports.geometry = __importStar(require("./geometry"));
31
+ exports.http = __importStar(require("./http"));
@@ -1,10 +1,19 @@
1
1
  import { StringDict } from "./stringDict";
2
+ /**
3
+ * Holds the information relating to an API HTTP request.
4
+ *
5
+ * @category API Response
6
+ */
2
7
  export declare class ApiRequest {
8
+ /** An object detailing the error. */
3
9
  error: StringDict;
10
+ /** Target of the request. */
4
11
  resources: string[];
12
+ /** Status of the request. Either "failure" or "success". */
5
13
  status: "failure" | "success";
6
14
  /** HTTP status code */
7
15
  statusCode: number;
16
+ /** URL of the request. */
8
17
  url: string;
9
18
  constructor(serverResponse: StringDict);
10
19
  }