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

resource-toolkit

v1.3.0

Published

Async and RESTful resource management tool.

Downloads

74

Readme

About

Often we consume RESTful APIs, like for a CRUD, using reactive UIs (with React or not). And there are a few state scenarios always present and our UI demands some questions to be answered:

  • Loading state
    • Is the loading a creation request and you want to disable or hide the creation form buttons?
    • Is the loading a reading operation and only a blinking skeleton is supposed to fill that table?
    • Or maybe you're updating a single row and you only want to disable that specific row action buttons with a cute spinning instead of disabling that creation form?
    • What if the loading is actually a deleting action?
    • And if all of that is happening at the same time?
    • Did you consider these loadings to have a minimal (and pleasant) delay?
  • Easy feedback for no data found
  • Stacking error messages
  • Repetitive fetching messages
    • For example, success messages for when something was successfully deleted
    • Another example, an error message when some specific request HTTP verb failed
  • Data fetching itself
    • Bored of having to write the same filter when you remove elements for your data?
    • Or tired of writing the same map for editing?
    • Or being confused with all that spread and rest operations for adding new stuff?

While this is not a UI lib, it's a state helper to automate all of these state changes above. It's type safed, has high test coverage (written with TDD). Your code commits using this for repetitive CRUDs will be short, readable, safer by pure automation. You'll be free to focus on crafting other complex designed behaviours.

This lib is a composition mostly pure functions (based on Reducer pattern), so It's also supposed to be easily integrated on any state manager you're using, like Redux, MobX or just raw React Hooks (or even class-based life cycle methods).

Install

npm install --save resource-toolkit

Example

You may already see a running To Do List application here, crafted for didactic reasons with a friend: https://github.com/Mazuh/octo-todo (warning: it is currently using an old version unstable version)

Usage

Here are a few dumb examples in React.

Simple example for fetching all

import React from "react";
import { makeReducerAssets } from 'resource-toolkit';

const usersResource = makeReducerAssets({
  name: 'user',
  idKey: 'userId',
  gateway: {
    fetchMany: async (ids = null, ...args) => {
      return [
        { userId: 42, name: 'Marcell' },
        { userId: 11, name: 'David' },
        { userId: 22, name: 'Rodrigo' },
      ];
    },
  },
});

export default function App() {
  const [users, dispatch] = React.useReducer(usersResource.reducer, usersResource.initialState);

  React.useEffect(() => {
    usersResource.actions.readAll()(dispatch);
  }, [dispatch]);

  if (users.isLoading) {
    return <p>Doing something...</p>;
  }

  if (users.items.length === 0) {
    return <p>No users found.</p>;
  }

  return (
    <div>
      <ul>
        {users.items.map(user => (
          <li key={user.userId}>{user.name}</li>
        ))}
      </ul>
    </div>
  );
}

More complete usage demonstration

Check it out on CodeSandbox: https://codesandbox.io/s/resource-toolkit-usage-7h9td?fontsize=14&hidenavigation=1&theme=dark

Feel free to fork it and test the features by yourself.

Contributing

Please consult CONTIRBUTING for guidelines on contributing to this project.

Author

resource-toolkit © Mazuh, Released under the MIT License.