mindee 5.0.0-alpha2 → 5.0.0-rc1
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.
- package/CHANGELOG.md +6 -0
- package/package.json +9 -6
- package/src/v2/client/baseParameters.d.ts +0 -12
- package/src/v2/client/baseParameters.js +0 -36
- package/src/v2/client/index.d.ts +2 -1
- package/src/v2/client/index.js +1 -0
- package/src/v2/client/pollingOptions.d.ts +18 -9
- package/src/v2/client/pollingOptions.js +78 -1
- package/src/v2/client.d.ts +4 -3
- package/src/v2/client.js +7 -6
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mindee",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-rc1",
|
|
4
4
|
"description": "Mindee Client Library for Node.js",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
-
"bin":
|
|
6
|
+
"bin": {
|
|
7
|
+
"mindeeV1": "bin/mindeeV1.js",
|
|
8
|
+
"mindeeV2": "bin/mindeeV2.js"
|
|
9
|
+
},
|
|
7
10
|
"license": "MIT",
|
|
8
11
|
"type": "module",
|
|
9
12
|
"scripts": {
|
|
@@ -28,7 +31,7 @@
|
|
|
28
31
|
"CHANGELOG.md"
|
|
29
32
|
],
|
|
30
33
|
"engines": {
|
|
31
|
-
"node": ">=
|
|
34
|
+
"node": ">= 20.1"
|
|
32
35
|
},
|
|
33
36
|
"repository": {
|
|
34
37
|
"type": "git",
|
|
@@ -51,7 +54,7 @@
|
|
|
51
54
|
"@typescript-eslint/parser": "^8.55.0",
|
|
52
55
|
"eslint": "^9.39.2",
|
|
53
56
|
"eslint-plugin-jsdoc": "^52.0.4",
|
|
54
|
-
"mocha": "^11.
|
|
57
|
+
"mocha": "^11.3.0",
|
|
55
58
|
"tsc-alias": "^1.8.16",
|
|
56
59
|
"tsx": "^4.21.0",
|
|
57
60
|
"typedoc": "~0.28.16",
|
|
@@ -65,10 +68,10 @@
|
|
|
65
68
|
"undici": "^6.23.0"
|
|
66
69
|
},
|
|
67
70
|
"optionalDependencies": {
|
|
68
|
-
"sharp": "~0.34.5",
|
|
69
71
|
"@cantoo/pdf-lib": "^2.3.2",
|
|
72
|
+
"node-poppler": "^7.2.4",
|
|
70
73
|
"pdf.js-extract": "^0.2.1",
|
|
71
|
-
"
|
|
74
|
+
"sharp": "~0.34.5"
|
|
72
75
|
},
|
|
73
76
|
"keywords": [
|
|
74
77
|
"typescript",
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { ValidatedPollingOptions } from "../../v2/client/pollingOptions.js";
|
|
2
|
-
import { PollingOptions } from "../../v2/index.js";
|
|
3
1
|
/**
|
|
4
2
|
* Constructor parameters for BaseParameters and its subclasses.
|
|
5
3
|
*/
|
|
@@ -7,7 +5,6 @@ export interface BaseParametersConstructor {
|
|
|
7
5
|
modelId: string;
|
|
8
6
|
alias?: string;
|
|
9
7
|
webhookIds?: string[];
|
|
10
|
-
pollingOptions?: PollingOptions;
|
|
11
8
|
closeFile?: boolean;
|
|
12
9
|
}
|
|
13
10
|
/**
|
|
@@ -43,21 +40,12 @@ export declare abstract class BaseParameters {
|
|
|
43
40
|
* If empty, no webhooks will be used.
|
|
44
41
|
*/
|
|
45
42
|
webhookIds?: string[];
|
|
46
|
-
/**
|
|
47
|
-
* Client-side polling configuration (see {@link PollingOptions}).
|
|
48
|
-
*/
|
|
49
|
-
pollingOptions?: PollingOptions;
|
|
50
43
|
/**
|
|
51
44
|
* By default, the file is closed once the upload is finished.
|
|
52
45
|
* Set to `false` to keep it open.
|
|
53
46
|
*/
|
|
54
47
|
closeFile?: boolean;
|
|
55
48
|
protected constructor(params: BaseParametersConstructor);
|
|
56
|
-
/**
|
|
57
|
-
* Checks the values for asynchronous parsing. Returns their corrected value if they are undefined.
|
|
58
|
-
* @returns A valid `AsyncOptions`.
|
|
59
|
-
*/
|
|
60
|
-
getValidatedPollingOptions(): ValidatedPollingOptions;
|
|
61
49
|
/**
|
|
62
50
|
* Returns the form data to send to the API.
|
|
63
51
|
* @returns A `FormData` object.
|
|
@@ -26,42 +26,6 @@ export class BaseParameters {
|
|
|
26
26
|
this.alias = params.alias;
|
|
27
27
|
this.webhookIds = params.webhookIds;
|
|
28
28
|
this.closeFile = params.closeFile;
|
|
29
|
-
this.pollingOptions = params.pollingOptions;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Checks the values for asynchronous parsing. Returns their corrected value if they are undefined.
|
|
33
|
-
* @returns A valid `AsyncOptions`.
|
|
34
|
-
*/
|
|
35
|
-
getValidatedPollingOptions() {
|
|
36
|
-
const minDelaySec = 1;
|
|
37
|
-
const minInitialDelay = 1;
|
|
38
|
-
const minRetries = 2;
|
|
39
|
-
let newAsyncParams;
|
|
40
|
-
if (this.pollingOptions === undefined) {
|
|
41
|
-
newAsyncParams = {
|
|
42
|
-
delaySec: 1.5,
|
|
43
|
-
initialDelaySec: 2,
|
|
44
|
-
maxRetries: 80
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
newAsyncParams = { ...this.pollingOptions };
|
|
49
|
-
if (!newAsyncParams.delaySec ||
|
|
50
|
-
!newAsyncParams.initialDelaySec ||
|
|
51
|
-
!newAsyncParams.maxRetries) {
|
|
52
|
-
throw Error("Invalid polling options.");
|
|
53
|
-
}
|
|
54
|
-
if (newAsyncParams.delaySec < minDelaySec) {
|
|
55
|
-
throw Error(`Cannot set auto-parsing delay to less than ${minDelaySec} second(s).`);
|
|
56
|
-
}
|
|
57
|
-
if (newAsyncParams.initialDelaySec < minInitialDelay) {
|
|
58
|
-
throw Error(`Cannot set initial parsing delay to less than ${minInitialDelay} second(s).`);
|
|
59
|
-
}
|
|
60
|
-
if (newAsyncParams.maxRetries < minRetries) {
|
|
61
|
-
throw Error(`Cannot set retry to less than ${minRetries}.`);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return newAsyncParams;
|
|
65
29
|
}
|
|
66
30
|
/**
|
|
67
31
|
* Returns the form data to send to the API.
|
package/src/v2/client/index.d.ts
CHANGED
package/src/v2/client/index.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export interface TimerOptions {
|
|
2
|
+
ref?: boolean;
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
}
|
|
5
|
+
export interface PollingOptionsConstructor {
|
|
6
|
+
initialDelaySec?: number;
|
|
7
|
+
delaySec?: number;
|
|
8
|
+
maxRetries?: number;
|
|
9
|
+
initialTimerOptions?: TimerOptions;
|
|
10
|
+
recurringTimerOptions?: TimerOptions;
|
|
11
|
+
}
|
|
1
12
|
/**
|
|
2
13
|
* Parameters for the internal polling loop in `enqueueAndGetInference()`.
|
|
3
14
|
*
|
|
@@ -24,13 +35,13 @@
|
|
|
24
35
|
*
|
|
25
36
|
* const inference = await client.enqueueAndGetInference(inputDoc, params);
|
|
26
37
|
*/
|
|
27
|
-
export
|
|
38
|
+
export declare class PollingOptions {
|
|
28
39
|
/** Number of seconds to wait *before the first poll*. */
|
|
29
|
-
initialDelaySec
|
|
40
|
+
initialDelaySec: number;
|
|
30
41
|
/** Interval in seconds between two consecutive polls. */
|
|
31
|
-
delaySec
|
|
42
|
+
delaySec: number;
|
|
32
43
|
/** Maximum number of polling attempts (including the first one). */
|
|
33
|
-
maxRetries
|
|
44
|
+
maxRetries: number;
|
|
34
45
|
/** Options passed to the initial `setTimeout()`. */
|
|
35
46
|
initialTimerOptions?: {
|
|
36
47
|
ref?: boolean;
|
|
@@ -41,9 +52,7 @@ export interface PollingOptions {
|
|
|
41
52
|
ref?: boolean;
|
|
42
53
|
signal?: AbortSignal;
|
|
43
54
|
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
delaySec: number;
|
|
48
|
-
maxRetries: number;
|
|
55
|
+
constructor(params?: PollingOptionsConstructor);
|
|
56
|
+
validateOptions(): void;
|
|
57
|
+
toString(): string;
|
|
49
58
|
}
|
|
@@ -1 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
import { MindeeConfigurationError } from "../../errors/index.js";
|
|
2
|
+
import { logger } from "../../logger.js";
|
|
3
|
+
const minInitialDelay = 1;
|
|
4
|
+
const minDelaySec = 1;
|
|
5
|
+
const minRetries = 2;
|
|
6
|
+
/**
|
|
7
|
+
* Parameters for the internal polling loop in `enqueueAndGetInference()`.
|
|
8
|
+
*
|
|
9
|
+
* Default behavior:
|
|
10
|
+
* - `initialDelaySec` = 2s
|
|
11
|
+
* - `delaySec` = 1.5s
|
|
12
|
+
* - `maxRetries` = 80
|
|
13
|
+
*
|
|
14
|
+
* Validation rules:
|
|
15
|
+
* - `initialDelaySec` >= 1
|
|
16
|
+
* - `delaySec` >= 1
|
|
17
|
+
* - `maxRetries` >= 2
|
|
18
|
+
*
|
|
19
|
+
* The `initialTimerOptions` and `recurringTimerOptions` objects let you pass an
|
|
20
|
+
* `AbortSignal` or make the timer `unref`-ed to the `setTimeout()`.
|
|
21
|
+
*
|
|
22
|
+
* @category ClientV2
|
|
23
|
+
* @example
|
|
24
|
+
* const params = {
|
|
25
|
+
* initialDelaySec: 4,
|
|
26
|
+
* delaySec: 2,
|
|
27
|
+
* maxRetries: 50
|
|
28
|
+
* };
|
|
29
|
+
*
|
|
30
|
+
* const inference = await client.enqueueAndGetInference(inputDoc, params);
|
|
31
|
+
*/
|
|
32
|
+
export class PollingOptions {
|
|
33
|
+
constructor(params) {
|
|
34
|
+
if (!params) {
|
|
35
|
+
params = {};
|
|
36
|
+
}
|
|
37
|
+
if (!params.initialDelaySec) {
|
|
38
|
+
this.initialDelaySec = 2;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.initialDelaySec = params.initialDelaySec;
|
|
42
|
+
}
|
|
43
|
+
if (!params.delaySec) {
|
|
44
|
+
this.delaySec = 1.5;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.delaySec = params.delaySec;
|
|
48
|
+
}
|
|
49
|
+
if (!params.maxRetries) {
|
|
50
|
+
this.maxRetries = 80;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.maxRetries = params.maxRetries;
|
|
54
|
+
}
|
|
55
|
+
if (params.initialTimerOptions) {
|
|
56
|
+
this.initialTimerOptions = params.initialTimerOptions;
|
|
57
|
+
}
|
|
58
|
+
if (params.recurringTimerOptions) {
|
|
59
|
+
this.recurringTimerOptions = params.recurringTimerOptions;
|
|
60
|
+
}
|
|
61
|
+
this.validateOptions();
|
|
62
|
+
logger.debug(`Polling options initialized: ${this.toString()}`);
|
|
63
|
+
}
|
|
64
|
+
validateOptions() {
|
|
65
|
+
if (this.delaySec < minDelaySec) {
|
|
66
|
+
throw new MindeeConfigurationError(`Cannot set auto-parsing delay to less than ${minDelaySec} second(s).`);
|
|
67
|
+
}
|
|
68
|
+
if (this.initialDelaySec < minInitialDelay) {
|
|
69
|
+
throw new MindeeConfigurationError(`Cannot set initial parsing delay to less than ${minInitialDelay} second(s).`);
|
|
70
|
+
}
|
|
71
|
+
if (this.maxRetries < minRetries) {
|
|
72
|
+
throw new MindeeConfigurationError(`Cannot set retry to less than ${minRetries}.`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
toString() {
|
|
76
|
+
return `{ initialDelaySec: ${this.initialDelaySec}, delaySec: ${this.delaySec}, maxRetries: ${this.maxRetries} }`;
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/v2/client.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Dispatcher } from "undici";
|
|
|
2
2
|
import { InputSource } from "../input/index.js";
|
|
3
3
|
import { JobResponse } from "./parsing/index.js";
|
|
4
4
|
import { MindeeApiV2 } from "./http/mindeeApiV2.js";
|
|
5
|
-
import {
|
|
5
|
+
import { PollingOptions, PollingOptionsConstructor } from "./client/index.js";
|
|
6
6
|
import { BaseProduct } from "../v2/product/baseProduct.js";
|
|
7
7
|
/**
|
|
8
8
|
* Options for the V2 Mindee Client.
|
|
@@ -65,15 +65,16 @@ export declare class Client {
|
|
|
65
65
|
* @param inputSource file or URL to parse.
|
|
66
66
|
* @param params parameters relating to prediction options.
|
|
67
67
|
*
|
|
68
|
+
* @param pollingOptions options for the polling loop, see {@link PollingOptions}.
|
|
68
69
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
69
70
|
* @category Synchronous
|
|
70
71
|
* @returns a `Promise` containing parsing results.
|
|
71
72
|
*/
|
|
72
|
-
enqueueAndGetResult<P extends typeof BaseProduct>(product: P, inputSource: InputSource, params: InstanceType<P["parametersClass"]> | ConstructorParameters<P["parametersClass"]>[0]): Promise<InstanceType<P["responseClass"]>>;
|
|
73
|
+
enqueueAndGetResult<P extends typeof BaseProduct>(product: P, inputSource: InputSource, params: InstanceType<P["parametersClass"]> | ConstructorParameters<P["parametersClass"]>[0], pollingOptions?: PollingOptionsConstructor): Promise<InstanceType<P["responseClass"]>>;
|
|
73
74
|
/**
|
|
74
75
|
* Send a document to an endpoint and poll the server until the result is sent or
|
|
75
76
|
* until the maximum number of tries is reached.
|
|
76
77
|
* @protected
|
|
77
78
|
*/
|
|
78
|
-
protected pollForResult<P extends typeof BaseProduct>(product: typeof BaseProduct, pollingOptions:
|
|
79
|
+
protected pollForResult<P extends typeof BaseProduct>(product: typeof BaseProduct, pollingOptions: PollingOptions, queueId: string): Promise<InstanceType<P["responseClass"]>>;
|
|
79
80
|
}
|
package/src/v2/client.js
CHANGED
|
@@ -4,6 +4,7 @@ import { errorHandler } from "../errors/handler.js";
|
|
|
4
4
|
import { LOG_LEVELS, logger } from "../logger.js";
|
|
5
5
|
import { MindeeApiV2 } from "./http/mindeeApiV2.js";
|
|
6
6
|
import { MindeeHttpErrorV2 } from "./http/errors.js";
|
|
7
|
+
import { PollingOptions } from "./client/index.js";
|
|
7
8
|
/**
|
|
8
9
|
* Mindee Client V2 class that centralizes most basic operations.
|
|
9
10
|
*
|
|
@@ -76,15 +77,16 @@ export class Client {
|
|
|
76
77
|
* @param inputSource file or URL to parse.
|
|
77
78
|
* @param params parameters relating to prediction options.
|
|
78
79
|
*
|
|
80
|
+
* @param pollingOptions options for the polling loop, see {@link PollingOptions}.
|
|
79
81
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
80
82
|
* @category Synchronous
|
|
81
83
|
* @returns a `Promise` containing parsing results.
|
|
82
84
|
*/
|
|
83
|
-
async enqueueAndGetResult(product, inputSource, params) {
|
|
85
|
+
async enqueueAndGetResult(product, inputSource, params, pollingOptions) {
|
|
84
86
|
const paramsInstance = new product.parametersClass(params);
|
|
85
|
-
const
|
|
87
|
+
const pollingOptionsInstance = new PollingOptions(pollingOptions);
|
|
86
88
|
const jobResponse = await this.enqueue(product, inputSource, paramsInstance);
|
|
87
|
-
return await this.pollForResult(product,
|
|
89
|
+
return await this.pollForResult(product, pollingOptionsInstance, jobResponse.job.id);
|
|
88
90
|
}
|
|
89
91
|
/**
|
|
90
92
|
* Send a document to an endpoint and poll the server until the result is sent or
|
|
@@ -114,8 +116,7 @@ export class Client {
|
|
|
114
116
|
await setTimeout(pollingOptions.delaySec * 1000, undefined, pollingOptions.recurringTimerOptions);
|
|
115
117
|
retryCounter++;
|
|
116
118
|
}
|
|
117
|
-
throw new MindeeError(
|
|
118
|
-
pollingOptions
|
|
119
|
-
" seconds");
|
|
119
|
+
throw new MindeeError(`Polling failed to retrieve a result after ${retryCounter} attempts. ` +
|
|
120
|
+
"You can increase poll attempts by passing the pollingOptions argument to enqueueAndGetResult()");
|
|
120
121
|
}
|
|
121
122
|
}
|