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

@appnest/typed-actions

v1.0.6

Published

A lightweight library to create and type check flux/redux actions in typescript

Downloads

148

Readme

@appnest/typed-actions

What is this?

typed-actions is a lightweight library to create and type check flux/redux actions in typescript.

The library doesn't come with a dispatcher, its only purpose is to type check actions.

:fire: Features

:sparkles: Type magic - Define actions with the least amount of work on your side.

:mag: Small - No dependencies. Gzipped size: 483 B.

:vertical_traffic_light: Built in status - All statuses come with either a START, SUCCESS or FAILURE status. This removes boilerplate for asynchronous action dispatching.

:sparkles: Type Magic

Using this library you avoid keeping track of uncomfortable many boilerplate interfaces in order to type check actions in typescript. Read the section without this library to see what typing hell you avoid.

This is based the following Typescript features, including features from 3.0: type guards, conditional types, union types, generic rest parameters and optional elements in tuple types.

:vertical_traffic_light: Built in status

All actions created using this library comes with a status. This status can be either START, SUCCESS or FAILURE. All actions must have a status because this become very handy when creating actions in an asynchronous flow. This way you avoid a lot of boilerplate (see without this library)

Example

Let's start with a general example of how you would use this library.

Here we use an async action creator. You only need to define this action creator once for a given action, then it can be used to easily create and typecheck actions with different statuses: START, SUCCESS and FAILURE. This is done using the methods start, success and failure on the async action creator.

import { defaultAsyncActionCreator, isAction, Action } from "@appnest/typed-actions";

// 1: Make an action creator
const listUsers = defaultAsyncActionCreator<string[]>("LIST_USERS");

// 2: Create an action
const action = listUsers.success(["John", "Jane"]);
// { type: "LIST_USERS/SUCCESS", payload: ["John", "Jane"], id: "LIST_USERS", status: "SUCCESS" }

// 3: Dispatch the action any way you like

// 4: Handle the action in a store
function handler (action: Action) {

  if (isAction(action, listUsers.success)) {
    // Payload is type of "string[]" in this scope.
    console.log(action.payload.join(", "));

  } else if (isAction(action, listUsers.failure)) {
    // Payload is type of "Error" in this scope.
    console.log(`Something went wrong: ${action.payload.message}`)
  }

}

Walkhrough

Step 1: Make an action creator

In order to generate type safe actions you will first need to make an action creator. actionCreator takes a generic parameter that becomes the type of the action payload.

import { actionCreator } from "@appnest/typed-actions";

const sendMsg = actionCreator<string>("SENG_MSG_ACTION");

Step 2: Create the action

The action creator can now be used to create actions with a payload of the specified type.

const action = sendMsg("Hello World");

Here's the content of this action.

{ 
  type: "MY_ACTION/SUCCESS",
  id: "MY_ACTION",
  status: "SUCCESS",
  payload: "Hello World",
  meta: undefined
}

You will notice that the status is "SUCCESS". All actions must have a status becauses this become very handy when spawning actions in an asynchronous flow. "SUCCESS" is the default status.

Step 3: Dispatch the action

The action can be dispatched any way you like (for example redux store.dispatch(action)). This library is only build for creating and typechecking actions, not for dispatching them.

Step 4: Handle the action

Now you are ready to check the action type. This is done using the isAction function.

import { isAction, Action } from "@appnest/typed-actions";

function handler (action: Action) {

  // Check if "action" is of type "sendMsg"
  if (isAction(action, sendMsg)) {

    // Type of "payload" is "string" in this scope
    console.log(action.payload); 

  }

}

Without this library

Here is an example of what you would have to do if you did not use this library. Typed actions are achieved using discriminated unions. In addition it's difficult to automate dispatching eg. failure actions because the status is baked into the type.

// 1: Make a bunch of interfaces
interface ListUserStartAction {
  type: "LIST_USER/START";
}

interface ListUserSuccessAction {
  type: "LIST_USER/SUCCESS";
  payload: string[];
}

interface ListUserFailureAction {
  type: "LIST_USER/FAILURE";
  payload: Error;
}

type Action = ListUserStartAction | ListUserSuccessAction | ListUserFailureAction;

// 2: Create an action
const action: Action = {
  type: "LIST_USER/SUCCESS",
  payload: ["John", "Jane"]
};

// 3: Dispatch the action

// 4: Handle the action
function handler (action: Action) {
  switch (action.type) {

    case "LIST_USER/SUCCESS":
      // Payload is type of "string[]" in this scope.
      console.log(action.payload.join(", "));
      break;

    case "LIST_USER/FAILURE":
      // Payload is type of "Error" in this scope.
      console.log(`Something went wrong: ${action.payload.message}`);
      break;
  }
}