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

@colonel-sandvich/trpc-vue-query

v0.7.0

Published

A simple package to bridge the gap between [TRPC](https://trpc.io/) and [TanStack Query for Vue](https://tanstack.com/query/v5/docs/vue/overview) much like how TRPC has their own in-house [React Query Integration](https://trpc.io/docs/client/react)

Downloads

74

Readme

trpc-vue-query

A simple package to bridge the gap between TRPC and TanStack Query for Vue much like how TRPC has their own in-house React Query Integration

Why this package?

If you're using @tanstack/vue-query then you might know that working with query keys and query functions can sometimes become cumbersome. A lead maintainer of Tanstack Query, TkDodo, has said that "Separating QueryKey from QueryFunction was a mistake".

So this package tightly couples your keys and functions leading to brilliant DX :rocket:

Before:

const currentUserQuery = queryOptions({
  queryKey: ["user", "current"],
  queryFn: () => trpc.user.current.query(),
});

const { data } = useQuery(currentUserQuery);

const { mutateAsync } = useMutation({
  mutationFn: (input: UnwrapRef<typeof form>) => trpc.user.signUp.mutate(input),
  onSuccess: async () => {
    await useQueryClient().invalidateQueries({
      queryKey: currentUserQuery.queryKey,
    });
    await navigateTo("/onboarding");
  },
});

After:

const { data } = useClient().user.current.useQuery();

const { mutateAsync } = useClient().user.signUp.useMutation({
  onSuccess: async () => {
    await useClient().user.current.invalidate();
    await navigateTo("/onboarding");
  },
});

Install

pnpm i @colonel-sandvich/trpc-vue-query

Setup (Vue)

1. Plug in the plugin

// main.ts
import { TrpcVueQueryPlugin } from "@colonel-sandvich/trpc-vue-query";
import { VueQueryPlugin } from "@tanstack/vue-query";
import { httpBatchLink } from "@trpc/client";
import { createApp } from "vue";
import App from "./src/App.vue";
import { trpc } from "your-path-to-trpc-client";
// ^ See https://trpc.io/docs/client/vanilla/setup#3-initialize-the-trpc-client

export const app = createApp(App);

app
  .use(VueQueryPlugin) // Make sure {@tanstack/vue-query}'s plugin goes first
  .use(TrpcVueQueryPlugin, {
    trpcClient: trpc,
  })
  .mount("#app");

2. Make a composable

// src/composables/useClient.ts
import { TrpcVueClient, clientKey } from "@colonel-sandvich/trpc-vue-query";
import { inject } from "vue";
import type { AppRouter } from "your-path-to-trpc-app-router-type";

export function useClient() {
  return inject(clientKey) as TrpcVueClient<AppRouter>;
}

Setup (Nuxt)

0. Setup @tanstack/vue-query for Nuxt if you haven't already

// src/plugins/01.vueQueryPlugin.ts

// Important that this plugin comes before the `02.clientPlugin` since that has this plugin as a dependency
export default defineNuxtPlugin((nuxt) => {
  nuxt.vueApp.use(VueQueryPlugin);

  // Below is for SSR. Remove if you don't need this
  // Provided from TanStack Query docs: https://tanstack.com/query/v5/docs/vue/guides/ssr
  const vueQueryState = useState<DehydratedState | null>("vue-query");

  if (process.server) {
    nuxt.hooks.hook("app:rendered", () => {
      vueQueryState.value = dehydrate(queryClient);
    });
  }

  if (process.client) {
    nuxt.hooks.hook("app:created", () => {
      hydrate(queryClient, vueQueryState.value);
    });
  }
});

1. Make a plugin

// src/plugins/02.clientPlugin.ts

export default defineNuxtPlugin(() => {
  const trpc = createTRPCProxyClient<AppRouter>({
    links: [
      httpBatchLink({
        url: "/api/trpc",
        headers: useRequestHeaders(),
        fetch: customFetchWrapper(), // Crucial for SSR
      }),
    ],
  });

  const client = createTrpcVueClient(trpc, useQueryClient());

  return {
    provide: {
      client,
    },
  };
});

2. Make a composable

// src/composables/useClient.ts

export const useClient = () => {
  return useNuxtApp().$client;
};

You're Done!

Go check out /examples to see some basic uses.

Quickstart for testing examples/vue-minimal

pnpm i Anywhere

cd examples/vue-minimal

pnpm dev

If ports 3000 (client) and 3001 (server) are available then you should up and running

Goals of the project

  • [x] Easier integration with Vue and Nuxt
  • [ ] Feature parity with TRPC's React Query (or at least as much as is possible with Vue Query)
    • [ ] Subscriptions
  • [ ] Documentation

Contributing

Please please please absolutely make an issue or PR for any bugs or feature requests, I highly encourage it.

Acknowledgments

Big thanks to Robert Soriano for his trpc-nuxt package that inspired this package.