DEV Community

Cover image for Stop writing axios wrappers. Use this instead.
Ramadan Ahmed
Ramadan Ahmed

Posted on

Stop writing axios wrappers. Use this instead.

**Every project. Same file. lib/http.ts, wrapping axios with retry logic, auth headers, timeout, error handling. Copy paste. Rename. Repeat.
axios is great. But it's missing everything production apps actually need. And you end up building it yourself every single time.
So I stopped copying and built @firekid/hurl.

Before:
const client = axios.create({ baseURL: 'https://api.example.com' })

client.interceptors.request.use((config) => {
config.headers.Authorization = Bearer ${token}
return config
})

try {
const res = await client.get('/users')
} catch (err) {
if (err.response) console.log(err.response.status)
}

After:
hurl.defaults.set({
baseUrl: 'https://api.example.com',
auth: { type: 'bearer', token },
retry: 3,
})

const res = await hurl.get('/users')

Same result. Half the code. Retry built in.
What you get out of the box:
Built on native fetch, runs on Cloudflare Workers, Vercel Edge, Deno, Bun, Node 18+. No adapters. No config.
Retry with exponential backoff. Bearer, Basic and API key auth. Upload and download progress. Request deduplication. In-memory caching. Debug mode. Full TypeScript. Zero dependencies.
Throws on 4xx and 5xx automatically. No more checking err.response manually.

Production setup:
const api = hurl.create({
baseUrl: 'https://api.example.com',
auth: { type: 'bearer', token: process.env.API_TOKEN },
retry: 3,
timeout: 8000,
})

const users = await api.get('/users')
const post = await api.post('/posts', { title: 'hello' })

If you've copy pasted that axios wrapper one too many times, give this a try.
npm: npmjs.com/package/@firekid/hurl
GitHub: github.com/Firekid-is-him/hurl**

Top comments (0)