Update the request options with stored configuration applicable to this URL.
The URL.
Optional
options: RequestInitThe request options.
The updated options, if any. If no options object is passed, and no
configuration applies to this URL, then returns undefined
.
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);
Removes all configurations.
Sets the 'Authorization' header for the specified URL prefix.
Note: this is a convenience function that calls setHeader internally:
setHeader(urlPrefix, 'Authorization', value)
The URL prefix.
The header value
// 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' }
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).
The URL prefix.
The header name.
The header value.
// 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);
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, butexample.com/foo
is not.Note: If you plan to use the same configuration for different schemes (e.g
http
andhttps
, 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 useFetcher
, then you have to patch the request yourself (see the example below).