declarative-fetch

Fetch description generator. This package does not actually execute any fetches, it simply provides an API identical to the standard fetch for producing descriptions of fetch operations that are compatible with redux-fetch middleware.

Usage no npm install needed!

<script type="module">
  import declarativeFetch from 'https://cdn.skypack.dev/declarative-fetch';
</script>

README

declarative-fetch

Pure fetch function. Mimics the fetch API, but produces a javascript object describing the fetch to be performed that can be executed by libraries like redux-effects-fetch.

Installation

npm install declarative-fetch

Usage

The API is identical to HTML5 fetch, except that it returns a declarative action rather than imperatively trigger an actual fetch request. You may then pass that action into an interpreter stack, like redux-effects and it will execute your request for you. To receive the result of your request, you may decorate it with bound handlers, either by manually adding your handlers to .meta.steps on the returned action, or using a small convenience library like bind-effect.

Example

import bind from 'bind-effect'
import fetch from 'declarative-fetch'
import {createAction} from 'redux-actions'

function createUser (user) {
  return bind(fetch('/user', {
    method: 'POST',
    body: user
  }), user => userCreated)
}

const userCreated = createAction('USER_CREATED')