All Projects → benjamine → fetch-wrap

benjamine / fetch-wrap

Licence: MIT License
extend WHATWG fetch wrapping it with middlewares

Programming Languages

javascript
184084 projects - #8 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to fetch-wrap

Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (+61.9%)
Mutual labels:  fetch, http-client, fetch-api
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+34071.43%)
Mutual labels:  fetch, http-client, fetch-api
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+4961.9%)
Mutual labels:  fetch, http-client, fetch-api
Php Fetch
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP
Stars: ✭ 95 (+352.38%)
Mutual labels:  fetch, http-client, fetch-api
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (+304.76%)
Mutual labels:  fetch, fetch-api
Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+4842.86%)
Mutual labels:  fetch, fetch-api
Fetcher Ts
Type-safe wrapper around Fetch API
Stars: ✭ 87 (+314.29%)
Mutual labels:  fetch, http-client
Fetch Ponyfill
WHATWG fetch ponyfill
Stars: ✭ 209 (+895.24%)
Mutual labels:  fetch, fetch-api
Zl Fetch
A library that makes the Fetch API a breeze
Stars: ✭ 186 (+785.71%)
Mutual labels:  fetch, fetch-api
axios-endpoints
Axios endpoints helps you to create a more concise endpoint mapping with axios.
Stars: ✭ 41 (+95.24%)
Mutual labels:  fetch, fetch-api
fetch
A fetch API polyfill for React Native with text streaming support.
Stars: ✭ 27 (+28.57%)
Mutual labels:  fetch, fetch-api
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (+38.1%)
Mutual labels:  fetch, http-client
legible
the cleanest way to make http requests in js / node
Stars: ✭ 49 (+133.33%)
Mutual labels:  fetch, fetch-api
Ky
🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
Stars: ✭ 7,047 (+33457.14%)
Mutual labels:  fetch, http-client
fitch.js
A lightweight Promise based HTTP client, using Fetch API.
Stars: ✭ 35 (+66.67%)
Mutual labels:  fetch, http-client
Wretch
A tiny wrapper built around fetch with an intuitive syntax. 🍬
Stars: ✭ 2,285 (+10780.95%)
Mutual labels:  fetch, http-client
Fetch Examples
A repository of Fetch examples. See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API for the corresponding documentation.
Stars: ✭ 431 (+1952.38%)
Mutual labels:  fetch, fetch-api
Fetch Suspense
A React hook compatible with React 16.6's Suspense component.
Stars: ✭ 479 (+2180.95%)
Mutual labels:  fetch, fetch-api
fennch
Modern fetch-based axios-like HTTP client for the browser and node.js
Stars: ✭ 12 (-42.86%)
Mutual labels:  fetch, http-client
bestfetch
fetch ⭐️caching ⭐️deduplication
Stars: ✭ 44 (+109.52%)
Mutual labels:  fetch, fetch-api

fetch-wrap

Build Status Code Climate Test Coverage NPM version NPM dependencies

extend WHATWG fetch API with middleware

  • transparent, your extended fetch mantains fetch API fetch(url, options)
  • 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
  • isomorphic

Install

npm install fetch-wrap --save

Usage

const fetchWrap = require('fetch-wrap');

// you can use native fetch(), or the implementation you prefer
let fetch = require('fetch-ponyfill')();

// extend fetch with a list of wrappers
fetch = fetchWrap(fetch, [
  function middleware1(url, options, innerFetch) {
    // this middleware does nothing
    return innerFetch(url, options);
  },
  middleware2,
  middleware3,
]);

// use your extended fetch
fetch('http://localhost:8080/file.json').then(result => console.log(result));

Built-in Middleware

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 = require('fetch-wrap');
var middleware = require('fetch-wrap/middleware');
var fetch = fetchWrap(fetch, [
  // support options.params, replace tokens in url and adds query string params
  middleware.urlParams({
    host: 'localhost'
  }),
  // apply options based on url (supports wildcards)
  middleware.optionsByUrlPattern([
    {
      for: 'http://localhost*',
      options: {
        headers: {
          Authorization: 'Token 1234'
        },
        timeouts: {
          // will send log events at 2s and 5s with these levels
          2: 'warn',
          5: 'error' // 5sec timeout from localhost, error!
        }
      }
    }
  ]),
  // automatically serialize body to JSON if needed
  middleware.sendJson(),
  // automatically parse JSON (revives Dates), optionally send Accept header
  //   throws on http errors
  middleware.receiveJson()
  // logs events (start, success, fail, timeouts), defaults to console but supports custom .log handler
  middleware.logger()
]);

fetch('http://{host}:8080/test.json', {
  params: {
    utm_source: 'nodejs'
  }
}).then(result => console.log(result));

Write your own Middleware!

const fetchWrap = require('fetchWrap');
fetch = fetchWrap(fetch, [

  function(url, options, fetch) {
    // modify url or options
    return fetch(url.replace(/^(http:)?/, 'https:'), options);
  },

  function(url, options, fetch) {
    // add headers
    return fetch(url, fetchWrap.merge({}, options, {
      headers: {
        Authorization: 'Token 123456'
      }
    });
  }

  function(url, options, fetch) {
    // modify result
    return fetch(url, options).then(function(response) {
      if (!response.ok) {
        throw new Error(result.status + ' ' + result.statusText);
      }
      if (/application\/json/.test(result.headers.get('content-type'))) {
        return response.json();
      }
      return response.text();
    });
  }

  function(url, options, fetch) {
    // catch errors
    return fetch(url, options).catch(function(err) {
      console.error(err);
      throw err;
    });
  }

]);

// use your customized fetch!

fetch('http://somedomain.com/news.json').then(function(news) {
  // GET https://somedomain.com/news.json with Authorization header, and parsed to json
  console.log(news.items);
});

Testing

For unit testing, you can use the built-in testing middleware to mock or spy fetch calls.

var fetchWrap = require('fetch-wrap');
var middleware = require('fetch-wrap/middleware');
var spyLog = [];
var fetch = fetchWrap(fetch, [
  middleware.optionsByUrlPattern([
    {
      for: 'http://localhost*',
      options: {
        // mock every request to this url
        mock: { name: 'john' }
      }
    }
  ])
  middleware.testing({
    // optional spy function
    spy(url, options) {
      spyLog.push({ url: url, options: options })
    }
  })
]);

// it will fail if no `options.mock` is found, to prevent real requests during unit-testing
fetch('http://localhost:8080').then(function(result) {
  expect(spyLog[0].url).to.eql('http://localhost:8080');
  expect(result).to.eql({ name: 'john' });
})

For details on built-in middleware check src/middleware.js

Benchmark

node src/benchmark

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)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].