Skip to content

Commit b3daae4

Browse files
📝 update technical documentation (#194)
1 parent ce9fcf7 commit b3daae4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+461
-49
lines changed

src/client.ts

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import { LOG_LEVELS, logger } from "./logger";
2121
import { InferenceFactory } from "./parsing/common/inference";
2222
import { CustomV1 } from "./product";
2323

24+
/**
25+
* Options relating to predictions.
26+
*/
2427
export interface PredictOptions {
25-
endpointName?: string;
26-
accountName?: string;
27-
endpointVersion?: string;
28+
/** A custom endpoint. */
2829
endpoint?: Endpoint;
2930
/**
3031
* Whether to include the full text for each page.
@@ -38,19 +39,11 @@ export interface PredictOptions {
3839
* This performs a cropping operation on the server and will increase response time.
3940
*/
4041
cropper?: boolean;
41-
pageOptions?: PageOptions;
42-
}
43-
44-
export interface CustomConfigParams {
45-
/** Your organization's username on the API Builder. */
46-
accountName: string;
47-
/** The "API name" field in the "Settings" page of the API Builder. */
48-
endpointName: string;
49-
/**
50-
* If set, locks the version of the model to use.
51-
* If not set, use the latest version of the model.
42+
/**
43+
* If set, remove pages from the document as specified.
44+
* This is done before sending the file to the server and is useful to avoid page limitations.
5245
*/
53-
version?: string;
46+
pageOptions?: PageOptions;
5447
}
5548

5649
export interface ClientOptions {
@@ -63,13 +56,16 @@ export interface ClientOptions {
6356
}
6457

6558
/**
66-
* Mindee Client
59+
* Mindee Client class that centralizes most basic operations.
60+
*
61+
* @category Client
6762
*/
6863
export class Client {
64+
/** Key of the API. */
6965
protected apiKey: string;
7066

7167
/**
72-
* @param options
68+
* @param options options for the initialization of a client.
7369
*/
7470
constructor(
7571
{ apiKey, throwOnError, debug }: ClientOptions = {
@@ -89,8 +85,14 @@ export class Client {
8985

9086
/**
9187
* Send a document to a synchronous endpoint and parse the predictions.
92-
* @param productClass
93-
* @param params
88+
*
89+
* @param productClass product class to use for calling the API and parsing the response.
90+
* @param inputSource document to parse.
91+
* @param params parameters relating to prediction options.
92+
*
93+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
94+
* @category Synchronous
95+
* @returns a `Promise` containing parsing results.
9496
*/
9597
async parse<T extends Inference>(
9698
productClass: new (httpResponse: StringDict) => T,
@@ -118,8 +120,11 @@ export class Client {
118120

119121
/**
120122
* Send the document to an asynchronous endpoint and return its ID in the queue.
121-
* @param productClass
122-
* @param params
123+
* @param productClass product class to use for calling the API and parsing the response.
124+
* @param params parameters relating to prediction options.
125+
* @category Asynchronous
126+
*
127+
* @returns a `Promise` containing the job (queue) corresponding to a document.
123128
*/
124129
async enqueue<T extends Inference>(
125130
productClass: new (httpResponse: StringDict) => T,
@@ -141,6 +146,18 @@ export class Client {
141146
return new AsyncPredictResponse<T>(productClass, rawResponse.data);
142147
}
143148

149+
/**
150+
* Polls a queue and returns its status as well as the prediction results if the parsing is done.
151+
*
152+
* @param productClass product class to use for calling the API and parsing the response.
153+
* @param queueId id of the queue to poll.
154+
* @param params parameters relating to prediction options.
155+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
156+
* @category Asynchronous
157+
*
158+
* @returns a `Promise` containing a `Job`, which also contains a `Document` if the
159+
* parsing is complete.
160+
*/
144161
async parseQueued<T extends Inference>(
145162
productClass: new (httpResponse: StringDict) => T,
146163
queueId: string,
@@ -152,10 +169,22 @@ export class Client {
152169
return new AsyncPredictResponse<T>(productClass, docResponse.data);
153170
}
154171

172+
/**
173+
* Forces boolean coercion on truthy/falsy parameters.
174+
* @param param input parameter to check.
175+
* @returns a strict boolean value.
176+
*/
155177
protected getBooleanParam(param?: boolean): boolean {
156178
return param !== undefined ? param : false;
157179
}
158180

181+
/**
182+
* Builds a custom endpoint.
183+
* @param endpointName name of the endpoint.
184+
* @param accountName name of the endpoint's owner.
185+
* @param endpointVersion version of the endpoint.
186+
* @returns a custom `Endpoint` object.
187+
*/
159188
#buildEndpoint(
160189
endpointName: string,
161190
accountName: string,
@@ -177,10 +206,13 @@ export class Client {
177206

178207
/**
179208
* Creates a custom endpoint with the given values. Raises an error if the endpoint is invalid.
180-
* @param productClass Class of the product
181-
* @param endpointName Name of a custom Endpoint
182-
* @param accountName Name of the account tied to the active Endpoint
183-
* @param version Version of a custom Endpoint
209+
* @param productClass product class to use for calling the API and parsing the response.
210+
* @param endpointName Name of the custom Endpoint.
211+
* @param accountName Name of the account tied to the Endpoint.
212+
* @param version Version of the custom Endpoint.
213+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
214+
*
215+
* @returns a new endpoint
184216
*/
185217
createEndpoint(
186218
endpointName: string,
@@ -229,9 +261,11 @@ export class Client {
229261

230262
/**
231263
* Checks that an account name is provided for custom builds, and sets the default one otherwise.
232-
* @param productClass Type of product
233-
* @param accountName Account name. Only required on custom builds.
234-
* @returns {string} The name of the account. Sends an error if one isn't provided for a custom build.
264+
* @param productClass product class to use for calling the API and parsing the response.
265+
* @param accountName name of the account's holder. Only required on custom builds.
266+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
267+
*
268+
* @returns the name of the account. Sends an error if one isn't provided for a custom build.
235269
*/
236270
#cleanAccountName<T extends Inference>(
237271
productClass: new (httpResponse: StringDict) => T,
@@ -251,8 +285,10 @@ export class Client {
251285

252286
/**
253287
* Get the name and version of an OTS endpoint.
254-
* @param productClass Type of product
255-
* @returns {[string, string]} An endpoint's name and version
288+
* @param productClass product class to use for calling the API and parsing the response. Mandatory to retrieve default OTS endpoint data.
289+
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
290+
*
291+
* @returns an endpoint's name and version.
256292
*/
257293
#getEndpoint<T extends Inference>(
258294
productClass: new (httpResponse: StringDict) => T
@@ -274,8 +310,8 @@ export class Client {
274310

275311
/**
276312
* Load an input document from a base64 encoded string.
277-
* @param inputString
278-
* @param filename
313+
* @param inputString input content, as a string.
314+
* @param filename file name.
279315
*/
280316
docFromBase64(inputString: string, filename: string): InputSource {
281317
return new Base64Input({
@@ -286,8 +322,8 @@ export class Client {
286322

287323
/**
288324
* Load an input document from a `stream.Readable` object.
289-
* @param inputStream
290-
* @param filename
325+
* @param inputStream input content, as a readable stream.
326+
* @param filename file name.
291327
*/
292328
docFromStream(inputStream: Readable, filename: string): InputSource {
293329
return new StreamInput({
@@ -298,8 +334,8 @@ export class Client {
298334

299335
/**
300336
* Load an input document from a bytes string.
301-
* @param inputBytes
302-
* @param filename
337+
* @param inputBytes input content, as readable bytes.
338+
* @param filename file name.
303339
*/
304340
docFromBytes(inputBytes: string, filename: string): InputSource {
305341
return new BytesInput({
@@ -310,7 +346,7 @@ export class Client {
310346

311347
/**
312348
* Load an input document from a URL.
313-
* @param url
349+
* @param url input url. Must be HTTPS.
314350
*/
315351
docFromUrl(url: string): InputSource {
316352
return new UrlInput({
@@ -320,8 +356,8 @@ export class Client {
320356

321357
/**
322358
* Load an input document from a Buffer.
323-
* @param buffer
324-
* @param filename
359+
* @param buffer input content, as a buffer.
360+
* @param filename file name.
325361
*/
326362
docFromBuffer(buffer: Buffer, filename: string): InputSource {
327363
return new BufferInput({

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * as product from "./product";
2-
export { Client, CustomConfigParams, PredictOptions } from "./client";
2+
export { Client, PredictOptions } from "./client";
33
export {
44
AsyncPredictResponse,
55
PredictResponse,

src/input/pageOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** Options to pass to the `parse` method for cutting multi-page documents. */
12
export interface PageOptions {
23
/**
34
* Zero-based list of page indexes.
@@ -15,7 +16,10 @@ export interface PageOptions {
1516
onMinPages: number;
1617
}
1718

19+
/** Operation to apply on the document, given the page indexes specified. */
1820
export enum PageOptionsOperation {
21+
/** Only keep pages matching the provided indexes. */
1922
KeepOnly = "KEEP_ONLY",
23+
/** Remove pages matching the provided indexes. */
2024
Remove = "REMOVE",
2125
}

src/parsing/common/apiRequest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { StringDict } from "./stringDict";
2-
2+
/**
3+
* Holds the information relating to an API HTTP request.
4+
*
5+
* @category API Response
6+
*/
37
export class ApiRequest {
8+
/** An object detailing the error. */
49
error: StringDict;
10+
/** Target of the request. */
511
resources: string[];
12+
/** Status of the request. Either "failure" or "success". */
613
status: "failure" | "success";
714
/** HTTP status code */
815
statusCode: number;
16+
/** URL of the request. */
917
url: string;
1018

1119
constructor(serverResponse: StringDict) {

src/parsing/common/apiResponse.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { ApiRequest } from "./apiRequest";
22
import { StringDict } from "./stringDict";
33

4+
5+
/** Base wrapper for API requests.
6+
*
7+
* @category API Response
8+
*/
49
export abstract class ApiResponse {
10+
/** Initial request sent to the API. */
511
apiRequest: ApiRequest;
612

13+
/**
14+
*
15+
* @param serverResponse JSON response from the server.
16+
*/
717
constructor(serverResponse: StringDict) {
818
this.apiRequest = new ApiRequest(serverResponse["api_request"]);
919
}

src/parsing/common/asyncPredictResponse.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import { StringDict } from "./stringDict";
33
import { Inference } from "./inference";
44
import { Document } from "./document";
55

6+
/** Wrapper for asynchronous request queues. Holds information regarding a job (queue).
7+
*
8+
* @category API Response
9+
* @category Asynchronous
10+
*/
611
export class Job {
12+
/** Timestamp noting the enqueueing of a document. */
713
issuedAt: Date;
14+
/** Timestamp noting the availability of a prediction for an enqueued document. */
815
availableAt?: Date;
16+
/** ID of the job. */
917
id: string;
18+
/** Status of the job. */
1019
status?: "waiting" | "processing" | "completed";
11-
/**
12-
* The time taken to process the job, in milliseconds.
13-
*/
20+
/** The time taken to process the job, in milliseconds. */
1421
milliSecsTaken?: number;
1522

1623
constructor(jsonResponse: StringDict) {
@@ -40,10 +47,24 @@ export class Job {
4047
}
4148
}
4249

50+
/** Wrapper for asynchronous jobs and parsing results.
51+
*
52+
* @category API Response
53+
* @category Asynchronous
54+
*/
4355
export class AsyncPredictResponse<T extends Inference> extends ApiResponse {
56+
/** Job for a queue. */
4457
job: Job;
58+
/** Prediction for an asynchronous request. Will not be available so long as the job is not
59+
* `completed`.
60+
*/
4561
document?: Document<T>;
4662

63+
/**
64+
*
65+
* @param inferenceClass constructor signature for an inference.
66+
* @param httpResponse raw http response.
67+
*/
4768
constructor(
4869
inferenceClass: new (httpResponse: StringDict) => T,
4970
httpResponse: StringDict

src/parsing/common/document.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@ import { Inference } from "./inference";
44
import { Ocr } from "./ocr";
55
import { StringDict } from "./stringDict";
66

7+
/**
8+
* Document prediction wrapper class. Holds the results of a parsed document.
9+
* @typeParam T an extension of an `Inference`. Mandatory in order to properly create an inference.
10+
*/
711
export class Document<T extends Inference> {
12+
/** File name as sent back by the server. */
813
filename: string;
14+
/** Result of the base inference. */
915
inference: T;
16+
/** Id of the document as sent back by the server. */
1017
id: string;
18+
/** Potential `Extras` fields sent back along the prediction. */
1119
extras?: Extras;
20+
/** Raw-text response for `allWords` parsing. */
1221
ocr?: Ocr;
1322

23+
/**
24+
*
25+
* @param inferenceClass constructor signature for an inference.
26+
* @param httpResponse raw http response.
27+
*/
1428
constructor(
1529
inferenceClass: new (httpResponse: StringDict) => T,
1630
httpResponse: StringDict
@@ -20,7 +34,7 @@ export class Document<T extends Inference> {
2034
this.ocr = httpResponse?.ocr ?? undefined;
2135
this.inference = new inferenceClass(httpResponse["inference"]);
2236
// Note: this is a convoluted but functional way of being able to implement/use Extras fields
23-
// as an extension of a Map object (like having an adapted toString() method, for instance)
37+
// as an extension of a Map object (like having an adapted toString() method, for instance).
2438
if (
2539
httpResponse["extras"] &&
2640
Object.keys(httpResponse["extras"].length > 0)
@@ -38,6 +52,10 @@ export class Document<T extends Inference> {
3852
}
3953
}
4054

55+
56+
/**
57+
* Default string representation.
58+
*/
4159
toString() {
4260
return `########\nDocument\n########
4361
:Mindee ID: ${this.id}

0 commit comments

Comments
 (0)