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

@guanghechen/redux-actions

v1.9.8

Published

Utility funcs for handling blob objects (Such as download / create a blob from dataUrl)

Downloads

20

Readme

@guanghechen/redux-actions provides utility funcs for creating async actions, async state item and reducer.

Install

  • npm

    npm install --save @guanghechen/redux-actions
  • yarn

    yarn add @guanghechen/redux-actions

Usage

Types

  • Action

    /**
     * Action passed in redux Flow
      */
    export interface Action<T extends symbol | string, P extends unknown> {
      /**
       * Action type
        */
      type: T
      /**
       * Action payload
        */
      payload?: P
    }
  • AsyncActions

    /**
     * Async action passed in redux Flow
      */
    export interface AsyncAction<
      T extends symbol | string,
      P extends unknown,
      S extends AsyncActionStatus
      > extends Action<T, P> {
      /**
       * Action type
        */
      type: T
      /**
       * Status of request of async action
        */
      status: S
      /**
       * Action payload
        */
      payload?: P
    }
    • /**
       * Status of async action
        */
      export enum AsyncActionStatus {
        /**
         * Requested
          */
        REQUESTED = 'REQUESTED',
        /**
         * Request succeed
          */
        SUCCEED = 'SUCCEED',
        /**
         * Request failed
          */
        FAILED = 'FAILED',
      }
      
      /**
       * Response of failed request
        */
      export interface AsyncFailureResponse {
        /**
         * Error code
          */
        code: number
        /**
         * Error message
          */
        message: string
        /**
         * Debugging information
          */
        debug?: string
      }
  • AsyncStateItem

    /**
    * State item with data fetch/updated through async funcs
    */
    export interface AsyncStateItem<D> {
      /**
      * Whether is loading
      */
      loading: boolean
      /**
      * Data
      */
      data: D | null
      /**
      * Error message object
      */
      error: AsyncFailureResponse | null
    }

