http.etag.formatETag
v0.1.0 latestFormat an ETag object to its RFC 9110 §8.8.3 HTTP header string representation.
Format an ETag object to its RFC 9110 §8.8.3 HTTP header string representation.
Signature
function formatETag(tag: ETag): string
Type Definitions:
ETag— interface
Problem
Serializing ETag objects back to HTTP header format requires correct quoting and W/ prefix handling. Ad-hoc string concatenation risks malformed headers.
How It Works
Produces the correct header format: strong ETags as "value", weak ETags as W/"value".
Boundaries
- Throws TypeError if tag is null/undefined or tag.value is not a string.
Replaces
Common boilerplate this function replaces:
tag.weak ? `W/"${tag.value}"` : `"${tag.value}"`
Examples
http.etag.formatETag({ weak: false, value: "abc" }); // '"abc"'
http.etag.formatETag({ weak: true, value: "abc" }); // 'W/"abc"'
Standards
Caveats
- Does not validate the opaque-tag value — any string is accepted.
FAQ
How to format an ETag for an HTTP response header?
http.etag.formatETag({ weak: false, value: 'abc' }) returns '"abc"'. Use this to set the ETag response header.
How to create a weak ETag header?
http.etag.formatETag({ weak: true, value: 'abc' }) returns 'W/"abc"'.
What happens with an invalid ETag object?
Throws TypeError for null, undefined, or objects without a string value property.
Is formatETag the inverse of parseETag?
Yes. formatETag(parseETag(header)) produces the original header for valid inputs.
Does formatETag validate the value content?
No. Any string is accepted as the opaque-tag value.