mindee 5.5.0 → 5.6.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 (209) hide show
  1. package/CHANGELOG.md +17 -2
  2. package/package.json +26 -14
  3. package/src/geometry/bbox.d.ts +5 -0
  4. package/src/geometry/bbox.js +1 -0
  5. package/src/geometry/minMax.d.ts +3 -0
  6. package/src/geometry/polygonUtils.d.ts +23 -0
  7. package/src/geometry/polygonUtils.js +23 -0
  8. package/src/http/baseSettings.d.ts +5 -0
  9. package/src/http/baseSettings.js +2 -0
  10. package/src/image/extractedImage.d.ts +4 -0
  11. package/src/image/extractedImages.d.ts +1 -0
  12. package/src/image/extractedImages.js +1 -0
  13. package/src/image/imageCompressor.d.ts +1 -1
  14. package/src/image/imageExtractor.d.ts +5 -6
  15. package/src/image/imageExtractor.js +71 -73
  16. package/src/input/base64Input.d.ts +3 -0
  17. package/src/input/base64Input.js +3 -0
  18. package/src/input/bufferInput.d.ts +2 -0
  19. package/src/input/bufferInput.js +2 -0
  20. package/src/input/bytesInput.d.ts +3 -0
  21. package/src/input/bytesInput.js +3 -0
  22. package/src/input/inputSource.d.ts +15 -0
  23. package/src/input/inputSource.js +15 -0
  24. package/src/input/localInputSource.d.ts +6 -0
  25. package/src/input/localInputSource.js +3 -0
  26. package/src/input/pathInput.d.ts +4 -0
  27. package/src/input/pathInput.js +3 -0
  28. package/src/input/streamInput.d.ts +4 -0
  29. package/src/input/streamInput.js +4 -0
  30. package/src/input/urlInput.d.ts +12 -0
  31. package/src/input/urlInput.js +52 -10
  32. package/src/logger.js +1 -1
  33. package/src/parsing/dateParser.d.ts +4 -0
  34. package/src/parsing/dateParser.js +5 -1
  35. package/src/parsing/localResponseBase.d.ts +2 -0
  36. package/src/parsing/localResponseBase.js +2 -0
  37. package/src/parsing/stringDict.d.ts +1 -0
  38. package/src/pdf/extractedPdf.d.ts +5 -0
  39. package/src/pdf/extractedPdf.js +1 -0
  40. package/src/pdf/extractedPdfs.d.ts +1 -0
  41. package/src/pdf/extractedPdfs.js +1 -0
  42. package/src/pdf/pdfCompressor.js +12 -0
  43. package/src/pdf/pdfExtractor.js +3 -0
  44. package/src/pdf/pdfOperation.d.ts +3 -0
  45. package/src/pdf/pdfOperation.js +3 -0
  46. package/src/pdf/pdfUtils.js +5 -0
  47. package/src/v1/cli.d.ts +3 -0
  48. package/src/v1/cli.js +84 -26
  49. package/src/v1/client.d.ts +16 -16
  50. package/src/v1/extraction/invoiceSplitterExtractor/extractedInvoiceSplitterImage.d.ts +2 -0
  51. package/src/v1/extraction/invoiceSplitterExtractor/invoiceSplitterExtractor.js +14 -0
  52. package/src/v1/extraction/multiReceiptsExtractor/extractedMultiReceiptImage.d.ts +2 -0
  53. package/src/v1/extraction/multiReceiptsExtractor/multiReceiptsExtractor.js +3 -0
  54. package/src/v1/http/apiSettingsV1.d.ts +5 -0
  55. package/src/v1/http/apiSettingsV1.js +4 -0
  56. package/src/v1/http/errors.d.ts +7 -1
  57. package/src/v1/http/errors.js +65 -86
  58. package/src/v1/http/httpParams.d.ts +12 -0
  59. package/src/v1/parsing/common/asyncPredictResponse.d.ts +4 -0
  60. package/src/v1/parsing/common/asyncPredictResponse.js +5 -2
  61. package/src/v1/parsing/common/executionPriority.d.ts +4 -0
  62. package/src/v1/parsing/common/executionPriority.js +4 -0
  63. package/src/v1/parsing/common/extras/cropperExtra.d.ts +2 -0
  64. package/src/v1/parsing/common/extras/cropperExtra.js +3 -0
  65. package/src/v1/parsing/common/extras/extras.d.ts +3 -0
  66. package/src/v1/parsing/common/extras/extras.js +2 -0
  67. package/src/v1/parsing/common/extras/fullTextOcrExtra.d.ts +3 -0
  68. package/src/v1/parsing/common/extras/fullTextOcrExtra.js +1 -0
  69. package/src/v1/parsing/common/extras/ragExtra.d.ts +1 -0
  70. package/src/v1/parsing/common/extras/ragExtra.js +1 -0
  71. package/src/v1/parsing/common/feedback/feedbackResponse.js +1 -0
  72. package/src/v1/parsing/common/index.d.ts +1 -1
  73. package/src/v1/parsing/common/index.js +1 -1
  74. package/src/v1/parsing/common/inference.d.ts +2 -0
  75. package/src/v1/parsing/common/inference.js +2 -0
  76. package/src/v1/parsing/common/prediction.d.ts +1 -0
  77. package/src/v1/parsing/common/prediction.js +1 -0
  78. package/src/v1/parsing/common/summaryHelper.d.ts +12 -1
  79. package/src/v1/parsing/common/summaryHelper.js +18 -1
  80. package/src/v1/parsing/generated/generatedObject.d.ts +6 -0
  81. package/src/v1/parsing/generated/generatedObject.js +38 -27
  82. package/src/v1/parsing/localResponse.d.ts +1 -0
  83. package/src/v1/parsing/localResponse.js +1 -0
  84. package/src/v1/parsing/standard/base.d.ts +6 -0
  85. package/src/v1/parsing/standard/base.js +1 -0
  86. package/src/v1/parsing/standard/boolean.d.ts +1 -1
  87. package/src/v1/parsing/standard/companyRegistration.d.ts +1 -0
  88. package/src/v1/parsing/standard/companyRegistration.js +1 -0
  89. package/src/v1/parsing/standard/date.d.ts +1 -0
  90. package/src/v1/parsing/standard/date.js +1 -0
  91. package/src/v1/parsing/standard/paymentDetails.d.ts +5 -5
  92. package/src/v1/parsing/standard/position.d.ts +2 -2
  93. package/src/v1/parsing/standard/tax.d.ts +1 -0
  94. package/src/v1/parsing/standard/tax.js +1 -0
  95. package/src/v1/parsing/standard/text.d.ts +1 -1
  96. package/src/v1/parsing/standard/word.d.ts +3 -0
  97. package/src/v1/parsing/standard/word.js +1 -0
  98. package/src/v1/product/financialDocument/financialDocumentV1LineItem.js +16 -58
  99. package/src/v1/product/generated/generatedV1Prediction.d.ts +1 -0
  100. package/src/v1/product/generated/generatedV1Prediction.js +2 -1
  101. package/src/v1/product/invoice/invoiceV4LineItem.js +5 -16
  102. package/src/v1/product/receipt/receiptV5LineItem.js +2 -6
  103. package/src/v1/product/resume/resumeV1Certificate.js +5 -21
  104. package/src/v1/product/resume/resumeV1Education.js +8 -36
  105. package/src/v1/product/resume/resumeV1Language.js +3 -11
  106. package/src/v1/product/resume/resumeV1ProfessionalExperience.js +10 -46
  107. package/src/v1/product/resume/resumeV1SocialNetworksUrl.js +3 -11
  108. package/src/v2/cli.d.ts +3 -0
  109. package/src/v2/cli.js +21 -0
  110. package/src/v2/client.d.ts +1 -0
  111. package/src/v2/client.js +1 -0
  112. package/src/v2/clientOptions/pollingOptions.d.ts +5 -0
  113. package/src/v2/clientOptions/pollingOptions.js +2 -0
  114. package/src/v2/parsing/baseResponse.d.ts +2 -0
  115. package/src/v2/parsing/baseResponse.js +1 -0
  116. package/src/v2/parsing/error/errorDetails.d.ts +1 -0
  117. package/src/v2/parsing/index.d.ts +1 -1
  118. package/src/v2/parsing/index.js +1 -1
  119. package/src/v2/parsing/inference/baseInference.d.ts +2 -0
  120. package/src/v2/parsing/inference/baseInference.js +2 -0
  121. package/src/v2/parsing/inference/failedInferenceResponse.d.ts +33 -0
  122. package/src/v2/parsing/inference/failedInferenceResponse.js +17 -0
  123. package/src/v2/parsing/inference/field/baseField.d.ts +3 -0
  124. package/src/v2/parsing/inference/field/fieldConfidence.d.ts +4 -0
  125. package/src/v2/parsing/inference/field/fieldConfidence.js +4 -0
  126. package/src/v2/parsing/inference/field/fieldFactory.d.ts +5 -0
  127. package/src/v2/parsing/inference/field/fieldFactory.js +5 -0
  128. package/src/v2/parsing/inference/field/fieldLocation.d.ts +1 -0
  129. package/src/v2/parsing/inference/field/fieldLocation.js +1 -0
  130. package/src/v2/parsing/inference/field/inferenceFields.d.ts +5 -0
  131. package/src/v2/parsing/inference/field/inferenceFields.js +4 -0
  132. package/src/v2/parsing/inference/field/listField.d.ts +2 -0
  133. package/src/v2/parsing/inference/field/listField.js +2 -0
  134. package/src/v2/parsing/inference/field/objectField.d.ts +4 -0
  135. package/src/v2/parsing/inference/field/objectField.js +3 -0
  136. package/src/v2/parsing/inference/field/ragMetadata.d.ts +1 -0
  137. package/src/v2/parsing/inference/field/ragMetadata.js +1 -0
  138. package/src/v2/parsing/inference/field/rawText.d.ts +2 -0
  139. package/src/v2/parsing/inference/field/rawText.js +2 -0
  140. package/src/v2/parsing/inference/field/simpleField.d.ts +2 -0
  141. package/src/v2/parsing/inference/field/simpleField.js +2 -0
  142. package/src/v2/parsing/inference/inferenceFile.d.ts +2 -0
  143. package/src/v2/parsing/inference/inferenceFile.js +2 -0
  144. package/src/v2/parsing/inference/inferenceModel.d.ts +2 -0
  145. package/src/v2/parsing/inference/inferenceModel.js +2 -0
  146. package/src/v2/parsing/job/job.d.ts +1 -1
  147. package/src/v2/parsing/job/job.js +1 -3
  148. package/src/v2/parsing/job/jobResponse.d.ts +3 -0
  149. package/src/v2/parsing/job/jobResponse.js +3 -0
  150. package/src/v2/product/classification/classification.d.ts +3 -0
  151. package/src/v2/product/classification/classification.js +3 -0
  152. package/src/v2/product/classification/classificationClassifier.d.ts +2 -0
  153. package/src/v2/product/classification/classificationClassifier.js +2 -0
  154. package/src/v2/product/classification/classificationInference.d.ts +2 -0
  155. package/src/v2/product/classification/classificationInference.js +2 -0
  156. package/src/v2/product/classification/classificationResponse.d.ts +1 -0
  157. package/src/v2/product/classification/classificationResponse.js +1 -0
  158. package/src/v2/product/classification/classificationResult.d.ts +2 -0
  159. package/src/v2/product/classification/classificationResult.js +2 -0
  160. package/src/v2/product/crop/crop.d.ts +3 -0
  161. package/src/v2/product/crop/crop.js +3 -0
  162. package/src/v2/product/crop/cropInference.d.ts +2 -0
  163. package/src/v2/product/crop/cropInference.js +2 -0
  164. package/src/v2/product/crop/cropItem.d.ts +2 -0
  165. package/src/v2/product/crop/cropItem.js +2 -0
  166. package/src/v2/product/crop/cropResponse.d.ts +1 -0
  167. package/src/v2/product/crop/cropResponse.js +1 -0
  168. package/src/v2/product/crop/cropResult.d.ts +2 -0
  169. package/src/v2/product/crop/cropResult.js +2 -0
  170. package/src/v2/product/extraction/dataSchemaActiveOption.d.ts +1 -0
  171. package/src/v2/product/extraction/dataSchemaActiveOption.js +1 -0
  172. package/src/v2/product/extraction/extraction.d.ts +3 -0
  173. package/src/v2/product/extraction/extraction.js +3 -0
  174. package/src/v2/product/extraction/extractionActiveOptions.d.ts +2 -0
  175. package/src/v2/product/extraction/extractionActiveOptions.js +2 -0
  176. package/src/v2/product/extraction/extractionInference.d.ts +2 -0
  177. package/src/v2/product/extraction/extractionInference.js +2 -0
  178. package/src/v2/product/extraction/extractionResponse.d.ts +1 -0
  179. package/src/v2/product/extraction/extractionResponse.js +1 -0
  180. package/src/v2/product/extraction/extractionResult.d.ts +2 -0
  181. package/src/v2/product/extraction/extractionResult.js +2 -0
  182. package/src/v2/product/extraction/params/dataSchema.d.ts +9 -6
  183. package/src/v2/product/extraction/params/dataSchema.js +2 -0
  184. package/src/v2/product/extraction/params/dataSchemaField.d.ts +25 -1
  185. package/src/v2/product/extraction/params/dataSchemaField.js +3 -0
  186. package/src/v2/product/extraction/params/dataSchemaReplace.d.ts +9 -4
  187. package/src/v2/product/extraction/params/dataSchemaReplace.js +2 -0
  188. package/src/v2/product/ocr/ocr.d.ts +3 -0
  189. package/src/v2/product/ocr/ocr.js +3 -0
  190. package/src/v2/product/ocr/ocrInference.d.ts +2 -0
  191. package/src/v2/product/ocr/ocrInference.js +2 -0
  192. package/src/v2/product/ocr/ocrPage.d.ts +2 -0
  193. package/src/v2/product/ocr/ocrPage.js +2 -0
  194. package/src/v2/product/ocr/ocrResponse.d.ts +1 -0
  195. package/src/v2/product/ocr/ocrResponse.js +1 -0
  196. package/src/v2/product/ocr/ocrResult.d.ts +1 -0
  197. package/src/v2/product/ocr/ocrResult.js +1 -0
  198. package/src/v2/product/ocr/ocrWord.d.ts +2 -0
  199. package/src/v2/product/ocr/ocrWord.js +2 -0
  200. package/src/v2/product/split/split.d.ts +3 -0
  201. package/src/v2/product/split/split.js +3 -0
  202. package/src/v2/product/split/splitInference.d.ts +2 -0
  203. package/src/v2/product/split/splitInference.js +2 -0
  204. package/src/v2/product/split/splitRange.d.ts +1 -0
  205. package/src/v2/product/split/splitRange.js +1 -0
  206. package/src/v2/product/split/splitResponse.d.ts +1 -0
  207. package/src/v2/product/split/splitResponse.js +1 -0
  208. package/src/v2/product/split/splitResult.d.ts +1 -0
  209. package/src/v2/product/split/splitResult.js +1 -0
