@md-oss/http-client
v0.5.0
Published
Fetch-based HTTP client with retries, timeouts, and typed results
Downloads
1,517
Readme
@md-oss/http-client
Opinionated fetch-based HTTP client with retries, timeouts, and typed responses.
Features
- Base URL + path parameter resolution
- Query serialization (including arrays)
- Automatic JSON
Accept/Content-Typedefaults when appropriate - Timeout and retry with exponential backoff + jitter
- Unified success/error result shape
- JSON or text response parsing modes (raw response always included)
Usage
import { createHttpClient, isHTTPErrorResponse } from '@md-oss/http-client';
const api = createHttpClient({
serviceName: 'billing-api',
baseUrl: 'https://api.example.com',
});
const result = await api.request<{ id: string; total: number }>(
'/invoices/:id',
{
method: 'GET',
pathParams: { id: 'inv_123' },
timeoutMs: 5000,
retries: 2,
}
);
if (isHTTPErrorResponse(result)) {
console.error(result.message);
return;
}
console.log(result.data.total);Utility
import { serializeJson } from '@md-oss/http-client';
const payload = serializeJson({ createdAt: new Date() });