@hqoss/http-client
Advanced tools
Comparing version 0.1.11-0 to 0.1.12-0
/// <reference types="node" /> | ||
import { IncomingMessage, RequestOptions } from "http"; | ||
import { Consumable, RequestInterceptor } from "./types"; | ||
import { Consumable, RequestInterceptor, TransformedResponse } from "./types"; | ||
export declare class HttpClient { | ||
@@ -9,3 +9,3 @@ private readonly baseReqOpts; | ||
constructor(baseUrl: string, baseReqOpts?: RequestOptions); | ||
get: (pathOrUrl: string | URL, reqOpts?: RequestOptions | undefined) => Promise<IncomingMessage>; | ||
get: (pathOrUrl: string | URL, reqOpts?: RequestOptions | undefined) => Promise<TransformedResponse<Buffer>>; | ||
delete: (pathOrUrl: string | URL, reqOpts?: RequestOptions | undefined) => Promise<IncomingMessage>; | ||
@@ -12,0 +12,0 @@ post: (pathOrUrl: string | URL, body: Consumable, reqOpts?: RequestOptions | undefined) => Promise<IncomingMessage>; |
@@ -5,24 +5,59 @@ "use strict"; | ||
const http_1 = require("http"); | ||
const events_1 = require("events"); | ||
const stream_1 = require("stream"); | ||
const types_1 = require("./types"); | ||
const transform_1 = require("./transform"); | ||
class HttpClient { | ||
constructor(baseUrl, baseReqOpts) { | ||
this.get = (pathOrUrl, reqOpts) => { | ||
const resolver = new events_1.EventEmitter(); | ||
const url = this.buildUrl(pathOrUrl); | ||
const opts = this.combineOpts(types_1.Method.Get, reqOpts); | ||
const req = http_1.request(url, opts); | ||
// Successful request | ||
req.once("response", (response) => { | ||
// Error reading response | ||
// response.once("error", error => { | ||
// resolver.emit("reject", error) | ||
// req.removeAllListeners() | ||
// response.removeAllListeners() | ||
// }) | ||
// Premature connection close after response has been received | ||
response.once("aborted", () => { | ||
resolver.emit("reject", new Error("premature connection close after the response has been received")); | ||
req.removeAllListeners(); | ||
response.removeAllListeners(); | ||
}); | ||
// const chunks: Array<Buffer> = [] | ||
// response.once("data", chunk => { | ||
// chunks.push(chunk) | ||
// }) | ||
// response.on("end", () => { | ||
// resolver.emit("resolve", Buffer.concat(chunks)) | ||
// req.removeAllListeners() | ||
// response.removeAllListeners() | ||
// }) | ||
transform_1.toBuffer(response) | ||
.then((tranformedResponse) => { | ||
resolver.emit("resolve", tranformedResponse); | ||
req.removeAllListeners(); | ||
response.removeAllListeners(); | ||
}) | ||
.catch((error) => { | ||
resolver.emit("reject", error); | ||
req.removeAllListeners(); | ||
response.removeAllListeners(); | ||
}); | ||
}); | ||
req.once("error", (error) => { | ||
resolver.emit("reject", error); | ||
req.removeAllListeners(); | ||
}); | ||
req.end(); | ||
if (this.willSendRequest) { | ||
this.willSendRequest(url, req); | ||
} | ||
return new Promise((resolve, reject) => req | ||
.once("error", (error) => { | ||
// @ts-ignore | ||
if (error.code === "ECONNRESET") { | ||
// Retry | ||
return this.get(url, opts).then(resolve).catch(reject); | ||
} | ||
return reject(error); | ||
}) | ||
.once("response", resolve) | ||
.end()); | ||
return new Promise((resolve, reject) => { | ||
resolver.once("resolve", resolve).once("reject", reject); | ||
}); | ||
}; | ||
@@ -29,0 +64,0 @@ this.delete = (pathOrUrl, reqOpts) => { |
@@ -32,17 +32,15 @@ "use strict"; | ||
const res = await httpClient.get("/200"); | ||
const { headers, statusCode, statusMessage } = res; | ||
const resBuffer = await readableToBuffer(res); | ||
const { headers, statusCode, statusMessage, data } = res; | ||
assert_1.default.equal(headers["x-method-ack"], "get"); | ||
assert_1.default.equal(statusCode, 200); | ||
assert_1.default.equal(statusMessage, "OK"); | ||
assert_1.default.deepStrictEqual(resBuffer, Buffer.from("hello")); | ||
assert_1.default.deepStrictEqual(data, Buffer.from("hello")); | ||
}); | ||
tests.set("performs GET request, gets back 204", async () => { | ||
const res = await httpClient.get("/204"); | ||
const { headers, statusCode, statusMessage } = res; | ||
const resBuffer = await readableToBuffer(res); | ||
const { headers, statusCode, statusMessage, data } = res; | ||
assert_1.default.equal(headers["x-method-ack"], "get"); | ||
assert_1.default.equal(statusCode, 204); | ||
assert_1.default.equal(statusMessage, "No Content"); | ||
assert_1.default.deepStrictEqual(resBuffer, Buffer.alloc(0)); | ||
assert_1.default.deepStrictEqual(data, Buffer.alloc(0)); | ||
}); | ||
@@ -49,0 +47,0 @@ tests.set("performs DELETE request, gets back 202", async () => { |
import { Agent, IncomingMessage, RequestOptions, request } from "http"; | ||
import { EventEmitter } from "events"; | ||
import { Readable } from "stream"; | ||
import { Consumable, Method, RequestInterceptor } from "./types"; | ||
import { | ||
Consumable, | ||
Method, | ||
RequestInterceptor, | ||
TransformedResponse, | ||
} from "./types"; | ||
import { toBuffer } from "./transform"; | ||
@@ -34,3 +41,5 @@ export class HttpClient { | ||
reqOpts?: RequestOptions, | ||
): Promise<IncomingMessage> => { | ||
): Promise<TransformedResponse<Buffer>> => { | ||
const resolver = new EventEmitter(); | ||
const url = this.buildUrl(pathOrUrl); | ||
@@ -41,2 +50,60 @@ const opts = this.combineOpts(Method.Get, reqOpts); | ||
// Successful request | ||
req.once("response", (response) => { | ||
// Error reading response | ||
// response.once("error", error => { | ||
// resolver.emit("reject", error) | ||
// req.removeAllListeners() | ||
// response.removeAllListeners() | ||
// }) | ||
// Premature connection close after response has been received | ||
response.once("aborted", () => { | ||
resolver.emit( | ||
"reject", | ||
new Error( | ||
"premature connection close after the response has been received", | ||
), | ||
); | ||
req.removeAllListeners(); | ||
response.removeAllListeners(); | ||
}); | ||
// const chunks: Array<Buffer> = [] | ||
// response.once("data", chunk => { | ||
// chunks.push(chunk) | ||
// }) | ||
// response.on("end", () => { | ||
// resolver.emit("resolve", Buffer.concat(chunks)) | ||
// req.removeAllListeners() | ||
// response.removeAllListeners() | ||
// }) | ||
toBuffer(response) | ||
.then((tranformedResponse) => { | ||
resolver.emit("resolve", tranformedResponse); | ||
req.removeAllListeners(); | ||
response.removeAllListeners(); | ||
}) | ||
.catch((error) => { | ||
resolver.emit("reject", error); | ||
req.removeAllListeners(); | ||
response.removeAllListeners(); | ||
}); | ||
}); | ||
req.once("error", (error) => { | ||
resolver.emit("reject", error); | ||
req.removeAllListeners(); | ||
}); | ||
req.end(); | ||
if (this.willSendRequest) { | ||
@@ -46,16 +113,5 @@ this.willSendRequest(url, req); | ||
return new Promise((resolve, reject) => | ||
req | ||
.once("error", (error) => { | ||
// @ts-ignore | ||
if (error.code === "ECONNRESET") { | ||
// Retry | ||
return this.get(url, opts).then(resolve).catch(reject); | ||
} | ||
return reject(error); | ||
}) | ||
.once("response", resolve) | ||
.end(), | ||
); | ||
return new Promise((resolve, reject) => { | ||
resolver.once("resolve", resolve).once("reject", reject); | ||
}); | ||
}; | ||
@@ -62,0 +118,0 @@ |
{ | ||
"name": "@hqoss/http-client", | ||
"version": "0.1.11-0", | ||
"version": "0.1.12-0", | ||
"description": "A light-weight, performant, composable blueprint for writing consistent and re-usable Node.js HTTP clients", | ||
@@ -5,0 +5,0 @@ "main": "./dist/lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166269
1003