@@ -60,34 +60,34 @@ export interface WorkflowOptions extends BaseOptions {
60
60
  */
61
61
  publicUrl?: string;
62
62
  }
63
+ /** Timer options forwarded to `setTimeout` in polling loops. */
64
+ export interface TimerOptions {
65
+ /** Whether the timer should keep the event loop active. */
66
+ ref?: boolean;
67
+ /** Optional signal used to abort timer waits. */
68
+ signal?: AbortSignal;
69
+ }
63
70
  /**
64
71
  * Asynchronous polling parameters.
65
72
  */
66
73
  export interface OptionalAsyncOptions extends PredictOptions {
74
+ /** Delay in seconds before the first polling attempt. */
67
75
  initialDelaySec?: number;
76
+ /** Delay in seconds between polling attempts. */
68
77
  delaySec?: number;
78
+ /** Maximum number of polling attempts. */
69
79
  maxRetries?: number;
70
- initialTimerOptions?: {
71
- ref?: boolean;
72
- signal?: AbortSignal;
73
- };
74
- recurringTimerOptions?: {
75
- ref?: boolean;
76
- signal?: AbortSignal;
77
- };
80
+ /** Timer options used for the first delay. */
81
+ initialTimerOptions?: TimerOptions;
82
+ /** Timer options used for recurring polling delays. */
83
+ recurringTimerOptions?: TimerOptions;
78
84
  }
