@@ -13,12 +13,14 @@ import {
1313 RequestOptions
1414} from "@/http/apiCore.js" ;
1515import { InputSource , LocalInputSource , UrlInput } from "@/input/index.js" ;
16- import { MindeeDeserializationError } from "@/errors/index.js" ;
16+ import { MindeeDeserializationError , MindeeError } from "@/errors/index.js" ;
1717import { MindeeHttpErrorV2 } from "./errors.js" ;
1818import { logger } from "@/logger.js" ;
1919import { BaseProduct } from "@/v2/product/baseProduct.js" ;
2020
21-
21+ /**
22+ * Mindee V2 API handler.
23+ */
2224export class MindeeApiV2 {
2325 settings : ApiSettings ;
2426
@@ -27,12 +29,11 @@ export class MindeeApiV2 {
2729 }
2830
2931 /**
30- * Sends a file to the extraction inference queue.
32+ * Sends a file to the product inference queue.
3133 * @param product product to enqueue.
3234 * @param inputSource Local file loaded as an input.
3335 * @param params {ExtractionParameters} parameters relating to the enqueueing options.
34- * @category V2
35- * @throws Error if the server's response contains one.
36+ * @throws Error if the server's response contains an error.
3637 * @returns a `Promise` containing a job response.
3738 */
3839 async enqueueProduct (
@@ -51,35 +52,48 @@ export class MindeeApiV2 {
5152 }
5253
5354 /**
54- * Requests the results of a queued document from the API.
55- * Throws an error if the server's response contains one.
56- * @param jobId The document's ID in the queue.
57- * @category Asynchronous
58- * @returns a `Promise` containing information on the queue.
55+ * Get the specified Job.
56+ * Throws an error if the server's response contains an error.
57+ * @param jobId The Job ID as returned by the enqueue request.
58+ * @returns a `Promise` containing the job response.
5959 */
6060 async getJob ( jobId : string ) : Promise < JobResponse > {
6161 const response = await this . #reqGetJob( jobId ) ;
6262 return this . #processResponse( response , JobResponse ) ;
6363 }
6464
6565 /**
66- * Requests the job of a queued document from the API.
67- * Throws an error if the server's response contains one .
66+ * Get the result of a queued document from the API.
67+ * Throws an error if the server's response contains an error .
6868 * @param product
69- * @param inferenceId The document's ID in the queue.
70- * @category Asynchronous
71- * @returns a `Promise` containing either the parsed result, or information on the queue.
69+ * @param inferenceId The inference ID for the result.
70+ * @returns a `Promise` containing the parsed result.
7271 */
73- async getProductResult < P extends typeof BaseProduct > (
72+ async getProductResultById < P extends typeof BaseProduct > (
7473 product : P ,
7574 inferenceId : string ,
7675 ) : Promise < InstanceType < P [ "responseClass" ] > > {
7776 const queueResponse : BaseHttpResponse = await this . #reqGetProductResult(
78- inferenceId , product . slug
77+ `https:// ${ this . settings . hostname } /v2/products/ ${ product . slug } /results/ ${ inferenceId } `
7978 ) ;
8079 return this . #processResponse( queueResponse , product . responseClass ) as InstanceType < P [ "responseClass" ] > ;
8180 }
8281
82+ /**
83+ * Get the result of a queued document from the API.
84+ * Throws an error if the server's response contains an error.
85+ * @param product
86+ * @param url The URL as returned by a Job's resultUrl property.
87+ * @returns a `Promise` containing the parsed result.
88+ */
89+ async getProductResultByUrl < P extends typeof BaseProduct > (
90+ product : P ,
91+ url : string ,
92+ ) : Promise < InstanceType < P [ "responseClass" ] > > {
93+ const queueResponse : BaseHttpResponse = await this . #reqGetProductResult( url ) ;
94+ return this . #processResponse( queueResponse , product . responseClass ) as InstanceType < P [ "responseClass" ] > ;
95+ }
96+
8397 #processResponse< T extends BaseResponse > (
8498 result : BaseHttpResponse ,
8599 responseClass : ResponseConstructor < T > ,
@@ -114,7 +128,6 @@ export class MindeeApiV2 {
114128
115129 /**
116130 * Sends a document to the inference queue.
117- *
118131 * @param product Product to enqueue.
119132 * @param inputSource Local or remote file as an input.
120133 * @param params {ExtractionParameters} parameters relating to the enqueueing options.
@@ -155,19 +168,18 @@ export class MindeeApiV2 {
155168
156169 /**
157170 * Make a request to GET the status of a document in the queue.
158- * @param inferenceId ID of the inference.
159- * @param slug "jobs" or "inferences"...
160- * @category Asynchronous
161- * @returns a `Promise` containing either the parsed result, or information on the queue.
171+ * @param url URL path to the result.
172+ * @returns a `Promise` containing the parsed result.
162173 */
163- async #reqGetProductResult( inferenceId : string , slug : string ) : Promise < BaseHttpResponse > {
174+ async #reqGetProductResult( url : string ) : Promise < BaseHttpResponse > {
164175 const options : RequestOptions = {
165176 method : "GET" ,
166177 headers : this . settings . baseHeaders ,
167- hostname : this . settings . hostname ,
168- path : `/v2/products/${ slug } /results/${ inferenceId } ` ,
169178 timeoutSecs : this . settings . timeoutSecs ,
170179 } ;
171- return await sendRequestAndReadResponse ( this . settings . dispatcher , options ) ;
180+ if ( ! url . startsWith ( "https://" ) ) {
181+ throw new MindeeError ( `Invalid URL: ${ url } ` ) ;
182+ }
183+ return await sendRequestAndReadResponse ( this . settings . dispatcher , options , url ) ;
172184 }
173185}
0 commit comments