File tree Expand file tree Collapse file tree 4 files changed +58
-1
lines changed Expand file tree Collapse file tree 4 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
- * RegistrationResponse is a type that represents a response from a record scanning service.
2
+ * RegistrationResponse is a type that represents a response from a record scanning service's registration endpoint .
3
3
*
4
4
* @example
5
5
* const registrationResponse: RegistrationResponse = {
Original file line number Diff line number Diff line change
1
+ /**
2
+ * StatusResponse is a type that represents a response from a record scanning service's status endpoint.
3
+ *
4
+ * @example
5
+ * const statusResponse: StatusResponse = {
6
+ * synced: true,
7
+ * percentage: 100,
8
+ * }
9
+ */
10
+ export interface StatusResponse {
11
+ synced : boolean ;
12
+ percentage : number ;
13
+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import { RecordsFilter } from "./models/record-scanner/recordsFilter";
9
9
import { RecordsResponseFilter } from "./models/record-scanner/recordsResponseFilter" ;
10
10
import { RegistrationRequest } from "./models/record-scanner/registrationRequest" ;
11
11
import { RegistrationResponse } from "./models/record-scanner/registrationResponse" ;
12
+ import { StatusResponse } from "./models/record-scanner/statusResponse" ;
12
13
13
14
type RecordScannerOptions = {
14
15
url : string ;
@@ -192,6 +193,29 @@ class RecordScanner implements RecordProvider {
192
193
}
193
194
}
194
195
196
+ /**
197
+ * Check the status of a record scanner indexing job.
198
+ *
199
+ * @param {string } jobId The job id to check.
200
+ * @returns {Promise<StatusResponse> } The status of the job.
201
+ */
202
+ async checkStatus ( jobId : string ) : Promise < StatusResponse > {
203
+ try {
204
+ const response = await this . request (
205
+ new Request ( `${ this . url } /status/${ jobId } ` , {
206
+ method : "POST" ,
207
+ headers : { "Content-Type" : "application/json" } ,
208
+ body : JSON . stringify ( jobId ) ,
209
+ } ) ,
210
+ ) ;
211
+
212
+ return await response . json ( ) ;
213
+ } catch ( error ) {
214
+ console . error ( `Failed to check status of job: ${ error } ` ) ;
215
+ throw error ;
216
+ }
217
+ }
218
+
195
219
/**
196
220
* Find a record in the record scanner service.
197
221
*
Original file line number Diff line number Diff line change @@ -347,6 +347,26 @@ describe("RecordScanner", () => {
347
347
expect ( request . headers . get ( "Content-Type" ) ) . to . equal ( "application/json" ) ;
348
348
} ) ;
349
349
350
+ it ( "should return StatusResponse after successfully checking status" , async ( ) => {
351
+ recordScanner = new RecordScanner ( { url : "https://record-scanner.aleo.org" , account : defaultAccount } ) ;
352
+ const mockResponse = {
353
+ ok : true ,
354
+ status : 200 ,
355
+ text : ( ) => Promise . resolve ( JSON . stringify ( { synced : true , percentage : 100 } ) ) ,
356
+ json : ( ) => Promise . resolve ( { synced : true , percentage : 100 } ) ,
357
+ } ;
358
+ fetchStub . resolves ( mockResponse ) ;
359
+ const statusResponse = await recordScanner . checkStatus ( "test-job-id" ) ;
360
+ expect ( statusResponse ) . to . deep . equal ( { synced : true , percentage : 100 } ) ;
361
+
362
+ const request = fetchStub . firstCall . args [ 0 ] as Request ;
363
+ const body = await request . text ( ) ;
364
+ const expectedBody = JSON . stringify ( "test-job-id" ) ;
365
+ expect ( body ) . to . equal ( expectedBody ) ;
366
+ expect ( request . method ) . to . equal ( "POST" ) ;
367
+ expect ( request . headers . get ( "Content-Type" ) ) . to . equal ( "application/json" ) ;
368
+ } ) ;
369
+
350
370
it ( "should handle HTTP errors" , async ( ) => {
351
371
recordScanner = new RecordScanner ( { url : "https://record-scanner.aleo.org" , account : defaultAccount } ) ;
352
372
let mockResponse = {
You can’t perform that action at this time.
0 commit comments