http-types
TypeScript icon, indicating that this package has built-in type declarations

0.6.1 • Public • Published

HTTP Types in TypeScript

Build Status MIT licensed npm version

Typescript library to read and write records of HTTP exchanges in the HTTP types format.

Install

$ npm install http-types

Writing HTTP exchanges

Using HttpExchangeWriter a recording of HTTP traffic can be serialised for use with any program that can handle the HTTP Types format.

const writer = new HttpExchangeWriter();
 
const request = HttpRequestBuilder.fromPath({
  timestamp: timestamp,
  method: HttpMethod.GET,
  protocol: HttpProtocol.HTTPS,
  host: "example.com",
  headers: {
    "accept-encoding": "gzip, deflate, br",
    "cache-control": ["no-cache", "no-store"]
  },
  path: "/my/path?a=b&q=1&q=2",
  body: "request string body"
});
 
const response = HttpResponseBuilder.from({
  headers: {
    "accept-encoding": "gzip, deflate, br",
    "cache-control": "no-cache"
  },
  statusCode: 404,
  body: "response string body"
});
 
writer.write({ request, response });
 
// [...] (write multiple exchanges)
 
// writer.buffer contains the exchanges in the HTTP types JSON Lines format.
console.log(writer.buffer);

A HTTP request can also be created from query parameters as an object. The below request is identical to the one created above:

const request = HttpRequestBuilder.fromPathnameAndQuery({
  timestamp: timestamp,
  method: HttpMethod.GET,
  protocol: HttpProtocol.HTTPS,
  host: "example.com",
  headers: {
    "accept-encoding": "gzip, deflate, br",
    "cache-control": ["no-cache", "no-store"]
  },
  pathname: "/my/path",
  query: {
    a: "b",
    q: ["1", "2"]
  },
  body: "request string body"
});

Reading HTTP exchanges

With HttpExchangeReader HTTP Types recordings can be read for processing:

HttpExchangeReader.fromJsonLines(writer.buffer, exchange => {
  expect(exchange.request.host).toBe("example.com");
  expect(exchange.request.query.get("a")).toEqual("b");
});

Package Sidebar

Install

npm i http-types

Weekly Downloads

1

Version

0.6.1

License

MIT

Unpacked Size

30.7 kB

Total Files

9

Last publish

Collaborators

  • fornwall
  • kimmo-meeshkan