New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

reshort

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reshort

A way to write less in our actions

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source


Why?

Sometimes we write too much repetition in our action creators.

For example:

export const fetchProducts = () => ({
  type: FETCH_PRODUCTS
})

export const fetchProductsSuccessful = payload => ({
  type: FETCH_PRODUCTS_SUCCESSFUL,
  payload
})

export const fetchProductsFailure = error => ({
  type: FETCH_PRODUCTS_FAILURE,
  payload: error
})

export const fetchUsers = () => ({
  type: FETCH_USERS
})

export const fetchUsersSuccessful = payload => ({
  type: FETCH_USERS_SUCCESSFUL,
  payload
})

export const fetchUsersFailure = error => ({
  type: FETCH_USERS_FAILURE,
  payload: error
})

You can see that we are repeating the same patterns in all our actions!

This library tries to remove this repetition encapsulating common actions in an action generator, like so:

import reshort from "reshort";

const productsActions = reshort("Products");
const usersActions = reshort("Users");

Install and usage

Install using your package manager:

npm install --save reshort

Then you can create your action creators in one line and use them after!

import reshort from "reshort";

const productsActions = reshort("Products");

productsActions("request")
// {
//   type: "GET_PRODUCTS"
// }

productsActions("success", {test: 123})
// {
//   type: "GET_PRODUCTS_SUCCESSFUL",
//   payload: { test: 123 }
// }

productsActions("fail", {test: "error"})
// {
//   type: "GET_PRODUCTS_FAILURE",
//   payload: { test: "error" }
// }

Options

prefix (String = "GET")

Add the defined prefix to the constants of the three actions.

const productsActions = reshort("Products", {
  prefix: "FETCH"
});

productsActions("request")
// {
//   type: "FETCH_PRODUCTS"
// }

productsActions("success", {test: 123})
// {
//   type: "FETCH_PRODUCTS_SUCCESSFUL",
//   payload: { test: 123 }
// }

productsActions("fail", {test: "error"})
// {
//   type: "FETCH_PRODUCTS_FAILURE",
//   payload: { test: "error" }
// }

successSuffix (String = "SUCCESSFUL")

Add the defined suffix to the constant of the "success" action.

const productsActions = reshort("Products", {
  successSuffix: "WITH_SUCCESS"
});

productsActions("success", {test: 123})
// {
//   type: "FETCH_PRODUCTS_WITH_SUCCESS",
//   payload: { test: 123 }
// }

failSuffix (String = "FAILURE")

Add the defined suffix to the constant of the "error" action.

const productsActions = reshort("Products", {
  failSuffix: "REJECTED"
});

productsActions("fail", {test: "error"})
// {
//   type: "FETCH_PRODUCTS_REJECTED",
//   payload: { test: "error" }
// }

customRequestPayload (Function, receives payload)

Applies a custom payload directly to the request action. Useful if you want to remove the payload variable name around your payload, or if you want to do some last minute tweaks to your payload.

const productsActions = reshort("Products", {
  customRequestPayload: payload => payload
});

productsActions("request", {test: 123});
// {
//    type: "GET_PRODUCTS",
//    test: 123
// }

customSuccessPayload (Function, receives payload)

Applies a custom payload directly to the successful action. Useful if you want to remove the payload variable name around your payload, or if you want to do some last minute tweaks to your payload.

const productsActions = reshort("Products", {
  customSuccessPayload: (payload) => payload
});

productsActions("success", {test: 123});
// {
//    type: "GET_PRODUCTS_SUCCESSFUL",
//    test: 123
// }

customFailurePayload (Function, receives payload)

Applies a custom payload directly onto the fail action. Useful if you want to remove the payload variable name around your payload, or if you want to do some last minute tweaks to your payload.

const productsActions = reshort("Products", {
  customFailurePayload: (payload) => payload
});

productsActions("fail", {test: "error"})
// {
//    type: "GET_PRODUCTS_FAILURE",
//    test: "error"
// }

namespace (String = "PRODUCTS")

Add the defined namespace to the constant of the action.

const productsActions = reshort("Items", {
  namespace: "PRODUCTS"
});

productsActions("request", {test: "namespace"})
// {
//   type: "PRODUCTS/GET_ITEMS",
//   payload: { test: "namespace" }
// }

License

MIT © Robson Porto

Keywords

reshort

FAQs

Package last updated on 29 Oct 2018

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