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

Commit 6565516

Browse files
author
Paul Korzhyk
committed
Expose HTTP errors to the caller
1 parent 49df610 commit 6565516

File tree

12 files changed

+384
-604
lines changed

12 files changed

+384
-604
lines changed

lib/client.js

Lines changed: 51 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
"use strict";
21
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
32
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,123 +7,74 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
87
step((generator = generator.apply(thisArg, _arguments || [])).next());
98
});
109
};
11-
var __generator = (this && this.__generator) || function (thisArg, body) {
12-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14-
function verb(n) { return function (v) { return step([n, v]); }; }
15-
function step(op) {
16-
if (f) throw new TypeError("Generator is already executing.");
17-
while (_) try {
18-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19-
if (y = 0, t) op = [op[0] & 2, t.value];
20-
switch (op[0]) {
21-
case 0: case 1: t = op; break;
22-
case 4: _.label++; return { value: op[1], done: false };
23-
case 5: _.label++; y = op[1]; op = [0]; continue;
24-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
25-
default:
26-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30-
if (t[2]) _.ops.pop();
31-
_.trys.pop(); continue;
32-
}
33-
op = body.call(thisArg, _);
34-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36-
}
37-
};
38-
Object.defineProperty(exports, "__esModule", { value: true });
39-
exports.DgraphClient = void 0;
40-
var errors_1 = require("./errors");
41-
var txn_1 = require("./txn");
42-
var util_1 = require("./util");
43-
var DgraphClient = (function () {
44-
function DgraphClient() {
45-
var clients = [];
46-
for (var _i = 0; _i < arguments.length; _i++) {
47-
clients[_i] = arguments[_i];
48-
}
10+
import { ERR_NO_CLIENTS } from "./errors";
11+
import { Txn } from "./txn";
12+
import { stringifyMessage } from "./util";
13+
export class DgraphClient {
14+
constructor(...clients) {
4915
this.debugMode = false;
5016
this.queryTimeout = 600;
5117
if (clients.length === 0) {
52-
throw errors_1.ERR_NO_CLIENTS;
18+
throw ERR_NO_CLIENTS;
5319
}
5420
this.clients = clients;
5521
}
56-
DgraphClient.prototype.setQueryTimeout = function (timeout) {
22+
setQueryTimeout(timeout) {
5723
this.queryTimeout = timeout;
5824
return this;
59-
};
60-
DgraphClient.prototype.getQueryTimeout = function () {
25+
}
26+
getQueryTimeout() {
6127
return this.queryTimeout;
62-
};
63-
DgraphClient.prototype.alter = function (op) {
64-
return __awaiter(this, void 0, void 0, function () {
65-
var c;
66-
return __generator(this, function (_a) {
67-
this.debug("Alter request:\n" + util_1.stringifyMessage(op));
68-
c = this.anyClient();
69-
return [2, c.alter(op)];
70-
});
71-
});
72-
};
73-
DgraphClient.prototype.setAlphaAuthToken = function (authToken) {
74-
this.clients.forEach(function (c) {
75-
return c.setAlphaAuthToken(authToken);
28+
}
29+
alter(op) {
30+
return __awaiter(this, void 0, void 0, function* () {
31+
this.debug(`Alter request:\n${stringifyMessage(op)}`);
32+
const c = this.anyClient();
33+
return c.alter(op);
7634
});
77-
};
78-
DgraphClient.prototype.setSlashApiKey = function (apiKey) {
79-
this.clients.forEach(function (c) { return c.setSlashApiKey(apiKey); });
80-
};
81-
DgraphClient.prototype.login = function (userid, password) {
82-
return __awaiter(this, void 0, void 0, function () {
83-
var c;
84-
return __generator(this, function (_a) {
85-
this.debug("Login request:\nuserid: " + userid);
86-
c = this.anyClient();
87-
return [2, c.login(userid, password)];
88-
});
35+
}
36+
setAlphaAuthToken(authToken) {
37+
this.clients.forEach((c) => c.setAlphaAuthToken(authToken));
38+
}
39+
setSlashApiKey(apiKey) {
40+
this.clients.forEach((c) => c.setSlashApiKey(apiKey));
41+
}
42+
login(userid, password) {
43+
return __awaiter(this, void 0, void 0, function* () {
44+
this.debug(`Login request:\nuserid: ${userid}`);
45+
const c = this.anyClient();
46+
return c.login(userid, password);
8947
});
90-
};
91-
DgraphClient.prototype.logout = function () {
48+
}
49+
logout() {
9250
this.debug("Logout");
93-
this.clients.forEach(function (c) { return c.logout(); });
94-
};
95-
DgraphClient.prototype.newTxn = function (options) {
96-
return new txn_1.Txn(this, options);
97-
};
98-
DgraphClient.prototype.setDebugMode = function (mode) {
99-
if (mode === void 0) { mode = true; }
51+
this.clients.forEach((c) => c.logout());
52+
}
53+
newTxn(options) {
54+
return new Txn(this, options);
55+
}
56+
setDebugMode(mode = true) {
10057
this.debugMode = mode;
101-
};
102-
DgraphClient.prototype.fetchUiKeywords = function () {
58+
}
59+
fetchUiKeywords() {
10360
return this.anyClient().fetchUiKeywords();
104-
};
105-
DgraphClient.prototype.getHealth = function (all) {
106-
if (all === void 0) { all = true; }
107-
return __awaiter(this, void 0, void 0, function () {
108-
return __generator(this, function (_a) {
109-
return [2, this.anyClient().getHealth(all)];
110-
});
61+
}
62+
getHealth(all = true) {
63+
return __awaiter(this, void 0, void 0, function* () {
64+
return this.anyClient().getHealth(all);
11165
});
112-
};
113-
DgraphClient.prototype.getState = function () {
114-
return __awaiter(this, void 0, void 0, function () {
115-
return __generator(this, function (_a) {
116-
return [2, this.anyClient().getState()];
117-
});
66+
}
67+
getState() {
68+
return __awaiter(this, void 0, void 0, function* () {
69+
return this.anyClient().getState();
11870
});
119-
};
120-
DgraphClient.prototype.debug = function (msg) {
71+
}
72+
debug(msg) {
12173
if (this.debugMode) {
12274
console.log(msg);
12375
}
124-
};
125-
DgraphClient.prototype.anyClient = function () {
76+
}
77+
anyClient() {
12678
return this.clients[Math.floor(Math.random() * this.clients.length)];
127-
};
128-
return DgraphClient;
129-
}());
130-
exports.DgraphClient = DgraphClient;
79+
}
80+
}

0 commit comments

Comments
 (0)