Skip to content

Commit 3c761a0

Browse files
✨ add support for URL inputs in V2 + bump dependencies (#365)
1 parent f0cfa6e commit 3c761a0

File tree

10 files changed

+116
-89
lines changed

10 files changed

+116
-89
lines changed

.github/workflows/_test-integrations.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ jobs:
4747
WORKFLOW_ID: ${{ secrets.WORKFLOW_ID_SE_TESTS }}
4848
MINDEE_V2_API_KEY: ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }}
4949
MINDEE_V2_FINDOC_MODEL_ID: ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
50+
MINDEE_V2_SE_TESTS_BLANK_PDF_URL: ${{ secrets.MINDEE_V2_SE_TESTS_BLANK_PDF_URL }}
5051
run: npm run test-integration

package-lock.json

Lines changed: 60 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/clientV2.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
Base64Input, BufferInput, BytesInput,
3-
LocalInputSource, PathInput, StreamInput, UrlInput,
2+
Base64Input, BufferInput, BytesInput, InputSource,
3+
PathInput, StreamInput, UrlInput,
44
} from "./input";
55
import { errorHandler } from "./errors/handler";
66
import { LOG_LEVELS, logger } from "./logger";
@@ -124,7 +124,7 @@ export interface ClientOptions {
124124
* @category ClientV2
125125
*/
126126
export class ClientV2 {
127-
/** Key of the API. */
127+
/** Mindee API handler. */
128128
protected mindeeApi: MindeeApiV2;
129129

130130
/**
@@ -148,13 +148,13 @@ export class ClientV2 {
148148

149149
/**
150150
* Send the document to an asynchronous endpoint and return its ID in the queue.
151-
* @param inputSource file to parse.
151+
* @param inputSource file or URL to parse.
152152
* @param params parameters relating to prediction options.
153153
* @category Asynchronous
154154
* @returns a `Promise` containing the job (queue) corresponding to a document.
155155
*/
156156
async enqueueInference(
157-
inputSource: LocalInputSource,
157+
inputSource: InputSource,
158158
params: InferenceParameters
159159
): Promise<JobResponse> {
160160
if (inputSource === undefined) {
@@ -240,7 +240,7 @@ export class ClientV2 {
240240
* @returns a `Promise` containing parsing results.
241241
*/
242242
async enqueueAndGetInference(
243-
inputDoc: LocalInputSource,
243+
inputDoc: InputSource,
244244
params: InferenceParameters
245245
): Promise<InferenceResponse> {
246246
const validatedAsyncParams = this.#setAsyncParams(params.pollingOptions);

src/http/mindeeApiV2.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { InferenceResponse, JobResponse } from "../parsing/v2";
44
import FormData from "form-data";
55
import { RequestOptions } from "https";
66
import { BaseEndpoint, EndpointResponse } from "./baseEndpoint";
7-
import { LocalInputSource } from "../input";
7+
import { InputSource, LocalInputSource, UrlInput } from "../input";
88
import { MindeeApiV2Error, MindeeHttpErrorV2 } from "../errors/mindeeError";
99
import { logger } from "../logger";
1010

@@ -17,18 +17,18 @@ export class MindeeApiV2 {
1717

1818
/**
1919
* Sends a file to the inference queue.
20-
* @param inputDoc Local file loaded as an input.
20+
* @param inputSource Local file loaded as an input.
2121
* @param params {InferenceParameters} parameters relating to the enqueueing options.
2222
* @category V2
2323
* @throws Error if the server's response contains one.
2424
* @returns a `Promise` containing a job response.
2525
*/
26-
async reqPostInferenceEnqueue(inputDoc: LocalInputSource, params: InferenceParameters): Promise<JobResponse> {
27-
await inputDoc.init();
26+
async reqPostInferenceEnqueue(inputSource: InputSource, params: InferenceParameters): Promise<JobResponse> {
27+
await inputSource.init();
2828
if (params.modelId === undefined || params.modelId === null || params.modelId === "") {
2929
throw new Error("Model ID must be provided");
3030
}
31-
const result: EndpointResponse = await this.#documentEnqueuePost(inputDoc, params);
31+
const result: EndpointResponse = await this.#documentEnqueuePost(inputSource, params);
3232
if (result.data.error?.code !== undefined) {
3333
throw new MindeeHttpErrorV2(
3434
result.data.error.code,
@@ -86,10 +86,10 @@ export class MindeeApiV2 {
8686
/**
8787
* Sends a document to the inference queue.
8888
*
89-
* @param inputDoc Local file loaded as an input.
89+
* @param inputSource Local or remote file as an input.
9090
* @param params {InferenceParameters} parameters relating to the enqueueing options.
9191
*/
92-
#documentEnqueuePost(inputDoc: LocalInputSource, params: InferenceParameters): Promise<EndpointResponse> {
92+
#documentEnqueuePost(inputSource: InputSource, params: InferenceParameters): Promise<EndpointResponse> {
9393
const form = new FormData();
9494

9595
form.append("model_id", params.modelId);
@@ -99,9 +99,13 @@ export class MindeeApiV2 {
9999
if (params.webhookIds && params.webhookIds.length > 0) {
100100
form.append("webhook_ids", params.webhookIds.join(","));
101101
}
102-
form.append("file", inputDoc.fileObject, {
103-
filename: inputDoc.filename,
104-
});
102+
if (inputSource instanceof LocalInputSource) {
103+
form.append("file", inputSource.fileObject, {
104+
filename: inputSource.filename,
105+
});
106+
} else {
107+
form.append("url", (inputSource as UrlInput).url);
108+
}
105109
const path = "/v2/inferences/enqueue";
106110
const headers = { ...this.settings.baseHeaders, ...form.getHeaders() };
107111
const options: RequestOptions = {

src/input/sources/urlInput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { IncomingMessage } from "http";
88
import { BytesInput } from "./bytesInput";
99

1010
export class UrlInput extends InputSource {
11-
private readonly url: string;
11+
public readonly url: string;
1212

1313
constructor({ url }: { url: string }) {
1414
super();

src/parsing/v2/field/baseField.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import { FieldConfidence } from "./fieldConfidence";
2-
import { FieldLocation } from "./fieldLocation";
32
import { StringDict } from "../../common";
43

54
export abstract class BaseField {
65
protected _indentLevel: number;
7-
public locations: Array<FieldLocation> | undefined;
86
public confidence: FieldConfidence | undefined;
97

108
protected constructor(rawResponse: StringDict, indentLevel = 0) {
119
this._indentLevel = indentLevel;
12-
if (rawResponse["locations"]) {
13-
this.locations = rawResponse["locations"].map((location: StringDict | undefined) => {
14-
return location ? new FieldLocation(location) : "";
15-
});
16-
}
1710
if (rawResponse["confidence"] !== undefined) {
1811
this.confidence = rawResponse["confidence"] as FieldConfidence;
1912
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { BaseField } from "./baseField";
2+
import { FieldLocation } from "./fieldLocation";
3+
import { StringDict } from "../../common";
4+
5+
6+
export class DynamicField extends BaseField {
7+
public locations: Array<FieldLocation> | undefined;
8+
9+
constructor(rawResponse: StringDict, indentLevel = 0) {
10+
super(rawResponse, indentLevel);
11+
if (rawResponse["locations"]) {
12+
this.locations = rawResponse["locations"].map((location: StringDict | undefined) => {
13+
return location ? new FieldLocation(location) : "";
14+
});
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)