@sentinelagent/fetch-hook
v0.9.1
Published
Composable middleware hooks for the native fetch() API. Retry, timeout, auth.
Downloads
68
Maintainers
Readme
fetch-hook
Composable middleware hooks for the native fetch() API.
Install
npm install fetch-hookUsage
import { createFetch, authHook, logHook } from 'fetch-hook';
const api = createFetch({
before: [authHook(() => process.env.API_TOKEN!)],
after: [logHook('api')],
retry: { attempts: 3, delay: 300, on: [429, 503] },
timeout: 8000,
});
const data = await api('/users').then(r => r.json());Built-in hooks
| Hook | Description |
|------|-------------|
| authHook(getToken) | Injects Authorization: Bearer <token> |
| logHook(label?) | Logs status + URL per response |
| baseUrlHook(base) | Resolves relative URLs against a base |
Options
| Option | Type | Description |
|--------|------|-------------|
| before | BeforeHook[] | Runs before each request |
| after | AfterHook[] | Runs after each response |
| onError | ErrorHook[] | Runs on network error |
| retry | RetryOptions | Automatic retry with backoff |
| timeout | number | Abort after N milliseconds |
License
MIT © trysentinelagent
