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

@rocketmakers/api-swr

v2.0.1

Published

Rocketmakers front-end library for parsing a generated Typescript API client into a set of configurable React hooks for fetching and mutating data.

Downloads

3,423

Readme

Rocketmakers - API SWR

TypeScript react vercel

@rocketmakers/api-swr is a TypeScript library that provides a convenient React wrapper around SWR state management library. It allows developers to easily interface with a TypeScript API client while managing client side state.


Installation

Install from npm using your package manage of choice:

  • npm install @rocketmakers/api-swr
  • pnpm add @rocketmakers/api-swr
  • yarn add @rocketmakers/api-swr

Core concepts

  1. Controller factory - one created for each API, used to create a "controller" for each tag within the API client.
  2. Controller - one created for each tag within the API client, used to create the endpoint hooks.
  3. Endpoint hook - a hook consumed by React components, used to retrieve data, run side effects, and manage cache.

Full Documentation

Setup

OpenAPI + Axios usage

Generic API client usage (NEW!)

Endpoint hooks

Complex queries

Advanced tools


Quick start

This quick start guide assumes you're working from a generated TypeScript OpenAPI client, don't worry if you're not though, it's dead easy to work from a hand written client, see here.

1. Wrap your app with the API SWR provider

To allow all endpoint hooks to read the global cache, your app should be wrapped with the API SWR provider.

NOTE: If you are an Armstrong user, it's useful to make sure the Armstrong provider is outside the API SWR provider. This will allow you to dispatch Armstrong toast from your API SWR processing hook.

import * as React from 'react';
import { ApiSwrProvider } from '@rocketmakers/api-swr';

export const App: React.FC<React.PropsWithChildren> = ({ children }) => {
  return <ApiSwrProvider>{children}</ApiSwrProvider>;
};

2. Create a controller factory

API SWR requires a controller factory for each API that you want to integrate. This factory is used for creating controllers. You should pass a base URL for the deployed API.

import { axiosOpenApiControllerFactory } from '@rocketmakers/api-swr';

export const apiFactory = axiosOpenApiControllerFactory({
  basePath: 'https://my.example.api/dev',
});

3. Create a controller

One controller should be created for each tag within the OpenAPI client. Tags are often used for splitting generated clients by backend controller, (e.g. "auth", "user", "product" etc.) If the generated client does not use tags, it will export a single class called DefaultApi, and your app will only need a single controller.

import { apiFactory } from "../controllerFactory.ts"
import { UserApi } from "example-api-client";

export const userApi = apiFactory.createAxiosOpenApiController("user", UserApi);

4. Create an endpoint hook

There are multiple hook types available (see full docs), but for this quick start guide, here's a simple useQuery hook for retrieving a user by ID.

export const useGetUser = (userId: string) => {
  return userApi.getUser.useQuery({
    cacheKey: 'userId',
    params: { userId }
  })
};

5. Consume your endpoint hook in a React component

Here's a simple React component which uses our endpoint hook to display a user card.

import * as React from 'react';
import { useGetUser } from '../state/controllers/user';

interface IProps {
  userId: string;
}

export const UserCard: React.FC<IProps> = ({ userId }) => {

  const { data } = useGetUser(userId);

  return (
    <div>
      <img src={data?.profilePic} />
      <h2>{data?.name}</h2>
    </div>
  );
}