1.1.4 • Published 2 years ago

@wilkins-software/http-headers v1.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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 npm.io npm.io npm.io npm.io

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
HeadersDescription
AcceptMedia type(s) that is/are acceptable
Accept-CHClient Hints headers
Accept-CH-LifetimeLifetime of Client Hints headers
Accept-EncodingAcceptable content-codings
Accept-LanguageAcceptable languages for response
Accept-PatchAcceptable patch document formats
Accept-PostAcceptable media type for POST
Access-Control-*CORS headers
AgeAge of the response in seconds
AllowValid methods for a specific resource
Alt-SvcAlternate service for a resource
AuthorizationAuthentication credentials for HTTP auth
Cache-ControlDirectives for caching mechanisms
Clear-Site-DataClearing browsing data
Content-TypeMedia type of the resource
Content-LengthLength of the response body in octets
Content-LanguageNatural language of the intended audience
Content-EncodingEncodings applied to the data
Content-DispositionHow to display the response
Content-Security-PolicySecurity policy for the client UI
CookieHTTP cookie previously sent by the server
DNTDo Not Track request
ETagEntity tag of the associated resource
ExpectExpectations for the server to fulfill
ExpiresDate/time after which response is stale
ForwardedDiscloses original information of a client connecting to a web server through an HTTP proxy
FromEmail address of the user making the request
HostHost and port of the resource being requested
If-*Conditions for HTTP request headers
Keep-AliveKeep the connection open after the current transaction finishes
Last-ModifiedDate and time the resource was last modified
LocationURL of a redirected page
OriginInitiator of the request
PragmaImplementation-specific directives
Proxy-*Request and response behavior of connection via proxy
RangeRequest only part of an entity
RefererAddress of the previous web page
Retry-AfterIf you're rate-limited, when to try again
ServerDetails of the software used by the origin server
Set-CookieAn HTTP cookie
Strict-Transport-SecurityHTTP Strict Transport Security
TETransfer encodings the user agent is willing to accept
TrailerIndicate trailer fields in chunked transfer coding
Transfer-EncodingForm of encoding used to safely transfer the entity to the user
UpgradeAsk the client to upgrade to another protocol
User-AgentUser agent string of the user agent
VaryDetermine how to match future request headers to decide whether a cached response can be used
Via

Authors

1.1.4

2 years ago

1.1.3

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

1.1.2

2 years ago

0.3.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago