|
| 1 | +--- |
| 2 | +title: Vue Colada |
| 3 | +description: Simplify oRPC usage with minimal integration Pinia Colada |
| 4 | +--- |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +```package-install |
| 9 | +@orpc/client @orpc/vue-colada @pinia/colada |
| 10 | +``` |
| 11 | + |
| 12 | +## Setup |
| 13 | + |
| 14 | +```ts twoslash |
| 15 | +import { createORPCVueColadaUtils } from '@orpc/vue-colada'; |
| 16 | +import { createORPCClient } from '@orpc/client'; |
| 17 | +import { ORPCLink } from '@orpc/client/fetch'; |
| 18 | +import type { router } from 'examples/server'; |
| 19 | + |
| 20 | +const orpcLink = new ORPCLink({ |
| 21 | + url: 'http://localhost:3000/api', |
| 22 | + // fetch: optional override for the default fetch function |
| 23 | + // headers: provide additional headers |
| 24 | +}); |
| 25 | + |
| 26 | +// Create an ORPC client |
| 27 | +export const client = createORPCClient<typeof router>(orpcLink); |
| 28 | + |
| 29 | +// Create Vue Query utilities for ORPC |
| 30 | +export const orpc = createORPCVueColadaUtils(client); |
| 31 | + |
| 32 | +// @noErrors |
| 33 | +orpc.getting. |
| 34 | +// ^| |
| 35 | +``` |
| 36 | + |
| 37 | +## Multiple ORPC Instances |
| 38 | + |
| 39 | +To prevent conflicts when using multiple ORPC instances, you can provide a unique base path to `createORPCVueColadaUtils`. |
| 40 | + |
| 41 | +```tsx twoslash |
| 42 | +import { createORPCVueColadaUtils } from '@orpc/vue-colada' |
| 43 | + |
| 44 | +// Create separate ORPC instances with unique base paths |
| 45 | +const userORPC = createORPCVueColadaUtils('fake-client' as any, ['__user-api__']) |
| 46 | +const postORPC = createORPCVueColadaUtils('fake-client' as any, ['__post-api__']) |
| 47 | +``` |
| 48 | + |
| 49 | +This ensures that each instance manages its own set of query keys and avoids any conflicts. |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +### Standard Queries and Mutations |
| 54 | + |
| 55 | +```ts twoslash |
| 56 | +import { useMutation, useQuery, useQueryCache } from '@pinia/colada' |
| 57 | +import { orpc } from 'examples/vue-colada' |
| 58 | + |
| 59 | +// Fetch data |
| 60 | +const { data: gettingData } = useQuery(orpc.getting.queryOptions({ input: { name: 'unnoq' } })) |
| 61 | + |
| 62 | +// Perform mutations |
| 63 | +const { mutate: postMutate } = useMutation(orpc.post.create.mutationOptions()) |
| 64 | + |
| 65 | +// Invalidate queries |
| 66 | +const queryCache = useQueryCache() |
| 67 | +queryCache.invalidateQueries({ key: orpc.key() }) // Invalidate all queries |
| 68 | +queryCache.invalidateQueries({ key: orpc.post.find.key({ input: { id: 'example' } }) }) // Specific queries |
| 69 | +``` |
| 70 | + |
| 71 | +> **Note**: This library enhances [Pinia Colada](https://pinia-colada.esm.dev/) by managing keys and functions for you, providing a seamless developer experience. |
0 commit comments