Skip to content

Commit e76e69a

Browse files
committed
Use errback internally for connection
1 parent 9b70555 commit e76e69a

File tree

3 files changed

+72
-65
lines changed

3 files changed

+72
-65
lines changed

src/connection.ts

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isBrowser
77
} from "./util/request";
88

9+
import { Errback } from "./util/types";
910
import { Route } from "./route";
1011
import { byteLength } from "./util/bytelength";
1112
import { stringify as querystringify } from "querystring";
@@ -224,16 +225,19 @@ export class Connection {
224225
return new Route(this, path, headers);
225226
}
226227

227-
request({
228-
host,
229-
method = "GET",
230-
body,
231-
expectBinary = false,
232-
isBinary = false,
233-
isJsonStream = false,
234-
headers,
235-
...urlInfo
236-
}: RequestOptions) {
228+
request(
229+
{
230+
host,
231+
method = "GET",
232+
body,
233+
expectBinary = false,
234+
isBinary = false,
235+
isJsonStream = false,
236+
headers,
237+
...urlInfo
238+
}: RequestOptions,
239+
cb: Errback<ArangojsResponse>
240+
) {
237241
let contentType = "text/plain";
238242
if (isBinary) {
239243
contentType = "application/octet-stream";
@@ -265,55 +269,53 @@ export class Connection {
265269
);
266270
}
267271

268-
return new Promise<ArangojsResponse>((resolve, reject) => {
269-
this._queue.push({
270-
host,
271-
options: {
272-
url: this._buildUrl(urlInfo),
273-
headers: { ...extraHeaders, ...headers },
274-
method,
275-
expectBinary,
276-
body
277-
},
278-
reject,
279-
resolve: (res: ArangojsResponse) => {
280-
const contentType = res.headers["content-type"];
281-
let parsedBody: any = {};
282-
if (contentType && contentType.match(MIME_JSON)) {
283-
try {
284-
if (res.body && expectBinary) {
285-
parsedBody = (res.body as Buffer).toString("utf-8");
286-
} else {
287-
parsedBody = (res.body as string) || "";
288-
}
289-
parsedBody = JSON.parse(parsedBody);
290-
} catch (e) {
291-
if (!expectBinary) {
292-
e.response = res;
293-
reject(e);
294-
return;
295-
}
272+
this._queue.push({
273+
host,
274+
options: {
275+
url: this._buildUrl(urlInfo),
276+
headers: { ...extraHeaders, ...headers },
277+
method,
278+
expectBinary,
279+
body
280+
},
281+
reject: (err: Error) => cb(err),
282+
resolve: (res: ArangojsResponse) => {
283+
const contentType = res.headers["content-type"];
284+
let parsedBody: any = {};
285+
if (contentType && contentType.match(MIME_JSON)) {
286+
try {
287+
if (res.body && expectBinary) {
288+
parsedBody = (res.body as Buffer).toString("utf-8");
289+
} else {
290+
parsedBody = (res.body as string) || "";
291+
}
292+
parsedBody = JSON.parse(parsedBody);
293+
} catch (e) {
294+
if (!expectBinary) {
295+
e.response = res;
296+
cb(e);
297+
return;
296298
}
297-
}
298-
if (
299-
parsedBody &&
300-
parsedBody.hasOwnProperty("error") &&
301-
parsedBody.hasOwnProperty("code") &&
302-
parsedBody.hasOwnProperty("errorMessage") &&
303-
parsedBody.hasOwnProperty("errorNum")
304-
) {
305-
res.body = parsedBody;
306-
reject(new ArangoError(res));
307-
} else if (res.statusCode && res.statusCode >= 400) {
308-
res.body = parsedBody;
309-
reject(new HttpError(res));
310-
} else {
311-
if (!expectBinary) res.body = parsedBody;
312-
resolve(res);
313299
}
314300
}
315-
});
316-
this._runQueue();
301+
if (
302+
parsedBody &&
303+
parsedBody.hasOwnProperty("error") &&
304+
parsedBody.hasOwnProperty("code") &&
305+
parsedBody.hasOwnProperty("errorMessage") &&
306+
parsedBody.hasOwnProperty("errorNum")
307+
) {
308+
res.body = parsedBody;
309+
cb(new ArangoError(res));
310+
} else if (res.statusCode && res.statusCode >= 400) {
311+
res.body = parsedBody;
312+
cb(new HttpError(res));
313+
} else {
314+
if (!expectBinary) res.body = parsedBody;
315+
cb(null, res);
316+
}
317+
}
317318
});
319+
this._runQueue();
318320
}
319321
}

src/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ export class Route {
3030
opts.basePath = this._path;
3131
opts.headers = { ...this._headers, ...headers };
3232
opts.method = method ? method.toUpperCase() : "GET";
33-
return this._connection.request(opts);
33+
return new Promise<ArangojsResponse>((resolve, reject) =>
34+
this._connection.request(opts, (err, result) => {
35+
if (err) reject(err);
36+
else resolve(result);
37+
})
38+
);
3439
}
3540

3641
private _request1(method: string, ...args: any[]) {

src/test/00-basics.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("Configuring the driver", () => {
5050
done();
5151
}
5252
];
53-
conn.request({ headers: {} });
53+
conn.request({ headers: {} }, () => {});
5454
});
5555
});
5656
describe("with an arangoVersion", () => {
@@ -62,7 +62,7 @@ describe("Configuring the driver", () => {
6262
done();
6363
}
6464
];
65-
conn.request({ headers: {} });
65+
conn.request({ headers: {} }, () => {});
6666
});
6767
});
6868
describe("with agentOptions", () => {
@@ -135,28 +135,28 @@ describe("Configuring the driver", () => {
135135
let agent = function() {};
136136
let conn;
137137
conn = new Connection({ agent }); // default: http
138-
conn.request({ headers: {} });
138+
conn.request({ headers: {} }, () => {});
139139
expect(options).to.have.property("agent", agent);
140140
agent = function() {};
141141
conn = new Connection({ agent, url: "https://localhost:8529" });
142-
conn.request({ headers: {} });
142+
conn.request({ headers: {} }, () => {});
143143
expect(options).to.have.property("agent", agent);
144144
agent = function() {};
145145
conn = new Connection({ agent, url: "http://localhost:8529" });
146-
conn.request({ headers: {} });
146+
conn.request({ headers: {} }, () => {});
147147
expect(options).to.have.property("agent", agent);
148148
});
149149
it("uses the request function for the protocol", () => {
150150
const agent = function() {};
151151
let conn;
152152
conn = new Connection({ agent }); // default: http
153-
conn.request({ headers: {} });
153+
conn.request({ headers: {} }, () => {});
154154
expect(protocol).to.equal("http");
155155
conn = new Connection({ agent, url: "https://localhost:8529" });
156-
conn.request({ headers: {} });
156+
conn.request({ headers: {} }, () => {});
157157
expect(protocol).to.equal("https");
158158
conn = new Connection({ agent, url: "http://localhost:8529" });
159-
conn.request({ headers: {} });
159+
conn.request({ headers: {} }, () => {});
160160
expect(protocol).to.equal("http");
161161
});
162162
});

0 commit comments

Comments
 (0)