Skip to content

⚡ a simple, lightweight and blazingly fast fetch wrapper for making HTTP requests easily

License

Notifications You must be signed in to change notification settings

kaleb110/foxios

Repository files navigation

Logo

a lightweight fetch api wrapper

npm version install size Known Vulnerabilities license-badge

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.

Features

  • 🚫 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.

Installation

npm install foxios

or

yarn add foxios

Usage

Import Foxios

import foxios from 'foxios';

GET Request

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);

POST Request

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);

PUT Request

const response = await foxios.put(
  '/posts/1',
  {
    title: 'Updated Title',
    body: 'Updated Content.',
  },
  {
    baseURL: 'https://jsonplaceholder.typicode.com',
  }
);
console.log(response.data);

DELETE Request

const response = await foxios.delete('/posts/1', {
  baseURL: 'https://jsonplaceholder.typicode.com',
});
console.log(response.data);

Error Handling

try {
  const response = await foxios.get('/invalid-url', {
    baseURL: 'https://jsonplaceholder.typicode.com',
  });
} catch (error) {
  console.error('Request failed:', error);
}

Contribution Guidelines

How to Contribute

  • Fork the repository.
  • Create a feature branch.
  • Commit your changes with clear messages.
  • Open a pull request.

Code Style

  • Follow TypeScript best practices.
  • Use meaningful variable and function names.
  • Maintain clean and readable code.

Reporting Issues

  • Open an issue with clear reproduction steps.
  • Provide expected and actual behavior.

Foxios is open-source and contributions are welcome! 🚀

About

⚡ a simple, lightweight and blazingly fast fetch wrapper for making HTTP requests easily

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published