npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-minifetch

v1.0.0

Published

small composable HTTP requests for redux actions

Downloads

3

Readme

redux-minifetch

small composable JSON HTTP requests for redux actions

npm travis standard

Install

npm install redux-minifetch

Usage

Add the middleware to your store:

var minifetch = require('redux-minifetch').middleware

var middleware = applyMiddleware(
  minifetch({ baseUrl: 'https://mywebsite.com/api/' })
)

var store = createStore(reducer, middleware)

Then return minifetch actions from your action creators:

var { post } = require('minifetch')
function doLogin () {
  return post('/auth/login', {
    email: '[email protected]',
    password: 'qwerty123'
  })
}

Middleware Options

require('redux-minifetch').middleware({
  baseUrl: 'https://mywebsite.com/api/',
  fetch: global.fetch,
  configure: (opts) => {
    opts.headers['X-CSRF'] = global._csrf_token
    return opts
  }
})

baseUrl

The base URL for requests. Defaults to the current URL "directory", eg:

  • If the current page is https://mywebsite.com/whatever.html, the baseUrl is https://mywebsite.com/
  • If the current page is https://mywebsite.com/whatever/, the baseUrl is https://mywebsite.com/whatever/

This should work pretty well for SPAs that don't do history-based routing but it's better to be explicit it anyway.

fetch

A fetch function. Defaults to the global fetch. You can use this to use unfetch or node-fetch or something.

configure(opts)

A function to change the request options before they are passed to fetch. You can use this to set CSRF headers or authentication tokens for example. Return the changed options.

onError(response)

A function to extract the error data from a failed response. A response is failed when its status code is not 2xx (Response.ok). onError should return an Error instance or a Promise that resolves to an Error instance. The result of this function will be passed to the onError handlers on individual requests.

The default onError implementation returns new Error(response.statusText).

Actions

request(method, url, opts)

Create a new request. method is the HTTP method; url is the URL, which will be appended to the baseUrl option. opts can have:

  • opts.onStart() - An action creator that will be dispatched to the Redux store when the request is being sent.

  • opts.onComplete(response) - An action creator that will be dispatched to the Redux store when the request has completed. Receives the response JSON in the first parameter. The return value of dispatch(onComplete(response)) will be used as the resolution value of the request() Promise (see below).

  • opts.onError(error) - An action creator that will be dispatched to the Redux store when the request has errored. Receives the error object from fetch in the first parameter.

  • opts.qs - An object whose key/value pairs will be stringified and used as the query string. You can use a nested object and it will generate a query string like obj[nested]=value.

  • opts.data - An object with data that will be sent in the request body as JSON. Does nothing for GET requests.

When a request() is dispatched, it returns a Promise:

dispatch(request('get', '/me', {
  // assuming the API's response format is { data: [ user ] }
  onComplete: (response) => (dispatch, getState) => {
    var user = response.data[0]
    dispatch({ type: 'USER_DATA', payload: user }) // Dispatch an action using `redux-thunk`
    return user // Return the user object.
  }
})).then((user) => {
  console.log(user)
})

get(url, opts)

Shorthand to request('get', url, opts).

post(url, data, opts)

Shorthand to request('post', url, { data, ...opts }).

put(url, data, opts)

Shorthand to request('put', url, { data, ...opts }).

del(url, data, opts)

Shorthand to request('delete', url, { data, ...opts }).

License

Apache-2.0