npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wilkins-software/http-headers

v1.1.4

Published

A set of classes that can be used to programatically generate HTTP headers, with convienient methods of ease of use and readability.

Downloads

8

Readme

Wilkins-Software: Http Headers

A set of classes that can be used to programatically generate HTTP headers, with convienient methods of ease of use and readability.

Badges

npm version CI size

API Reference

HttpHeaders

HttpHeaders is this Library's main export/tool. It can be instantiated with any standard response header, and can either accept a builder function or a primitive value appropriate to that header.

Builder functions are unique to each particular header, and provide methods to configure that header without having to manually interpolate strings yourself.

For example to generate the following header object...

{
    'Age': '123',
    'Cache-Control': 'max-age=3600, public, immutable',
    'Clear-Site-Data': 'cache',
}

You could use the builders as such:

const headers = new HttpHeaders({
  Age: (builder) => builder.setAge(123),
  "Cache-Control": (builder) =>
    builder
      .setImmutable(true)
      .setIsPublic(true)
      // Note, setMaxAge is passed TimeConverters for your convenience
      .setMaxAge(({ hours }) => hours(1)),
  "Clear-Site-Data": (builder) => builder.setDirective("cache", true),
});

Extending Fetch Headers

The HttpHeaders class extends the built-in fetch Headers class, which means you can pass an instance of HttpHeaders directly to the fetch function as part of the request options.

This allows you to take full advantage of the convenience methods provided by HttpHeaders while still leveraging the compatibility and functionality of the native fetch API.

Here's an example of using HttpHeaders with fetch:

import { HttpHeaders } from "@wilkins-software/http-headers";

const headers = new HttpHeaders({
  "Content-Type": (builder) => builder.setMimeType("application/json"),
  Authorization: (builder) => builder.setBearerToken("your_token_here"),
});

fetch("https://api.example.com/data", {
  method: "GET",
  headers: headers,
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error("Error:", error));

In this example, we create an instance of HttpHeaders with the desired headers and pass it directly to the fetch function, simplifying the process of setting up headers for your requests.

Currently Supported Headers

| Headers | Description | | --------------------------- | --------------------------------------------------------------------------------------------- | | Accept | Media type(s) that is/are acceptable | | Accept-CH | Client Hints headers | | Accept-CH-Lifetime | Lifetime of Client Hints headers | | Accept-Encoding | Acceptable content-codings | | Accept-Language | Acceptable languages for response | | Accept-Patch | Acceptable patch document formats | | Accept-Post | Acceptable media type for POST | | Access-Control-* | CORS headers | | Age | Age of the response in seconds | | Allow | Valid methods for a specific resource | | Alt-Svc | Alternate service for a resource | | Authorization | Authentication credentials for HTTP auth | | Cache-Control | Directives for caching mechanisms | | Clear-Site-Data | Clearing browsing data | | Content-Type | Media type of the resource | | Content-Length | Length of the response body in octets | | Content-Language | Natural language of the intended audience | | Content-Encoding | Encodings applied to the data | | Content-Disposition | How to display the response | | Content-Security-Policy | Security policy for the client UI | | Cookie | HTTP cookie previously sent by the server | | DNT | Do Not Track request | | ETag | Entity tag of the associated resource | | Expect | Expectations for the server to fulfill | | Expires | Date/time after which response is stale | | Forwarded | Discloses original information of a client connecting to a web server through an HTTP proxy | | From | Email address of the user making the request | | Host | Host and port of the resource being requested | | If-* | Conditions for HTTP request headers | | Keep-Alive | Keep the connection open after the current transaction finishes | | Last-Modified | Date and time the resource was last modified | | Location | URL of a redirected page | | Origin | Initiator of the request | | Pragma | Implementation-specific directives | | Proxy-* | Request and response behavior of connection via proxy | | Range | Request only part of an entity | | Referer | Address of the previous web page | | Retry-After | If you're rate-limited, when to try again | | Server | Details of the software used by the origin server | | Set-Cookie | An HTTP cookie | | Strict-Transport-Security | HTTP Strict Transport Security | | TE | Transfer encodings the user agent is willing to accept | | Trailer | Indicate trailer fields in chunked transfer coding | | Transfer-Encoding | Form of encoding used to safely transfer the entity to the user | | Upgrade | Ask the client to upgrade to another protocol | | User-Agent | User agent string of the user agent | | Vary | Determine how to match future request headers to decide whether a cached response can be used | | Via |

Authors