@adviser/cement - v0.0.0
    Preparing search index...

    Class HttpHeader

    HTTP header container with multi-value support and type-safe operations.

    HttpHeader provides a comprehensive API for managing HTTP headers with support for multiple values per header name. All header names are normalized to lowercase for case-insensitive comparison. Supports conversion to/from various formats including native Headers, plain objects, and arrays.

    const headers = new HttpHeader();
    headers.Add('Content-Type', 'application/json');
    headers.Add('Accept', ['application/json', 'text/html']);

    const value = headers.Get('content-type'); // Case-insensitive
    const allAccept = headers.Values('accept'); // ['application/json', 'text/html']

    // Merge multiple header sources
    const merged = HttpHeader.from(
    { 'User-Agent': 'MyApp/1.0' },
    new Headers({ 'Authorization': 'Bearer token' })
    );
    Index

    Constructors

    Properties

    _headers: Map<string, Set<string>> = ...

    Methods

    • Converts various header formats to HttpHeader instance.

      Accepts Headers, arrays, objects, or existing HttpHeader instances. Automatically handles comma-separated values and normalizes header names.

      Parameters

      Returns HttpHeader

      HttpHeader instance

    • Creates HttpHeader by merging multiple header sources.

      Parameters

      Returns HttpHeader

      New HttpHeader with all headers merged

      const headers = HttpHeader.from(
      { 'Content-Type': 'application/json' },
      new Headers({ 'Authorization': 'Bearer token' }),
      [['X-Custom', 'value']]
      );
    • Returns Map<string, string>

    • Gets all values for a header (case-insensitive).

      Parameters

      • key: string

        Header name

      Returns string[]

      Array of all values for the header

    • Gets the first value for a header (case-insensitive).

      Parameters

      • key: string

        Header name

      Returns string | undefined

      First value or undefined if not present

    • Sets a header, replacing any existing values.

      Parameters

      • key: string

        Header name

      • valueOr: string | string[]

        Single value or array of values

      Returns HttpHeader

      This HttpHeader instance for chaining

    • Adds value(s) to a header, preserving existing values.

      Parameters

      • key: string

        Header name

      • value: string | string[] | undefined

        Single value, array of values, or undefined

      Returns HttpHeader

      This HttpHeader instance for chaining

    • Returns all headers as key-value array pairs.

      Each entry is a tuple of [headerName, values[]] where headerName is lowercase and values is an array of all values for that header. Headers with no values are excluded.

      Returns [string, string[]][]

      Array of [name, values] tuples

      const headers = new HttpHeader();
      headers.Add('Accept', ['application/json', 'text/html']);
      headers.Add('Content-Type', 'application/json');

      const items = headers.Items();
      // [
      // ['accept', ['application/json', 'text/html']],
      // ['content-type', ['application/json']]
      // ]
    • Returns all headers as sorted key-value array pairs.

      Same as Items() but sorted alphabetically by header name.

      Returns [string, string[]][]

      Array of [name, values] tuples sorted by name

      const headers = new HttpHeader();
      headers.Add('Content-Type', 'application/json');
      headers.Add('Accept', 'text/html');

      const sorted = headers.SortItems();
      // [
      // ['accept', ['text/html']],
      // ['content-type', ['application/json']]
      // ]
    • Creates a deep copy of the HttpHeader instance.

      Returns HttpHeader

      New HttpHeader with the same headers

      const original = new HttpHeader();
      original.Add('Content-Type', 'application/json');

      const copy = original.Clone();
      copy.Add('Accept', 'text/html');

      // original is unchanged
      original.Get('Accept'); // undefined
      copy.Get('Accept'); // 'text/html'
    • Converts headers to a plain object with string array values.

      Each header name maps to an array of all its values. Useful for serialization or when working with APIs that expect this format.

      Returns Record<string, string[]>

      Object with header names as keys and string arrays as values

      const headers = new HttpHeader();
      headers.Add('Accept', ['application/json', 'text/html']);
      headers.Add('Content-Type', 'application/json');

      const obj = headers.AsRecordStringStringArray();
      // {
      // 'accept': ['application/json', 'text/html'],
      // 'content-type': ['application/json']
      // }
    • Converts headers to a plain object with comma-separated string values.

      Multiple values for the same header are joined with ", ". Useful for compatibility with APIs that expect single string values per header.

      Returns Record<string, string>

      Object with header names as keys and comma-separated strings as values

      const headers = new HttpHeader();
      headers.Add('Accept', ['application/json', 'text/html']);
      headers.Add('Content-Type', 'application/json');

      const obj = headers.AsRecordStringString();
      // {
      // 'accept': 'application/json, text/html',
      // 'content-type': 'application/json'
      // }
    • Converts headers to HeadersInit format with only first value per header.

      Only the first value is used when multiple values exist for a header. Needed for Cloudflare Workers' HeadersInit type compatibility.

      Type Parameters

      • H extends HeadersInit

        HeadersInit type (for type compatibility)

      Returns H

      Object compatible with HeadersInit

      const headers = new HttpHeader();
      headers.Add('Accept', ['application/json', 'text/html']);
      headers.Add('Content-Type', 'application/json');

      const init = headers.AsHeaderInit();
      // {
      // 'accept': 'application/json', // only first value
      // 'content-type': 'application/json'
      // }
    • Converts to native Headers implementation.

      Multiple values are joined with ", " as per HTTP spec.

      Returns HeadersImpl

      HeadersImpl instance compatible with standard Headers interface

      const headers = new HttpHeader();
      headers.Add('Content-Type', 'application/json');

      const nativeHeaders = headers.AsHeaders();
      nativeHeaders.get('content-type'); // 'application/json'
    • Merges other headers into this instance (in-place mutation).

      Adds all headers from the provided sources to this instance. If headers already exist, values are added (not replaced).

      Parameters

      Returns HttpHeader

      This HttpHeader instance for chaining

      const headers = new HttpHeader();
      headers.Add('Content-Type', 'application/json');

      headers.MergeInplace(
      { 'Accept': 'text/html' },
      new Headers({ 'Authorization': 'Bearer token' })
      );

      // headers now contains all three headers
    • Merges other headers, returning a new instance.

      Creates a clone of this instance and merges the provided headers into it. The original instance remains unchanged.

      Parameters

      Returns HttpHeader

      New HttpHeader with merged headers

      const headers1 = new HttpHeader();
      headers1.Add('Content-Type', 'application/json');

      const headers2 = headers1.Merge({ 'Accept': 'text/html' });

      // headers1 is unchanged
      headers1.Get('Accept'); // undefined
      headers2.Get('Accept'); // 'text/html'