Skip to content

@zairakai/js-http-client ​

Features ​

  • Three factory functions — createHttpClient, createApiClient, createLaravelClient
  • Request tracking — reactive isLoading and requestCount per client instance
  • Built-in interceptors — CSRF token, auth bearer, retry with backoff, error logging, timeout
  • Laravel ready — automatic X-Requested-With and CSRF headers
  • TypeScript first — full type exports including AxiosInstance, AxiosResponse, HttpMethod

Documentation ​

The documentation site has this guide and the reference of the API generated from the source with TypeDoc, for every released version (selector at the top right, next is main). Build it with cd docs && npm ci && npm run dev. It is published with GitLab Pages from main.


Install ​

bash
npm install @zairakai/js-http-client

Usage ​

ts
import { createHttpClient, createLaravelClient, createApiClient } from '@zairakai/js-http-client'

// Generic API client
const api = createHttpClient({
  baseURL: 'https://api.example.com',
  timeout: 5000,
  authToken: () => localStorage.getItem('token'),
  retries: 3,
})

const { data } = await api.get<User>('/users/1')

// Laravel full-stack client (CSRF + credentials + Laravel headers)
const laravelApi = createLaravelClient({ baseURL: '/api' })
await laravelApi.post('/users', { name: 'Alice' })

// Pure API client (no Laravel headers)
const publicApi = createApiClient({ baseURL: 'https://public.api.com' })

// Request tracking
console.log(api.isLoading)     // true while requests are pending
console.log(api.requestCount)  // number of active requests

API ​

Factory functions ​

FunctionDescription
createHttpClient(options?)Base factory. Full control over all options.
createLaravelClient(options?)Adds withCredentials, X-Requested-With, and CSRF support.
createApiClient(options?)Clean JSON API client without Laravel-specific headers.

Options ​

OptionTypeDefaultDescription
baseURLstring''Base URL for all requests
timeoutnumber10000Request timeout in ms
authTokenstring | (() => string)—Bearer token or token factory
csrfTokenstring | (() => string)—CSRF token or factory
retriesnumber0Number of retry attempts on failure
retryDelaynumber1000Delay between retries in ms
trackRequestsbooleantrueEnable isLoading / requestCount
loggerLogger—Custom error logger

Standalone interceptors ​

ts
import { createRetryInterceptor, createAuthInterceptor, createCSRFInterceptor } from '@zairakai/js-http-client'

Development ​

bash
make quality       # eslint + prettier + knip + markdownlint
make quality-fix   # auto-fix all fixable issues
make test          # vitest
make test-all      # vitest + bats + coverage

Getting Help ​

[![License][license-badge]][license] [![Security Policy][security-badge]][security] [![Issues][issues-badge]][issues]

Made with ❤️ by [Zairakai][ecosystem]

Statistics ​

![Statistics of js-http-client][stats-card]

Documentation of 1.0.14 , built from 5520ec9 on 1.0.14 by pipeline #2985012441 on 2026-10-21.