@@ -42,15 +42,17 @@ export class DgraphClientStub {
42
42
public async detectApiVersion ( ) : Promise < string > {
43
43
try {
44
44
const response = await this . getHealth ( ) ;
45
-
46
- if ( response . data === "OK" ) {
47
- this . legacyApi = true ;
48
- return "1.0.x"
49
- } else {
50
- return response . data [ "version" ] ;
51
- }
45
+ return response . data [ "version" ] ;
52
46
}
53
47
catch ( err ) {
48
+ // Check if the the response is OK, but not JSON. If it is, enable the legacy api
49
+ if ( err . name === "FetchError" ) {
50
+ if ( err . response . status === 200 ) {
51
+ this . legacyApi = true ;
52
+ return "1.0.x" ;
53
+ }
54
+ }
55
+
54
56
throw new Error ( "Failed to obtain alpha health." ) ;
55
57
}
56
58
}
@@ -293,10 +295,10 @@ export class DgraphClientStub {
293
295
* @returns {Promise<Response> } Health in JSON
294
296
* @memberof DgraphClientStub
295
297
*/
296
- public async getHealth ( all : boolean = false ) : Promise < Response > {
298
+ public getHealth ( all : boolean = false ) : Promise < Response > {
297
299
const url = "health" + ( all ? "?all" : "" ) ;
298
300
299
- return await this . callAPI ( url , {
301
+ return this . callAPI ( url , {
300
302
method : "GET" ,
301
303
} ) ;
302
304
}
@@ -307,8 +309,8 @@ export class DgraphClientStub {
307
309
* @returns {Promise<Response> } State in JSON
308
310
* @memberof DgraphClientStub
309
311
*/
310
- public async getState ( ) : Promise < Response > {
311
- return await this . callAPI ( "state" , {
312
+ public getState ( ) : Promise < Response > {
313
+ return this . callAPI ( "state" , {
312
314
method : "GET" ,
313
315
} ) ;
314
316
}
@@ -347,26 +349,35 @@ export class DgraphClientStub {
347
349
}
348
350
349
351
private async callAPI < T > ( path : string , config : { method ?: string ; body ?: string ; headers ?: { [ k : string ] : string } } ) : Promise < T > {
350
- const url = this . getURL ( path ) ;
351
- if ( this . accessToken !== undefined && path !== "login" ) {
352
- config . headers = config . headers !== undefined ? config . headers : { } ;
353
- config . headers [ "X-Dgraph-AccessToken" ] = this . accessToken ;
354
- }
352
+ const url = this . getURL ( path ) ;
353
+ if ( this . accessToken !== undefined && path !== "login" ) {
354
+ config . headers = config . headers !== undefined ? config . headers : { } ;
355
+ config . headers [ "X-Dgraph-AccessToken" ] = this . accessToken ;
356
+ }
355
357
356
- const response = await fetch ( url , config ) ;
358
+ const response = await fetch ( url , config ) ;
357
359
358
- if ( response . status >= 300 || response . status < 200 ) {
359
- throw new Error ( `Invalid status code = ${ response . status } ` ) ;
360
- }
360
+ if ( response . status >= 300 || response . status < 200 ) {
361
+ throw new Error ( `Invalid status code = ${ response . status } ` ) ;
362
+ }
363
+
364
+ // Include response in err for legacy purposes
365
+ let json ;
366
+ try {
367
+ json = await response . json ( ) ;
368
+ }
369
+ catch ( err ) {
370
+ err . response = response ;
371
+ throw err ;
372
+ }
361
373
362
- const json = await response . json ( ) ;
363
- const errors = ( < { errors : APIResultError [ ] } > < any > json ) . errors ; // tslint:disable-line no-any
374
+ const errors = ( < { errors : APIResultError [ ] } > < any > json ) . errors ; // tslint:disable-line no-any
364
375
365
- if ( errors !== undefined ) {
366
- throw new APIError ( url , errors ) ;
367
- }
376
+ if ( errors !== undefined ) {
377
+ throw new APIError ( url , errors ) ;
378
+ }
368
379
369
- return json ;
380
+ return json ;
370
381
}
371
382
372
383
private getURL ( path : string ) : string {
0 commit comments