slim-fetchy

fetch json-enhanced api (ES6) - using isomorphic-fetch lib

Usage no npm install needed!

<script type="module">
  import slimFetchy from 'https://cdn.skypack.dev/slim-fetchy';
</script>

README

slim-fetchy 🚀

Enhanced fetch API.

https://github.com/justsml/slim-fetchy.git

  • ✔ Simple HTTP request
  • ✔ Small
  • ✔ Encode POST Body
  • ✔ Response parser
  • ✔ Smarter error handling

Why?

I was listening to Slim Shady EP. And all the other names were taken.

Install

# Add to your project:
npm install slim-fetchy --save

# Optionally, clone Code:
git clone https://github.com/justsml/slim-fetchy.git
cd slim-fetchy
npm install
npm test

Seriously, Why?

The current fetch API spec and the implementation from Github are ill-suited for the majority of use-cases.

Specifically, JSON-backed services require repetitive encoding and response parsing. Additionally, acquiring HTTP response body content requires 2 promise .then() calls. (Some Promise libraries, like bluebird will automatically handle this for you.)

I decided to create a wrapper to eliminate frequent hurdles & repetitive code.

slim-fetchy JSON optimized usage:

// Using slim-fetchy (optional: es6 destructuring)
fetch('/users.json')
  .then(({body}) => {
    document.body.innerHTML = body; // SINGLE `.then` - has access to headers, status & statusText
  })

Compared to current fetch API/spec:

// Using github's implementation
fetch('/users.json')
  .then(function(response) {
    return response.text()
  }).then(function(body) {
    document.body.innerHTML = body
  })

Stats & Support Info

  • Requires Promise support - native or polyfilled. Tested with Bluebird and es6-promise
  • Runs on: Browser & NodeJS
  • Gzipped: 2.8K