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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cytools/vue-query

v0.1.1

Published

Introducing elegant way of making api requests and asynchronous functions with the composition api in Vue

Readme

How to install

The installation is very simple. Just install the package with your favourite package manager:

NPM

npm install --save @cytools/vue-query

Yarn

yarn add @cytools/vue-query


And you are all set. Now comes the fun part.

image

How to use

Simple

You can use vue-query wherever you want. But it does require the installation of vue@next or @vue/composition-api.

import { useQuery } from '@cytools/vue-query';

const { data, isLoading } = useQuery(
    'luke-man',
    async () => {
        const response = await fetch('https://swapi.dev/api/people/1');
        const data = await response.json();

        return data;
    }
);

console.log(data.value.name); // Should Print "Luke Skywalker"

Simple but with key change

You can pass an array as the key of the query and in that array you can put a Ref object from vue. If the ref changes the query will be refetched if the generated key is not in the cache.

import { ref, watch } from '@vue/composition-api'; // you can change composition api to vue
import { useQuery } from '@cytools/vue-query';

const personId = ref(1);
const { data, isLoading } = useQuery(
    ['star-wars-person', personId], // => this generates a key something like this ['star-wars-person', {value: 1}] 
    async (currentPersonId) => {
        const response = await fetch(`https://swapi.dev/api/people/${currentPersonId}`);
        const data = await response.json();

        return data;
    }
);

console.log(data.value.name); // Should Print "Luke Skywalker"

personId.value = 2; // this will update the key of the query and refetch with a different person

watch(data, (newPerson) => {
    console.log(newPerson.name) // => Should print C-3PO
});

Since the queries are cached, if you change the personId.value to 1, it will not refetch, but it will retrieve the data from the cache.

personId.value = 1;

// after event loop

console.log(data.value.name); // => Should print Luke Skywalker

Pagination

To take advantage of pagination, you can use useQuery, but everytime you change the page key it will flicker as it removes old data while fetching for the new one. You can disable this behaviour with the option keepPreviousData

import { ref } from '@vue/composition-api'; // you can change composition api to vue
import { useQuery } from '@cytools/vue-query';

const page = ref(1);
const { data } = useQuery(
    ['star-wars-people', page],
    async (currentPage) => {
        const response = await fetch(`https://swapi.dev/api/people/${currentPage}`);
        const data = await response.json();

        return data;
    },
    {
        keepPreviousData: true,
    }
);

But there is a better version of this with some helpers returned. It returns a bunch of helpers for pagination and it handles the change of page logic inside it. The only additional thing to be done is to update the return data from the query.

import { usePaginateQuery } from '@cytools/vue-query';

const {
    data,
    currentPage,
    hasMorePages,
    fetchPrevPage,
    fetchNextPage,
    isPrevButtonActive,
    isNextButtonActive,
    canShowPaginationButtons,
} = usePaginateQuery(
    'star-wars-people',
    async (currentPage) => {
        const response = await fetch(`https://swapi.dev/api/people/${currentPage}`);
        const data = await response.json();

        return { data, hasNextPage: false };
    }
);
  • canShowPaginationButtons: It returns true only if we have more than one pages. It is useful if you do not want to show the pagination buttons if the returned data doesn't have more than only one page.

  • currentPage: It returns the current page the pagination query is on.

  • fetchPrevPage: Gets the previous page.

  • fetchNextPage: Gets the next page.

  • isPrevButtonActive: Returns true if we can go back a page.

  • isNextButtonActive: Returns true if we can go to the next page.

How to mutate

This one is a very simple step, if you want to update the name of Luke Skywalker from the query cache, you can do that with a mutation.

import { useQuery, useQueryClient, useMutation } from '@cytools/vue-query';

const { data, isLoading } = useQuery(
    'luke-man',
    async () => {
        const response = await fetch('https://swapi.dev/api/people/1');
        const data = await response.json();

        return data;
    }
);

console.log(data.value.name); // Should Print "Luke Skywalker"

// here we are going to change the name of Luke Skywalker
const { queryClient } = useQueryClient();
useMutation(
    () => { name: 'Anakin Skywalker' },
    {
        onSuccess: (response) => {
            // the update data accepts a callback
            // that passes the data we have in the cache
            queryClient.getQuery('luke-man')
                .updateData(data => ({ ...data, name: response.name }));
        }
    }
);

This can also be achieved without the query client

import { useQuery, useMutation } from '@cytools/vue-query';

const { data, isLoading, updateQueryData } = useQuery(
    'luke-man',
    async () => {
        const response = await fetch('https://swapi.dev/api/people/1');
        const data = await response.json();

        return data;
    }
);

console.log(data.value.name); // Should Print "Luke Skywalker"

// here we are going to change the name of Luke Skywalker
const { queryClient } = useQueryClient();
useMutation(
    () => { name: 'Anakin Skywalker' },
    {
        onSuccess: (response) => {
            // Here we pass directly function returned fron useQuery
            // take note that if you have a changeable key, this would update
            // the query cache of the latest key
            updateQueryData(data => ({ ...data, name: response.name }));
        }
    }
);