Skip to content

Commit 858b0d1

Browse files
committed
Fixed legacy response
1 parent 76a0e09 commit 858b0d1

File tree

2 files changed

+61
-58
lines changed

2 files changed

+61
-58
lines changed

lib/clientStub.js

+24-32
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,16 @@ var DgraphClientStub = (function () {
6060
return [4, this.getHealth()];
6161
case 1:
6262
response = _a.sent();
63-
if (response.data === "OK") {
64-
this.legacyApi = true;
65-
return [2, "1.0.x"];
66-
}
67-
else {
68-
return [2, response.data["version"]];
69-
}
70-
return [3, 3];
63+
return [2, response.data["version"]];
7164
case 2:
7265
err_1 = _a.sent();
73-
throw new Error("Invalid status code");
66+
if (err_1.name === "FetchError") {
67+
if (err_1.response.status === 200) {
68+
this.legacyApi = true;
69+
return [2, "1.0.x"];
70+
}
71+
}
72+
throw new Error("Failed to obtain alpha health.");
7473
case 3: return [2];
7574
}
7675
});
@@ -303,30 +302,14 @@ var DgraphClientStub = (function () {
303302
};
304303
DgraphClientStub.prototype.getHealth = function (all) {
305304
if (all === void 0) { all = false; }
306-
return __awaiter(this, void 0, void 0, function () {
307-
var url;
308-
return __generator(this, function (_a) {
309-
switch (_a.label) {
310-
case 0:
311-
url = "health" + (all ? "?all" : "");
312-
return [4, this.callAPI(url, {
313-
method: "GET",
314-
})];
315-
case 1: return [2, _a.sent()];
316-
}
317-
});
305+
var url = "health" + (all ? "?all" : "");
306+
return this.callAPI(url, {
307+
method: "GET",
318308
});
319309
};
320310
DgraphClientStub.prototype.getState = function () {
321-
return __awaiter(this, void 0, void 0, function () {
322-
return __generator(this, function (_a) {
323-
switch (_a.label) {
324-
case 0: return [4, this.callAPI("state", {
325-
method: "GET",
326-
})];
327-
case 1: return [2, _a.sent()];
328-
}
329-
});
311+
return this.callAPI("state", {
312+
method: "GET",
330313
});
331314
};
332315
DgraphClientStub.prototype.setAutoRefresh = function (val) {
@@ -354,7 +337,7 @@ var DgraphClientStub = (function () {
354337
};
355338
DgraphClientStub.prototype.callAPI = function (path, config) {
356339
return __awaiter(this, void 0, void 0, function () {
357-
var url, response, json, errors;
340+
var url, response, json, err_2, errors;
358341
return __generator(this, function (_a) {
359342
switch (_a.label) {
360343
case 0:
@@ -369,9 +352,18 @@ var DgraphClientStub = (function () {
369352
if (response.status >= 300 || response.status < 200) {
370353
throw new Error("Invalid status code = " + response.status);
371354
}
372-
return [4, response.json()];
355+
_a.label = 2;
373356
case 2:
357+
_a.trys.push([2, 4, , 5]);
358+
return [4, response.json()];
359+
case 3:
374360
json = _a.sent();
361+
return [3, 5];
362+
case 4:
363+
err_2 = _a.sent();
364+
err_2.response = response;
365+
throw err_2;
366+
case 5:
375367
errors = json.errors;
376368
if (errors !== undefined) {
377369
throw new errors_1.APIError(url, errors);

src/clientStub.ts

+37-26
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)