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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v4.1.0 - 2023-08-31
4
+ ### Changes
5
+ * :boom: remove support for node versions <16
6
+ * :sparkles: add support for auto-polling in asynchronous calls
7
+ * :sparkles: update HTTP error management system
8
+ * :sparkles: add possibility to access raw server response
9
+ * :arrow_up: add support for node 20
10
+ * :wrench: expose more internal tools
11
+ * :memo: add better technical documentation
12
+ * :recycle: remove deprecated examples
13
+ * :recycle: update linting rules
14
+ * :arrow_up: update testing library
15
+
16
+ ### Fixes
17
+ * :bug: fix miscellaneous bugs related to http parsing issues
18
+
19
+
20
+ ## v4.0.2 - 2023-08-24
21
+ ### Changes
22
+ * :recycle: updated technical documentation
23
+
24
+ ### Fixes
25
+ * :bug: fix url source document not being sent properly
26
+
27
+
3
28
  ## v4.0.1 - 2023-08-22
4
29
  ### Fixes
5
30
  * :bug: fixed pre-commit dependency script blocking installs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mindee",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "description": "Mindee Client Library for Node.js",
5
5
  "main": "src/index.js",
6
6
  "bin": "bin/mindee.js",
@@ -23,7 +23,7 @@
23
23
  "CHANGELOG.md"
24
24
  ],
25
25
  "engines": {
26
- "node": ">= 14"
26
+ "node": ">= 16"
27
27
  },
28
28
  "repository": {
29
29
  "type": "git",
@@ -46,9 +46,10 @@
46
46
  "@typescript-eslint/eslint-plugin": "^5.57.1",
47
47
  "@typescript-eslint/parser": "^5.57.1",
48
48
  "chai": "^4.3.7",
49
- "eslint": "^8.32.0",
49
+ "eslint": "^8.47.0",
50
50
  "lint-staged": "^13.2.3",
51
51
  "mocha": "^10.1.0",
52
+ "nock": "^13.3.3",
52
53
  "ts-node": "^10.9.1",
53
54
  "typedoc": "~0.23",
54
55
  "typescript": "^5.0.4"
package/src/cli.js CHANGED
@@ -193,8 +193,7 @@ function getPredictParams(options) {
193
193
  };
194
194
  return predictParams;
195
195
  }
