mindee 4.3.1 → 4.3.3
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 +15 -0
- package/package.json +5 -4
- package/src/cli.js +52 -16
- package/src/client.d.ts +35 -6
- package/src/client.js +47 -16
- package/src/http/apiSettings.d.ts +15 -0
- package/src/http/{mindeeApi.js → apiSettings.js} +10 -13
- package/src/http/baseEndpoint.d.ts +27 -0
- package/src/http/baseEndpoint.js +65 -0
- package/src/http/endpoint.d.ts +27 -38
- package/src/http/endpoint.js +46 -60
- package/src/http/error.d.ts +11 -11
- package/src/http/error.js +29 -29
- package/src/http/index.d.ts +3 -2
- package/src/http/index.js +5 -5
- package/src/input/base.d.ts +7 -6
- package/src/input/base.js +1 -4
- package/src/parsing/common/apiResponse.d.ts +1 -1
- package/src/parsing/common/apiResponse.js +1 -1
- package/src/parsing/common/asyncPredictResponse.d.ts +4 -4
- package/src/parsing/common/asyncPredictResponse.js +8 -8
- package/src/parsing/common/extras/cropperExtra.js +1 -1
- package/src/parsing/common/feedback/feedbackResponse.d.ts +15 -0
- package/src/parsing/common/feedback/feedbackResponse.js +19 -0
- package/src/parsing/common/index.d.ts +1 -0
- package/src/parsing/common/index.js +3 -1
- package/src/parsing/common/inference.js +9 -5
- package/src/parsing/common/orientation.d.ts +1 -4
- package/src/parsing/common/orientation.js +1 -4
- package/src/parsing/common/page.d.ts +2 -3
- package/src/parsing/common/page.js +2 -3
- package/src/parsing/common/predictResponse.d.ts +3 -3
- package/src/parsing/common/predictResponse.js +5 -5
- package/src/parsing/custom/listField.d.ts +6 -3
- package/src/parsing/custom/listField.js +3 -0
- package/src/parsing/standard/amount.d.ts +4 -6
- package/src/parsing/standard/amount.js +1 -4
- package/src/parsing/standard/base.d.ts +9 -4
- package/src/parsing/standard/base.js +2 -4
- package/src/parsing/standard/companyRegistration.d.ts +3 -2
- package/src/parsing/standard/date.d.ts +4 -6
- package/src/parsing/standard/date.js +1 -4
- package/src/parsing/standard/field.d.ts +4 -15
- package/src/parsing/standard/field.js +2 -6
- package/src/parsing/standard/index.d.ts +1 -1
- package/src/parsing/standard/locale.d.ts +1 -3
- package/src/parsing/standard/locale.js +1 -3
- package/src/parsing/standard/paymentDetails.d.ts +11 -8
- package/src/parsing/standard/paymentDetails.js +1 -8
- package/src/parsing/standard/tax.d.ts +8 -12
- package/src/parsing/standard/tax.js +1 -7
- package/src/product/financialDocument/financialDocumentV1Document.js +2 -2
- package/src/product/invoice/invoiceV4Document.d.ts +33 -32
- package/src/product/invoice/invoiceV4Document.js +89 -104
- package/src/product/invoice/invoiceV4LineItem.d.ts +16 -16
- package/src/product/invoice/invoiceV4LineItem.js +58 -60
- package/src/product/invoiceSplitter/invoiceSplitterV1Document.d.ts +2 -2
- package/src/product/invoiceSplitter/invoiceSplitterV1Document.js +1 -2
- package/src/product/invoiceSplitter/invoiceSplitterV1PageGroup.d.ts +1 -1
- package/src/product/invoiceSplitter/invoiceSplitterV1PageGroup.js +1 -1
- package/src/product/us/w9/w9V1.d.ts +1 -1
- package/src/product/us/w9/w9V1.js +1 -1
- package/src/product/us/w9/w9V1Document.d.ts +1 -1
- package/src/product/us/w9/w9V1Document.js +1 -1
- package/src/product/us/w9/w9V1Page.d.ts +1 -1
- package/src/product/us/w9/w9V1Page.js +1 -1
- package/src/http/mindeeApi.d.ts +0 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## v4.3.2 - 2023-11-07
|
|
4
|
+
### Changes
|
|
5
|
+
* :recycle: update invoice splitter
|
|
6
|
+
|
|
7
|
+
### Fixes
|
|
8
|
+
* :bug: fix display issues when products didn't have pages
|
|
9
|
+
* :memo: fix doc typos
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## v4.3.2 - 2023-10-04
|
|
13
|
+
### Changes
|
|
14
|
+
* :arrow_up: dependency upgrade
|
|
15
|
+
* :recycle: minor refactoring
|
|
16
|
+
|
|
17
|
+
|
|
3
18
|
## v4.3.1 - 2023-09-20
|
|
4
19
|
### Fixes
|
|
5
20
|
* :bug: take line height tolerance into account when evaluating fields
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mindee",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.3",
|
|
4
4
|
"description": "Mindee Client Library for Node.js",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": "bin/mindee.js",
|
|
@@ -45,9 +45,10 @@
|
|
|
45
45
|
"@types/node": "^18.15.11",
|
|
46
46
|
"@typescript-eslint/eslint-plugin": "^5.57.1",
|
|
47
47
|
"@typescript-eslint/parser": "^5.57.1",
|
|
48
|
-
"chai": "^4.3.
|
|
49
|
-
"eslint": "^8.
|
|
50
|
-
"
|
|
48
|
+
"chai": "^4.3.10",
|
|
49
|
+
"eslint": "^8.49.0",
|
|
50
|
+
"eslint-plugin-jsdoc": "^46.8.2",
|
|
51
|
+
"lint-staged": "^13.3.0",
|
|
51
52
|
"mocha": "^10.1.0",
|
|
52
53
|
"nock": "^13.3.3",
|
|
53
54
|
"ts-node": "^10.9.1",
|
package/src/cli.js
CHANGED
|
@@ -162,11 +162,10 @@ const CLI_COMMAND_CONFIG = new Map([
|
|
|
162
162
|
// EXECUTE THE COMMANDS
|
|
163
163
|
//
|
|
164
164
|
function initClient(options) {
|
|
165
|
-
|
|
165
|
+
return new client_1.Client({
|
|
166
166
|
apiKey: options.apiKey,
|
|
167
167
|
debug: options.debug,
|
|
168
168
|
});
|
|
169
|
-
return mindeeClient;
|
|
170
169
|
}
|
|
171
170
|
function getConfig(command) {
|
|
172
171
|
const conf = CLI_COMMAND_CONFIG.get(command);
|
|
@@ -187,11 +186,10 @@ function getPageOptions(options) {
|
|
|
187
186
|
return pageOptions;
|
|
188
187
|
}
|
|
189
188
|
function getPredictParams(options) {
|
|
190
|
-
|
|
189
|
+
return {
|
|
191
190
|
allWords: options.allWords,
|
|
192
191
|
cropper: options.cropper,
|
|
193
192
|
};
|
|
194
|
-
return predictParams;
|
|
195
193
|
}
|
|
196
194
|
async function callParse(productClass, command, inputPath, options) {
|
|
197
195
|
const mindeeClient = initClient(options);
|
|
@@ -250,6 +248,19 @@ async function callEnqueueAndParse(productClass, command, inputPath, options) {
|
|
|
250
248
|
printResponse(response.document, options);
|
|
251
249
|
}
|
|
252
250
|
}
|
|
251
|
+
async function callGetDocument(productClass, documentId, options) {
|
|
252
|
+
const mindeeClient = initClient(options);
|
|
253
|
+
const response = await mindeeClient.getDocument(productClass, documentId);
|
|
254
|
+
printResponse(response.document, options);
|
|
255
|
+
}
|
|
256
|
+
async function callSendFeedback(productClass, documentId, feedbackStr, options) {
|
|
257
|
+
const mindeeClient = initClient(options);
|
|
258
|
+
const feedback = {
|
|
259
|
+
feedback: JSON.parse(feedbackStr),
|
|
260
|
+
};
|
|
261
|
+
const response = await mindeeClient.sendFeedback(productClass, documentId, feedback);
|
|
262
|
+
console.log(response.feedback);
|
|
263
|
+
}
|
|
253
264
|
function printResponse(document, options) {
|
|
254
265
|
if (options.allWords) {
|
|
255
266
|
document.ocr?.mVisionV1.pages.forEach((page) => {
|
|
@@ -287,13 +298,16 @@ function addDisplayOptions(prog) {
|
|
|
287
298
|
prog.option("-p, --pages", "show content of individual pages");
|
|
288
299
|
}
|
|
289
300
|
function routeSwitchboard(command, inputPath, allOptions) {
|
|
290
|
-
|
|
301
|
+
if (command.parent === null || command.parent === undefined) {
|
|
302
|
+
throw new Error(`Improperly configured command: ${command.name()}`);
|
|
303
|
+
}
|
|
304
|
+
const docClass = getConfig(command.parent.name()).docClass;
|
|
291
305
|
if ("async" in command.opts() && command.opts()["async"]) {
|
|
292
306
|
return callEnqueueAndParse(docClass, command.name(), inputPath, allOptions);
|
|
293
307
|
}
|
|
294
308
|
return callParse(docClass, command.name(), inputPath, allOptions);
|
|
295
309
|
}
|
|
296
|
-
function
|
|
310
|
+
function addPredictAction(prog) {
|
|
297
311
|
if (prog.name() === COMMAND_CUSTOM) {
|
|
298
312
|
prog.action(function (inputPath, options, command) {
|
|
299
313
|
const allOptions = {
|
|
@@ -318,11 +332,33 @@ function addAction(prog) {
|
|
|
318
332
|
}
|
|
319
333
|
}
|
|
320
334
|
function cli() {
|
|
321
|
-
program.name("mindee")
|
|
322
|
-
|
|
335
|
+
program.name("mindee")
|
|
336
|
+
.description("Command line interface for Mindee products.")
|
|
337
|
+
.option("-d, --debug", "high verbosity mode");
|
|
323
338
|
CLI_COMMAND_CONFIG.forEach((info, name) => {
|
|
324
|
-
const
|
|
325
|
-
|
|
339
|
+
const productCmd = program.command(name)
|
|
340
|
+
.description(info.displayName);
|
|
341
|
+
if (info.async) {
|
|
342
|
+
const getDocProductCmd = productCmd.command("fetch")
|
|
343
|
+
.description("Fetch previously parsed results.")
|
|
344
|
+
.argument("<documentId>", "Unique ID of the document.")
|
|
345
|
+
.action(async (documentId, options) => {
|
|
346
|
+
const docClass = getConfig(name).docClass;
|
|
347
|
+
await callGetDocument(docClass, documentId, { ...options, ...productCmd.opts(), ...program.opts() });
|
|
348
|
+
});
|
|
349
|
+
addMainOptions(getDocProductCmd);
|
|
350
|
+
}
|
|
351
|
+
const feedbackProductCmd = productCmd.command("feedback")
|
|
352
|
+
.description("Send feedback for a document.")
|
|
353
|
+
.argument("<documentId>", "Unique ID of the document.")
|
|
354
|
+
.argument("<feedback>", "Feedback to send, ex '{\"key\": \"value\"}'.")
|
|
355
|
+
.action(async (documentId, feedback, options) => {
|
|
356
|
+
const docClass = getConfig(name).docClass;
|
|
357
|
+
await callSendFeedback(docClass, documentId, feedback, { ...options, ...productCmd.opts(), ...program.opts() });
|
|
358
|
+
});
|
|
359
|
+
addMainOptions(feedbackProductCmd);
|
|
360
|
+
const predictProductCmd = productCmd.command("parse")
|
|
361
|
+
.description("Send a file for parsing.");
|
|
326
362
|
if (info.async) {
|
|
327
363
|
const asyncOpt = new commander_1.Option("-A, --async", "Call asynchronously");
|
|
328
364
|
if (info.sync) {
|
|
@@ -332,15 +368,15 @@ function cli() {
|
|
|
332
368
|
asyncOpt.default(true);
|
|
333
369
|
asyncOpt.hideHelp();
|
|
334
370
|
}
|
|
335
|
-
|
|
371
|
+
predictProductCmd.addOption(asyncOpt);
|
|
336
372
|
}
|
|
337
373
|
if (name === COMMAND_CUSTOM) {
|
|
338
|
-
addCustomPostOptions(
|
|
374
|
+
addCustomPostOptions(predictProductCmd);
|
|
339
375
|
}
|
|
340
|
-
addMainOptions(
|
|
341
|
-
addDisplayOptions(
|
|
342
|
-
addPostOptions(
|
|
343
|
-
|
|
376
|
+
addMainOptions(predictProductCmd);
|
|
377
|
+
addDisplayOptions(predictProductCmd);
|
|
378
|
+
addPostOptions(predictProductCmd, info);
|
|
379
|
+
addPredictAction(predictProductCmd);
|
|
344
380
|
});
|
|
345
381
|
program.parse(process.argv);
|
|
346
382
|
}
|
package/src/client.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { Readable } from "stream";
|
|
5
5
|
import { InputSource, PageOptions } from "./input";
|
|
6
6
|
import { Endpoint } from "./http";
|
|
7
|
-
import {
|
|
7
|
+
import { AsyncPredictResponse, FeedbackResponse, Inference, PredictResponse, StringDict } from "./parsing/common";
|
|
8
8
|
/**
|
|
9
9
|
* Options relating to predictions.
|
|
10
10
|
*/
|
|
@@ -63,14 +63,14 @@ export declare class Client {
|
|
|
63
63
|
/** Key of the API. */
|
|
64
64
|
protected apiKey: string;
|
|
65
65
|
/**
|
|
66
|
-
* @param options options for the initialization of a client.
|
|
66
|
+
* @param {ClientOptions} options options for the initialization of a client.
|
|
67
67
|
*/
|
|
68
68
|
constructor({ apiKey, throwOnError, debug }?: ClientOptions);
|
|
69
69
|
/**
|
|
70
70
|
* Send a document to a synchronous endpoint and parse the predictions.
|
|
71
71
|
*
|
|
72
72
|
* @param productClass product class to use for calling the API and parsing the response.
|
|
73
|
-
* @param inputSource
|
|
73
|
+
* @param inputSource file to parse.
|
|
74
74
|
* @param params parameters relating to prediction options.
|
|
75
75
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
76
76
|
* @category Synchronous
|
|
@@ -80,6 +80,7 @@ export declare class Client {
|
|
|
80
80
|
/**
|
|
81
81
|
* Send the document to an asynchronous endpoint and return its ID in the queue.
|
|
82
82
|
* @param productClass product class to use for calling the API and parsing the response.
|
|
83
|
+
* @param inputSource file to parse.
|
|
83
84
|
* @param params parameters relating to prediction options.
|
|
84
85
|
* @category Asynchronous
|
|
85
86
|
* @returns a `Promise` containing the job (queue) corresponding to a document.
|
|
@@ -97,6 +98,35 @@ export declare class Client {
|
|
|
97
98
|
* parsing is complete.
|
|
98
99
|
*/
|
|
99
100
|
parseQueued<T extends Inference>(productClass: new (httpResponse: StringDict) => T, queueId: string, params?: PredictOptions): Promise<AsyncPredictResponse<T>>;
|
|
101
|
+
/**
|
|
102
|
+
* Fetch prediction results from a document already processed.
|
|
103
|
+
*
|
|
104
|
+
* @param productClass product class to use for calling the API and parsing the response.
|
|
105
|
+
* @param documentId id of the document to fetch.
|
|
106
|
+
* @param params optional parameters.
|
|
107
|
+
* @param params.endpoint Endpoint, only specify if using a custom product.
|
|
108
|
+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
109
|
+
* @category Synchronous
|
|
110
|
+
* @returns a `Promise` containing parsing results.
|
|
111
|
+
*/
|
|
112
|
+
getDocument<T extends Inference>(productClass: new (httpResponse: StringDict) => T, documentId: string, params?: {
|
|
113
|
+
endpoint?: Endpoint;
|
|
114
|
+
}): Promise<PredictResponse<T>>;
|
|
115
|
+
/**
|
|
116
|
+
* Send a feedback for a document.
|
|
117
|
+
*
|
|
118
|
+
* @param productClass product class to use for calling the API and parsing the response.
|
|
119
|
+
* @param documentId id of the document to send feedback for.
|
|
120
|
+
* @param feedback the feedback to send.
|
|
121
|
+
* @param params optional parameters.
|
|
122
|
+
* @param params.endpoint Endpoint, only specify if using a custom product.
|
|
123
|
+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
124
|
+
* @category Synchronous
|
|
125
|
+
* @returns a `Promise` containing feedback results.
|
|
126
|
+
*/
|
|
127
|
+
sendFeedback<T extends Inference>(productClass: new (httpResponse: StringDict) => T, documentId: string, feedback: StringDict, params?: {
|
|
128
|
+
endpoint?: Endpoint;
|
|
129
|
+
}): Promise<FeedbackResponse>;
|
|
100
130
|
/**
|
|
101
131
|
* Send a document to an asynchronous endpoint and poll the server until the result is sent or
|
|
102
132
|
* until the maximum amount of tries is reached.
|
|
@@ -118,13 +148,12 @@ export declare class Client {
|
|
|
118
148
|
protected getBooleanParam(param?: boolean): boolean;
|
|
119
149
|
/**
|
|
120
150
|
* Creates a custom endpoint with the given values. Raises an error if the endpoint is invalid.
|
|
121
|
-
* @param productClass product class to use for calling the API and parsing the response.
|
|
122
151
|
* @param endpointName Name of the custom Endpoint.
|
|
123
152
|
* @param accountName Name of the account tied to the Endpoint.
|
|
124
|
-
* @param
|
|
153
|
+
* @param endpointVersion Version of the custom Endpoint.
|
|
125
154
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
126
155
|
*
|
|
127
|
-
* @returns a new endpoint
|
|
156
|
+
* @returns Endpoint a new product endpoint
|
|
128
157
|
*/
|
|
129
158
|
createEndpoint(endpointName: string, accountName: string, endpointVersion?: string): Endpoint;
|
|
130
159
|
/**
|
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_validateAsyncParams,
|
|
7
|
+
var _Client_instances, _Client_validateAsyncParams, _Client_buildProductEndpoint, _Client_buildApiSettings, _Client_initializeOTSEndpoint, _Client_cleanAccountName, _Client_getOtsEndpoint;
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.Client = void 0;
|
|
10
10
|
const input_1 = require("./input");
|
|
@@ -22,7 +22,7 @@ const promises_1 = require("node:timers/promises");
|
|
|
22
22
|
*/
|
|
23
23
|
class Client {
|
|
24
24
|
/**
|
|
25
|
-
* @param options options for the initialization of a client.
|
|
25
|
+
* @param {ClientOptions} options options for the initialization of a client.
|
|
26
26
|
*/
|
|
27
27
|
constructor({ apiKey, throwOnError, debug } = {
|
|
28
28
|
apiKey: "",
|
|
@@ -42,7 +42,7 @@ class Client {
|
|
|
42
42
|
* Send a document to a synchronous endpoint and parse the predictions.
|
|
43
43
|
*
|
|
44
44
|
* @param productClass product class to use for calling the API and parsing the response.
|
|
45
|
-
* @param inputSource
|
|
45
|
+
* @param inputSource file to parse.
|
|
46
46
|
* @param params parameters relating to prediction options.
|
|
47
47
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
48
48
|
* @category Synchronous
|
|
@@ -69,6 +69,7 @@ class Client {
|
|
|
69
69
|
/**
|
|
70
70
|
* Send the document to an asynchronous endpoint and return its ID in the queue.
|
|
71
71
|
* @param productClass product class to use for calling the API and parsing the response.
|
|
72
|
+
* @param inputSource file to parse.
|
|
72
73
|
* @param params parameters relating to prediction options.
|
|
73
74
|
* @category Asynchronous
|
|
74
75
|
* @returns a `Promise` containing the job (queue) corresponding to a document.
|
|
@@ -102,6 +103,39 @@ class Client {
|
|
|
102
103
|
const docResponse = await endpoint.getQueuedDocument(queueId);
|
|
103
104
|
return new common_1.AsyncPredictResponse(productClass, docResponse.data);
|
|
104
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Fetch prediction results from a document already processed.
|
|
108
|
+
*
|
|
109
|
+
* @param productClass product class to use for calling the API and parsing the response.
|
|
110
|
+
* @param documentId id of the document to fetch.
|
|
111
|
+
* @param params optional parameters.
|
|
112
|
+
* @param params.endpoint Endpoint, only specify if using a custom product.
|
|
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 getDocument(productClass, documentId, params = {}) {
|
|
118
|
+
const endpoint = params?.endpoint ?? __classPrivateFieldGet(this, _Client_instances, "m", _Client_initializeOTSEndpoint).call(this, productClass);
|
|
119
|
+
const response = await endpoint.getDocument(documentId);
|
|
120
|
+
return new common_1.PredictResponse(productClass, response.data);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Send a feedback for a document.
|
|
124
|
+
*
|
|
125
|
+
* @param productClass product class to use for calling the API and parsing the response.
|
|
126
|
+
* @param documentId id of the document to send feedback for.
|
|
127
|
+
* @param feedback the feedback to send.
|
|
128
|
+
* @param params optional parameters.
|
|
129
|
+
* @param params.endpoint Endpoint, only specify if using a custom product.
|
|
130
|
+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
131
|
+
* @category Synchronous
|
|
132
|
+
* @returns a `Promise` containing feedback results.
|
|
133
|
+
*/
|
|
134
|
+
async sendFeedback(productClass, documentId, feedback, params = {}) {
|
|
135
|
+
const endpoint = params?.endpoint ?? __classPrivateFieldGet(this, _Client_instances, "m", _Client_initializeOTSEndpoint).call(this, productClass);
|
|
136
|
+
const response = await endpoint.sendFeedback(documentId, feedback);
|
|
137
|
+
return new common_1.FeedbackResponse(response.data);
|
|
138
|
+
}
|
|
105
139
|
/**
|
|
106
140
|
* Send a document to an asynchronous endpoint and poll the server until the result is sent or
|
|
107
141
|
* until the maximum amount of tries is reached.
|
|
@@ -162,13 +196,12 @@ Job status: ${pollResults.job.status}.`);
|
|
|
162
196
|
}
|
|
163
197
|
/**
|
|
164
198
|
* Creates a custom endpoint with the given values. Raises an error if the endpoint is invalid.
|
|
165
|
-
* @param productClass product class to use for calling the API and parsing the response.
|
|
166
199
|
* @param endpointName Name of the custom Endpoint.
|
|
167
200
|
* @param accountName Name of the account tied to the Endpoint.
|
|
168
|
-
* @param
|
|
201
|
+
* @param endpointVersion Version of the custom Endpoint.
|
|
169
202
|
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
|
|
170
203
|
*
|
|
171
|
-
* @returns a new endpoint
|
|
204
|
+
* @returns Endpoint a new product endpoint
|
|
172
205
|
*/
|
|
173
206
|
createEndpoint(endpointName, accountName, endpointVersion) {
|
|
174
207
|
const cleanAccountName = __classPrivateFieldGet(this, _Client_instances, "m", _Client_cleanAccountName).call(this, product_1.CustomV1, accountName);
|
|
@@ -183,7 +216,7 @@ Job status: ${pollResults.job.status}.`);
|
|
|
183
216
|
else {
|
|
184
217
|
cleanEndpointVersion = endpointVersion;
|
|
185
218
|
}
|
|
186
|
-
return __classPrivateFieldGet(this, _Client_instances, "m",
|
|
219
|
+
return __classPrivateFieldGet(this, _Client_instances, "m", _Client_buildProductEndpoint).call(this, endpointName, cleanAccountName, cleanEndpointVersion);
|
|
187
220
|
}
|
|
188
221
|
/**
|
|
189
222
|
* Load an input document from a local path.
|
|
@@ -262,20 +295,18 @@ _Client_instances = new WeakSet(), _Client_validateAsyncParams = function _Clien
|
|
|
262
295
|
if (!Number.isInteger(asyncParams.maxRetries)) {
|
|
263
296
|
throw Error("Retry amount must be an integer.");
|
|
264
297
|
}
|
|
265
|
-
},
|
|
266
|
-
|
|
298
|
+
}, _Client_buildProductEndpoint = function _Client_buildProductEndpoint(endpointName, accountName, endpointVersion) {
|
|
299
|
+
return new http_1.Endpoint(endpointName, accountName, endpointVersion, __classPrivateFieldGet(this, _Client_instances, "m", _Client_buildApiSettings).call(this));
|
|
300
|
+
}, _Client_buildApiSettings = function _Client_buildApiSettings() {
|
|
301
|
+
return new http_1.ApiSettings({
|
|
267
302
|
apiKey: this.apiKey,
|
|
268
|
-
urlName: endpointName,
|
|
269
|
-
version: endpointVersion,
|
|
270
|
-
owner: accountName,
|
|
271
303
|
});
|
|
272
|
-
return new http_1.Endpoint(endpointName, accountName, endpointVersion, apiSettings);
|
|
273
304
|
}, _Client_initializeOTSEndpoint = function _Client_initializeOTSEndpoint(productClass) {
|
|
274
305
|
if (productClass.name === "CustomV1") {
|
|
275
306
|
throw new Error("Incorrect parameters for Custom build.");
|
|
276
307
|
}
|
|
277
|
-
const [endpointName, endpointVersion] = __classPrivateFieldGet(this, _Client_instances, "m",
|
|
278
|
-
return __classPrivateFieldGet(this, _Client_instances, "m",
|
|
308
|
+
const [endpointName, endpointVersion] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_getOtsEndpoint).call(this, productClass);
|
|
309
|
+
return __classPrivateFieldGet(this, _Client_instances, "m", _Client_buildProductEndpoint).call(this, endpointName, http_1.STANDARD_API_OWNER, endpointVersion);
|
|
279
310
|
}, _Client_cleanAccountName = function _Client_cleanAccountName(productClass, accountName) {
|
|
280
311
|
if (productClass.name === "CustomV1") {
|
|
281
312
|
if (!accountName || accountName.length === 0) {
|
|
@@ -285,7 +316,7 @@ _Client_instances = new WeakSet(), _Client_validateAsyncParams = function _Clien
|
|
|
285
316
|
return accountName;
|
|
286
317
|
}
|
|
287
318
|
return http_1.STANDARD_API_OWNER;
|
|
288
|
-
},
|
|
319
|
+
}, _Client_getOtsEndpoint = function _Client_getOtsEndpoint(productClass) {
|
|
289
320
|
const [endpointName, endpointVersion] = inference_1.InferenceFactory.getEndpoint(productClass);
|
|
290
321
|
return [endpointName, endpointVersion];
|
|
291
322
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const API_KEY_ENVVAR_NAME: string;
|
|
2
|
+
export declare const API_HOST_ENVVAR_NAME: string;
|
|
3
|
+
export declare const STANDARD_API_OWNER: string;
|
|
4
|
+
interface MindeeApiConstructorProps {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class ApiSettings {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
baseHeaders: Record<string, string>;
|
|
10
|
+
hostname: string;
|
|
11
|
+
constructor({ apiKey, }: MindeeApiConstructorProps);
|
|
12
|
+
protected apiKeyFromEnv(): string;
|
|
13
|
+
protected hostnameFromEnv(): string;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.ApiSettings = exports.STANDARD_API_OWNER = exports.API_HOST_ENVVAR_NAME = exports.API_KEY_ENVVAR_NAME = void 0;
|
|
27
27
|
const logger_1 = require("../logger");
|
|
28
28
|
const package_json_1 = require("../../package.json");
|
|
29
29
|
const os = __importStar(require("os"));
|
|
@@ -32,31 +32,28 @@ exports.API_HOST_ENVVAR_NAME = "MINDEE_API_HOST";
|
|
|
32
32
|
exports.STANDARD_API_OWNER = "mindee";
|
|
33
33
|
const DEFAULT_MINDEE_API_HOST = "api.mindee.net";
|
|
34
34
|
const USER_AGENT = `mindee-api-nodejs@v${package_json_1.version} nodejs-${process.version} ${os.type().toLowerCase()}`;
|
|
35
|
-
class
|
|
36
|
-
constructor({ apiKey = "",
|
|
35
|
+
class ApiSettings {
|
|
36
|
+
constructor({ apiKey = "", }) {
|
|
37
37
|
if (!apiKey || apiKey.length === 0) {
|
|
38
|
-
|
|
39
|
-
process.env[exports.API_KEY_ENVVAR_NAME].length === 0) {
|
|
40
|
-
throw new Error(`Missing API key for ${urlName} v${version} (belonging to ${owner}) check your Client Configuration.
|
|
41
|
-
You can set this using the ${exports.API_KEY_ENVVAR_NAME} environment variable.`);
|
|
42
|
-
}
|
|
43
|
-
this.apiKey = process.env[exports.API_KEY_ENVVAR_NAME];
|
|
38
|
+
this.apiKey = this.apiKeyFromEnv();
|
|
44
39
|
}
|
|
45
40
|
else {
|
|
46
41
|
this.apiKey = apiKey;
|
|
47
42
|
}
|
|
43
|
+
if (!this.apiKey || this.apiKey.length === 0) {
|
|
44
|
+
throw new Error("Your API key could not be set, check your Client Configuration\n."
|
|
45
|
+
+ `You can set this using the ${exports.API_KEY_ENVVAR_NAME} environment variable.`);
|
|
46
|
+
}
|
|
48
47
|
this.baseHeaders = {
|
|
49
48
|
"User-Agent": USER_AGENT,
|
|
50
49
|
Authorization: `Token ${this.apiKey}`,
|
|
51
50
|
};
|
|
52
51
|
this.hostname = this.hostnameFromEnv();
|
|
53
|
-
this.urlName = urlName;
|
|
54
|
-
this.version = version;
|
|
55
52
|
}
|
|
56
53
|
apiKeyFromEnv() {
|
|
57
54
|
const envVarValue = process.env[exports.API_KEY_ENVVAR_NAME];
|
|
58
55
|
if (envVarValue) {
|
|
59
|
-
logger_1.logger.debug(`Set
|
|
56
|
+
logger_1.logger.debug(`Set API key from environment: ${exports.API_KEY_ENVVAR_NAME}`);
|
|
60
57
|
return envVarValue;
|
|
61
58
|
}
|
|
62
59
|
return "";
|
|
@@ -70,4 +67,4 @@ You can set this using the ${exports.API_KEY_ENVVAR_NAME} environment variable.`
|
|
|
70
67
|
return DEFAULT_MINDEE_API_HOST;
|
|
71
68
|
}
|
|
72
69
|
}
|
|
73
|
-
exports.
|
|
70
|
+
exports.ApiSettings = ApiSettings;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { ApiSettings } from "./apiSettings";
|
|
4
|
+
import { IncomingMessage, ClientRequest } from "http";
|
|
5
|
+
import { RequestOptions } from "https";
|
|
6
|
+
export interface EndpointResponse {
|
|
7
|
+
messageObj: IncomingMessage;
|
|
8
|
+
data: {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Base endpoint for the Mindee API.
|
|
14
|
+
*/
|
|
15
|
+
export declare abstract class BaseEndpoint {
|
|
16
|
+
/** Settings relating to the API. */
|
|
17
|
+
settings: ApiSettings;
|
|
18
|
+
protected constructor(settings: ApiSettings);
|
|
19
|
+
/**
|
|
20
|
+
* Reads a response from the API and processes it.
|
|
21
|
+
* @param options options related to the request itself.
|
|
22
|
+
* @param resolve the resolved response
|
|
23
|
+
* @param reject promise rejection reason.
|
|
24
|
+
* @returns the processed request.
|
|
25
|
+
*/
|
|
26
|
+
protected readResponse(options: RequestOptions, resolve: (value: EndpointResponse | PromiseLike<EndpointResponse>) => void, reject: (reason?: any) => void): ClientRequest;
|
|
27
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseEndpoint = void 0;
|
|
4
|
+
const logger_1 = require("../logger");
|
|
5
|
+
const https_1 = require("https");
|
|
6
|
+
/**
|
|
7
|
+
* Base endpoint for the Mindee API.
|
|
8
|
+
*/
|
|
9
|
+
class BaseEndpoint {
|
|
10
|
+
constructor(settings) {
|
|
11
|
+
this.settings = settings;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reads a response from the API and processes it.
|
|
15
|
+
* @param options options related to the request itself.
|
|
16
|
+
* @param resolve the resolved response
|
|
17
|
+
* @param reject promise rejection reason.
|
|
18
|
+
* @returns the processed request.
|
|
19
|
+
*/
|
|
20
|
+
readResponse(options, resolve, reject) {
|
|
21
|
+
logger_1.logger.debug(`${options.method}: https://${options.hostname}${options.path}`);
|
|
22
|
+
const req = (0, https_1.request)(options, function (res) {
|
|
23
|
+
// when the encoding is set, data chunks will be strings
|
|
24
|
+
res.setEncoding("utf-8");
|
|
25
|
+
let responseBody = "";
|
|
26
|
+
res.on("data", function (chunk) {
|
|
27
|
+
logger_1.logger.debug("Receiving data ...");
|
|
28
|
+
responseBody += chunk;
|
|
29
|
+
});
|
|
30
|
+
res.on("end", function () {
|
|
31
|
+
logger_1.logger.debug("Parsing the response ...");
|
|
32
|
+
// handle empty responses from server, for example in the case of redirects
|
|
33
|
+
if (!responseBody) {
|
|
34
|
+
responseBody = "{}";
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const parsedResponse = JSON.parse(responseBody);
|
|
38
|
+
try {
|
|
39
|
+
resolve({
|
|
40
|
+
messageObj: res,
|
|
41
|
+
data: parsedResponse,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
logger_1.logger.error("Could not construct the return object.");
|
|
46
|
+
reject(error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
logger_1.logger.error("Could not parse the return as JSON.");
|
|
51
|
+
logger_1.logger.debug(responseBody);
|
|
52
|
+
resolve({
|
|
53
|
+
messageObj: res,
|
|
54
|
+
data: { reconstructedResponse: responseBody },
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
req.on("error", (err) => {
|
|
60
|
+
reject(err);
|
|
61
|
+
});
|
|
62
|
+
return req;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.BaseEndpoint = BaseEndpoint;
|
package/src/http/endpoint.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import { RequestOptions } from "https";
|
|
4
1
|
import { InputSource } from "../input";
|
|
5
|
-
import { IncomingMessage, ClientRequest } from "http";
|
|
6
2
|
import { PageOptions } from "../input";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
import { ApiSettings } from "./apiSettings";
|
|
4
|
+
import { BaseEndpoint, EndpointResponse } from "./baseEndpoint";
|
|
5
|
+
import { StringDict } from "../parsing/common";
|
|
6
|
+
export interface PredictParams {
|
|
7
|
+
inputDoc: InputSource;
|
|
8
|
+
includeWords: boolean;
|
|
9
|
+
pageOptions?: PageOptions;
|
|
10
|
+
cropper: boolean;
|
|
13
11
|
}
|
|
14
12
|
/**
|
|
15
|
-
* Endpoint
|
|
13
|
+
* Endpoint for a product (OTS or Custom).
|
|
16
14
|
*/
|
|
17
|
-
export declare class Endpoint {
|
|
15
|
+
export declare class Endpoint extends BaseEndpoint {
|
|
18
16
|
#private;
|
|
19
17
|
/** URL of a product. */
|
|
20
18
|
urlName: string;
|
|
@@ -24,43 +22,42 @@ export declare class Endpoint {
|
|
|
24
22
|
version: string;
|
|
25
23
|
/** Entire root of the URL for API calls. */
|
|
26
24
|
urlRoot: string;
|
|
27
|
-
|
|
28
|
-
settings: MindeeApi;
|
|
29
|
-
constructor(urlName: string, owner: string, version: string, settings: MindeeApi);
|
|
25
|
+
constructor(urlName: string, owner: string, version: string, settings: ApiSettings);
|
|
30
26
|
/**
|
|
31
27
|
* Sends a prediction to the API and parses out the result.
|
|
32
28
|
* Throws an error if the server's response contains one.
|
|
33
|
-
* @param params parameters relating to prediction options.
|
|
29
|
+
* @param {PredictParams} params parameters relating to prediction options.
|
|
34
30
|
* @category Synchronous
|
|
35
31
|
* @returns a `Promise` containing parsing results.
|
|
36
32
|
*/
|
|
37
|
-
predict(params:
|
|
38
|
-
inputDoc: InputSource;
|
|
39
|
-
includeWords: boolean;
|
|
40
|
-
pageOptions?: PageOptions;
|
|
41
|
-
cropper: boolean;
|
|
42
|
-
}): Promise<EndpointResponse>;
|
|
33
|
+
predict(params: PredictParams): Promise<EndpointResponse>;
|
|
43
34
|
/**
|
|
44
35
|
* Enqueues a prediction to the API.
|
|
45
36
|
* Throws an error if the server's response contains one.
|
|
46
|
-
* @param params parameters relating to prediction options.
|
|
37
|
+
* @param {PredictParams} params parameters relating to prediction options.
|
|
47
38
|
* @category Asynchronous
|
|
48
39
|
* @returns a `Promise` containing queue data.
|
|
49
40
|
*/
|
|
50
|
-
predictAsync(params:
|
|
51
|
-
inputDoc: InputSource;
|
|
52
|
-
includeWords: boolean;
|
|
53
|
-
pageOptions?: PageOptions;
|
|
54
|
-
cropper: boolean;
|
|
55
|
-
}): Promise<EndpointResponse>;
|
|
41
|
+
predictAsync(params: PredictParams): Promise<EndpointResponse>;
|
|
56
42
|
/**
|
|
57
43
|
* Requests the results of a queued document from the API.
|
|
58
44
|
* Throws an error if the server's response contains one.
|
|
59
|
-
* @param
|
|
45
|
+
* @param queueId The document's ID in the queue.
|
|
60
46
|
* @category Asynchronous
|
|
61
47
|
* @returns a `Promise` containing the parsed result.
|
|
62
48
|
*/
|
|
63
49
|
getQueuedDocument(queueId: string): Promise<EndpointResponse>;
|
|
50
|
+
/**
|
|
51
|
+
* Send a feedback
|
|
52
|
+
* @param {string} documentId
|
|
53
|
+
*/
|
|
54
|
+
getDocument(documentId: string): Promise<EndpointResponse>;
|
|
55
|
+
/**
|
|
56
|
+
* Send a feedback
|
|
57
|
+
* @param {string} documentId - ID of the document to send feedback to.
|
|
58
|
+
* @param {StringDict} feedback - Feedback object to send.
|
|
59
|
+
*/
|
|
60
|
+
sendFeedback(documentId: string, feedback: StringDict): Promise<EndpointResponse>;
|
|
64
61
|
/**
|
|
65
62
|
* Send a file to a prediction API.
|
|
66
63
|
* @param input
|
|
@@ -69,12 +66,4 @@ export declare class Endpoint {
|
|
|
69
66
|
* @param cropper
|
|
70
67
|
*/
|
|
71
68
|
protected sendFileForPrediction(input: InputSource, predictUrl: string, includeWords?: boolean, cropper?: boolean): Promise<EndpointResponse>;
|
|
72
|
-
/**
|
|
73
|
-
* Reads a response from the API and processes it.
|
|
74
|
-
* @param options options related to the request itself.
|
|
75
|
-
* @param resolve the resolved response
|
|
76
|
-
* @param reject promise rejection reason.
|
|
77
|
-
* @returns the processed request.
|
|
78
|
-
*/
|
|
79
|
-
protected readResponse(options: RequestOptions, resolve: (value: EndpointResponse | PromiseLike<EndpointResponse>) => void, reject: (reason?: any) => void): ClientRequest;
|
|
80
69
|
}
|