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

v1.0.4

Published

Middleware to block redux actions based on provided criteria.

Downloads

18

Readme

redux-cut

Middleware to block redux actions based on provided criteria.

What's it for?

Redux Cut is middleware for the Redux framework which enables you to block certain actions from firing, based on certain criteria. The middleware intercepts these actions and instead dispatches a "blocked action" action which you can listen for in your reducer. The new action includes the original action as a payload. You can use this to help with the implementation of protected functionality, permissions, or simply as a way to group this sort of functionality which you may have had in your reducers.

Can't I do this kind of thing with [insert some other library]?

Definitely. Redux Cut just provides a certain way of going about things that you may prefer to other methods.

How do I use it?

You can install Redux Cut through NPM:

npm install redux-cut --save

Import it:

import cut from 'redux-cut'; //or your module import syntax of choice

Then apply it to your store like so:

let store = createStore(reducer, initialState, applyMiddleware(cut(criteria), ...otherMiddlewares));

You'll notice that the imported cut function takes a single argument which then returns the middleware itself. This function (the criteria function), is one which you must provide, which determines whether actions are allowed to be dispatched. The function is passed two arguments, the current state of your store, and the action dispatched.

This signature will feel incredibly familiar to you if you if you have used Redux's reducers (which presumably you have or you probably wouldn't be reading this); the only difference is that instead of returning a new version of the state, you return false if the action is not permitted. You may return true to allow the function to be dispatched, but you may also return nothing at all, the middleware doesn't care about the return value of this function unless it is false. Let's look at an example:

const permitActions = (state, action) => {

    switch(action.type) {
        case 'DELETE_ALL_CUSTOMER_INFORMATION':
            if ('manager' != state.currentUser.role) return false;
            break;
        case 'INSERT_NEW_RECORD':
            if (state.records.entries.size() >= state.records.maxSize) return false;
    }
};

export default permitActions;

Obviously Redux Cut isn't meant to be a replacement for a real server validated permissions system (someone trying to delete all records could probably work out how to set their role to manager through the console), it can be handy to provide some immediate user feedback in cases where permissions might be denied at the server level. You may also use it for soft validation on non-destructive actions like attempting to exceed the maximum size of some data store. I'm sure you could come up with other uses too.

Combining Criteria

Much like Redux's reducers, you can also combine multiple criteria functions into a single function using the provided combineCriteria function:

action-criteria/index.js

import { combineCriteria } from 'redux-cut'
import playlistCriteria from './playlist'
import songCriteria from './song'

export default combineCriteria({
  playlistCriteria,
  songCriteria
})

App.js

import criteria from './action-criteria/index';

let store = createStore(reducer, initialState, applyMiddleware(cut(criteria), ...otherMiddlewares));

Listening for blocked actions

You can listen for blocked actions by importing the avilable isBlockedAction function:

import { isBlockedAction } from '../index';

if (isBlockedAction(action)) {
  return { modalVisible: true, message: 'Sorry you, can\'t do that!' }
}

Order of middleware

This one is fairly self explanatory, middleware applied before your Redux Cut will receive the original action, while middleware after will get the blocked action.

applyMiddleware(myLoggingMiddleware, cut(criteria), ...otherMiddlewares));

Here, myLoggingMiddleware will receive the action as-is, while all other middlewares will receive the blocked version.

Is it redux-devtools friendly?

You bet! Apply it before Devtools.instrument() and you'll see this in your monitor:

Like so, if you aren't completely familiar with the concept:

enhancer = compose(
    applyMiddleware(cut(criteria), APIMiddleware),
    DevTools.instrument()
);