extend WHATWG fetch API with middleware
- transparent, your extended fetch mantains fetch API
- recursive, extend fetch, extend extended fetch, ...
- use over any fetch implementation you like (native fetch, fetch-ponyfill, fetch-polyfill, etc.)
- pick from built-in middleware and/or write yours
- unit tested and benchmarked against plain fetch
npm install fetch-wrap --save
const fetchWrap = ;// you can use native fetch(), or the implementation you preferlet fetch = ;// extend fetch with a list of wrappersfetch =;// use your extended fetch;
There's some useful middleware in this package that you can optionally import see src/middleware.js for details, here's a full example:
var fetchWrap = ;var middleware = ;var fetch =;;
Write your own Middleware!
const fetchWrap = ;fetch =
For unit testing, you can use the built-in
testing middleware to mock or spy fetch calls.
var fetchWrap = ;var middleware = ;var spyLog = ;var fetch =;// it will fail if no `options.mock` is found, to prevent real requests during unit-testing
For details on built-in middleware check src/middleware.js
compares fetch (fetch-ponyfill, not extended), with extended fetch (fetch-ponyfill extended with some of the built-in middleware).
Typically results show performance cost is neglectable, example:
fetch GET json x 435 ops/sec ±1.52% (80 runs sampled) extended fetch GET json x 438 ops/sec ±1.24% (81 runs sampled)