BBizKit

http.etag.formatIfNoneMatch

v0.1.0 latest

Format an IfNoneMatch object to its RFC 9110 §13.1.2 HTTP header string.

httpetagformattingif-none-matchrfc9110

Format an IfNoneMatch object to its RFC 9110 §13.1.2 HTTP header string.

Signature

function formatIfNoneMatch(v: IfNoneMatch): string

Type Definitions:

Problem

Serializing If-None-Match values for HTTP requests requires correct formatting of * wildcards, single ETags, and comma-separated lists with proper quoting.

How It Works

Produces * for { any: true }, or a comma-separated list of formatted ETags for tag lists.

Boundaries

  • Throws TypeError for invalid IfNoneMatch objects.
  • { any: true } produces "*".

Replaces

Common boilerplate this function replaces:

v.any ? '*' : v.tags.map(t => formatETag(t)).join(', ')

Examples

http.etag.formatIfNoneMatch({ any: true });  // "*"
http.etag.formatIfNoneMatch({ tags: [{ weak: false, value: "a" }] });  // '"a"'

Standards

Caveats

  • formatIfNoneMatch is the inverse of parseIfNoneMatch for valid inputs.

FAQ

How to format an If-None-Match header?

formatIfNoneMatch({ tags: [{ weak: false, value: 'abc' }] }) returns '"abc"'.

How to format the * wildcard?

formatIfNoneMatch({ any: true }) returns '*'.

Is formatIfNoneMatch the inverse of parseIfNoneMatch?

Yes, for valid inputs: formatIfNoneMatch(parseIfNoneMatch(header)) produces the original header.

How to format multiple ETags?

Pass an array of tags: formatIfNoneMatch({ tags: [tag1, tag2] }) produces '"v1", "v2"'.

What happens with an invalid IfNoneMatch object?

Throws TypeError for objects that are neither { any: true } nor { tags: [...] }.