Skip to content

brysonbw/fetchet

fetchet

The compact, Promise-based, and browser-native HTTP fetch wrapper.

github tag npm downloads ci (test)

Documentation

For more detailed documentation, please visit the dedicated fetchet API Reference.

Installation

Fetchet is intended for client-side use and is available as a NPM package.

npm install fetchet

Usage

const response = await fetchet("https://dummyjson.com/posts");
const data = await response.json();
console.log(data);

GET is the default HTTP method if no method is specified in the options, similar to the native fetch API.

Why use fetchet?

Fetchet is a lightweight wrapper around the native fetch API. It enhances convenience by extending the options object with features like automatic JSON parsing and support for passing query parameters as an object.

Features

options.parameters

The parameters option (an object) in the request options is automatically parsed into URL search params string and appended to the request URL.

const parameters = { limit: "10", skip: "10", select: "name,ingredients,instructions" };
const response = await fetchet("https://dummyjson.com/recipes", { parameters });
const data = await response.json();
console.log(data);

Request URL: https://dummyjson.com/recipes?limit=10&skip=10&select=name,ingredients,instructions

options.json

The json option (a boolean) is set to true by default. This means you don't need to manually set headers: { 'Content-Type': 'application/json' } or use JSON.stringify(body) for POST, PUT, or PATCH requests. If you want to send the body in a non-JSON format, set json to false.

const response = await fetchet('https://dummyjson.com/posts/add', {
  method: 'POST',
  body: {
    title: 'How can AI and/or AGI be leveraged to improve the well-being and future of humanity?',
    userId: 5,
    /* other post data */
  }
});
const data = await response.json();
console.log(data);

Contributing

If you have suggestions for how this project could be improved, or want to report a bug, feel free to open an issue! We welcome all contributions.

Likewise, before contributing please read the contribution guide.

Credits

Fetchet is heavily inspired by axios and of course fetch API.

Motivation

I wrote a fetch wrapper for a small side project. Rather than copying and pasting it into other projects, I decided to publish it as a public package.

Resources

License

MIT

About

The compact, Promise-based, and browser-native HTTP fetch wrapper

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •