Skip to content

Headers

Models HTTP headers for the unstable HTTP client and server modules.

Headers values are immutable maps keyed by lowercase header name. This module converts common header inputs into that shape, provides helpers for reading and updating header values, and redacts configured sensitive headers when values are inspected.

20 exports Added in v4.0.0 Source

Combinators

get

Added in v4.0.0 Source

Gets a header value by name safely.

Details

The lookup lowercases the provided header name and returns Option.none() when absent.

Signature

declare const get: {
  (key: string): (self: Headers) => Option<string>;
  (self: Headers, key: string): Option<string>;
};

has

Added in v4.0.0 Source

Returns true when a header with the given name is present.

Details

The lookup lowercases the provided header name.

Signature

declare const has: {
  (key: string): (self: Headers) => boolean;
  (self: Headers, key: string): boolean;
};

merge

Added in v4.0.0 Source

Returns a new Headers collection containing headers from both collections.

Details

Headers from the second collection override headers from the first collection with the same name.

Signature

declare const merge: {
  (headers: Headers): (self: Headers) => Headers;
  (self: Headers, headers: Headers): Headers;
};

redact

Added in v4.0.0 Source

Returns a plain record with selected header values wrapped in Redacted.

Details

String keys are normalized to lowercase before matching; regular expressions are tested against the stored header names.

Signature

declare const redact: {
  (key: string | RegExp | readonly Array<string | RegExp>): (self: Headers) => Record<string, string | Redacted.Redacted>;
  (self: Headers, key: string | RegExp | readonly Array<string | RegExp>): Record<string, string | Redacted.Redacted>;
}

remove

Added in v4.0.0 Source

Returns a new Headers collection with the named header removed.

Details

The provided header name is normalized to lowercase before removal.

Signature

declare const remove: {
  (key: string): (self: Headers) => Headers;
  (self: Headers, key: string): Headers;
};

removeMany

Added in v4.0.0 Source

Returns a new Headers collection with each named header removed.

Details

Each provided header name is normalized to lowercase before removal.

Signature

declare const removeMany: {
  (keys: Iterable<string>): (self: Headers) => Headers;
  (self: Headers, keys: Iterable<string>): Headers;
};

set

Added in v4.0.0 Source

Returns a new Headers collection with the given header set.

Details

The header name is normalized to lowercase.

Signature

declare const set: {
  (key: string, value: string): (self: Headers) => Headers;
  (self: Headers, key: string, value: string): Headers;
};

setAll

Added in v4.0.0 Source

Returns a new Headers collection with all provided headers set.

Details

Input headers are normalized with fromInput and override existing headers with the same lowercase name.

Signature

declare const setAll: {
  (headers: Input): (self: Headers) => Headers;
  (self: Headers, headers: Input): Headers;
};

Constructors

empty

Added in v4.0.0 Source

An empty Headers collection.

Signature

declare const empty: Headers;

fromInput

Added in v4.0.0 Source

Creates Headers from a record or iterable of header entries.

Details

Header names are normalized to lowercase. Array values in record input are joined with ", ", and undefined values are omitted.

Signature

declare const fromInput: (input?: Input) => Headers;

Treats an existing record as Headers unsafely.

Gotchas

This mutates the record's prototype and does not normalize header names; callers must provide the expected lowercase keys.

Signature

declare function fromRecordUnsafe(input: Record.ReadonlyRecord<string, string>): Headers;

Guards

isHeaders

Added in v4.0.0 Source

Returns true if the provided value is a Headers value.

Signature

declare function isHeaders(u: unknown): u is Headers;

Instances

Equivalence

Added in v4.0.0 Source

Provides an Equivalence instance that compares Headers by header names and string values.

Signature

declare const Equivalence: Equ.Equivalence<Headers>;

Models

Headers interface

Added in v4.0.0 Source

Represents an immutable HTTP header collection keyed by lowercase header name.

Details

Headers values also support redaction through the Redactable protocol.

Signature

interface Headers extends Redactable {
  [key: string]: string;
  readonly [TypeId]: typeof TypeId;
}

Input type

Added in v4.0.0 Source

Input accepted when constructing headers.

Details

Records may contain string values, string arrays, or undefined; arrays are joined with ", ", and undefined values are omitted.

Signature

type Input =
  | Record.ReadonlyRecord<string, string | ReadonlyArray<string> | undefined>
  | Iterable<readonly [string, string]>;

Schemas

Schema for Headers values encoded as records of string header values.

Details

Decoding normalizes header names through fromInput; encoding returns a plain record.

Signature

declare const HeadersSchema: HeadersSchema;

HeadersSchema interface

Added in v4.0.0 Source

Schema interface for Headers values encoded as records of string header values.

Signature

interface HeadersSchema extends declare<
  Headers,
  {
    [x: string]: string;
  }
> {
  constructor(_: never);
}

Services

Context reference listing header names or patterns that should be redacted when Headers are inspected or rendered.

Details

Defaults include authorization, cookie, set-cookie, and x-api-key.

Signature

declare const CurrentRedactedNames: Reference<readonly Array<string | RegExp>>

Type IDs

TypeId

Added in v4.0.0 Source

Runtime type identifier for Headers values.

Signature

declare const TypeId: unique symbol;

TypeId type

Added in v4.0.0 Source

Type of the unique symbol used to brand Headers values.

Signature

type TypeId = typeof TypeId;