RezoHeaders
RezoHeaders is a full-featured HTTP header manager with convenience methods, proxy-based property access, and multiple serialization formats. It is used throughout Rezo for request and response headers.
Constructor
new RezoHeaders(init?: RezoHeadersInit) Supported Init Types
| Type | Description |
|---|---|
[string, string][] | Array of key-value pairs. |
Record<string, string> | Plain object with string values. |
Record<string, string[]> | Object with array values (multi-value headers). |
Headers | Native Headers instance. |
RezoHeaders | Another RezoHeaders instance (cloned). |
OutgoingHttpHeaders | Node.js OutgoingHttpHeaders object. |
Example
import { RezoHeaders } from 'rezo';
// From object
const headers = new RezoHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer token123'
});
// From array of tuples
const h2 = new RezoHeaders([
['Accept', 'application/json'],
['Accept-Language', 'en-US']
]);
// From multi-value object
const h3 = new RezoHeaders({
'Set-Cookie': ['a=1', 'b=2']
});
// Proxy access (bracket notation)
headers['x-custom'] = 'value';
console.log(headers['x-custom']); // "value" Standard Methods (Inherited from Headers)
RezoHeaders supports all native Headers methods with the same API.
get(name)
get(name: string): string | null set(name, value)
set(name: string, value: string): void has(name)
has(name: string): boolean delete(name)
delete(name: string): void append(name, value)
Appends a value to an existing header (multi-value support).
append(name: string, value: string): void Set-Cookie Methods
getAll(name)
Returns all values for a header that supports multiple values. Primarily used for Set-Cookie.
getAll(name: 'set-cookie' | 'Set-Cookie'): string[] getSetCookie()
Returns all Set-Cookie header values as an array.
getSetCookie(): string[] Example
const cookies = headers.getSetCookie();
// ["session=abc; Path=/", "theme=dark; Max-Age=3600"]
const all = headers.getAll('set-cookie');
// Same result size
Returns the number of headers in the collection.
get size: number const headers = new RezoHeaders({ 'Accept': 'text/html', 'Host': 'example.com' });
console.log(headers.size); // 2 Convenience Methods
setContentType(value) / getContentType()
setContentType(value: string): this
getContentType(): string | undefined setAuthorization(value)
setAuthorization(value: string): this setUserAgent(value) / getUserAgent()
setUserAgent(value: string): this
getUserAgent(): string | undefined Example
const headers = new RezoHeaders();
headers
.setContentType('application/json')
.setAuthorization('Bearer token123')
.setUserAgent('MyApp/1.0');
console.log(headers.getContentType()); // "application/json"
console.log(headers.getUserAgent()); // "MyApp/1.0" Extraction Methods
getKeys()
Returns all header names as an array.
getKeys(): string[] getValues()
Returns all header values as an array.
getValues(): string[] Example
const headers = new RezoHeaders({ 'Accept': 'text/html', 'Host': 'example.com' });
headers.getKeys(); // ["accept", "host"]
headers.getValues(); // ["text/html", "example.com"] Serialization Methods
toEntries()
Returns headers as an array of [key, value] tuples.
toEntries(): [string, string][] toNative()
Creates a new native Headers instance with the same data.
toNative(): Headers toRaw()
Returns headers as an array of [key, value] tuples, preserving multi-value headers.
toRaw(): [string, string][] toArray()
Returns headers as an array of { key, value } objects.
toArray(): { key: string; value: string }[] toOrderedObject(order)
Returns headers as a plain object with keys in the specified order. Keys in order come first; remaining keys are appended. Used by stealth adapters to match browser header ordering.
toOrderedObject(order: string[]): Record<string, string | string[]> Example
const headers = new RezoHeaders({
'Accept': 'text/html',
'Host': 'example.com',
'User-Agent': 'Chrome/131'
});
// Browser-like header ordering
const ordered = headers.toOrderedObject([
'Host', 'User-Agent', 'Accept'
]);
// { host: 'example.com', 'user-agent': 'Chrome/131', accept: 'text/html' } toObject(omit?)
Converts headers to a plain object. Optionally omits specified keys.
toObject(omit?: Array<keyof RezoHttpHeaders> | keyof RezoHttpHeaders): Record<string, string | string[]> Example
const obj = headers.toObject();
// { accept: 'text/html', host: 'example.com' }
const filtered = headers.toObject(['host']);
// { accept: 'text/html' }
const filtered2 = headers.toObject('authorization');
// Omits the authorization header toString()
Returns a string representation of all headers.
toString(): string Symbol.iterator
RezoHeaders is iterable. Use for...of to iterate over [key, value] pairs.
for (const [key, value] of headers) {
console.log(`${key}: ${value}`);
}
// Spread into array
const entries = [...headers];