196
- async function callParse(command, inputPath, options) {
197
- const conf = getConfig(command);
196
+ async function callParse(productClass, command, inputPath, options) {
198
197
  const mindeeClient = initClient(options);
199
198
  const predictParams = getPredictParams(options);
200
199
  const pageOptions = getPageOptions(options);
@@ -202,7 +201,7 @@ async function callParse(command, inputPath, options) {
202
201
  let response;
203
202
  if (command === COMMAND_CUSTOM) {
204
203
  const customEndpoint = mindeeClient.createEndpoint(options.endpoint, options.account, options.version);
205
- response = await mindeeClient.parse(conf.docClass, inputSource, {
204
+ response = await mindeeClient.parse(productClass, inputSource, {
206
205
  endpoint: customEndpoint,
207
206
  pageOptions: pageOptions,
208
207
  allWords: predictParams.allWords,
@@ -210,7 +209,7 @@ async function callParse(command, inputPath, options) {
210
209
  });
211
210
  }
212
211
  else {
213
- response = await mindeeClient.parse(conf.docClass, inputSource, {
212
+ response = await mindeeClient.parse(productClass, inputSource, {
214
213
  pageOptions: pageOptions,
215
214
  allWords: predictParams.allWords,
216
215
  cropper: predictParams.cropper,
@@ -218,8 +217,7 @@ async function callParse(command, inputPath, options) {
218
217
  }
219
218
  printResponse(response.document, options);
220
219
  }
221
- async function callEnqueue(command, inputPath, options) {
222
- const conf = getConfig(command);
220
+ async function callEnqueueAndParse(productClass, command, inputPath, options) {
223
221
  const mindeeClient = initClient(options);
224
222
  const predictParams = getPredictParams(options);
225
223
  const pageOptions = getPageOptions(options);
@@ -227,32 +225,30 @@ async function callEnqueue(command, inputPath, options) {
227
225
  let response;
228
226
  if (command === COMMAND_CUSTOM) {
229
227
  const customEndpoint = mindeeClient.createEndpoint(options.endpoint, options.account, options.version);
230
- response = await mindeeClient.enqueue(conf.docClass, inputSource, {
228
+ response = await mindeeClient.enqueueAndParse(productClass, inputSource, {
231
229
  endpoint: customEndpoint,
232
230
  pageOptions: pageOptions,
233
231
  allWords: predictParams.allWords,
234
232
  cropper: predictParams.cropper,
233
+ initialDelaySec: 6,
234
+ delaySec: 3,
235
+ maxRetries: 10,
235
236
  });
236
237
  }
237
238
  else {
238
- response = await mindeeClient.enqueue(conf.docClass, inputSource, {
239
+ response = await mindeeClient.enqueueAndParse(productClass, inputSource, {
239
240
  pageOptions: pageOptions,
240
241
  allWords: predictParams.allWords,
241
242
  cropper: predictParams.cropper,
243
+ initialDelaySec: 6,
244
+ delaySec: 3,
245
+ maxRetries: 10,
242
246
  });
243
- }
244
- console.log(response.job);
245
- }
246
- async function callParseQueued(command, queueId, options) {
247
- const conf = getConfig(command);
248
- const mindeeClient = initClient(options);
249
- const response = await mindeeClient.parseQueued(conf.docClass, queueId);
250
- if (response.document !== undefined) {
247
+ if (!response.document) {
248
+ throw Error("Document could not be retrieved");
249
+ }
251
250
  printResponse(response.document, options);
252
251
  }
253
- else {
254
- console.log(response.job);
255
- }
256
252
  }
257
253
  function printResponse(document, options) {
258
254
  if (options.allWords) {
@@ -291,20 +287,11 @@ function addDisplayOptions(prog) {
291
287
  prog.option("-p, --pages", "show content of individual pages");
292
288
  }
293
289
  function routeSwitchboard(command, inputPath, allOptions) {
294
- switch (command.parent?.name()) {
295
- case "parse": {
296
- return callParse(command.name(), inputPath, allOptions);
297
- }
298
- case "enqueue": {
299
- return callEnqueue(command.name(), inputPath, allOptions);
300
- }
301
- case "parse-queued": {
302
- return callParseQueued(command.name(), inputPath, allOptions);
303
- }
304
- default: {
305
- throw new Error("Unhandled parent command.");
306
- }
290
+ const docClass = getConfig(command.name()).docClass;
291
+ if ("async" in command.opts() && command.opts()["async"]) {
292
+ return callEnqueueAndParse(docClass, command.name(), inputPath, allOptions);
307
293
  }
294
+ return callParse(docClass, command.name(), inputPath, allOptions);
308
295
  }
309
296
  function addAction(prog) {
310
297
  if (prog.name() === COMMAND_CUSTOM) {
@@ -333,44 +320,27 @@ function addAction(prog) {
333
320
  function cli() {
334
321
  program.name("mindee");
335
322
  program.option("-d, --debug", "high verbosity mode");
336
- const predict = program.command("parse").description("Parse synchronously.");
337
- addMainOptions(predict);
338
- const enqueue = program
339
- .command("enqueue")
340
- .description("Add to async parse queue.");
341
- addMainOptions(enqueue);
342
- const parseQueued = program
343
- .command("parse-queued")
344
- .description("Parse from async queue.");
345
- addMainOptions(parseQueued);
346
323
  CLI_COMMAND_CONFIG.forEach((info, name) => {
347
- if (info.sync) {
348
- const prog = predict
349
- .command(name)
350
- .description(`Parse an ${info.displayName}.`);
351
- if (name === COMMAND_CUSTOM) {
352
- addCustomPostOptions(prog);
353
- }
354
- addDisplayOptions(prog);
355
- addPostOptions(prog, info);
356
- addAction(prog);
357
- }
324
+ const prog = program.command(name);
325
+ prog.description(`${info.displayName} document`);
358
326
  if (info.async) {
359
- const progEnqueue = enqueue
360
- .command(name)
361
- .description(`Add an ${info.displayName} to the queue.`);
362
- if (name === COMMAND_CUSTOM) {
363
- addCustomPostOptions(progEnqueue);
327
+ const asyncOpt = new commander_1.Option("-A, --async", "Call asynchronously");
328
+ if (info.sync) {
329
+ asyncOpt.default(false);
330
+ }
331
+ else {
332
+ asyncOpt.default(true);
333
+ asyncOpt.hideHelp();
364
334
  }
365
- addPostOptions(progEnqueue, info);
366
- addAction(progEnqueue);
367
- const progParse = parseQueued
368
- .command(name)
369
- .description(`Parse an ${info.displayName} from the queue.`)
370
- .argument("<doc_id>", "ID of the document");
371
- addDisplayOptions(progParse);
372
- addAction(progParse);
335
+ prog.addOption(asyncOpt);
336
+ }
337
+ if (name === COMMAND_CUSTOM) {
338
+ addCustomPostOptions(prog);
373
339
  }
340
+ addMainOptions(prog);
341
+ addDisplayOptions(prog);
342
+ addPostOptions(prog, info);
343
+ addAction(prog);
374
344
  });
375
345
  program.parse(process.argv);
376
346
  }
package/src/client.d.ts CHANGED
@@ -4,10 +4,11 @@ import { Readable } from "stream";
4
4
  import { InputSource, PageOptions } from "./input";
5
5
  import { Endpoint } from "./http";
6
6
  import { Inference, AsyncPredictResponse, StringDict, PredictResponse } from "./parsing/common";
7
+ /**
8
+ * Options relating to predictions.
9
+ */
7
10
  export interface PredictOptions {
8
- endpointName?: string;
9
- accountName?: string;
10
- endpointVersion?: string;
11
+ /** A custom endpoint. */
11
12
  endpoint?: Endpoint;
12
13
  /**
13
14
  * Whether to include the full text for each page.
@@ -21,18 +22,19 @@ export interface PredictOptions {
21
22
  * This performs a cropping operation on the server and will increase response time.
22
23
  */
23
24
  cropper?: boolean;
24
- pageOptions?: PageOptions;
25
- }
26
- export interface CustomConfigParams {
27
- /** Your organization's username on the API Builder. */
28
- accountName: string;
29
- /** The "API name" field in the "Settings" page of the API Builder. */
30
- endpointName: string;
31
25
  /**
32
- * If set, locks the version of the model to use.
33
- * If not set, use the latest version of the model.
26
+ * If set, remove pages from the document as specified.
27
+ * This is done before sending the file to the server and is useful to avoid page limitations.
34
28
  */
35
- version?: string;
29
+ pageOptions?: PageOptions;
30
+ }
31
+ /**
32
+ * Asynchronous polling parameters.
33
+ */
34
+ export interface AsyncOptions extends PredictOptions {
35
+ initialDelaySec: number;
36
+ delaySec: number;
37
+ maxRetries: number;
36
38
  }
37
39
  export interface ClientOptions {
38
40
  /** Your API key for all endpoints. */
@@ -43,35 +45,77 @@ export interface ClientOptions {
43
45
  debug?: boolean;
44
46
  }
45
47
  /**
46
- * Mindee Client
48
+ * Mindee Client class that centralizes most basic operations.
49
+ *
50
+ * @category Client
47
51
  */
48
52
  export declare class Client {
49
53
  #private;
54
+ /** Key of the API. */
50
55
  protected apiKey: string;
51
56
  /**
52
- * @param options
57
+ * @param options options for the initialization of a client.
53
58
  */
54
59
  constructor({ apiKey, throwOnError, debug }?: ClientOptions);
55
60
  /**
56
61
  * Send a document to a synchronous endpoint and parse the predictions.
57
- * @param productClass
58
- * @param params
62
+ *
63
+ * @param productClass product class to use for calling the API and parsing the response.
64
+ * @param inputSource document to parse.
65
+ * @param params parameters relating to prediction options.
66
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
67
+ * @category Synchronous
68
+ * @returns a `Promise` containing parsing results.
59
69
  */
60
70
  parse<T extends Inference>(productClass: new (httpResponse: StringDict) => T, inputSource: InputSource, params?: PredictOptions): Promise<PredictResponse<T>>;
61
71
  /**
62
72
  * Send the document to an asynchronous endpoint and return its ID in the queue.
63
- * @param productClass
64
- * @param params
73
+ * @param productClass product class to use for calling the API and parsing the response.
74
+ * @param params parameters relating to prediction options.
75
+ * @category Asynchronous
76
+ * @returns a `Promise` containing the job (queue) corresponding to a document.
65
77
  */
66
78
  enqueue<T extends Inference>(productClass: new (httpResponse: StringDict) => T, inputSource: InputSource, params?: PredictOptions): Promise<AsyncPredictResponse<T>>;
79
+ /**
80
+ * Polls a queue and returns its status as well as the prediction results if the parsing is done.
81
+ *
82
+ * @param productClass product class to use for calling the API and parsing the response.
83
+ * @param queueId id of the queue to poll.
84
+ * @param params parameters relating to prediction options.
85
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
86
+ * @category Asynchronous
87
+ * @returns a `Promise` containing a `Job`, which also contains a `Document` if the
88
+ * parsing is complete.
89
+ */
67
90
  parseQueued<T extends Inference>(productClass: new (httpResponse: StringDict) => T, queueId: string, params?: PredictOptions): Promise<AsyncPredictResponse<T>>;
91
+ /**
92
+ * Send a document to an asynchronous endpoint and poll the server until the result is sent or
93
+ * until the maximum amount of tries is reached.
94
+ *
95
+ * @param productClass product class to use for calling the API and parsing the response.
96
+ * @param inputSource document to parse.
97
+ * @param asyncParams parameters relating to prediction options.
98
+ *
99
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
100
+ * @category Synchronous
101
+ * @returns a `Promise` containing parsing results.
102
+ */
103
+ enqueueAndParse<T extends Inference>(productClass: new (httpResponse: StringDict) => T, inputSource: InputSource, asyncParams?: AsyncOptions): Promise<AsyncPredictResponse<T>>;
104
+ /**
105
+ * Forces boolean coercion on truthy/falsy parameters.
106
+ * @param param input parameter to check.
107
+ * @returns a strict boolean value.
108
+ */
68
109
  protected getBooleanParam(param?: boolean): boolean;
69
110
  /**
70
111
  * Creates a custom endpoint with the given values. Raises an error if the endpoint is invalid.
71
- * @param productClass Class of the product
72
- * @param endpointName Name of a custom Endpoint
73
- * @param accountName Name of the account tied to the active Endpoint
74
- * @param version Version of a custom Endpoint
112
+ * @param productClass product class to use for calling the API and parsing the response.
113
+ * @param endpointName Name of the custom Endpoint.
114
+ * @param accountName Name of the account tied to the Endpoint.
115
+ * @param version Version of the custom Endpoint.
116
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
117
+ *
118
+ * @returns a new endpoint
75
119
  */
76
120
  createEndpoint(endpointName: string, accountName: string, endpointVersion?: string): Endpoint;
77
121
  /**
@@ -81,31 +125,31 @@ export declare class Client {
81
125
  docFromPath(inputPath: string): InputSource;
82
126
  /**
83
127
  * Load an input document from a base64 encoded string.
84
- * @param inputString
85
- * @param filename
128
+ * @param inputString input content, as a string.
129
+ * @param filename file name.
86
130
  */
87
131
  docFromBase64(inputString: string, filename: string): InputSource;
88
132
  /**
89
133
  * Load an input document from a `stream.Readable` object.
90
- * @param inputStream
91
- * @param filename
134
+ * @param inputStream input content, as a readable stream.
135
+ * @param filename file name.
92
136
  */
93
137
  docFromStream(inputStream: Readable, filename: string): InputSource;
94
138
  /**
95
139
  * Load an input document from a bytes string.
96
- * @param inputBytes
97
- * @param filename
140
+ * @param inputBytes input content, as readable bytes.
141
+ * @param filename file name.
98
142
  */
99
143
  docFromBytes(inputBytes: string, filename: string): InputSource;
100
144
  /**
101
145
  * Load an input document from a URL.
102
- * @param url
146
+ * @param url input url. Must be HTTPS.
103
147
  */
104
148
  docFromUrl(url: string): InputSource;
105
149
  /**
106
150
  * Load an input document from a Buffer.
107
- * @param buffer
108
- * @param filename
151
+ * @param buffer input content, as a buffer.
152
+ * @param filename file name.
109
153
  */
110
154
  docFromBuffer(buffer: Buffer, filename: string): InputSource;
111
155
  }
package/src/client.js CHANGED
@@ -4,7 +4,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
4
4
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
5
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
6
  };
7
- var _Client_instances, _Client_buildEndpoint, _Client_initializeOTSEndpoint, _Client_cleanAccountName, _Client_getEndpoint;
7
+ var _Client_instances, _Client_validateAsyncParams, _Client_buildEndpoint, _Client_initializeOTSEndpoint, _Client_cleanAccountName, _Client_getEndpoint;
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.Client = void 0;
10
10
  const input_1 = require("./input");
@@ -14,12 +14,15 @@ const handler_1 = require("./errors/handler");
14
14
  const logger_1 = require("./logger");
15
15
  const inference_1 = require("./parsing/common/inference");
16
16
  const product_1 = require("./product");
17
+ const promises_1 = require("node:timers/promises");
17
18
  /**
18
- * Mindee Client
19
+ * Mindee Client class that centralizes most basic operations.
20
+ *
21
+ * @category Client
19
22
  */
20
23
  class Client {
21
24
  /**
22
- * @param options
25
+ * @param options options for the initialization of a client.
23
26
  */
24
27
  constructor({ apiKey, throwOnError, debug } = {
25
28
  apiKey: "",
@@ -37,8 +40,13 @@ class Client {
37
40
  }
38
41
  /**
39
42
  * Send a document to a synchronous endpoint and parse the predictions.
40
- * @param productClass
41
- * @param params
43
+ *
44
+ * @param productClass product class to use for calling the API and parsing the response.
45
+ * @param inputSource document to parse.
46
+ * @param params parameters relating to prediction options.
47
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
48
+ * @category Synchronous
49
+ * @returns a `Promise` containing parsing results.
42
50
  */
43
51
  async parse(productClass, inputSource, params = {
44
52
  endpoint: undefined,
@@ -60,8 +68,10 @@ class Client {
60
68
  }
61
69
  /**
62
70
  * Send the document to an asynchronous endpoint and return its ID in the queue.
63
- * @param productClass
64
- * @param params
71
+ * @param productClass product class to use for calling the API and parsing the response.
72
+ * @param params parameters relating to prediction options.
73
+ * @category Asynchronous
74
+ * @returns a `Promise` containing the job (queue) corresponding to a document.
65
75
  */
66
76
  async enqueue(productClass, inputSource, params = {}) {
67
77
  const endpoint = params?.endpoint ?? __classPrivateFieldGet(this, _Client_instances, "m", _Client_initializeOTSEndpoint).call(this, productClass);
@@ -76,20 +86,87 @@ class Client {
76
86
  });
77
87
  return new common_1.AsyncPredictResponse(productClass, rawResponse.data);
78
88
  }
89
+ /**
90
+ * Polls a queue and returns its status as well as the prediction results if the parsing is done.
91
+ *
92
+ * @param productClass product class to use for calling the API and parsing the response.
93
+ * @param queueId id of the queue to poll.
94
+ * @param params parameters relating to prediction options.
95
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
96
+ * @category Asynchronous
97
+ * @returns a `Promise` containing a `Job`, which also contains a `Document` if the
98
+ * parsing is complete.
99
+ */
79
100
  async parseQueued(productClass, queueId, params = {}) {
80
101
  const endpoint = params?.endpoint ?? __classPrivateFieldGet(this, _Client_instances, "m", _Client_initializeOTSEndpoint).call(this, productClass);
81
102
  const docResponse = await endpoint.getQueuedDocument(queueId);
82
103
  return new common_1.AsyncPredictResponse(productClass, docResponse.data);
83
104
  }
105
+ /**
106
+ * Send a document to an asynchronous endpoint and poll the server until the result is sent or
107
+ * until the maximum amount of tries is reached.
108
+ *
109
+ * @param productClass product class to use for calling the API and parsing the response.
110
+ * @param inputSource document to parse.
111
+ * @param asyncParams parameters relating to prediction options.
112
+ *
113
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
114
+ * @category Synchronous
115
+ * @returns a `Promise` containing parsing results.
116
+ */
117
+ async enqueueAndParse(productClass, inputSource, asyncParams = {
118
+ endpoint: undefined,
119
+ allWords: undefined,
120
+ cropper: undefined,
121
+ pageOptions: undefined,
122
+ initialDelaySec: 6,
123
+ delaySec: 3,
124
+ maxRetries: 10,
125
+ }) {
126
+ __classPrivateFieldGet(this, _Client_instances, "m", _Client_validateAsyncParams).call(this, asyncParams);
127
+ const enqueueResponse = await this.enqueue(productClass, inputSource, asyncParams);
128
+ if (enqueueResponse.job.id === undefined || enqueueResponse.job.id.length === 0) {
129
+ throw Error("Enqueueing of the document failed.");
130
+ }
131
+ const queueId = enqueueResponse.job.id;
132
+ logger_1.logger.debug(`Successfully enqueued document with job id: ${queueId}.`);
133
+ await (0, promises_1.setTimeout)(asyncParams.initialDelaySec * 1000);
134
+ let retryCounter = 1;
135
+ let pollResults;
136
+ pollResults = await this.parseQueued(productClass, queueId, asyncParams);
137
+ while (retryCounter < asyncParams.maxRetries) {
138
+ logger_1.logger.debug(`Polling server for parsing result with queueId: ${queueId}.
139
+ Attempt n°${retryCounter}/${asyncParams.maxRetries}.
140
+ Job status: ${pollResults.job.status}.`);
141
+ if (pollResults.job.status === "completed") {
142
+ break;
143
+ }
144
+ await (0, promises_1.setTimeout)(asyncParams.delaySec * 1000);
145
+ pollResults = await this.parseQueued(productClass, queueId, asyncParams);
146
+ retryCounter++;
147
+ }
148
+ if (pollResults.job.status !== "completed") {
149
+ throw Error(`Asynchronous parsing request timed out after ${asyncParams.delaySec * retryCounter} seconds`);
150
+ }
151
+ return pollResults;
152
+ }
153
+ /**
154
+ * Forces boolean coercion on truthy/falsy parameters.
155
+ * @param param input parameter to check.
156
+ * @returns a strict boolean value.
157
+ */
84
158
  getBooleanParam(param) {
85
159
  return param !== undefined ? param : false;
86
160
  }
87
161
  /**
88
162
  * Creates a custom endpoint with the given values. Raises an error if the endpoint is invalid.
89
- * @param productClass Class of the product
90
- * @param endpointName Name of a custom Endpoint
91
- * @param accountName Name of the account tied to the active Endpoint
92
- * @param version Version of a custom Endpoint
163
+ * @param productClass product class to use for calling the API and parsing the response.
164
+ * @param endpointName Name of the custom Endpoint.
165
+ * @param accountName Name of the account tied to the Endpoint.
166
+ * @param version Version of the custom Endpoint.
167
+ * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
168
+ *
169
+ * @returns a new endpoint
93
170
  */
94
171
  createEndpoint(endpointName, accountName, endpointVersion) {
95
172
  const cleanAccountName = __classPrivateFieldGet(this, _Client_instances, "m", _Client_cleanAccountName).call(this, product_1.CustomV1, accountName);
@@ -117,8 +194,8 @@ class Client {
117
194
  }
118
195
  /**
119
196
  * Load an input document from a base64 encoded string.
120
- * @param inputString
121
- * @param filename
197
+ * @param inputString input content, as a string.
198
+ * @param filename file name.
122
199
  */
123
200
  docFromBase64(inputString, filename) {
124
201
  return new input_1.Base64Input({
@@ -128,8 +205,8 @@ class Client {
128
205
  }
129
206
  /**
130
207
  * Load an input document from a `stream.Readable` object.
131
- * @param inputStream
132
- * @param filename
208
+ * @param inputStream input content, as a readable stream.
209
+ * @param filename file name.
133
210
  */
134
211
  docFromStream(inputStream, filename) {
135
212
  return new input_1.StreamInput({
@@ -139,8 +216,8 @@ class Client {
139
216
  }
140
217
  /**
141
218
  * Load an input document from a bytes string.
142
- * @param inputBytes
143
- * @param filename
219
+ * @param inputBytes input content, as readable bytes.
220
+ * @param filename file name.
144
221
  */
145
222
  docFromBytes(inputBytes, filename) {
146
223
  return new input_1.BytesInput({
@@ -150,7 +227,7 @@ class Client {
150
227
  }
151
228
  /**
152
229
  * Load an input document from a URL.
153
- * @param url
230
+ * @param url input url. Must be HTTPS.
154
231
  */
155
232
  docFromUrl(url) {
156
233
  return new input_1.UrlInput({
@@ -159,8 +236,8 @@ class Client {
159
236
  }
160
237
  /**
161
238
  * Load an input document from a Buffer.
162
- * @param buffer
163
- * @param filename
239
+ * @param buffer input content, as a buffer.
240
+ * @param filename file name.
164
241
  */
165
242
  docFromBuffer(buffer, filename) {
166
243
  return new input_1.BufferInput({
@@ -170,7 +247,17 @@ class Client {
170
247
  }
171
248
  }
172
249
  exports.Client = Client;
173
- _Client_instances = new WeakSet(), _Client_buildEndpoint = function _Client_buildEndpoint(endpointName, accountName, endpointVersion) {
250
+ _Client_instances = new WeakSet(), _Client_validateAsyncParams = function _Client_validateAsyncParams(asyncParams) {
251
+ if (asyncParams.delaySec < 2) {
252
+ throw Error("Cannot set auto-parsing delay to less than 2 seconds.");
253
+ }
254
+ if (asyncParams.initialDelaySec < 4) {
255
+ throw Error("Cannot set initial parsing delay to less than 4 seconds.");
256
+ }
257
+ if (!Number.isInteger(asyncParams.maxRetries)) {
258
+ throw Error("Retry amount must be an integer.");
259
+ }
260
+ }, _Client_buildEndpoint = function _Client_buildEndpoint(endpointName, accountName, endpointVersion) {
174
261
  const apiSettings = new http_1.MindeeApi({
175
262
  apiKey: this.apiKey,
176
263
  urlName: endpointName,