HttpHeaders
An extended HTTP headers object with a strictly-typed schema. Fully compatible with the built-in
Headers class.
import { HttpHeaders } from '@zimic/http';
const headers = new HttpHeaders<{
accept?: string;
'content-type'?: string;
}>({
accept: '*',
'content-type': 'application/json',
});
const contentType = headers.get('content-type');
console.log(contentType); // 'application/json'
constructor()
Creates a new HttpHeaders instance, optionally initialized with a plain object or another headers instance.
new HttpHeaders<Schema>();
new HttpHeaders<Schema>(init);
Arguments:
-
init:
HttpHeadersInit | undefinedA plain object, another headers instance, or an array of tuples with (name, value) pairs to initialize the headers with. If not provided, the created headers will be empty.
Type arguments:
-
Schema:
HttpHeadersSchema.LooseAn object type whose keys are the header names and values are the expected types of those headers. This schema is used to enforce type safety when using the headers instance.
headers.set()
Sets a header value. If the value already exists, it will be replaced.
headers.set(name, value);
Arguments:
-
name:
stringThe name of the header to set.
-
value:
stringThe value to set for the header.
Related:
headers.append()
Appends a value to a header, or adds the header if it does not exist.
headers.append(name, value);
Arguments:
-
name:
stringThe name of the header to append to.
-
value:
stringThe value to append for the header.
Related:
headers.get()
Retrieves the value of a given header.
headers.get(name);
Arguments:
-
name:
stringThe name of the header to retrieve.
Returns:
The value of the header, or null if the header is not present.
Related:
headers.getSetCookie()
Retrieves the value of the Set-Cookie header.
headers.getSetCookie();
Returns: string[]
An array of strings representing the values of the Set-Cookie header, or an empty array if the header is not present.
Related:
headers.has()
Checks if a header with the given name exists.
headers.has(name);
Arguments:
-
name:
stringThe name of the header to check.
Returns: boolean
true if the header exists, false otherwise.
Related:
headers.delete()
Removes a header and its value from the headers object.
headers.delete(name);
Arguments:
-
name:
stringThe name of the header to delete.
Related:
headers.forEach()
Executes a function for header (name, value) pair.
headers.forEach(callback);
headers.forEach(callback, thisArg);
Arguments:
-
callback:
(value: string, name: string, headers: HttpHeaders) => voidFunction to execute for each element.
-
thisArg:
HttpHeaders | undefinedValue to use as
thiswhen executingcallback.
Related:
headers.keys()
headers.keys();
Returns: Iterator<string>
An iterator over all header names.
Related:
headers.values()
headers.values();
Returns: Iterator<string>
An iterator over all header values.
Related:
headers.entries()
headers.entries();
Returns: Iterator<[string, string]>
An iterator over all header (name, value) pairs.
Related:
headers.equals()
Compares an HttpHeaders instance with another to check if they are equal. Equality is defined as having the same keys
and values, regardless of the order of the keys.
headers.equals(otherHeaders);
Arguments:
-
otherHeaders:
HttpHeadersThe
HttpHeadersinstance to compare against.
Returns: boolean
true if the headers are equal, false otherwise.
headers.contains()
Checks if an HttpHeaders instance contains all headers from another HttpHeaders instance. This method is less strict
than headers.equals() and only requires that the current headers contain all keys and values from the
other headers.
headers.contains(otherHeaders);
Arguments:
-
otherHeaders:
HttpHeadersThe
HttpHeadersinstance to check against.
Returns: boolean
true if all headers from otherHeaders are present in the current headers, false otherwise.
headers.assign()
Assigns another HttpHeaders instance to the current instance, similarly to
Object.assign(). Only the
instance where this method is called will be modified.
headers.assign(otherHeaders);
Returns: void
const headers = new HttpHeaders({
accept: '*/*',
'content-type': 'application/json',
});
const otherHeaders = new HttpHeaders({
'content-type': 'text/html',
});
headers.assign(otherHeaders);
console.log(headers.get('accept')); // '*/*'
console.log(headers.get('content-type')); // 'text/html'
headers.toObject()
Converts an HttpHeaders instance to a plain object. This method is useful for serialization and debugging purposes.
headers.toObject();
Returns: Record<string, string>
A plain object representation of the headers.
const headers = new HttpHeaders({
accept: '*/*',
'content-type': 'application/json',
});
const object = headers.toObject();
console.log(object); // { accept: '*/*', 'content-type': 'application/json' }