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

query-wrapper

v0.2.1

Published

Simple request state handling in component.

Downloads

15

Readme

Query Wrapper · GitHub license npm version npm version npm version

Spoiler alert: there is nothing similar/related to jQuery.

Query Wrapper is a TypeScript library based on React which can help you with handling server request states: loading, error or ready data state. The motivation has its roots in react-apollo Query React Component and useQuery React Hook. However, this library is oriented on RESTful web-services. Browser will care itself about the caching your data. When a web cache has a requested resource in its store, it intercepts the request and returns its copy instead of re-downloading from the originating server. You can find more information about HTTP caching here.

Installation

// using npm
npm i query-wrapper
// using yarn
yarn add query-wrapper

Documentation

<QueryProvider /> - React Context Provider. Optional global config provider.

Props:

  • axios - optional configured axios instance function, which will be used as a base for the requests. If it is not provided every component will use native fetch by default. Default value: null.

  • defaultOptions - object with options, which will be used in every request, unless they were are not overwritten. Default value: {}.


<Query /> - React Component. Used for making requests to a server. Optional.

Props:

  • options - object for specifying request options (e.g. url, method, body, etc.). Note: At least url property must be provided, if it was not - request won't be sent. Default value: {}.

useQuery - React Hook. Used for making requests to a server.

Props:

  • options - object for specifying request options (e.g. url, method, body, etc.). Note: At least url property must be provided, if it was not - request won't be sent. Default value: {}.

Examples

Wrapping the entire application into <QueryProvider /> is only useful when you have axios instance or default options you want be applied to all the requests. It's an optional step, you can ignore if you have do not have any props you want to provide. More examples you can find here: https://github.com/jeckvld/query-wrapper/tree/master/examples

import axios from 'axios';
import { QueryProvider } from 'query-wrapper';

const instance = axios.create({...});

const options = {
  credentials: "same-origin",
  headers: {
      "Content-Type": "application/json",
  }
};

export default function App() {
  return (
    <QueryProvider axios={instance} options={options}>
      ... // your application
    </QueryProvider>
  );
}

And then wrap your component which will deal with a server data:

import { Query } from 'query-wrapper';

const options = { url: '/api/user/info' };

export default function UserInfo() {
  return (
    <Query options={options}>
      {({ data, error, loading }) => {
        if (loading) {
          return ... // handle loading
        }

        if (error) {
          return ... // handle loading
        }

        return ... // handle response data
      }}
    </Query>
  );
}

Another option could be using useQuery hook instead:

import { useQuery } from 'query-wrapper';

const options = { url: '/api/user/info' };

export default function UserInfo() {
  const { data, loading, error } = useQuery(options);

  if (loading) {
    return ... // handle loading
  }

  if (error) {
    return ... // handle loading
  }

  return ... // handle response data
}