🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

hideaway

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hideaway

Hideaway middleware for Redux

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Hideaway Middleware build status NPM

This middleware helps to standardize and reduce the code when you use stages (Request, Response, Error) or use redux with nested path.

Installation

npm install hideaway

or

yarn add hideaway

Why do I need this?

If you want to standardize the use redux and/or redux-thunk inside react with react-redux.

It is useful to work with calls to API and handling loading actions. (Require redux-thunk)

Work with nested path inside the reducer.

Examples

This is an interactive version of the code that you can play with online.

Settings

Store

Then, to enable hideaway, use applyMiddleware():

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { hideaway } from 'hideaway';

const middleware = [hideaway(), thunk];
createStore(reducers, applyMiddleware(...middleware));

Composition

Simple (without use of thunk)

action.js

export const incrementAction = () => ({
  type: 'INCREMENT',
});

export const decrementAction = () => ({
  type: 'DECREMENT',
});

reducer.js

import { combineReducers } from 'redux';
import { ReducerManagement } from 'hideaway';

const reducerManagement = new ReducerManagement({
  initialState: 0,
});

const counterReducers = reducerManagement.combine({
  INCREMENT: (state) => state + 1,
  DECREMENT: (state) => state - 1,
});

export const reducers = combineReducers({ counter: counterReducers });

selector.js

import { getValue } from 'hideaway';

export const getCounter = (state) => {
  return getValue(state, {
    path: ['counter'],
  });
};

API (use of redux-thunk)

action.js

import { generateAction } from 'hideaway';

export const getScore = () =>
  generateStateManagerAction('FETCH_SCORE', () => fetch('http://<HOST>'));

// ...

reducer.js

import { combineReducers } from 'redux';
import { ReducerManagement } from 'hideaway';

const reducerManagement = new ReducerManagement({
  initialState: 0,
  isStateManager: true,
});

const counterReducers = reducerManagement.combine({
  INCREMENT: (state) => state + 1,
  DECREMENT: (state) => state - 1,
});

export const reducers = combineReducers({ counter: counterReducers });

selector.js

import { getState } from 'hideaway';

export const getCounter = (state) => {
  return getState(state, {
    path: ['counter'],
  });
};

Combinations

Check the examples to see the variations.

Documentation

hideaway

The method enables to set the following settings:

parameterdescription
onErrorIt calls when an error on API occurs. It receives the action with the response inside the payload.
withExtraArgumentInject a custom argument to be on API calls.
const onError = (action) => console.log(action);
const secret = 42;

const store = createStore(
  reducer,
  applyMiddleware(hideaway({ onError, withExtraArgument: { secret } }), thunk),
);

// later
export const fetchUser = (id) =>
  generateAction('FETCH_USER', (dispatch, getState, extraArg) => {
    // API call
  }));

generateAction

Format the action to be readable to the hideaway.

parameterdescription
typeProperty that indicates the type of action being performed
apiFunction that returns a promise. The function receive (dispatch, getState, extra) from the middleware.
optionsAdditional settings (see the attributes below)

options

parameterdescription
apiPreReducerIt receives the body after the api call and expect a result that will send to the reducer.
keysIt is used to generate the nest path.
pathIt is used with keys to generate the nested path. If the keys match with an item inside the path, the value of the key will replace it.
allObjectIt returns the object instead the value from the nested path.
complementA complement for the action, to be used inside the reducer.
predicateSkip the fetch if predicate is false.
onErrorIt calls when an error on API occurs. It receives the action with the response inside the payload.
isStateManagerDefine how to handle the api request. The default is false (It handles REQUEST, RESPONSE, ERROR).
export const fetchUser = (id) =>
  generateAction('FETCH_USER', (dispatch, getState, extraArg) => {
    // API call
  }, {
    // options here
  }));

generateStateManagerAction

Format the action to be readable to the hideaway and request to use the state manager feature.

parameterdescription
typeProperty that indicates the type of action being performed
apiFunction that returns a promise. The function receive (dispatch, getState, extra) from the middleware.
optionsAdditional settings (see the attributes below)

options

parameterdescription
apiPreReducerIt receives the body after the api call and expect a result that will send to the reducer.
keysIt is used to generate the nest path.
pathIt is used with keys to generate the nested path. If the keys match with an item inside the path, the value of the key will replace it.
allObjectIt returns the object instead the value from the nested path.
complementA complement for the action, to be used inside the reducer.
predicateSkip the fetch if predicate is false.
onErrorIt calls when an error on API occurs. It receives the action with the response inside the payload.
isStateManagerDefine how to handle the api request. The default is true (It handles REQUEST, RESPONSE, ERROR).
export const fetchUser = (id) =>
  generateStateManagerAction('FETCH_USER', (dispatch, getState, extraArg) => {
    // API call
  }, {
    // options here
  }));

ReducerManagement

parameterdescription
initialStateSset an initial state for the reducer. For nested that doesn't set the nested, it will assing to a root key
displayErrorIt display the error on console if the fetch fails.
isNestedIt enables nested path. (Default: false)
nestedSettings necessary if it sets isNested. (*)
isStateManagerIt enables the state manager for API use. (default: false)

(*) The setting will use with initialState for the fist time only.

nested

parameterdescription
keysIt is used to generate the nest path.
pathIt is used with keys to generate the nested path. If the keys match with an item inside the path, the value of the key will replace it.

ReducerStateManagement

parameterdescription
initialStateSset an initial state for the reducer. For nested that doesn't set the nested, it will assing to a root key
displayErrorIt display the error on console if the fetch fails.
isNestedIt enables nested path. (Default: false)
nestedSettings necessary if it sets isNested. (*)
isStateManagerIt enables the state manager for API use. (default: true)

(*) The setting will use with initialState for the fist time only.

nested

parameterdescription
keysIt is used to generate the nest path.
pathIt is used with keys to generate the nested path. If the keys match with an item inside the path, the value of the key will replace it.

getValue

Retrieve the value from state

parameterdescription
stateThe state container
optionsAdditional settings (see the attributes below)

options

parameterdescription
pathIt is used to find the initial path. (nested path is the complement)
defaultValueValue to return if doesn't find the path or the value is null. (default: null)
nestedSee nested.
isStateManagerInform to return the loading, value and error when the result is empty or null. (default: false)

getState

Retrieve the value from state using the state manager format

parameterdescription
stateThe state container
optionsAdditional settings (see the attributes below)

options

parameterdescription
pathIt is used to find the initial path. (nested path is the complement)
defaultValueValue to return if doesn't find the path or the value is null. (default: null)
nestedSee nested.
isStateManagerInform to return the loading, value and error when the result is empty or null. (default: true)

Keywords

react

FAQs

Package last updated on 13 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts