Skip to content
jshttpPublic

About

Manipulate the HTTP Vary header

Topics

Resources

Code of conduct

Security policy

Stars

65 stars

Watchers

12 watching

Forks

Repository files navigation

vary

NPM Version NPM Downloads Node.js Version Build Status Coverage Status

Manipulate the HTTP Vary header.

Installation

npm install vary

API

import { vary, append, format, parse } from "vary";

vary(res, field)

Adds the given header field to the Vary response header of res. This can be a string of a single field, a string of a valid Vary header, or an array of multiple fields.

This will append the header if not already listed, otherwise leaves it listed in the current location.

// Append "Origin" to the Vary header of the response
vary(res, "Origin");

append(header, field)

Returns the updated Vary header string after adding field to header. Pass a single field name, a comma-separated string of field names, or an array of field names.

Field names are compared case-insensitively. Each new name is appended and existing names stay in place. If either argument contains a wildcard *, the result is "*". Invalid field names in field throw a TypeError.

// Get header string appending "Origin" to "Accept, User-Agent"
append("Accept, User-Agent", "Origin");

parse(header)

Splits a Vary header into an array of strings.

parse("Accept-Encoding, Origin"); // ["Accept-Encoding", "Origin"]

format(fields)

Validates and joins field names with , , preserving all fields as-is. Invalid field names throw a TypeError.

format(["Accept-Encoding", "Origin", "origin"]); // "Accept-Encoding, Origin, origin"

Examples

Updating the Vary header when content is based on it

// In a request handler that serves content based on the User-Agent header
vary(res, "User-Agent");

License

MIT

About

Manipulate the HTTP Vary header

Topics

Resources

Code of conduct

Security policy

Stars

65 stars

Watchers

12 watching

Forks

Releases

Packages

Used by

Contributors

Languages