HttpConfiguration: {
    applyConfiguration: ((url, options?) => RequestInit | undefined);
    clear: (() => void);
    setAuth: ((urlPrefix, value) => void);
    setHeader: ((urlPrefix, name, value) => void);
}

Contains configuration for HTTP requests.

Configuration is based on URL prefixes: each configuration entry applies to an URL prefix and will apply to any URL that matches this prefix. Longer prefixes have more precedence over shorter ones, so that you can cascade configurations. For example, you can have a general configuration for the example.com domain, then more specific configuration entries for sub-paths in the same domain.

Note: URL prefixes must be valid absolute URLs (including scheme): http://example.com/foo is a valid prefix, but example.com/foo is not.

Note: If you plan to use the same configuration for different schemes (e.g http and https, you must register the configuration twice, one for each scheme).

Important: this module do not automatically process outgoing HTTP requests. It is not a service worker or a middleware. The Fetcher module automatically processes the requests by querying configuration from this module, but if you can't or don't want to use Fetcher, then you have to patch the request yourself (see the example below).

Type declaration

  • applyConfiguration: ((url, options?) => RequestInit | undefined)
      • (url, options?): RequestInit | undefined
      • Update the request options with stored configuration applicable to this URL.

        Parameters

        • url: string

          The URL.

        • Optional options: RequestInit

          The request options.

        Returns RequestInit | undefined

        The updated options, if any. If no options object is passed, and no configuration applies to this URL, then returns undefined.

        Example

        HttpConfiguration.setHeader('http://example.com', 'Foo', 'bar');

        const fetchOptions = {
        method: 'POST',
        body: 'whatever',
        headers: {
        Width: 200,
        }
        };

        // Let's update the options with headers applicable to 'http://example.com'
        HttpConfiguration.applyConfiguration('http://example.com', fetchOptions);

        // now fetchOptions should be
        // {
        // method: 'POST',
        // body: 'whatever',
        // headers: {
        // Width: 200,
        // Foo: 'bar',
        // }
        // }

        // We can now send our HTTP request with correct headers
        fetch('http://example.com/index.html', fetchOptions);
  • clear: (() => void)
      • (): void
      • Removes all configurations.

        Returns void

  • setAuth: ((urlPrefix, value) => void)
      • (urlPrefix, value): void
      • Sets the 'Authorization' header for the specified URL prefix.

        Note: this is a convenience function that calls setHeader internally:

        setHeader(urlPrefix, 'Authorization', value)
        

        Parameters

        • urlPrefix: string

          The URL prefix.

        • value: string

          The header value

        Returns void

        Example

        // We wish to set the Authorization header for the 'example.com'
        // domain to 'Bearer TOPLEVEL', except for the resources under
        // 'example.com/sub/resource', where we use 'Bearer SUBRESOURCE'.
        //
        // Since 'example.com/sub/resource' is a longer prefix than 'example.com',
        // its headers will have precedence and will be applied to HTTP requests
        // that match this URL prefix.
        HttpConfiguration.setAuth('https://example.com', 'Bearer TOPLEVEL');
        HttpConfiguration.setAuth('https://example.com/sub/resource', 'Bearer SUBRESOURCE');

        HttpConfiguration.applyConfiguration('https://example.com/index.html')
        // { 'Authorization', 'Bearer TOPLEVEL' }
        HttpConfiguration.applyConfiguration('https://example.com/sub/resource/index.html')
        // { 'Authorization', 'Bearer SUBRESOURCE' }
  • setHeader: ((urlPrefix, name, value) => void)
      • (urlPrefix, name, value): void
      • Sets the header for all HTTP requests that match the provided URL prefix.

        Note: The URL prefix must be a valid URL (e.g must contain a scheme and and host).

        Parameters

        • urlPrefix: string

          The URL prefix.

        • name: string

          The header name.

        • value: string

          The header value.

        Returns void

Example

// Set the `Accept-Language` header to `fr-CH` for all requests under `http://example.com`.
HttpConfiguration.setHeader('http://example.com', 'Accept-Language', 'fr-CH');

// Later, query the configuration for a resource under `http://example.com`
const fetchOptions = HttpConfiguration.applyConfiguration('http://example.com/myPage.html');

// And put the options in the fetch request.
fetch('http://example.com/myPage.html', fetchOptions);