mindee 5.0.1 → 5.0.2
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 +7 -1
- package/package.json +1 -1
- package/src/http/apiCore.d.ts +4 -3
- package/src/http/apiCore.js +3 -3
- package/src/v2/client.d.ts +1 -8
- package/src/v2/client.js +8 -10
- package/src/v2/http/apiSettings.d.ts +3 -0
- package/src/v2/http/apiSettings.js +3 -0
- package/src/v2/http/mindeeApiV2.d.ts +22 -14
- package/src/v2/http/mindeeApiV2.js +34 -25
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/http/apiCore.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { Dispatcher, FormData } from "undici";
|
|
|
2
2
|
import { InputSource, PageOptions } from "../input/index.js";
|
|
3
3
|
export declare const TIMEOUT_SECS_DEFAULT: number;
|
|
4
4
|
export interface RequestOptions {
|
|
5
|
-
hostname
|
|
6
|
-
path
|
|
5
|
+
hostname?: string;
|
|
6
|
+
path?: string;
|
|
7
7
|
method: any;
|
|
8
8
|
timeoutSecs: number;
|
|
9
9
|
headers: any;
|
|
@@ -25,6 +25,7 @@ export declare function cutDocPages(inputDoc: InputSource, pageOptions: PageOpti
|
|
|
25
25
|
* Reads a response from the API and processes it.
|
|
26
26
|
* @param dispatcher custom dispatcher to use for the request.
|
|
27
27
|
* @param options options related to the request itself.
|
|
28
|
+
* @param url override the URL of the request.
|
|
28
29
|
* @returns the processed request.
|
|
29
30
|
*/
|
|
30
|
-
export declare function sendRequestAndReadResponse(dispatcher: Dispatcher, options: RequestOptions): Promise<BaseHttpResponse>;
|
|
31
|
+
export declare function sendRequestAndReadResponse(dispatcher: Dispatcher, options: RequestOptions, url?: string): Promise<BaseHttpResponse>;
|
package/src/http/apiCore.js
CHANGED
|
@@ -16,11 +16,11 @@ export async function cutDocPages(inputDoc, pageOptions) {
|
|
|
16
16
|
* Reads a response from the API and processes it.
|
|
17
17
|
* @param dispatcher custom dispatcher to use for the request.
|
|
18
18
|
* @param options options related to the request itself.
|
|
19
|
+
* @param url override the URL of the request.
|
|
19
20
|
* @returns the processed request.
|
|
20
21
|
*/
|
|
21
|
-
export async function sendRequestAndReadResponse(dispatcher, options) {
|
|
22
|
-
|
|
23
|
-
logger.debug(`${options.method}: ${url}`);
|
|
22
|
+
export async function sendRequestAndReadResponse(dispatcher, options, url) {
|
|
23
|
+
url ?? (url = `https://${options.hostname}${options.path}`);
|
|
24
24
|
const response = await request(url, {
|
|
25
25
|
method: options.method,
|
|
26
26
|
headers: options.headers,
|
package/src/v2/client.d.ts
CHANGED
|
@@ -6,8 +6,6 @@ import { PollingOptions, PollingOptionsConstructor } from "./clientOptions/index
|
|
|
6
6
|
import { BaseProduct } from "../v2/product/baseProduct.js";
|
|
7
7
|
/**
|
|
8
8
|
* Options for the V2 Mindee Client.
|
|
9
|
-
*
|
|
10
|
-
* @category ClientV2
|
|
11
9
|
* @example
|
|
12
10
|
* const client = new MindeeClientV2({
|
|
13
11
|
* apiKey: "YOUR_API_KEY",
|
|
@@ -25,8 +23,6 @@ export interface ClientOptions {
|
|
|
25
23
|
}
|
|
26
24
|
/**
|
|
27
25
|
* Mindee Client V2 class that centralizes most basic operations.
|
|
28
|
-
*
|
|
29
|
-
* @category ClientV2
|
|
30
26
|
*/
|
|
31
27
|
export declare class Client {
|
|
32
28
|
/** Mindee V2 API handler. */
|
|
@@ -42,7 +38,6 @@ export declare class Client {
|
|
|
42
38
|
* @param product the product to retrieve.
|
|
43
39
|
* @param inferenceId id of the queue to poll.
|
|
44
40
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
45
|
-
* @category Asynchronous
|
|
46
41
|
* @returns a `Promise` containing the inference.
|
|
47
42
|
*/
|
|
48
43
|
getResult<P extends typeof BaseProduct>(product: P, inferenceId: string): Promise<InstanceType<P["responseClass"]>>;
|
|
@@ -52,7 +47,6 @@ export declare class Client {
|
|
|
52
47
|
*
|
|
53
48
|
* @param jobId id of the queue to poll.
|
|
54
49
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
55
|
-
* @category Asynchronous
|
|
56
50
|
* @returns a `Promise` containing a `Job`, which also contains a `Document` if the
|
|
57
51
|
* parsing is complete.
|
|
58
52
|
*/
|
|
@@ -67,7 +61,6 @@ export declare class Client {
|
|
|
67
61
|
*
|
|
68
62
|
* @param pollingOptions options for the polling loop, see {@link PollingOptions}.
|
|
69
63
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
70
|
-
* @category Synchronous
|
|
71
64
|
* @returns a `Promise` containing parsing results.
|
|
72
65
|
*/
|
|
73
66
|
enqueueAndGetResult<P extends typeof BaseProduct>(product: P, inputSource: InputSource, params: InstanceType<P["parametersClass"]> | ConstructorParameters<P["parametersClass"]>[0], pollingOptions?: PollingOptionsConstructor): Promise<InstanceType<P["responseClass"]>>;
|
|
@@ -76,5 +69,5 @@ export declare class Client {
|
|
|
76
69
|
* until the maximum number of tries is reached.
|
|
77
70
|
* @protected
|
|
78
71
|
*/
|
|
79
|
-
protected pollForResult<P extends typeof BaseProduct>(product: typeof BaseProduct, pollingOptions: PollingOptions,
|
|
72
|
+
protected pollForResult<P extends typeof BaseProduct>(product: typeof BaseProduct, pollingOptions: PollingOptions, jobId: string): Promise<InstanceType<P["responseClass"]>>;
|
|
80
73
|
}
|
package/src/v2/client.js
CHANGED
|
@@ -7,8 +7,6 @@ import { MindeeHttpErrorV2 } from "./http/errors.js";
|
|
|
7
7
|
import { PollingOptions } from "./clientOptions/index.js";
|
|
8
8
|
/**
|
|
9
9
|
* Mindee Client V2 class that centralizes most basic operations.
|
|
10
|
-
*
|
|
11
|
-
* @category ClientV2
|
|
12
10
|
*/
|
|
13
11
|
export class Client {
|
|
14
12
|
/**
|
|
@@ -49,12 +47,11 @@ export class Client {
|
|
|
49
47
|
* @param product the product to retrieve.
|
|
50
48
|
* @param inferenceId id of the queue to poll.
|
|
51
49
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
52
|
-
* @category Asynchronous
|
|
53
50
|
* @returns a `Promise` containing the inference.
|
|
54
51
|
*/
|
|
55
52
|
async getResult(product, inferenceId) {
|
|
56
53
|
logger.debug(`Attempting to get inference with ID: ${inferenceId} using response type: ${product.name}`);
|
|
57
|
-
return await this.mindeeApi.
|
|
54
|
+
return await this.mindeeApi.getProductResultById(product, inferenceId);
|
|
58
55
|
}
|
|
59
56
|
/**
|
|
60
57
|
* Get the processing status of a previously enqueued request.
|
|
@@ -62,7 +59,6 @@ export class Client {
|
|
|
62
59
|
*
|
|
63
60
|
* @param jobId id of the queue to poll.
|
|
64
61
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
65
|
-
* @category Asynchronous
|
|
66
62
|
* @returns a `Promise` containing a `Job`, which also contains a `Document` if the
|
|
67
63
|
* parsing is complete.
|
|
68
64
|
*/
|
|
@@ -79,7 +75,6 @@ export class Client {
|
|
|
79
75
|
*
|
|
80
76
|
* @param pollingOptions options for the polling loop, see {@link PollingOptions}.
|
|
81
77
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
82
|
-
* @category Synchronous
|
|
83
78
|
* @returns a `Promise` containing parsing results.
|
|
84
79
|
*/
|
|
85
80
|
async enqueueAndGetResult(product, inputSource, params, pollingOptions) {
|
|
@@ -93,15 +88,15 @@ export class Client {
|
|
|
93
88
|
* until the maximum number of tries is reached.
|
|
94
89
|
* @protected
|
|
95
90
|
*/
|
|
96
|
-
async pollForResult(product, pollingOptions,
|
|
91
|
+
async pollForResult(product, pollingOptions, jobId) {
|
|
97
92
|
logger.debug(`Waiting ${pollingOptions.initialDelaySec} seconds before polling.`);
|
|
98
93
|
await setTimeout(pollingOptions.initialDelaySec * 1000, undefined, pollingOptions.initialTimerOptions);
|
|
99
|
-
logger.debug(`Start polling for inference using job ID: ${
|
|
94
|
+
logger.debug(`Start polling for inference using job ID: ${jobId}.`);
|
|
100
95
|
let retryCounter = 1;
|
|
101
96
|
let pollResults;
|
|
102
97
|
while (retryCounter < pollingOptions.maxRetries + 1) {
|
|
103
98
|
logger.debug(`Attempt ${retryCounter} of ${pollingOptions.maxRetries}`);
|
|
104
|
-
pollResults = await this.getJob(
|
|
99
|
+
pollResults = await this.getJob(jobId);
|
|
105
100
|
const error = pollResults.job.error;
|
|
106
101
|
if (error) {
|
|
107
102
|
throw new MindeeHttpErrorV2(error);
|
|
@@ -111,7 +106,10 @@ export class Client {
|
|
|
111
106
|
break;
|
|
112
107
|
}
|
|
113
108
|
if (pollResults.job.status === "Processed") {
|
|
114
|
-
|
|
109
|
+
if (!pollResults.job.resultUrl) {
|
|
110
|
+
throw new MindeeError("The result URL is undefined. This is a server error, try again later or contact support.");
|
|
111
|
+
}
|
|
112
|
+
return this.mindeeApi.getProductResultByUrl(product, pollResults.job.resultUrl);
|
|
115
113
|
}
|
|
116
114
|
await setTimeout(pollingOptions.delaySec * 1000, undefined, pollingOptions.recurringTimerOptions);
|
|
117
115
|
retryCounter++;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { BaseSettings, MindeeApiConstructorProps } from "../../http/baseSettings.js";
|
|
2
|
+
/**
|
|
3
|
+
* Settings for the V2 API.
|
|
4
|
+
*/
|
|
2
5
|
export declare class ApiSettings extends BaseSettings {
|
|
3
6
|
baseHeaders: Record<string, string>;
|
|
4
7
|
constructor({ apiKey, dispatcher, }: MindeeApiConstructorProps);
|
|
@@ -4,6 +4,9 @@ import { MindeeConfigurationError } from "../../errors/index.js";
|
|
|
4
4
|
const API_V2_KEY_ENVVAR_NAME = "MINDEE_V2_API_KEY";
|
|
5
5
|
const API_V2_HOST_ENVVAR_NAME = "MINDEE_V2_API_HOST";
|
|
6
6
|
const DEFAULT_MINDEE_API_HOST = "api-v2.mindee.net";
|
|
7
|
+
/**
|
|
8
|
+
* Settings for the V2 API.
|
|
9
|
+
*/
|
|
7
10
|
export class ApiSettings extends BaseSettings {
|
|
8
11
|
constructor({ apiKey, dispatcher, }) {
|
|
9
12
|
super(apiKey, dispatcher);
|
|
@@ -4,35 +4,43 @@ import { BaseParameters } from "../../v2/index.js";
|
|
|
4
4
|
import { JobResponse } from "../../v2/parsing/index.js";
|
|
5
5
|
import { InputSource } from "../../input/index.js";
|
|
6
6
|
import { BaseProduct } from "../../v2/product/baseProduct.js";
|
|
7
|
+
/**
|
|
8
|
+
* Mindee V2 API handler.
|
|
9
|
+
*/
|
|
7
10
|
export declare class MindeeApiV2 {
|
|
8
11
|
#private;
|
|
9
12
|
settings: ApiSettings;
|
|
10
13
|
constructor(dispatcher?: Dispatcher, apiKey?: string);
|
|
11
14
|
/**
|
|
12
|
-
* Sends a file to the
|
|
15
|
+
* Sends a file to the product inference queue.
|
|
13
16
|
* @param product product to enqueue.
|
|
14
17
|
* @param inputSource Local file loaded as an input.
|
|
15
18
|
* @param params {ExtractionParameters} parameters relating to the enqueueing options.
|
|
16
|
-
* @
|
|
17
|
-
* @throws Error if the server's response contains one.
|
|
19
|
+
* @throws Error if the server's response contains an error.
|
|
18
20
|
* @returns a `Promise` containing a job response.
|
|
19
21
|
*/
|
|
20
22
|
enqueueProduct(product: typeof BaseProduct, inputSource: InputSource, params: BaseParameters): Promise<JobResponse>;
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
23
|
-
* Throws an error if the server's response contains
|
|
24
|
-
* @param jobId The
|
|
25
|
-
* @
|
|
26
|
-
* @returns a `Promise` containing information on the queue.
|
|
24
|
+
* Get the specified Job.
|
|
25
|
+
* Throws an error if the server's response contains an error.
|
|
26
|
+
* @param jobId The Job ID as returned by the enqueue request.
|
|
27
|
+
* @returns a `Promise` containing the job response.
|
|
27
28
|
*/
|
|
28
29
|
getJob(jobId: string): Promise<JobResponse>;
|
|
29
30
|
/**
|
|
30
|
-
*
|
|
31
|
-
* Throws an error if the server's response contains
|
|
31
|
+
* Get the result of a queued document from the API.
|
|
32
|
+
* Throws an error if the server's response contains an error.
|
|
32
33
|
* @param product
|
|
33
|
-
* @param inferenceId The
|
|
34
|
-
* @
|
|
35
|
-
* @returns a `Promise` containing either the parsed result, or information on the queue.
|
|
34
|
+
* @param inferenceId The inference ID for the result.
|
|
35
|
+
* @returns a `Promise` containing the parsed result.
|
|
36
36
|
*/
|
|
37
|
-
|
|
37
|
+
getProductResultById<P extends typeof BaseProduct>(product: P, inferenceId: string): Promise<InstanceType<P["responseClass"]>>;
|
|
38
|
+
/**
|
|
39
|
+
* Get the result of a queued document from the API.
|
|
40
|
+
* Throws an error if the server's response contains an error.
|
|
41
|
+
* @param product
|
|
42
|
+
* @param url The URL as returned by a Job's resultUrl property.
|
|
43
|
+
* @returns a `Promise` containing the parsed result.
|
|
44
|
+
*/
|
|
45
|
+
getProductResultByUrl<P extends typeof BaseProduct>(product: P, url: string): Promise<InstanceType<P["responseClass"]>>;
|
|
38
46
|
}
|
|
@@ -8,21 +8,23 @@ import { ApiSettings } from "./apiSettings.js";
|
|
|
8
8
|
import { ErrorResponse, JobResponse, } from "../../v2/parsing/index.js";
|
|
9
9
|
import { sendRequestAndReadResponse } from "../../http/apiCore.js";
|
|
10
10
|
import { LocalInputSource } from "../../input/index.js";
|
|
11
|
-
import { MindeeDeserializationError } from "../../errors/index.js";
|
|
11
|
+
import { MindeeDeserializationError, MindeeError } from "../../errors/index.js";
|
|
12
12
|
import { MindeeHttpErrorV2 } from "./errors.js";
|
|
13
13
|
import { logger } from "../../logger.js";
|
|
14
|
+
/**
|
|
15
|
+
* Mindee V2 API handler.
|
|
16
|
+
*/
|
|
14
17
|
export class MindeeApiV2 {
|
|
15
18
|
constructor(dispatcher, apiKey) {
|
|
16
19
|
_MindeeApiV2_instances.add(this);
|
|
17
20
|
this.settings = new ApiSettings({ dispatcher: dispatcher, apiKey: apiKey });
|
|
18
21
|
}
|
|
19
22
|
/**
|
|
20
|
-
* Sends a file to the
|
|
23
|
+
* Sends a file to the product inference queue.
|
|
21
24
|
* @param product product to enqueue.
|
|
22
25
|
* @param inputSource Local file loaded as an input.
|
|
23
26
|
* @param params {ExtractionParameters} parameters relating to the enqueueing options.
|
|
24
|
-
* @
|
|
25
|
-
* @throws Error if the server's response contains one.
|
|
27
|
+
* @throws Error if the server's response contains an error.
|
|
26
28
|
* @returns a `Promise` containing a job response.
|
|
27
29
|
*/
|
|
28
30
|
async enqueueProduct(product, inputSource, params) {
|
|
@@ -34,26 +36,35 @@ export class MindeeApiV2 {
|
|
|
34
36
|
return __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_processResponse).call(this, result, JobResponse);
|
|
35
37
|
}
|
|
36
38
|
/**
|
|
37
|
-
*
|
|
38
|
-
* Throws an error if the server's response contains
|
|
39
|
-
* @param jobId The
|
|
40
|
-
* @
|
|
41
|
-
* @returns a `Promise` containing information on the queue.
|
|
39
|
+
* Get the specified Job.
|
|
40
|
+
* Throws an error if the server's response contains an error.
|
|
41
|
+
* @param jobId The Job ID as returned by the enqueue request.
|
|
42
|
+
* @returns a `Promise` containing the job response.
|
|
42
43
|
*/
|
|
43
44
|
async getJob(jobId) {
|
|
44
45
|
const response = await __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_reqGetJob).call(this, jobId);
|
|
45
46
|
return __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_processResponse).call(this, response, JobResponse);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
48
|
-
*
|
|
49
|
-
* Throws an error if the server's response contains
|
|
49
|
+
* Get the result of a queued document from the API.
|
|
50
|
+
* Throws an error if the server's response contains an error.
|
|
51
|
+
* @param product
|
|
52
|
+
* @param inferenceId The inference ID for the result.
|
|
53
|
+
* @returns a `Promise` containing the parsed result.
|
|
54
|
+
*/
|
|
55
|
+
async getProductResultById(product, inferenceId) {
|
|
56
|
+
const queueResponse = await __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_reqGetProductResult).call(this, `https://${this.settings.hostname}/v2/products/${product.slug}/results/${inferenceId}`);
|
|
57
|
+
return __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_processResponse).call(this, queueResponse, product.responseClass);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get the result of a queued document from the API.
|
|
61
|
+
* Throws an error if the server's response contains an error.
|
|
50
62
|
* @param product
|
|
51
|
-
* @param
|
|
52
|
-
* @
|
|
53
|
-
* @returns a `Promise` containing either the parsed result, or information on the queue.
|
|
63
|
+
* @param url The URL as returned by a Job's resultUrl property.
|
|
64
|
+
* @returns a `Promise` containing the parsed result.
|
|
54
65
|
*/
|
|
55
|
-
async
|
|
56
|
-
const queueResponse = await __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_reqGetProductResult).call(this,
|
|
66
|
+
async getProductResultByUrl(product, url) {
|
|
67
|
+
const queueResponse = await __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_reqGetProductResult).call(this, url);
|
|
57
68
|
return __classPrivateFieldGet(this, _MindeeApiV2_instances, "m", _MindeeApiV2_processResponse).call(this, queueResponse, product.responseClass);
|
|
58
69
|
}
|
|
59
70
|
}
|
|
@@ -80,7 +91,6 @@ _MindeeApiV2_instances = new WeakSet(), _MindeeApiV2_processResponse = function
|
|
|
80
91
|
}, _MindeeApiV2_reqPostProductEnqueue =
|
|
81
92
|
/**
|
|
82
93
|
* Sends a document to the inference queue.
|
|
83
|
-
*
|
|
84
94
|
* @param product Product to enqueue.
|
|
85
95
|
* @param inputSource Local or remote file as an input.
|
|
86
96
|
* @param params {ExtractionParameters} parameters relating to the enqueueing options.
|
|
@@ -115,18 +125,17 @@ async function _MindeeApiV2_reqPostProductEnqueue(product, inputSource, params)
|
|
|
115
125
|
}, _MindeeApiV2_reqGetProductResult =
|
|
116
126
|
/**
|
|
117
127
|
* Make a request to GET the status of a document in the queue.
|
|
118
|
-
* @param
|
|
119
|
-
* @
|
|
120
|
-
* @category Asynchronous
|
|
121
|
-
* @returns a `Promise` containing either the parsed result, or information on the queue.
|
|
128
|
+
* @param url URL path to the result.
|
|
129
|
+
* @returns a `Promise` containing the parsed result.
|
|
122
130
|
*/
|
|
123
|
-
async function _MindeeApiV2_reqGetProductResult(
|
|
131
|
+
async function _MindeeApiV2_reqGetProductResult(url) {
|
|
124
132
|
const options = {
|
|
125
133
|
method: "GET",
|
|
126
134
|
headers: this.settings.baseHeaders,
|
|
127
|
-
hostname: this.settings.hostname,
|
|
128
|
-
path: `/v2/products/${slug}/results/${inferenceId}`,
|
|
129
135
|
timeoutSecs: this.settings.timeoutSecs,
|
|
130
136
|
};
|
|
131
|
-
|
|
137
|
+
if (!url.startsWith("https://")) {
|
|
138
|
+
throw new MindeeError(`Invalid URL: ${url}`);
|
|
139
|
+
}
|
|
140
|
+
return await sendRequestAndReadResponse(this.settings.dispatcher, options, url);
|
|
132
141
|
};
|