a lightweight fetch api wrapper
Foxios is a simple HTTP client inspired by foxios but built around the Fetch API. It provides a clean interface for making HTTP requests with support for query parameters, request/response interceptors, and error handling.- 🚫 Zero Dependency: No extra libraries required.
- 🔥 100% TypeScript: Fully typed for robust development.
- 📦 Small Bundle Size: Approximately 600 bytes gzipped.
- 🌐 Relative & Absolute URL Support: Automatically resolves relative URLs based on
baseURL
or the current browser origin. - ⏱️ Timeout & Cancellation: Built-in support for request timeouts and cancellation using AbortController.
- 🛠️ Nested Query Parameters & interceptors: Easily pass deep nested objects and arrays as query parameters and inject using interceptors.
- ⚡ Modern Fetch API: Leverages the native Fetch API for fast and efficient HTTP requests.
- ❌ Enhanced Error Handling: Rich error objects (
FoxiosError
) with detailed status, response, and additional flags for timeouts and cancellations.
npm install foxios
or
yarn add foxios
import foxios from 'foxios';
const response = await foxios.get('/posts', {
baseURL: 'https://jsonplaceholder.typicode.com',
queryParams: { _limit: 5 },
headers: {
'Content-Type': 'application/json',
},
withCredentials: true,
});
console.log(response.data);
const response = await foxios.post(
'/posts',
{
title: 'Hello World',
body: 'This is a test post.',
userId: 1,
},
{
baseURL: 'https://jsonplaceholder.typicode.com',
headers: {
'Content-Type': 'application/json',
}, // optional: used to set http header as json
withCredentials: true, // optional: used to pass cookies
}
);
console.log(response.data);
const response = await foxios.put(
'/posts/1',
{
title: 'Updated Title',
body: 'Updated Content.',
},
{
baseURL: 'https://jsonplaceholder.typicode.com',
}
);
console.log(response.data);
const response = await foxios.delete('/posts/1', {
baseURL: 'https://jsonplaceholder.typicode.com',
});
console.log(response.data);
try {
const response = await foxios.get('/invalid-url', {
baseURL: 'https://jsonplaceholder.typicode.com',
});
} catch (error) {
console.error('Request failed:', error);
}
- Fork the repository.
- Create a feature branch.
- Commit your changes with clear messages.
- Open a pull request.
- Follow TypeScript best practices.
- Use meaningful variable and function names.
- Maintain clean and readable code.
- Open an issue with clear reproduction steps.
- Provide expected and actual behavior.
Foxios is open-source and contributions are welcome! 🚀