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

zustand-api-manager

v1.0.4

Published

A Zustand plugin for managing API states

Downloads

405

Readme

Zustand API Manager

npm version downloads GitHub

A powerful and flexible API state management solution built on top of Zustand.

Table of Contents

Installation

npm install zustand-api-manager

Features

  • Easy-to-use API state management
  • Built on top of Zustand for efficient state updates
  • Support for loading, success, and error states
  • Persistent state options
  • Middleware support for customizing API call behavior
  • Global error handling
  • TypeScript support with strong typing

Usage

Basic Usage

  1. First, enable the MapSet functionality in your main application file (e.g., index.js or App.js):
import { enableMapSet } from 'immer';

enableMapSet();
  1. Import the necessary functions:
import { useApiStore, useApiHandler, FetchStatus } from "zustand-api-store";
  1. Use the useApiHandler hook in your components:
interface UserData {
  id: number;
  username: string;
}

interface UserParams {
  id: number;
}

function MyComponent() {
  const { data, isLoading, isError, handleApi } =
    useApiHandler<UserData, UserParams>("user");

  const params = {
    id: 13
  }

  useEffect(() => {
    handleApi(
      () => fetchUserData(params), // API call
      {
        onSuccess: () => {
          console.log("User data fetched successfully!");
          // Add any other success logic here
        },
        onError: () => {
          console.error("An error occurred while fetching user data.");
          // Handle the error or show additional UI feedback here
        },
        persist: true;
      }
    );
  }, []);

  if (isLoading) return <div>Loading...</div>;
  if (isError) return <div>Error occurred</div>;

  return <div>{data?.name}</div>;
}

Advanced Usage

  1. Create a custom API composer for your specific API structure:
import { createApiComposer, ApiEndpoint } from "zustand-api-manager";

interface MyApiStructure {
  getUsers: ApiEndpoint<void, User[]>;
  getPost: ApiEndpoint<{ id: number }, Post>;

  // You have to add this to prevent TS error
  [key: string]: ApiEndpoint<any, any>;
}

export const useApi = createApiComposer<MyApiStructure>();
  1. Use the custom API composer in your components:
function UserProfile() {
  const { data: userData, isLoading, handleApi } = useApi("user");

  useEffect(() => {
    handleApi(() => fetchUserData({ id: 1 }));
  }, []);

  // Render component...
}

Global Loading

The useLoadingStates hook allows you to check the loading state of one or multiple API calls:

import { getLoadingStates } from "zustand-api-store";

function Dashboard() {
  // Check if any API is loading
  const isAnyLoading = getLoadingStates();

  // Check if specific APIs are loading
  const isUserOrPostsLoading = getLoadingStates(["user", "posts"]);

  // Check if a single API is loading
  const isUserLoading = getLoadingStates("user");

  return (
    <div>
      {isAnyLoading && <div>Loading something...</div>}
      {isUserOrPostsLoading && <div>Loading user or posts...</div>}
      {isUserLoading && <div>Loading user data...</div>}
      {/* Rest of your component */}
    </div>
  );
}

API Reference

useApiStore

The main store for managing API states. Provides the following methods:

  • setApiState: Update the state for a specific API key
  • resetApiState: Reset the state for a specific API key
  • handleApi: Handle an API call with automatic state management
  • getLoadingStates: Check loading states for one or more API keys
  • addMiddleware: Add middleware to customize API call behavior
  • addErrorHandler: Add a global error handler

useApiHandler

A hook for managing individual API calls. Returns an object with:

  • isLoading: Boolean indicating if the API is currently loading
  • isError: Boolean indicating if an error occurred
  • isSuccess: Boolean indicating if the API call was successful
  • data: The data returned from the API call
  • error: Any error that occurred during the API call
  • handleApi: Function to trigger the API call
  • resetApi: Function to reset the API state

createApiComposer

A function to create a strongly-typed API composer for your specific API structure.

FetchStatus

An enum representing the different states of an API call:

  • IDLE
  • LOADING
  • SUCCESS
  • ERROR

Middleware and Error Handling

You can add custom middleware and error handlers to customize the behavior of your API calls:

const apiStore = useApiStore.getState();

// Add middleware
apiStore.addMiddleware((next) => async (key, apiCall, options) => {
  console.log(`API call started: ${key}`);
  await next(key, apiCall, options);
  console.log(`API call finished: ${key}`);
});

// Add error handler
apiStore.addErrorHandler((error, key) => {
  console.error(`Error in API call ${key}:`, error);
});

TypeScript Support

This package is written in TypeScript and provides strong typing out of the box. Use the createApiComposer function to create a strongly-typed API composer for your specific API structure.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.