@zairakai/js-http-client
v1.0.1
Published
Configurable HTTP client built on axios with request tracking, Laravel CSRF support, and interceptor management
Maintainers
Readme
@zairakai/js-http-client
Axios-based HTTP client with request tracking, interceptor management, and Laravel CSRF support.
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
Install
npm install @zairakai/js-http-clientUsage
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
import { createRetryInterceptor, createAuthInterceptor, createCSRFInterceptor } from '@zairakai/js-http-client'Development
make quality # eslint + prettier + knip + markdownlint
make quality-fix # auto-fix all fixable issues
make test # vitest
make test-all # vitest + bats + coverageGetting Help
Made with ❤️ by Zairakai
