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-rest-services

v0.2.7

Published

[![Test Coverage](https://api.codeclimate.com/v1/badges/d58763c3cba4d9f5293c/test_coverage)](https://codeclimate.com/github/amazingdesign/redux-rest-services/test_coverage) [![Maintainability](https://api.codeclimate.com/v1/badges/d58763c3cba4d9f5293c/mai

Downloads

5

Readme

Test Coverage Maintainability

redux-rest-services

Demo

Simple todo list + store props table with React

The same demo on CodeSandbox - play with it!

What for?

To minimize boilerplate code with redux async HTTP request, but preserve elastic possibilities of use.

What it does?

  • allows you to declare your services (endpoints) and actions (find, update etc.) that will be called on endpoint
  • you can write your onw action declarations or use default REST API CRUD actions
  • then redux-rest-services will generate object with actionTypes, syncActions, actions & reducers
  • you can bring your own fetch function (to provide eg. token auth) or use built in (isomorphic-fetch)
  • you can build your own reducers based on generated action types
  • you can create callbacks on each action call to dispatch another actions (eg. firing notifications on errors)

Installation

npm i redux-rest-services

It has two peer dependencies - "redux": "4.x" and "redux-thunk": "2.x".

Usage

Just import default function from the package and call it with an array of services (endpoints) declarations as first param.

Service declaration is an object with properties listed below.

import makeRestServices, { crudActionsDeclarations } from 'redux-rest-services'

// this function maps an object into array of objects
// and puts object keys into key property of array items
const mapObjectToArray = (obj) => (
  Object.entries(obj || {})
    .map(([key, value]) => (
      typeof value === 'object' ?
        {...value, key}
        :
        {key, value}
    ))
)


const restServices = makeRestServices(
  [
    {
      name: 'todoList', // required
      url: 'https://redux-rest-services.firebaseio.com/todo/:id.json',  // required - can contain URL params
      transformer : mapObjectToArray,  
      onError: (...all) => console.log('I am onError callback!', ...all), // optional
      onStartFetching: (...all) => console.log('I am onStartFetching callback!', ...all), // optional
      onStopFetching: (...all) => console.log('I am onStopFetching callback!', ...all), // optional
      onReceivesData: (...all) => console.log('I am onReceivesData callback!', ...all), // optional
      actionsDeclarations: crudActionsDeclarations, // required - you need to import it or decare own
    },
  ]
)

export default restServices

restServices is an object with properties:

  • actions - all async actions creators for all action declarations
  • syncActions - all sync actions for all action declarations
  • reducers - all reducers for all action declarations
  • actionTypes - all action types for all action declarations

makeRestServices can be called with second parameter - fetch function. It can be any function that takes two parameters - url and options and returns a promise.

As default we use isomorphic-fetch with checks on response status code. But you can use axios or whatever http client you want, by wrapping it into function described above.

Action declarations

They are names and params to make http requests. Technically it is an array of objects with at least name and method properties.

All props but name will be passed to fetch function as options, merged with data passed when action is dispatched.

So you can define here all kind of options like headers etc.

Below they are default action declarations (crudActionsDeclarations):

export default [
  {
    name: 'create',
    method: 'POST',
  },
  {
    name: 'get',
    method: 'GET',
  },
  {
    name: 'find',
    method: 'GET',
  },
  {
    name: 'update',
    method: 'PUT',
  },
  {
    name: 'delete',
    method: 'DELETE',
  },
]

As you can see, by default, there are two almost identical actions with GET method. There are coded that way to provide two separate places in store for getting lists of data and single items (get and find).

Dispatching an action

All async action creators are in actions property of object returned from calling makeRestServices. That property is also an object, and have declared action names as a key.

So to get all todos in above example we must dispatch:

restServices.actions.todoList.find()

To get specific one by id:

restServices.actions.todoList.find({ id: '-LsceA0pakWjfnTA6hDY'})

And for create new one:

restServices.actions.todoList.create({}, { body: JSON.stringify(newTaskText) })

All actions irrespectively of http method, can be called same way. First argument is object with params and second is data to bi merged with action declaration (without name) and passed as options to fetch function (thats the place for request body/data ;)).

Parameters and query strings

Parameters and query strings are handled together in params object when action is dispatched.

URL parameters (if they are in URL and param object) are pasted into URL. If the URL param is missing in param object the param is deleted form URL.

The rest are stringified by qs and append as query string.

State structure

redux-rest-services prepare state for each declared service. The state will include "global" variables for all actions in service, and action-specyfic variables.

So YES, you can call GET to obtain data and then POST to add new, and you will have these two responses in two separate places in store!

[serviceName]: {
  isLoading: false,
  isError: false,
  touched: null,
  [actionName]: {
    isLoading: false,
    isError: false,
    error: null,
    touched: null,
    data: null,
    rawData: null,
  }
}

Inspiration

This package would never appeared without inspiring by feathers-redux and redux-api.