DEV Community

Discussion on: Who's looking for open source contributors? (October 15th edition)

Collapse
 
robbporto profile image
Robson Porto • Edited

I hope I'm not too late!

Reshort - write less in your actions!

Intro

Hi, everyone! I developed this simple little lib (my first lib!) to help me reduce some repetition in my actions. I have some ideas of new implementations and I just want to see if anyone is interested in this at all or find this project interesting and useful. If you have any constructive critiques I would be very pleased to hear it. If you have some interest in the project and can help, it would be awesome!

The problem:

Let's say that I want to fetch a list of books. Using redux, I have to create three actions:

  1. the first one is the "REQUEST" action;
  2. the second one is the "SUCCESS" action;
  3. the third one is the "FAIL" action.

Now let's say that I want to fetch a list of authors. Or a list of users. Or a list of bookmarks... This pattern of REQUEST-SUCCESS-FAIL can become very repetitive and verbose.

The solution

Using reshort you can create one "complete action" using one line, instead of three. Like so:

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" }
// }
Collapse
 
gerbilsinspace profile image
Joseph Abell

This is a nice simple idea, and a great candidate for your first lib - I certainly have felt there being a bit too much boilerplate code for redux in the past.

One piece of functionality I'd like is to let the user define how they want their payloads to render. I like defining my values explicitly rather than having a payload value that could have anything inside it.

I'd consider doing this by letting the user pass a method that adds the data to the type... actually, I'm going to create an MR while I think this through... :D

Collapse
 
robbporto profile image
Robson Porto

Hey, Joseph! Thanks for taking the time to contribute!