@smart-auth/fetch
v0.1.2
Published
Fetch API wrapper for Smart Auth SDK — automatic token injection, 401 handling, and silent refresh with retry
Downloads
345
Maintainers
Readme
@smart-auth/fetch
Fetch API wrapper for the Smart Auth SDK ecosystem.
What it does
@smart-auth/fetch wraps the native fetch() API with automatic authentication. Your access token is injected into every request. If a 401 comes back, it silently refreshes and retries. Zero config, zero boilerplate.
Features
- 🔑 Auto token injection — Authorization header added to every request
- 🔄 Silent 401 refresh — Expired token? Refreshed and retried automatically
- 🌐 Base URL support — Prefix all requests with a base URL
- 📦 Lightweight — ~1KB, wraps native fetch, no dependencies
Installation
npm install @smart-auth/fetch @smart-auth/coreQuick Start
import { createAuth } from '@smart-auth/core';
import { createSmartFetch } from '@smart-auth/fetch';
const auth = createAuth({
apiBaseUrl: '/api',
refresh: { endpoint: '/auth/refresh' },
});
const smartFetch = createSmartFetch({ auth, baseUrl: '/api' });
// Use like normal fetch — auth is automatic
const response = await smartFetch('/users');
const users = await response.json();
// POST requests work too
const newUser = await smartFetch('/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice' }),
});Part of Smart Auth SDK
| Package | Description |
| -------------------------------------------------------------------------- | ------------------------------ |
| @smart-auth/core | Core auth engine (required) |
| @smart-auth/react | React Provider & hooks |
| @smart-auth/axios | Axios interceptors alternative |
| @smart-auth/express | Express middleware |
License
MIT © Durjoy Ghosh
