Skip to content

createClient

The recommended entry point for super-http. Wraps HttpClientFactory.create() with preset support and a single-object API.

typescript
import { createClient } from 'super-http'

How it relates to HttpClientFactory

createClient is built on top of HttpClientFactory:

createClient({ baseURL, preset?, pool?, ...httpConfig })

HttpClientFactory.create(baseURL, httpConfig, mergedPool)

  applies preset defaults (if any)

returns HttpClient (singleton per baseURL)

Both share the same singleton cacheHttpClientFactory.clear() resets clients created by either API.


Signature

typescript
function createClient(options: CreateClientOptions): HttpClient

interface CreateClientOptions extends HttpClientRequestConfig {
  baseURL: string
  preset?: 'high-throughput' | 'resilient-api' | 'low-latency'
  pool?: PoolConfig
}
FieldTypeDescription
baseURLstringRequired. Base URL for all requests
presetPresetOptional resilience preset — see Presets
poolPoolConfigPool options, merged with preset defaults
headersRecord<string,string>Default headers on every request
timeoutnumberDefault timeout in ms (overridden by pool.timeout if set)
authAxiosBasicCredentialsBasic auth
...HttpClientRequestConfigAny Axios request config

Examples

typescript
const api = createClient({ baseURL: 'https://api.example.com' })

// Identical to:
// HttpClientFactory.create('https://api.example.com')
typescript
const payments = createClient({
  baseURL: 'https://payments.internal',
  preset: 'resilient-api',
  headers: { Authorization: `Bearer ${KEY}` },
})
// Includes: circuit breaker + retry + bulkhead, all pre-configured
typescript
const catalog = createClient({
  baseURL: 'https://catalog.internal',
  preset: 'high-throughput',
  pool: { maxSockets: 300 },  // override one pool value, keep rest
})
typescript
const api = createClient({ baseURL: 'https://api.example.com' })

// Apply resilience manually after creation
api
  .retry(3, new ExponentialJitterRetryStrategy(100, 10_000))
  .circuitBreak({ failureThreshold: 5, successThreshold: 2, timeoutMs: 10_000 })
  .use(LoggerPlugin())

Preset defaults

PresetmaxSocketstimeoutretrycircuit breakerbulkhead
high-throughput2005 s1× jitter (50–500ms)
resilient-api10015 s3× jitter (100–10s)fail@10, recover@10s50c / queue 200
low-latency5002 s

TIP

Preset settings can always be overridden — call .retry(), .circuitBreak(), etc. after createClient(). Manual method calls always take precedence over the preset.


Singleton behaviour

Calling createClient multiple times with the same baseURL returns the same instance. Options passed on subsequent calls are ignored:

typescript
const a = createClient({ baseURL: 'https://api.example.com', preset: 'resilient-api' })
const b = createClient({ baseURL: 'https://api.example.com', preset: 'low-latency' }) // ignored!

console.log(a === b) // true

To create a fresh client for the same URL (e.g. in tests):

typescript
import { HttpClientFactory } from 'super-http'

afterEach(() => HttpClientFactory.clear())

Released under the MIT License.