|
6 | 6 | isBrowser |
7 | 7 | } from "./util/request"; |
8 | 8 |
|
| 9 | +import { Errback } from "./util/types"; |
9 | 10 | import { Route } from "./route"; |
10 | 11 | import { byteLength } from "./util/bytelength"; |
11 | 12 | import { stringify as querystringify } from "querystring"; |
@@ -224,16 +225,19 @@ export class Connection { |
224 | 225 | return new Route(this, path, headers); |
225 | 226 | } |
226 | 227 |
|
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 | + ) { |
237 | 241 | let contentType = "text/plain"; |
238 | 242 | if (isBinary) { |
239 | 243 | contentType = "application/octet-stream"; |
@@ -265,55 +269,53 @@ export class Connection { |
265 | 269 | ); |
266 | 270 | } |
267 | 271 |
|
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; |
296 | 298 | } |
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); |
313 | 299 | } |
314 | 300 | } |
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 | + } |
317 | 318 | }); |
| 319 | + this._runQueue(); |
318 | 320 | } |
319 | 321 | } |
0 commit comments