Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/v2/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ export class Client {
return await this.mindeeApi.getProductResultById(product, inferenceId);
}

/**
* Retrieves the result of a previously enqueued request.
* This method is used when manually polling the server for the result URL.
*
* @param product the product to retrieve.
* @param url URL as given in the `getJob()` response.
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
* @returns a `Promise` containing the inference.
*/
async getResultByUrl<P extends typeof BaseProduct>(
product: P,
url: string
): Promise<InstanceType<P["responseClass"]>> {
logger.debug(
`Attempting to get inference from: ${url} using response type: ${product.name}`
);
return await this.mindeeApi.getProductResultByUrl(product, url);
}

/**
* Get the processing status of a previously enqueued request.
* Can be used for polling.
Expand Down Expand Up @@ -182,7 +201,7 @@ export class Client {
"The result URL is undefined. This is a server error, try again later or contact support."
);
}
return this.mindeeApi.getProductResultByUrl(product, pollResults.job.resultUrl);
return this.getResultByUrl(product, pollResults.job.resultUrl);
}
await setTimeout(
pollingOptions.delaySec * 1000,
Expand Down
22 changes: 17 additions & 5 deletions src/v2/parsing/localResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ import { BaseResponse } from "./baseResponse.js";
export class LocalResponse extends LocalResponseBase {

/**
* Deserialize the loaded local response into the requested CommonResponse-derived class.
* Deserialize the loaded local response into a product response class.
*
* Typically used when dealing with V2 webhook callbacks.
* Typically used when dealing with V2 webhook callbacks to parse the raw
* JSON payload pushed to your endpoint into a typed response object.
*
* @typeParam ResponseT - A class that extends `CommonResponse`.
* @param responseClass - The constructor of the class into which the payload should be deserialized.
* @returns An instance of `responseClass` populated with the file content.
* Pass the response class that matches the product you used when enqueuing
* the document. All product response classes are exported from the `mindee`
* package and can be used here:
*
* - `mindee.product.ExtractionResponse`
* - `mindee.product.ClassificationResponse`
* - `mindee.product.OcrResponse`
* - `mindee.product.CropResponse`
* - `mindee.product.SplitResponse`
*
* @typeParam ResponseT - A class that extends `BaseResponse`.
* @param responseClass - The constructor of the product response class into
* which the payload should be deserialized.
* @returns An instance of `responseClass` populated with the webhook payload.
* @throws MindeeError If the provided class cannot be instantiated.
*/
public async deserializeResponse<ResponseT extends BaseResponse>(
Expand Down
19 changes: 9 additions & 10 deletions tests/input/sources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import { RESOURCE_PATH, V1_PRODUCT_PATH } from "../index.js";
describe("Input Sources - load different types of input", () => {

it("should accept base64 inputs", async () => {
const b64Input = await fs.promises.readFile(
path.join(RESOURCE_PATH, "file_types/receipt.txt")
const b64String = await fs.promises.readFile(
path.join(RESOURCE_PATH, "file_types/receipt.txt"), "ascii"
);
const b64String = b64Input.toString();
// don't provide an extension to see if we can detect MIME
// type based on contents
const filename = "receipt";
Expand All @@ -49,15 +48,15 @@ describe("Input Sources - load different types of input", () => {

it("should accept JPEG files from a path", async () => {
const inputSource = new PathInput({
inputPath: path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg"),
inputPath: path.join(RESOURCE_PATH, "file_types/receipt.jpg"),
});
await inputSource.init();

const expectedResult = await fs.promises.readFile(
path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg")
path.join(RESOURCE_PATH, "file_types/receipt.jpg")
);
assert.strictEqual(inputSource.inputType, INPUT_TYPE_PATH);
assert.strictEqual(inputSource.filename, "default_sample.jpg");
assert.strictEqual(inputSource.filename, "receipt.jpg");
assert.strictEqual(inputSource.mimeType, "image/jpeg");
assert.ok(!inputSource.isPdf());
assert.strictEqual(await inputSource.getPageCount(), 1);
Expand Down Expand Up @@ -113,9 +112,9 @@ describe("Input Sources - load different types of input", () => {
});

it("should accept read streams", async () => {
const filePath = path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg");
const filePath = path.join(RESOURCE_PATH, "file_types/receipt.jpg");
const stream = fs.createReadStream(filePath);
const filename = "default_sample.jpg";
const filename = "receipt.jpg";
const inputSource = new StreamInput({
inputStream: stream,
filename: filename,
Expand Down Expand Up @@ -207,8 +206,8 @@ describe("Input Sources - load different types of input", () => {
});

it("should accept raw bytes", async () => {
const filePath = path.join(V1_PRODUCT_PATH, "expense_receipts/default_sample.jpg");
const inputBytes = await fs.promises.readFile(filePath);
const filePath = path.join(RESOURCE_PATH, "file_types/receipt.jpg");
const inputBytes = new Uint8Array(await fs.promises.readFile(filePath));
// don't provide an extension to see if we can detect MIME
// type based on contents
const filename = "receipt";
Expand Down
Loading