2.0.2 • Published 3 years ago

@jcbhmr/http-serializer v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

HTTPSerializer

šŸ“œ Convert Request/Response objects to raw HTTP/1.1 text

Code screenshot

It's like XMLSerializer, but for fetch()-related things

Works on Request and Response. Useful to debug, display, or otherwise inspect HTTP requests/responses. Yeah, it's async, but that can't be helped; the .text() functions return Promise futures, so we have to wait for them.

Installation

npm jsDelivr

You can install this using NPM, or by directly importing it from an NPM CDN like ESM.sh or jsDelivr

npm install @jcbhmr/http-serializer
import HTTPSerializer from "@jcbhmr/http-serializer";
import HTTPSerializer from "https://esm.run/@jcbhmr/http-serializer";

Usage

Node.js Deno HTML5

This module works on any compliant Request and Response objects. If you are using Node.js, you'll need to polyfill the global fetch() function if you are below Node.js v18 (which added a global fetch()). You can do this with a package like undici or node-fetch by exposing their exports to the global scope.

Example serializing an HTTP Request

import HTTPSerializer from "@jcbhmr/http-serializer";

const request = new Request("https://httpbin.org/uuid");
const http = await new HTTPSerializer().serializeToString(request);
console.log(http);
GET /uuid HTTP/1.1
host: httpbin.org

Another example showing an HTTP Response

import HTTPSerializer from "@jcbhmr/http-serializer";

const response = await fetch("https://httpbin.org/uuid");
const http = await new HTTPSerializer().serializeToString(response);
console.log(http);
HTTP/1.1 200
content-length: 53
content-type: application/json

{
  "uuid": "581d291d-6280-4228-a004-5f1d06732434"
}

šŸ“ If you use a non-HTTP scheme it will still work, but there may not be a valid Host header making it an invalid HTTP request

āš ļø I highly recommend Node.js v18. Other Node.js versions might work, but they aren't official.

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago