Skip to content
rhttp.io

createHttp()

The framework-agnostic factory that creates a fully featured HTTP client.

import { createHttp } from "rhttp.io"; 
 
const http = createHttp(config?);
// → HttpClientInstance

Signature

function createHttp(config?: CreateHttpConfig): HttpClientInstance;

CreateHttpConfig

OptionTypeDefaultDescription
baseURLstringBase URL prepended to all request URLs.
defaultHeadersRecord<string, string>{}Headers sent with every request (medium priority).
defaultFetchOptionsRequestInit{}Native fetch() options (lowest priority for headers).
timeoutnumber30_000 (30 s)Global timeout in milliseconds.
retryPartial<RetryConfig>{attempts:0}Retry configuration.
cachePartial<CacheConfig>{enabled:false}In-memory cache configuration.
csrfPartial<CsrfConfig>{enabled:false}CSRF protection configuration.
observabilityPartial<ObservabilityConfig>{logger:false}Logging, tracing, and metrics.
authPartial<AuthConfig>Authentication configuration.
requestContext() => anyReturns the current request (SSR, TanStack Start).
fetchtypeof globalThis.fetchglobalThis.fetchCustom fetch implementation.
circuitBreakerPartial<CircuitBreakerConfig>{enabled:true}Circuit breaker configuration.
requestPoolPartial<RequestPoolConfig>{enabled:false}Request concurrency limiter.
etagPartial<ETagConfig>{enabled:true}ETag / conditional request support.
hooksRequestHooksGlobal lifecycle hooks.
pluginsPluginConfig[][]Plugin instances registered at construction.
requestValidatorRequestValidatorGlobal request validator — throw to reject.
responseTransformerResponseTransformerGlobal response transformer — mutates data before returning.

Header merging priority

When a request is sent, headers are merged in three layers with increasing priority:

1. config.defaultFetchOptions.headers   (lowest)
2. config.defaultHeaders
3. options.headers                      (per-request, highest)

All keys are normalized to lowercase before merging. A later layer replaces an earlier one with the same key.

When auth.forwardCookies is true and a request context is available (via requestContext() or the async_hooks store), the Cookie header from the incoming request is copied to every outbound request.

const http = createHttp({
  auth: { forwardCookies: true },
});

Request context resolution

The engine resolves the active request through:

  1. requestContextStore.getStore() — an async_hooks store set via setRequestContextStore().
  2. config.requestContext() — a user-provided function.
  3. Falls back to no context if both are unavailable.

Environment-specific factories

For most use cases, prefer the environment-specific wrappers which pre-configure sensible defaults:

FactoryEntry pointKey defaults
createHttp()rhttp.ioNo defaults — full control.
createClientHttp()rhttp.io/clientcredentials:"include", JSON headers, CSRF on, token from localStorage.
createServerHttp()rhttp.io/serverforwardCookies:true, logger+tracing on, prod metrics.