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

@neovision/react-query

v2.9.14

Published

**Author** : Alan BLANCHET

Downloads

26

Readme

@neovision/react-query

Author : Alan BLANCHET

Installation

NPM

npm i @neovision/react-query

Add issues

You can add issues to : https://github.com/NeovisionSAS/react-query/issues

Examples

Components

Query Options Provider

<QueryOptionsProvider
  value={{
    domain: 'https://api.publicapis.org',
    parameterType: 'path',
    mode: 'development',
    headers: () => promiseReturningHeader,
  }}
>
  <CRUD endPoints={'entries'}>
    {({ read }) => {
      const { data, loading } = read;
      if (loading) return <div>Loading..</div>;

      return <div>{JSON.stringify(data)}</div>;
    }}
  </CRUD>
</QueryOptionsProvider>
Value prop

| Value | Type | description | | ------------- | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | | domain | string | The domain where the component needs to point to | | parameterType | "path" or "queryString" | Where the component should place the primary key in requests | | mode | "development" or "production" | The environment which is used to manage logging | | headers | () => Promise<HeadersInit \| undefined> | Before doing any request, this function is called. It is used if you need some sort of request to get a token before sending the real effective request | | idName | string | The name of the general parameter representing the default primary key name to use in request |

Go to the QueryOptionsProvider examples directory to see examples

Query

With query you can easily access the data you want. The Query object does a GET request for you and lets you know if the data is currently being retrieved (loading), if there was an error or what the received data is. You can type the received data if you want more structure. (for example here string[])

// Specify the API endpoint
<Query<string[]> query={`names/`}>
  {({data, loading, error}) => {
    // When the data is being retrieved
    if (loading) return <div>Loading ...</div>;
    // If an error occured
    if (error)
      return (
        <div>
          <div>Oops, there was an error</div>
          <div>{error}</div>
        </div>
      );

    // Whenever the data has been received
    return <div>{data}</div>;
  }}
</Query>

CRUD

The CRUD component is one of the best since it can handle all API operations and do it all in the background. It will give you, just like the Query component, an interface the retrieve the data and also some helper function to place in the onSubmit event of forms to create, update or delete the object

<CRUD<string> endPoints={`users/`}>
  {({ data }) => {
    const { read, handleUpdate } = data;
    if (read.loading) return <div>Loading...</div>;

    // On submit, the form will send a PUT request
    // { "name": "Gerald" } to the endpoint "server/endpoint/1/"
    // or
    // { "primary_key": 1, "name": "Gerald" } to the endpoint "server/endpoint/"
    // Depending on the parameterType of the QueryOptionsProvider ("path" or "queryString")
    return (
      <form
        onSubmit={(e) =>
          handleUpdate(e, { method: 'PUT', name: 'primary_key' })
        }
      >
        <input type={`number`} name={`primary_key`} value={1} />
        <input name="name" value={`Gerald`} />
      </form>
    );
  }}
</CRUD>

Go to the QueryOptionsProvider examples directory to see examples

Functions

request

The request function returns a promise with the data received from the endpoint.

Here is a quick example to show you how to use the request function :

request('mydomain.com', 'users/names').then((text) => {
  console.log(text);
});

request takes the domain as a first parameter, the path on the domain as a second parameter and finaly an options object to customize the request for your needs.

useRequest

You might most commonly use the useRequest hook since it will also retreive the data you passed to the QueryOptionsProvider component :

const UserList: FunctionComponent = () => {
  const [users, setUsers] = useState<any[]>();
  const request = useRequest();

  useEffect(() => {
    request('/users').then((users) => {
      setUsers(users);
    });
  }, []);

  return (
    <div>
      {users?.map((user: any) => {
        return <p>user.name</p>;
      })}
    </div>
  );
};

Unlike before, you don't need to pass the domain we already can detect which domain it is from the Provider. It enables to follow the DRY principle

Here are the available attributes used by the options object :

| Value | Type | description | | ------- | --------------------------------------------------- | ------------------------------------------------------------------------------------ | | headers | Promise<HeadersInit \| undefined> | A promise returning a headers object | | method | "GET", "POST", "PUT", "PATCH" or "DELETE" | The methods used in the requests to identify what you are trying to do on the server | | body | string | The content that you want to send to the server | | mode | "development" or "production" | Weither you are running in development or in production | | signal | AbortSignal or undefined | Send a signal to the request to stop it if you don't need the response anymore |