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

@pennlabs/rest-hooks

v0.1.8

Published

React hooks for fetching resources at RESTful endpoints built on top of SWR.

Downloads

68

Readme

REST hooks

React hooks for fetching resources at RESTful endpoints built on top of SWR.

This package exposes two hooks, useResource and useResourceList. Both hooks have similar semantics to SWR, with one key improvement: generic, optimistic client-side updates!

They take advantage of the mutation and POST request pattern in the SWR docs to make local changes optimistic and feel more or less instantaneous.

Documentation

useResource

{data, error, isValidating, mutate } = useResource(url, { ...configOptions })

  • url: the URL of the resource in question.
  • configOptions are any of the options specified in the SWR documentation.
  • data, error, and isValidating are passed through directly from SWR.
  • mutate<T>(patched: Partial<T>, { ...mutateOptions }) The mutateOptions can include:
    • method ["POST"]: HTTP method to use when sending update to backend.

    • sendRequest [true]: Whether or not to update the backend.

    • revalidate [true]: Whether or not to request fully new data from the backend after updating the resource.

useResourceList

The most magic in this package comes when dealing with lists, where we're able to patch in updates and new elements with ease. It's important to keep in mind that useResourceList expects all elements returned from the JSON list to have an id field which uniquely identifies that element within the list. {data, error, isValidating, mutate } = useResourceList(listUrl, getResourceUrl, { ...configOptions })

  • listUrl: the URL of the resource list in question.
  • getResourceUrl: (id: string) => string: A dynamic mapping from an element's ID to the URL it can be located at for updates.
  • configOptions are any of the options specified in the SWR documentation.
  • data, error, and isValidating are passed through directly from SWR.
  • mutate<T>(id: Identifier, patched: Partial<T>, { ...mutateOptions })
    • id is the identifier for this element. If append is false and there is no matching id, the list will not be updated locally. if id is undefined, the request to the backend will not occur and standard SWR revalidation will take place. This is so the semantics for calls like mutate() still apply.
    • patched are the new / updated fields of this element. If patched is null, then the element with the given ID will be removed from the list.
    The mutateOptions can include:
    • method ["POST"]: HTTP method to use when sending update to backend.
    • sendRequest [true]: Whether or not to update the backend.
    • revalidate [true]: Whether or not to request fully new data from the backend after updating the resource.
    • append [false]: Toggle this flag to enable append mode. In append mode, the patched object will be added into the list as its own element if an element with that ID does not exist in the list already.
    • sortBy: A comparison function which will define how the list will be sorted after the call to mutate.