Skip to content
This repository was archived by the owner on Sep 5, 2025. It is now read-only.

Commit 7676342

Browse files
committed
refactored CallApi to use async/await
1 parent 1af2bd4 commit 7676342

File tree

4 files changed

+58
-55
lines changed

4 files changed

+58
-55
lines changed

lib/clientStub.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -380,27 +380,30 @@ var DgraphClientStub = (function () {
380380
};
381381
DgraphClientStub.prototype.callAPI = function (path, config) {
382382
return __awaiter(this, void 0, void 0, function () {
383-
var url;
383+
var url, response, json, errors;
384384
return __generator(this, function (_a) {
385-
url = this.getURL(path);
386-
if (this.accessToken !== undefined && path !== "login") {
387-
config.headers = config.headers !== undefined ? config.headers : {};
388-
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
389-
}
390-
return [2, fetch(url, config)
391-
.then(function (response) {
385+
switch (_a.label) {
386+
case 0:
387+
url = this.getURL(path);
388+
if (this.accessToken !== undefined && path !== "login") {
389+
config.headers = config.headers !== undefined ? config.headers : {};
390+
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
391+
}
392+
return [4, fetch(url, config)];
393+
case 1:
394+
response = _a.sent();
392395
if (response.status >= 300 || response.status < 200) {
393396
throw new Error("Invalid status code = " + response.status);
394397
}
395-
return response.json();
396-
})
397-
.then(function (json) {
398-
var errors = json.errors;
398+
return [4, response.json()];
399+
case 2:
400+
json = _a.sent();
401+
errors = json.errors;
399402
if (errors !== undefined) {
400403
throw new errors_1.APIError(url, errors);
401404
}
402-
return json;
403-
})];
405+
return [2, json];
406+
}
404407
});
405408
});
406409
};

lib/zero.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,26 @@ var DgraphZero = (function () {
6868
};
6969
DgraphZero.prototype.callAPI = function (path, config) {
7070
return __awaiter(this, void 0, void 0, function () {
71-
var url;
71+
var url, response, json, errors;
7272
return __generator(this, function (_a) {
73-
url = this.getURL(path);
74-
return [2, fetch(url, config)
75-
.then(function (response) {
73+
switch (_a.label) {
74+
case 0:
75+
url = this.getURL(path);
76+
return [4, fetch(url, config)];
77+
case 1:
78+
response = _a.sent();
7679
if (response.status >= 300 || response.status < 200) {
7780
throw new Error("Invalid status code = " + response.status);
7881
}
79-
return response.json();
80-
})
81-
.then(function (json) {
82-
var errors = json.errors;
82+
return [4, response.json()];
83+
case 2:
84+
json = _a.sent();
85+
errors = json.errors;
8386
if (errors !== undefined) {
8487
throw new errors_1.APIError(url, errors);
8588
}
86-
return json;
87-
})];
89+
return [2, json];
90+
}
8891
});
8992
});
9093
};

src/clientStub.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,20 @@ export class DgraphClientStub {
364364
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
365365
}
366366

367-
return fetch(url, config) // tslint:disable-line no-unsafe-any
368-
.then((response: { status: number; json(): T }) => {
369-
if (response.status >= 300 || response.status < 200) {
370-
throw new Error(`Invalid status code = ${response.status}`);
371-
}
372-
return response.json();
373-
})
374-
.then((json: T) => {
375-
const errors = (<{ errors: APIResultError[] }><any>json).errors; // tslint:disable-line no-any
376-
if (errors !== undefined) {
377-
throw new APIError(url, errors);
378-
}
379-
380-
return json;
381-
});
367+
const response = await fetch(url, config);
368+
369+
if (response.status >= 300 || response.status < 200) {
370+
throw new Error(`Invalid status code = ${response.status}`);
371+
}
372+
373+
const json = await response.json();
374+
const errors = (<{ errors: APIResultError[] }><any>json).errors; // tslint:disable-line no-any
375+
376+
if (errors !== undefined) {
377+
throw new APIError(url, errors);
378+
}
379+
380+
return json;
382381
}
383382

384383
private getURL(path: string): string {

src/zero.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,25 @@ export class DgraphZero {
2828

2929
public async getHealth(all: boolean = false) : Promise<Response> {
3030
const url = "health" + (all? "?all" : "");
31-
return this.callAPI(url, {});
31+
return await this.callAPI(url, {});
3232
}
3333

3434
private async callAPI<T>(path: string, config: { method?: string; body?: string; headers?: { [k: string]: string } }): Promise<T> {
3535
const url = this.getURL(path);
36+
const response = await fetch(url, config);
3637

37-
return fetch(url, config) // tslint:disable-line no-unsafe-any
38-
.then((response: { status: number; json(): T }) => {
39-
if (response.status >= 300 || response.status < 200) {
40-
throw new Error(`Invalid status code = ${response.status}`);
41-
}
42-
return response.json();
43-
})
44-
.then((json: T) => {
45-
const errors = (<{ errors: APIResultError[] }><any>json).errors; // tslint:disable-line no-any
46-
if (errors !== undefined) {
47-
throw new APIError(url, errors);
48-
}
49-
50-
return json;
51-
});
38+
if (response.status >= 300 || response.status < 200) {
39+
throw new Error(`Invalid status code = ${response.status}`);
40+
}
41+
42+
const json = await response.json();
43+
const errors = (<{ errors: APIResultError[] }><any>json).errors; // tslint:disable-line no-any
44+
45+
if (errors !== undefined) {
46+
throw new APIError(url, errors);
47+
}
48+
49+
return json;
5250
}
5351

5452
private getURL(path: string): string {

0 commit comments

Comments
 (0)