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-restapi

v0.3.6

Published

REST API for redux infrastructure.

Downloads

23

Readme

Build Status npm npm

Introduction

redux-restapi is an utility for integrating a REST api with Redux. It helps you, to easily integrate your calls to a REST api with your store. It generates actions and reducer for making the calls to the server and can easly be combined with your actions.

All calls are made using superagent.

Installation

node:

npm install redux-restapi --save

Works with browserify and should work with webpack

Example

Let's say we have this:

- GET: http://mywebsite.com/api/v1/companies (will list all the companies)
- DELETE: http://mywebsite.com/api/v1/companies/:id (will delete company with id)
- POST: http://mywebsite.com/api/v1/companies (will create a company)

Main file

index.js

import rest from "redux-restapi";

rest.setBaseUrl("http://mywebsite.com/api/v1/");
rest.setHeaders({
    "Content-Type": "application/json",
    "x-api-key": "apiKey123"
});

Reducer

reducers/companies.js

import rest from "redux-restapi";
import reduceReducers from "reduce-reducers";

// Default reducer, this will be combined
// with the REST reducer
let defaultReducer = (state = {}, action = {}) => {

    switch (action.type) {
        case "myType": // do extra stuff
            return {
                ...state,
                "extra": true
            };
        default:
            return state;
    }
};

// Create REST reducer
let restReducer = rest.createReducer(rest.createEndpoint("companies"));

// Combine our reducer with rest reducer
export default reduceReducers(restReducer, defaultReducer);

This will create a reducer that can handle (pending, list, create, remove, update) action and handle extra action you want to handle.

Actions

actions/companies.js

import rest from "redux-restapi";

let endpoint = rest.createEndpoint("companies");
let restActions = rest.createActions(endpoint);

export default {
    select: (item) => {
        return {
            type: "positions/select",
            payload: item
        }

    },
    ...restActions
};

store.js

import companiesReducer from "reducers/companies";

let rootReducer = combineReducers({ 
    routing: routerReducer,
    companies: companiesReducer
});

let initialState = {};
let store = createStore(rootReducer, initialState, applyMiddleware(thunkMiddleware));

In your containers you can use this actions:

ExemplaContainer.js

import actions from "actions/companies";

const mapState = (state) => {
    return {
        ...
    };
};

const mapDispatch = function (dispatch) {

    let { create, remove, list } = actions;

    return {
        onAdd: (company) => {
            dispatch(create(company));
        },
        onDelete: (item) => {
            dispatch(remove(item._id));
        },
        onLoad: () => {
            dispatch(actions.list());
        }
    };
};

export default connect(mapState, mapDispatch)(Example);

Tests

gulp test