Appearance
@zairakai/js-http-client
Features
- Three factory functions —
createHttpClient,createApiClient,createLaravelClient - Request tracking — reactive
isLoadingandrequestCountper client instance - Built-in interceptors — CSRF token, auth bearer, retry with backoff, error logging, timeout
- Laravel ready — automatic
X-Requested-Withand 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-clientUsage
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 requestsAPI
Factory functions
| Function | Description |
|---|---|
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
| Option | Type | Default | Description |
|---|---|---|---|
baseURL | string | '' | Base URL for all requests |
timeout | number | 10000 | Request timeout in ms |
authToken | string | (() => string) | — | Bearer token or token factory |
csrfToken | string | (() => string) | — | CSRF token or factory |
retries | number | 0 | Number of retry attempts on failure |
retryDelay | number | 1000 | Delay between retries in ms |
trackRequests | boolean | true | Enable isLoading / requestCount |
logger | Logger | — | 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 + coverageGetting 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]