@@ -42,15 +42,17 @@ export class DgraphClientStub {
4242 public async detectApiVersion ( ) : Promise < string > {
4343 try {
4444 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" ] ;
5246 }
5347 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+
5456 throw new Error ( "Failed to obtain alpha health." ) ;
5557 }
5658 }
@@ -293,10 +295,10 @@ export class DgraphClientStub {
293295 * @returns {Promise<Response> } Health in JSON
294296 * @memberof DgraphClientStub
295297 */
296- public async getHealth ( all : boolean = false ) : Promise < Response > {
298+ public getHealth ( all : boolean = false ) : Promise < Response > {
297299 const url = "health" + ( all ? "?all" : "" ) ;
298300
299- return await this . callAPI ( url , {
301+ return this . callAPI ( url , {
300302 method : "GET" ,
301303 } ) ;
302304 }
@@ -307,8 +309,8 @@ export class DgraphClientStub {
307309 * @returns {Promise<Response> } State in JSON
308310 * @memberof DgraphClientStub
309311 */
310- public async getState ( ) : Promise < Response > {
311- return await this . callAPI ( "state" , {
312+ public getState ( ) : Promise < Response > {
313+ return this . callAPI ( "state" , {
312314 method : "GET" ,
313315 } ) ;
314316 }
@@ -347,26 +349,35 @@ export class DgraphClientStub {
347349 }
348350
349351 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+ }
355357
356- const response = await fetch ( url , config ) ;
358+ const response = await fetch ( url , config ) ;
357359
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+ }
361373
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
364375
365- if ( errors !== undefined ) {
366- throw new APIError ( url , errors ) ;
367- }
376+ if ( errors !== undefined ) {
377+ throw new APIError ( url , errors ) ;
378+ }
368379
369- return json ;
380+ return json ;
370381 }
371382
372383 private getURL ( path : string ) : string {
0 commit comments