Insomnia-Script-SDK-Docs
    Preparing search index...

    Class Header

    Represents an HTTP header with a key-value pair structure.

    This class provides methods for creating, parsing, and manipulating HTTP headers. It extends the Property class and includes additional functionality specific to headers.

    • The Header class supports parsing raw header strings into structured objects.
    • It also provides utilities for converting header objects back into string format.
    • The key property represents the header name, while the value property represents its value.
    const { Header } = require("insomnia-collection");

    const header = new Header('Content-Type: application/json');
    console.log(header.key); // 'Content-Type'
    console.log(header.value); // 'application/json'

    const parsedHeaders = Header.parse('Content-Type: application/json\nUser-Agent: MyClientLibrary/2.0\n');
    console.log(parsedHeaders); // [{ key: 'Content-Type', value: 'application/json' }, { key: 'User-Agent', value: 'MyClientLibrary/2.0' }]

    const headerString = Header.unparse(parsedHeaders);
    console.log(headerString); // 'Content-Type: application/json\nUser-Agent: MyClientLibrary/2.0'

    Hierarchy (View Summary)

    Index

    Constructors

    • Constructs a new instance of the class with the provided header definition or string.

      Parameters

      • opts: string | HeaderDefinition

        A HeaderDefinition object or a string representing a single header. If a string is provided, it will be parsed into a header object.

      • Optionalname: string

        (Optional) A string that overrides the key property of the header. If not provided, the name property from the opts object will be used.

      Returns Header

    Properties

    _parent: undefined | PropertyBase = undefined

    A reference to the parent property, if any. This allows for hierarchical relationships between properties. If no parent exists, the value will be undefined.

    description?: string

    An optional description providing additional details or context.

    disabled?: boolean

    Indicates whether the property is disabled. When set to true, the property is considered inactive or unavailable.

    id: string

    A unique identifier represented as a string.

    key: string

    Represents the name of the header.

    name?: string

    The optional name property. This can be used to specify a name or identifier.

    value: string

    Represents the value of a header.

    Methods

    • Traverses up the parent hierarchy to find an ancestor that contains the specified property. Optionally, a customizer function can be provided to determine if the ancestor should be returned.

      Parameters

      • property: string

        The name of the property to search for in the ancestors.

      • Optionalcustomizer: (ancester: PropertyBase) => boolean

        An optional function that takes an ancestor as input and returns a boolean. If provided, the traversal continues until the customizer returns a truthy value. If not provided, the traversal stops at the first ancestor that contains the property.

      Returns undefined | PropertyBase

      The first ancestor that satisfies the search criteria, or undefined if no such ancestor is found.

    • Iterates through the parent hierarchy of the current object, starting from its immediate parent. The iteration continues until the provided iterator function returns false or there are no more parents.

      Parameters

      • options: { withRoot?: boolean }

        Options to control the iteration behavior.

        • withRoot (optional): A flag to include the root object in the iteration.
      • iterator: (obj: PropertyBase) => boolean

        A callback function that is invoked for each parent object in the hierarchy.

        • The function receives a cloned instance of the parent object as its argument.
        • If the function returns false, the iteration stops.

      Returns undefined | PropertyBase | PropertyBase[]

      An array of cloned parent objects that were iterated over.

    • Updates the current header with a new key-value pair.

      Parameters

      • newHeader: { key: string; value: string }

        An object containing the new key and value for the header.

        • key: The new key for the header.
        • value: The new value for the header.

      Returns void

    • Creates a new Header instance.

      Parameters

      • Optionalinput: string | { key: string; value: string }

        An object containing key and value properties, or a string. If not provided, defaults to an object with empty key and value.

      • Optionalname: string

        An optional name for the header.

      Returns Header

      A new Header instance.

    • Parses a header string into an array of key-value pair objects.

      The input string is expected to have headers separated by newline characters. Each non-empty line is processed and converted into an object with key and value properties.

      Parameters

      • headerString: string

        The raw header string to be parsed.

      Returns { key: string; value: string }[]

      An array of objects, where each object represents a header with key and value properties.

    • Parses a single HTTP header string into an object containing the key and value.

      The input string should follow the format Key: Value, where the first colon (:) separates the header key from its value. Leading and trailing whitespace around the key and value will be trimmed.

      Parameters

      • headerStr: string

        The HTTP header string to parse.

      Returns { key: string; value: string }

      An object containing the key and value of the header.

      If the input string does not contain a colon or is otherwise invalid.

      const header = Headers.parseSingle('Content-Type: application/json');
      console.log(header); // { key: 'Content-Type', value: 'application/json' }
    • Determines if a given property key is considered a "meta" property. In the context of Insomnia, meta properties are defined as those that start with an underscore (_). The underscore character itself is also rejected as a valid meta property key.

      Parameters

      • _value: any

        The value associated with the property (currently unused in this method).

      • key: string

        The property key to evaluate.

      Returns boolean | ""

      true if the key starts with an underscore (_), otherwise false.

    • Replaces placeholders in the given content string with values from the provided variables. The placeholders are resolved using an interpolation mechanism, and the variables are merged in reverse order to determine the final context for substitution.

      Parameters

      • content: string

        The string containing placeholders to be replaced.

      • ...variables: object[]

        A list of objects containing key-value pairs for substitution. The objects are merged in reverse order to form the final context.

      Returns string

      The content string with placeholders replaced by corresponding values from the context.

      If the content parameter is not a string or if variables is not an array.

    • Replaces substitutions in the given object using the provided variables.

      This method takes an object and a list of variable objects, and replaces placeholders in the object with corresponding values from the variables. The variables are merged in reverse order, meaning the last variable in the list has the highest priority.

      Parameters

      • obj: object

        The object containing placeholders to be replaced.

      • ...variables: object[]

        A list of objects containing substitution values.

      Returns object

      A new object with substitutions replaced.

      If the first parameter is not an object or if the variables are not provided as an array.

      If an error occurs during the substitution process.

    • Converts an array of headers into a single string representation.

      Parameters

      • headers: PropertyList<Header> | { key: string; value: string }[]

        An array of header objects, each containing a key and value property, or a PropertyList of Header objects.

      • Optionalseparator: string

        An optional string used to separate the headers in the resulting string. Defaults to a newline character (\n) if not provided.

      Returns string

      A string representation of the headers, joined by the specified separator.

    • Converts a header object into a single header string in the format "key: value".

      Parameters

      • header: Header | { key: string; value: string }

        The header object to unparse. It can either be an object with key and value properties or an instance of the Header class.

      Returns string

      The header represented as a string in the format "key: value".