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

@codejamboree/action-builder

v1.1.0

Published

Create redux actions

Downloads

5

Readme

action-builder

This is inspired by https://www.npmjs.com/package/redux-saga-routines. The stages for fetch operations are used in the same way.

When working with a specific slice in a Redux Store, you can create and enforce a standard set of actions with a common prefix.

import actionBuilder from '@codejamboree/action-builder';

const build = actionBuilder('settings');
export const hide = build('HIDE');

console.log(changeName());
// { type: 'settings HIDE', payload: undefined }

Actions payloads can be typed.

export const rename = build<{name: string}>('RENAME');
// @ts-expect-error
const action = rename({name: 3});
// Valid
const action = rename({name: 'Lewis Moten'});
console.log(action.payload.name);
// Lewis Moten

Fetch Actions

Common stages for a fetch can be created as a group for consistency. The stages are trigger, request, success, error, and fulfill. Each stage may have its payload typed.

// default payload types
export const addItem = build.fetch('ADD_ITEM');

// Typed payloads
export const addItem = build.fetch<
  { name: string },            // trigger payload
  void,                        // request payload
  { id: number, name: string}, // success payload
  { error: string },           // error payload
  void,                        // fulfill payload
>('ADD_ITEM');

Dispatching fetch actions

You can dispatch the stages of a fetch request throughout your application.

const dispatch = useDipatch();

dispatch(addItem.trigger({name: 'Lewis Moten'}));
// same as addItem.trigger
dispatch(addItem({name: 'Lewis Moten'}));

dispatch(addItem.request());
dispatch(addItem.success({id: 42, name: 'Lewis Moten'}));
dispatch(addItem.failure({ error: 'Duplicate name' }));
dispatch(addItem.fulfill());

Lifecycle of Fetch actions in Saga

The various stages of fetch actions are most useful to the common lifecycles of redux saga fetch operations.

Of special note is that the upper-case key of each stage (TRIGGER, REQUEST, SUCCESS, FAILURE, FULFILL) provides a short-cut to the type.

export default takeEvery(

  addItem.TRIGGER, // or addItem.trigger.type

  function* worker(action) {
    yield put(addItem.request());
    try {
      const results = yield call(
        apiPost, 'addItem', action.payload
      );
      yield put(addItem.success(results))
    } catch (error) {
      yield put(addItem.failure({ error }));
    } finally {
      yield put(addItem.fulfill());
    }
  }
);

Handle Stages With Reducers

Reducers may use the upper-case keys as well to reduce the action into the current state.


const addItemRequest = produce((
  draft: ItemState,
  action: ReturnType<typeof addItem.request>
) => {
  draft.isLoading = true;
});

const addItemSuccess = produce((
    draft: ItemState,
    action: ReturnType<typeof addItem.success>
  ) => {
    const item = action.payload;
    draft.allIds.push(item.id);
    draft.byId[item.id] = item;
});

const addItemFulfill = produce((
  draft: ItemState,
  action: ReturnType<typeof addItem.fulfill>
) => {
  draft.isLoading = false;
});

handleActions({
  [addItem.REQUEST]: addItemRequest,
  [addItem.SUCCESS]: addItemSuccess,
  [addItem.FULFILL]: addItemFulfill
  },
  initialState
);

Progress

In addition to the stages for fetch operations, the action builder can also build progress/abort actions. Here is how to create progress actions, and combine them with fetch actions.

const readProgress = build.progress<
  { count: number, total: number }, // progress
  void,                             // abort
>('READ');

const readFetch = build.fetch<
  void,                             // trigger
  void,                             // request
  {id: number, name: string}[],     // success
  {error: string},                  // failure
  void                              // fulfill
>('READ');

export const read = Object.assign(readFetch, readProgress);

Drag & Drop

Basic Drag & Drop operations. Similar to the stages for fetch, the names of each operation have been kept to the same length, primarily for estetics & consistency.

const itemDnd = build.dnd<
  { dragId: number },                // Drag
  { dragId: number, dropId: number}  // Drop
  { dragId: number, dropId: number}  // Over
  { dragId: number }                 // Quit
  { dragId: number, dropId? number } // Pass
>('ITEM', 'DND');

// Drag Start
itemDnd.drag({ dragId: 1 });

// Dragging Over
itemDnd.over({ dragId: 1, dropId: 2 });

// Dragging Over invalid drop target
itemDnd.pass({ dragId: 1 });

// Cancel Drag and revert its effects
itemDnd.quit({ dragId: 1 });

// Drop
itemDnd.drop({ dragId: 1, dropId: 2});