Options
All
  • Public
  • Public/Protected
  • All
Menu

yafetch

Yafetch

Yet another fetch is a backward compatible, dependency free, drop-in wrapper for fetch API

npm License bundle size

Features

  • Plugins for various features such as retries, error handling, logging, parsing etc...
  • Matches the fetch API signature by extension and enhancements
  • Can be used with an alternative fetch implementation such as node-fetch
  • Automated parsing of request/response data
  • Global options
  • ... and more

How to use

npm i @yafetch/core --save
# or
yarn add @yafetch/core --save

Usage is mostly identical, besides a few added features, to the standard fetch API:

import yafetch from '@yafetch/core'
const html = await yafetch('https://www.example.com') // as opposed to fetch('https://www.example.com')

The only exception is the ability to pass body for GET requests, which yafetch will convert to URLSearchParams automatically:

import yafetch from '@yafetch/core'
const html = await yafetch('https://www.example.com', { body: { name: 'john' } }) // will send https://www.example.com/?name=john

Plugins

The most powerful aspect of yafetch is its plugin system, allowing full control over the API calls. There are three types of plugins categorized by their execution order relative to the main API call, "before" and "after" plugins run before or after the API request while "wrap" plugins will wrap the API call itself. These wrap plugins also wrap each other allowing for communication between multiple plugins and advanced scenarios such as retries, error handling etc...

Custom fetch implementation

Yafetch you can provide an alternative proxy function to use instead when running in a non-browser environment such as node.js. To use this feature simply pass a fetchProxy in the options, from there it's up to you how the API call happens. See an example of this in tests under the core package.

Contribution

Please make sure to read the Contributing Guide before you work on this project.

License

MIT

Generated using TypeDoc