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

v0.2.18

Published

Small typescript redux utility to improve typesafety between action creators and reducers.

Downloads

6

Readme

redux-keyable

Gitlab pipeline status Coverage Status David NPM npm Greenkeeper badge npm type definitions

Because type-safe redux should be easier.

Installation

npm i -s redux-keyable

Why redux-keyable?

Problem

Building an action creator in a type-safe manner is straight forward...

interface ActionCreatorParams {
  value: string;
}

const actionCreator = ({ value }: ActionCreatorParams) => ({
  type: ACTION_TYPE,
  value
}));

But later when we go to write our reducers we have a problem since our reducers will accept ALL of our actions.

interface State {
  storedValue: string;
}

const type AllActions = ???;

const reducer = (state: State, action: AllActions) => {
  // this gets messy fast
}

How do we easily type this?

People have solved this various ways:

Most solutions boil down to type-narrowing via a switch statement keying off a flux standard action type key.

switch (action.type) {
  case ADD:
    return [...state, action.payload];
  case TOGGLE:
    return state.map(item =>
      item.id === action.payload
        ? { ...item, completed: !item.completed }
        : item
    );
  default:
    return state;
}

Can we do better?

Solution

We first define a flux standard action type constant as usual.

const ACTION_TYPE = 'ACTION_TYPE';

Then we type our action using the FluxStandardAction helper interface. We'll use our action definition in both our reducer and our action creator.

interface Action extends FluxStandardAction<typeof ACTION_TYPE> {
  value: string;
}

We use our action definition to define the type of our action creator. The first generic argument is the type of arguments to pass to our creator, and the second is our action type. Passing in our types as generic arguments will help ensure that our action creator matches our reducer.

const actionCreator = createActionCreator<{ value: string }, Action>(
  ({ value }) => ({
    type: ACTION_TYPE,
    value
  })
);

When we go to create our reducers, we use the createKeyableReducer helper method and pass our state type and action type as generic parameters. Our action type and reducer are passed as arguments to the method. Our reducer will only ever get called for actions matching our specified type. No more giant switch statement, and we're still fully type safe!

interface State {
  storedValue: string;
}

const keyableReducer = createKeyableReducer<State, Action>(
  ACTION_TYPE,
  (state, { value }) => ({
    ...state,
    storedValue: value
  })
);

Then we finally combine all our keyable reducers acting on this state into one standard redux reducer. This can be used either as our root reducer or passed to utility methods like reduce-reducers.

// provide a default state
const defaultState = {
  storedValue: 'default'
};

const reducer = combineKeyableReducers<State>(defaultState)(
  keyableReducer,
  // we can pass additional keyable reducers here that will accept the same
  // state type
  reducerNotShownInExample
);

Basic Usage

Create our action interface and our action creator.

// changeDetailsAction.ts
import { createActionCreator, FluxStandardAction } from 'redux-keyable';

export interface DetailsUpdates {
  address: string;
  fullname: string;
}

export const CHANGE_DETAILS = 'CHANGE_DETAILS';

export interface ChangeDetailsAction
  extends FluxStandardAction<typeof CHANGE_DETAILS> {
  payload: DetailsUpdates;
}

export const changeDetails = createActionCreator<
  DetailsUpdates,
  ChangeDetailsAction
>(payload => ({
  payload,
  type: CHANGE_DETAILS
}));

Create our keyable reducer and our standard root reducer.

// changeDetailsReducer.ts
import { combineKeyableReducers, createKeyableReducer } from 'redux-keyable';
import { CHANGE_DETAILS, ChangeDetailsAction } from '../changeDetailsAction';
import { initialState, State } from '../stateDefinition';

export const changeDetailsReducer = createKeyableReducer<
  State,
  ChangeDetailsAction
>(CHANGE_DETAILS, (state, { payload }) => ({
  ...state,
  user: {
    ...state.user,
    ...payload
  }
}));

export const updateDetailsStandardRootReducer = combineKeyableReducers<State>(
  initialState
)(changeDetailsReducer);

License

Licensed under MIT