Utils

  • createActionCreator

    • /**
       * Create action creator
        * @param type              Action type
        * @param payloadRequired   Whether payload is required
        */
      export function createActionCreator<
        T extends symbol | string,
        P extends unknown
      >(type: T, payloadRequired: false)
        : (payload?: P) => Action<T, P>
      export function createActionCreator<
        T extends symbol | string,
        P extends unknown
      >(type: T, payloadRequired: true)
        : (payload: P) => Required<Action<T, P>>
      export function createActionCreator<
        A extends Action<symbol | string, unknown>
      >(
        type: A['type'], payloadRequired: false)
        : (payload?: A['payload']) =>
      export function createActionCreator<
        A extends Action<symbol | string, unknown>
      >(
        type: A['type'], payloadRequired: true)
        : (payload: A['payload']) => A
    • const UserCreator = createActionCreator<'@user/me', { name: string }>('@user/me', true)
      // => (payload: { name: string }) => ({ type: '@user/me', name })
  • createAsyncActionCreator

    • /**
       * Create async action types and async action creators
        * @param actionType
        */
      export function createAsyncActionCreator<
        T extends string | symbol,
        As extends AsyncActions<T>
      >(actionType: T): AsyncActionCreators<T, As>
    • // action for fetching user
      const FetchUserActionType = '@user/fetch'
      type FetchUserActionType = typeof FetchUserActionType
      type FetchUserActionRequestVo = { name: string }
      type FetchUserActionSucceedVo = { name: string, gender: 'male' | 'female' }
      type FetchUserActionFailedVo = AsyncFailureResponse
      
      const fetchUserActionCreators = createAsyncActionCreator<
        FetchUserActionType,
        AsyncActions<
          FetchUserActionType,
          FetchUserActionRequestVo,
          FetchUserActionSucceedVo,
          FetchUserActionFailedVo
        >(FetchUserActionType)
      
      // => fetchUserActionCreators = {
      //      request: (payload?: FetchUserActionRequestVo) => ({ type: '@user/fetch_user', status: 'REQUESTED', payload }),
      //      success: (payloadF: FetchUserActionSucceedVo) => ({ type: '@user/fetch_user', status: 'SUCCEED', payload }),
      //      failure: (payload?: FetchUserActionFailedVo) => ({ type: '@user/fetch_user', status: 'FAILED', payload }),
      //    }
  • createAsyncActionReducer

    • /**
       * Create reducer of async actions
        * @param actionType
        */
      export function createAsyncActionReducer<
        S extends AsyncStateItem<unknown>,
        T extends string | symbol,
        As extends AsyncActions<T>,
      >(
        actionType: T,
        handlers: {
          onRequestedAction?: AsyncActionHandler<S, T, As['request']>,
          onSucceedAction?: AsyncActionHandler<S, T, As['success']>,
          onFailedAction?: AsyncActionHandler<S, T, As['failure']>,
        } = {},
      ): AsyncActionReducer<S, T, As>
    • type UserStateData { name: string; gender: string }
      type UserState = AsyncStateItem<UserStateData>
      
      // fetch user action
      const FetchUserActionType = '@user/fetch'
      type FetchUserActionType = typeof FetchUserActionType
      type FetchUserActionRequestVo = { name: string }
      type FetchUserActionSucceedVo = { name: string, gender: 'male' | 'female' }
      type FetchUserActionFailedVo = AsyncFailureResponse
      
      const fetchUserActionReducer = createAsyncActionReducer<
        UserState,
        FetchUserActionType,
        AsyncActions<
          FetchUserActionType,
          FetchUserActionRequestVo,
          FetchUserActionSucceedVo,
          FetchUserActionFailedVo
        >(FetchUserActionType)
      
      // login action
      type LoginActionType = '@user/login'
      type LoginAction = Action<LoginActionType, { username: string }>
      
      // action for fetching user
      type UserActionTypes = FetchUserActionType | LoginActionType
      export const userReducer = assembleActionReducers<UserState, UserActionTypes>([
        fetchUserActionReducer,
        {
          actionType: '@user/login',
          process: (state: UserState, action: LoginAction) => ({
            ...state,
            name: action.payload.username,
          }),
        },
        // other action handlers
      ])
      
      // use userReducer in redux
      import { combineReducers } from 'redux'
      export const rootReducer = combineReducers({
        user: userReducer,
      })
  • createInitAsyncStateItem

    • /**
       * Create initial state item
        * @param data
        */
      export function createInitAsyncStateItem<D>(data?: D | null): AsyncStateItem<D>
    • export type UserStateData { name: string; gender: string }
      export type UserState = AsyncStateItem<UserStateData>
      export const initialUserState = createInitAsyncStateItem<UserStateData>({
        name: 'alice',
        gender: 'female',
      })
      
      // => initialUserState = {
      //      loading: false,
      //      data: { name: 'alice', gender: 'female' },
      //      error: null,
      //    }
  • assembleActionReducers

    • export function assembleActionReducers<
        S extends AsyncStateItem<unknown>,
        T extends string | symbol,
        R extends AsyncActionReducer<S, T, AsyncActions<T>>
          = AsyncActionReducer<S, T, AsyncActions<T>>
      >(
        initialState: S,
        actionReducers: R[],
      ): Reducer<S, AsyncActions<T, unknown>>
  • createAsyncAction

    • /**
       * Shorthand for create both AsyncActionCreator and AsyncActionReducer
        * @param actionType
        * @param handlers
        */
      export function createAsyncAction<
        S extends AsyncStateItem<unknown>,
        T extends string | symbol,
        As extends AsyncActions<T>,
      >(
        actionType: T,
        handlers?: {
          onRequestedAction?: AsyncActionHandler<S, T, As['request']>,
          onSucceedAction?: AsyncActionHandler<S, T, As['success']>,
          onFailedAction?: AsyncActionHandler<S, T, As['failure']>,
        },
      ): {
        creator: AsyncActionCreators<T, As>,
        reducer: AsyncActionReducer<S, T, As>
      }