79
85
  export interface AsyncOptions extends PredictOptions {
80
86
  initialDelaySec: number;
81
87
  delaySec: number;
82
88
  maxRetries: number;
83
- initialTimerOptions?: {
84
- ref?: boolean;
85
- signal?: AbortSignal;
86
- };
87
- recurringTimerOptions?: {
88
- ref?: boolean;
89
- signal?: AbortSignal;
90
- };
89
+ initialTimerOptions?: TimerOptions;
90
+ recurringTimerOptions?: TimerOptions;
91
91
  }
92
92
  export interface ClientOptions {
93
93
  /** Your API key for all endpoints. */
@@ -3,7 +3,9 @@ import { ExtractedPdf } from "../../../pdf/index.js";
3
3
  * Wrapper class for extracted invoice pages.
4
4
  */
5
5
  export declare class ExtractedInvoiceSplitterImage extends ExtractedPdf {
6
+ /** Start page index of the extracted invoice segment. */
6
7
  readonly pageIdMin: number;
8
+ /** End page index of the extracted invoice segment. */
7
9
  readonly pageIdMax: number;
8
10
  constructor(bytes: Uint8Array, pageIndices: [number, number]);
9
11
  }
@@ -3,6 +3,9 @@ import { InvoiceSplitterV1 } from "../../../v1/product/index.js";
3
3
  import { ExtractedInvoiceSplitterImage } from "../../../v1/extraction/index.js";
4
4
  import { loadOptionalDependency } from "../../../dependency/index.js";
5
5
  let pdfLib = null;
6
+ /**
7
+ * Load the pdf-lib dependency.
8
+ */
6
9
  async function getPdfLib() {
7
10
  if (!pdfLib) {
8
11
  const pdfLibImport = await loadOptionalDependency("@cantoo/pdf-lib", "Text Embedding");
@@ -10,6 +13,12 @@ async function getPdfLib() {
10
13
  }
11
14
  return pdfLib;
12
15
  }
16
+ /**
17
+ * Split the given PDF document into multiple PDF documents.
18
+ * @param pdfDoc PDF document.
19
+ * @param invoicePageGroups Page groups.
20
+ * @returns Extracted invoice splitter images.
21
+ */
13
22
  async function splitPdf(pdfDoc, invoicePageGroups) {
14
23
  const pdfLib = await getPdfLib();
15
24
  if (invoicePageGroups.length === 0) {
@@ -31,6 +40,11 @@ async function splitPdf(pdfDoc, invoicePageGroups) {
31
40
  }
32
41
  return generatedPdfs;
33
42
  }
43
+ /**
44
+ * Load the PDF document from the given input file.
45
+ * @param inputFile Input file.
46
+ * @returns PDF document.
47
+ */
34
48
  async function getPdfDoc(inputFile) {
35
49
  const pdfLib = await getPdfLib();
36
50
  await inputFile.init();
@@ -3,7 +3,9 @@ import { ExtractedImage } from "../../../image/index.js";
3
3
  * Wrapper class for extracted multiple-receipts images.
4
4
  */
5
5
  export declare class ExtractedMultiReceiptImage extends ExtractedImage {
6
+ /** Receipt index within the source page. */
6
7
  readonly receiptId: number;
8
+ /** Source page identifier. */
7
9
  readonly pageId: number;
8
10
  constructor(buffer: Uint8Array, pageId: number, receiptId: number);
9
11
  }
@@ -4,6 +4,9 @@ import { ExtractedMultiReceiptImage } from "../../../v1/extraction/index.js";
4
4
  import { extractFromPage } from "../../../image/index.js";
5
5
  import { loadOptionalDependency } from "../../../dependency/index.js";
6
6
  let pdfLib = null;
7
+ /**
8
+ * Load the pdf-lib dependency.
9
+ */
7
10
  async function getPdfLib() {
8
11
  if (!pdfLib) {
9
12
  const pdfLibImport = await loadOptionalDependency("@cantoo/pdf-lib", "Text Embedding");
@@ -1,8 +1,13 @@
1
1
  import { BaseSettings, MindeeApiConstructorProps } from "../../http/baseSettings.js";
2
+ /** Default account owner for built-in v1 APIs. */
2
3
  export declare const STANDARD_API_OWNER: string;
4
+ /** Runtime settings container for v1 API calls. */
3
5
  export declare class ApiSettingsV1 extends BaseSettings {
6
+ /** Default headers sent with each v1 API request. */
4
7
  baseHeaders: Record<string, string>;
5
8
  constructor({ apiKey, dispatcher, }: MindeeApiConstructorProps);
9
+ /** Resolves the API key from environment variables. */
6
10
  protected apiKeyFromEnv(): string;
11
+ /** Resolves the API hostname from environment variables. */
7
12
  protected hostnameFromEnv(): string;
8
13
  }
@@ -1,10 +1,12 @@
1
1
  import { logger } from "../../logger.js";
2
2
  import { BaseSettings } from "../../http/baseSettings.js";
3
3
  import { MindeeConfigurationError } from "../../errors/index.js";
4
+ /** Default account owner for built-in v1 APIs. */
4
5
  export const STANDARD_API_OWNER = "mindee";
5
6
  const API_V1_KEY_ENVVAR_NAME = "MINDEE_API_KEY";
6
7
  const API_V1_HOST_ENVVAR_NAME = "MINDEE_API_HOST";
7
8
  const DEFAULT_MINDEE_API_HOST = "api.mindee.net";
9
+ /** Runtime settings container for v1 API calls. */
8
10
  export class ApiSettingsV1 extends BaseSettings {
9
11
  constructor({ apiKey, dispatcher, }) {
10
12
  super(apiKey, dispatcher);
@@ -18,6 +20,7 @@ export class ApiSettingsV1 extends BaseSettings {
18
20
  Authorization: `Token ${this.apiKey}`,
19
21
  };
20
22
  }
23
+ /** Resolves the API key from environment variables. */
21
24
  apiKeyFromEnv() {
22
25
  const envVarValue = process.env[API_V1_KEY_ENVVAR_NAME];
23
26
  if (envVarValue) {
@@ -26,6 +29,7 @@ export class ApiSettingsV1 extends BaseSettings {
26
29
  }
27
30
  return "";
28
31
  }
32
+ /** Resolves the API hostname from environment variables. */
29
33
  hostnameFromEnv() {
30
34
  const envVarValue = process.env[API_V1_HOST_ENVVAR_NAME];
31
35
  if (envVarValue) {
@@ -1,6 +1,12 @@
1
1
  import { MindeeError } from "../../errors/index.js";
2
2
  import { StringDict } from "../../parsing/stringDict.js";
3
- import { BaseHttpResponse } from "../../http/apiCore.js";
3
+ import { BaseHttpResponse } from "../../http/index.js";
4
+ /**
5
+ * Handle the given error.
6
+ * @param urlName URL name.
7
+ * @param response Response.
8
+ * @param serverError Server error.
9
+ */
4
10
  export declare function handleError(urlName: string, response: BaseHttpResponse, serverError?: string): void;
5
11
  /**
6
12
  * `Error` wrapper for server (HTTP) errors.
@@ -1,109 +1,80 @@
1
1
  import { MindeeError } from "../../errors/index.js";
2
2
  import { errorHandler } from "../../errors/handler.js";
3
- export function handleError(urlName, response, serverError) {
4
- let code;
3
+ const HTML_ERROR_PATTERNS = [
4
+ ["Maximum pdf pages", { message: "TooManyPages", details: "Maximum amount of pdf pages reached." }],
5
+ ["Max file size is", { message: "FileTooLarge", details: "Maximum file size reached." }],
6
+ ["Invalid file type", { message: "InvalidFiletype", details: "Invalid file type." }],
7
+ ["Gateway timeout", { message: "RequestTimeout", details: "Request timed out." }],
8
+ ["Bad gateway", { message: "BadRequest", details: "Bad Gateway" }],
9
+ ["Too Many Requests", { message: "TooManyRequests", details: "Too Many Requests." }],
10
+ ];
11
+ const ERROR_CLASSES_BY_CODE = new Map();
12
+ /**
13
+ * Extract the status code from the given response.
14
+ * @param response Response.
15
+ * @returns Status code.
16
+ */
17
+ function extractStatusCode(response) {
5
18
  try {
6
19
  if (response.data.api_request["status_code"] === 200 && response.data?.job?.error?.code) {
7
- code = 500;
8
20
  response.data.api_request.error = response.data.job.error;
21
+ return 500;
9
22
  }
10
- else if (response.data) {
11
- code = response.data.api_request["status_code"];
23
+ if (response.data) {
24
+ return response.data.api_request["status_code"];
12
25
  }
13
26
  }
14
27
  catch {
15
- code = 500;
28
+ return 500;
29
+ }
30
+ return undefined;
31
+ }
32
+ /**
33
+ * Extract the error object from an HTML error response body.
34
+ * @param reconstructed Reconstructed response body.
35
+ * @returns Error object.
36
+ */
37
+ function errorObjFromHtml(reconstructed) {
38
+ for (const [needle, obj] of HTML_ERROR_PATTERNS) {
39
+ if (reconstructed.includes(needle)) {
40
+ return { ...obj };
41
+ }
16
42
  }
17
- let errorObj;
43
+ return { message: "Unknown Server Error.", details: reconstructed };
44
+ }
45
+ /**
46
+ * Extract the error object from the given response.
47
+ * @param response Response.
48
+ * @returns Error object.
49
+ */
50
+ function extractErrorObj(response) {
18
51
  try {
19
- //Regular instances where the returned error is in JSON format.
20
- errorObj = response.data.api_request.error;
52
+ // Regular instances where the returned error is in JSON format.
53
+ return response.data.api_request.error;
21
54
  }
22
55
  catch {
23
- //Rare instances where errors are returned as HTML instead of JSON.
56
+ // Rare instances where errors are returned as HTML instead of JSON.
24
57
  if (!("reconstructedResponse" in response.data)) {
25
58
  response.data.reconstructedResponse = "";
26
59
  }
27
- if (response.data.reconstructedResponse.includes("Maximum pdf pages")) {
28
- errorObj = {
29
- message: "TooManyPages",
30
- details: "Maximum amount of pdf pages reached.",
31
- };
32
- }
33
- else if (response.data.reconstructedResponse.includes("Max file size is")) {
34
- errorObj = {
35
- message: "FileTooLarge",
36
- details: "Maximum file size reached.",
37
- };
38
- }
39
- else if (response.data.reconstructedResponse.includes("Invalid file type")) {
40
- errorObj = {
41
- message: "InvalidFiletype",
42
- details: "Invalid file type.",
43
- };
44
- }
45
- else if (response.data.reconstructedResponse.includes("Gateway timeout")) {
46
- errorObj = {
47
- message: "RequestTimeout",
48
- details: "Request timed out.",
49
- };
50
- }
51
- else if (response.data.reconstructedResponse.includes("Bad gateway")) {
52
- errorObj = {
53
- message: "BadRequest",
54
- details: "Bad Gateway",
55
- };
56
- }
57
- else if (response.data.reconstructedResponse.includes("Too Many Requests")) {
58
- errorObj = {
59
- message: "TooManyRequests",
60
- details: "Too Many Requests.",
61
- };
62
- }
63
- else {
64
- errorObj = {
65
- message: "Unknown Server Error.",
66
- details: response.data.reconstructedResponse,
67
- };
68
- }
60
+ return errorObjFromHtml(response.data.reconstructedResponse);
69
61
  }
62
+ }
63
+ /**
64
+ * Handle the given error.
65
+ * @param urlName URL name.
66
+ * @param response Response.
67
+ * @param serverError Server error.
68
+ */
69
+ export function handleError(urlName, response, serverError) {
70
+ const code = extractStatusCode(response);
71
+ const errorObj = extractErrorObj(response);
70
72
  if (serverError !== undefined &&
71
- (!("message" in errorObj) ||
72
- !errorObj.message ||
73
- errorObj.message.length === 0)) {
73
+ (!("message" in errorObj) || !errorObj.message || errorObj.message.length === 0)) {
74
74
  errorObj.message = serverError;
75
75
  }
76
- let errorToThrow;
77
- switch (code) {
78
- case 400:
79
- errorToThrow = new MindeeHttp400Error(errorObj, urlName, code);
80
- break;
81
- case 401:
82
- errorToThrow = new MindeeHttp401Error(errorObj, urlName, code);
83
- break;
84
- case 403:
85
- errorToThrow = new MindeeHttp403Error(errorObj, urlName, code);
86
- break;
87
- case 404:
88
- errorToThrow = new MindeeHttp404Error(errorObj, urlName, code);
89
- break;
90
- case 413:
91
- errorToThrow = new MindeeHttp413Error(errorObj, urlName, code);
92
- break;
93
- case 429:
94
- errorToThrow = new MindeeHttp429Error(errorObj, urlName, code);
95
- break;
96
- case 500:
97
- errorToThrow = new MindeeHttp500Error(errorObj, urlName, code);
98
- break;
99
- case 504:
100
- errorToThrow = new MindeeHttp504Error(errorObj, urlName, code);
101
- break;
102
- default:
103
- errorToThrow = new MindeeHttpErrorV1(errorObj, urlName, code);
104
- break;
105
- }
106
- errorHandler.throw(errorToThrow);
76
+ const errorClass = (code !== undefined && ERROR_CLASSES_BY_CODE.get(code)) || MindeeHttpErrorV1;
77
+ errorHandler.throw(new errorClass(errorObj, urlName, code));
107
78
  }
108
79
  /**
109
80
  * `Error` wrapper for server (HTTP) errors.
@@ -196,3 +167,11 @@ export class MindeeHttp504Error extends MindeeHttpErrorV1 {
196
167
  this.name = "MindeeHttp504Error";
197
168
  }
198
169
  }
170
+ ERROR_CLASSES_BY_CODE.set(400, MindeeHttp400Error);
171
+ ERROR_CLASSES_BY_CODE.set(401, MindeeHttp401Error);
172
+ ERROR_CLASSES_BY_CODE.set(403, MindeeHttp403Error);
173
+ ERROR_CLASSES_BY_CODE.set(404, MindeeHttp404Error);
174
+ ERROR_CLASSES_BY_CODE.set(413, MindeeHttp413Error);
175
+ ERROR_CLASSES_BY_CODE.set(429, MindeeHttp429Error);
176
+ ERROR_CLASSES_BY_CODE.set(500, MindeeHttp500Error);
177
+ ERROR_CLASSES_BY_CODE.set(504, MindeeHttp504Error);
@@ -1,19 +1,31 @@
1
1
  import { InputSource, PageOptions } from "../../input/index.js";
2
2
  import { ExecutionPriority } from "../../v1/parsing/common/index.js";
3
3
  interface HTTPParams {
4
+ /** Input source to submit to the API. */
4
5
  inputDoc: InputSource;
6
+ /** Enables full-text OCR output when supported. */
5
7
  fullText: boolean;
8
+ /** Optional local page filtering options applied before upload. */
6
9
  pageOptions?: PageOptions;
10
+ /** Enables Retrieval-Augmented Generation when supported. */
7
11
  rag?: boolean;
8
12
  }
13
+ /** Parameters sent to synchronous/asynchronous prediction endpoints. */
9
14
  export interface PredictParams extends HTTPParams {
15
+ /** Enables word-level OCR details in the response. */
10
16
  includeWords: boolean;
17
+ /** Enables cropper extras in the response. */
11
18
  cropper: boolean;
19
+ /** Optional workflow ID used for workflow-backed inference. */
12
20
  workflowId?: string;
13
21
  }
22
+ /** Parameters sent to workflow execution endpoints. */
14
23
  export interface WorkflowParams extends HTTPParams {
24
+ /** Custom alias assigned to the submitted file. */
15
25
  alias?: string;
26
+ /** Processing priority of the workflow execution. */
16
27
  priority?: ExecutionPriority;
28
+ /** Whether to request a public validation URL. */
17
29
  publicUrl?: string;
18
30
  }
19
31
  export {};
@@ -22,6 +22,10 @@ export declare class Job {
22
22
  milliSecsTaken?: number;
23
23
  constructor(jsonResponse: StringDict);
24
24
  }
25
+ /**
26
+ * Parse a date string into UTC.
27
+ * @param date Date string.
28
+ */
25
29
  export declare function datetimeWithTimezone(date: string): Date;
26
30
  /** Wrapper for asynchronous jobs and parsing results.
27
31
  *
@@ -20,9 +20,12 @@ export class Job {
20
20
  }
21
21
  }
22
22
  }
23
- // Hideous thing to make sure dates sent back by the server are parsed correctly in UTC.
23
+ /**
24
+ * Parse a date string into UTC.
25
+ * @param date Date string.
26
+ */
24
27
  export function datetimeWithTimezone(date) {
25
- if (date.search(/\+[0-9]{2}:[0-9]{2}$/) === -1) {
28
+ if (date.search(/\+\d{2}:\d{2}$/) === -1) {
26
29
  date += "+00:00";
27
30
  }
28
31
  return new Date(date);
@@ -1,5 +1,9 @@
1
+ /** Execution priority levels available for workflow requests. */
1
2
  export declare enum ExecutionPriority {
3
+ /** Low-priority background processing. */
2
4
  low = "low",
5
+ /** Standard processing priority. */
3
6
  medium = "medium",
7
+ /** High-priority processing. */
4
8
  high = "high"
5
9
  }
@@ -1,6 +1,10 @@
1
+ /** Execution priority levels available for workflow requests. */
1
2
  export var ExecutionPriority;
2
3
  (function (ExecutionPriority) {
4
+ /** Low-priority background processing. */
3
5
  ExecutionPriority["low"] = "low";
6
+ /** Standard processing priority. */
4
7
  ExecutionPriority["medium"] = "medium";
8
+ /** High-priority processing. */
5
9
  ExecutionPriority["high"] = "high";
6
10
  })(ExecutionPriority || (ExecutionPriority = {}));
@@ -1,7 +1,9 @@
1
1
  import { PositionField } from "../../../../v1/parsing/standard/position.js";
2
2
  import { StringDict } from "../../../../parsing/stringDict.js";
3
3
  import { ExtraField } from "./extras.js";
4
+ /** Cropper extra payload returned by compatible APIs. */
4
5
  export declare class CropperExtra extends ExtraField {
6
+ /** Cropped regions detected on the page. */
5
7
  cropping: PositionField[];
6
8
  constructor(rawPrediction: StringDict, pageId?: number);
7
9
  /**
@@ -1,9 +1,12 @@
1
1
  import { PositionField } from "../../../../v1/parsing/standard/position.js";
2
+ // eslint-disable-next-line no-restricted-imports
2
3
  import { cleanOutString } from "../summaryHelper.js";
3
4
  import { ExtraField } from "./extras.js";
5
+ /** Cropper extra payload returned by compatible APIs. */
4
6
  export class CropperExtra extends ExtraField {
5
7
  constructor(rawPrediction, pageId) {
6
8
  super();
9
+ /** Cropped regions detected on the page. */
7
10
  this.cropping = [];
8
11
  if (rawPrediction["cropping"] !== undefined) {
9
12
  rawPrediction["cropping"].forEach((crop) => {
@@ -1,9 +1,12 @@
1
+ /** Base class for optional extras attached to predictions. */
1
2
  export declare abstract class ExtraField {
2
3
  }
3
4
  interface ExtraDict<T extends ExtraField> {
4
5
  [key: string]: T | (() => string);
5
6
  }
7
+ /** Dictionary-like container for extra fields. */
6
8
  export declare class Extras<ExtraT extends ExtraField = ExtraField> implements ExtraDict<ExtraT> {
9
+ /** Dynamic map of extra values keyed by extra name. */
7
10
  [key: string]: ExtraT | (() => string);
8
11
  constructor(fields: Record<string, ExtraT>);
9
12
  /**
@@ -1,5 +1,7 @@
1
+ /** Base class for optional extras attached to predictions. */
1
2
  export class ExtraField {
2
3
  }
4
+ /** Dictionary-like container for extra fields. */
3
5
  export class Extras {
4
6
  constructor(fields) {
5
7
  if (Object.keys(fields).length > 0) {
@@ -1,7 +1,10 @@
1
1
  import { StringDict } from "../../../../parsing/stringDict.js";
2
2
  import { ExtraField } from "./extras.js";
3
+ /** Full-text OCR extra payload returned by compatible APIs. */
3
4
  export declare class FullTextOcrExtra extends ExtraField {
5
+ /** Concatenated OCR content. */
4
6
  content?: string;
7
+ /** OCR language hints associated with the content. */
5
8
  languages?: string;
6
9
  constructor(rawPrediction: StringDict);
7
10
  /**
@@ -1,4 +1,5 @@
1
1
  import { ExtraField } from "./extras.js";
2
+ /** Full-text OCR extra payload returned by compatible APIs. */
2
3
  export class FullTextOcrExtra extends ExtraField {
3
4
  constructor(rawPrediction) {
4
5
  super();
@@ -1,5 +1,6 @@
1
1
  import { ExtraField } from "./extras.js";
2
2
  import { StringDict } from "../../../../parsing/stringDict.js";
3
+ /** RAG extra payload returned by compatible APIs. */
3
4
  export declare class RAGExtra extends ExtraField {
4
5
  /**
5
6
  * ID reference of the document matched by the Retrieval-Augmented Generation.
@@ -1,4 +1,5 @@
1
1
  import { ExtraField } from "./extras.js";
2
+ /** RAG extra payload returned by compatible APIs. */
2
3
  export class RAGExtra extends ExtraField {
3
4
  constructor(rawPrediction) {
4
5
  super();
@@ -1,3 +1,4 @@
1
+ // eslint-disable-next-line no-restricted-imports
1
2
  import { ApiResponse } from "../apiResponse.js";
2
3
  /**
3
4
  * Wrapper for feedback response.
@@ -10,7 +10,7 @@ export { AsyncPredictResponse } from "./asyncPredictResponse.js";
10
10
  export { PredictResponse } from "./predictResponse.js";
11
11
  export { Prediction } from "./prediction.js";
12
12
  export { Page } from "./page.js";
13
- export { cleanOutString, lineSeparator, floatToString, cleanSpecialChars } from "./summaryHelper.js";
13
+ export { cleanOutString, lineSeparator, floatToString, cleanSpecialChars, cleanAndTruncate } from "./summaryHelper.js";
14
14
  export * as extras from "./extras/index.js";
15
15
  export { parseDate } from "../../../parsing/dateParser.js";
16
16
  export { WorkflowResponse } from "./workflowResponse.js";
@@ -9,7 +9,7 @@ export { AsyncPredictResponse } from "./asyncPredictResponse.js";
9
9
  export { PredictResponse } from "./predictResponse.js";
10
10
  export { Prediction } from "./prediction.js";
11
11
  export { Page } from "./page.js";
12
- export { cleanOutString, lineSeparator, floatToString, cleanSpecialChars } from "./summaryHelper.js";
12
+ export { cleanOutString, lineSeparator, floatToString, cleanSpecialChars, cleanAndTruncate } from "./summaryHelper.js";
13
13
  export * as extras from "./extras/index.js";
14
14
  export { parseDate } from "../../../parsing/dateParser.js";
15
15
  export { WorkflowResponse } from "./workflowResponse.js";
@@ -4,6 +4,7 @@ import type { Prediction } from "./prediction.js";
4
4
  import { Product } from "./product.js";
5
5
  import { Extras } from "./extras/index.js";
6
6
  /**
7
+ * Generic base class for parsed v1 inference payloads.
7
8
  *
8
9
  * @typeParam DocT an extension of a `Prediction`. Is generic by default to
9
10
  * allow for easier optional `PageT` generic typing.
@@ -29,6 +30,7 @@ export declare abstract class Inference<DocT extends Prediction = Prediction, Pa
29
30
  /**
30
31
  * Default string representation.
31
32
  */
33
+ /** Returns a human-readable representation of the inference payload. */
32
34
  toString(): string;
33
35
  /**
34
36
  * Takes in an input string and replaces line breaks with `\n`.
@@ -1,6 +1,7 @@
1
1
  import { CropperExtra, FullTextOcrExtra, RAGExtra } from "./extras/index.js";
2
2
  import { MindeeConfigurationError } from "../../../errors/index.js";
3
3
  /**
4
+ * Generic base class for parsed v1 inference payloads.
4
5
  *
5
6
  * @typeParam DocT an extension of a `Prediction`. Is generic by default to
6
7
  * allow for easier optional `PageT` generic typing.
@@ -35,6 +36,7 @@ export class Inference {
35
36
  /**
36
37
  * Default string representation.
37
38
  */
39
+ /** Returns a human-readable representation of the inference payload. */
38
40
  toString() {
39
41
  let pages = "";
40
42
  if (this.pages.toString().length > 0) {
@@ -1,2 +1,3 @@
1
+ /** Marker base type for v1 prediction payload models. */
1
2
  export declare class Prediction {
2
3
  }
@@ -1,2 +1,3 @@
1
+ /** Marker base type for v1 prediction payload models. */
1
2
  export class Prediction {
2
3
  }
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Cleans a string by removing all new line characters.
3
+ * @param outStr Output string.
4
+ */
1
5
  export declare function cleanOutString(outStr: string): string;
2
6
  /**
3
7
  *
@@ -9,9 +13,16 @@ export declare function lineSeparator(columnSizes: number[], separator: string):
9
13
  /**
10
14
  * Replaces all special characters like \n, \r, \t, with an equivalent that can be displayed on a single line.
11
15
  * Also trims line breaks at the end of the string.
12
- * @param outStr
16
+ * @param outStr Output string.
13
17
  */
14
18
  export declare function cleanSpecialChars(outStr: string): string;
19
+ /**
20
+ * Cleans a string with `cleanSpecialChars` and truncates it to `maxLength` characters,
21
+ * appending "..." when truncation occurs. Returns an empty string for null/undefined/empty input.
22
+ * @param value String to clean and truncate.
23
+ * @param maxLength Maximum length of the returned string (including the "..." suffix when truncated).
24
+ */
25
+ export declare function cleanAndTruncate(value: string | null | undefined, maxLength: number): string;
15
26
  /**
16
27
  * Return a float as a string with at least 2 levels of precision.
17
28
  */
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Cleans a string by removing all new line characters.
3
+ * @param outStr Output string.
4
+ */
1
5
  export function cleanOutString(outStr) {
2
6
  const lines = / \n/gm;
3
7
  return outStr.replace(lines, "\n");
@@ -18,7 +22,7 @@ export function lineSeparator(columnSizes, separator) {
18
22
  /**
19
23
  * Replaces all special characters like \n, \r, \t, with an equivalent that can be displayed on a single line.
20
24
  * Also trims line breaks at the end of the string.
21
- * @param outStr
25
+ * @param outStr Output string.
22
26
  */
23
27
  export function cleanSpecialChars(outStr) {
24
28
  return outStr
@@ -26,6 +30,19 @@ export function cleanSpecialChars(outStr) {
26
30
  .replace(/\r/g, "\\r")
27
31
  .replace(/\t/g, "\\t");
28
32
  }
33
+ /**
34
+ * Cleans a string with `cleanSpecialChars` and truncates it to `maxLength` characters,
35
+ * appending "..." when truncation occurs. Returns an empty string for null/undefined/empty input.
36
+ * @param value String to clean and truncate.
37
+ * @param maxLength Maximum length of the returned string (including the "..." suffix when truncated).
38
+ */
39
+ export function cleanAndTruncate(value, maxLength) {
40
+ if (!value) {
41
+ return "";
42
+ }
43
+ const cleaned = cleanSpecialChars(value);
44
+ return cleaned.length <= maxLength ? cleaned : cleaned.slice(0, maxLength - 3) + "...";
45
+ }
29
46
  /**
30
47
  * Return a float as a string with at least 2 levels of precision.
31
48
  */