diff --git a/src/v2/client.ts b/src/v2/client.ts index b8b7f5b7..32cf0610 100644 --- a/src/v2/client.ts +++ b/src/v2/client.ts @@ -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

( + product: P, + url: string + ): Promise> { + 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. @@ -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, diff --git a/src/v2/parsing/localResponse.ts b/src/v2/parsing/localResponse.ts index 165c75e8..ae107d19 100644 --- a/src/v2/parsing/localResponse.ts +++ b/src/v2/parsing/localResponse.ts @@ -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( diff --git a/tests/input/sources.spec.ts b/tests/input/sources.spec.ts index 717ea008..26968e77 100644 --- a/tests/input/sources.spec.ts +++ b/tests/input/sources.spec.ts @@ -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"; @@ -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); @@ -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, @@ -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";