Skip to content

Commit 999d654

Browse files
authored
feat(svelte-query): Tanstack Query integration for Svelte (#229)
closes: https://github.com/unnoq/orpc/issues/136 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Expanded framework support to include Svelte with integrated query utilities. - Introduced a dedicated SvelteKit playground for interactive exploration. - **Documentation** - Updated multiple user guides and integration references to showcase Svelte support. - Enhanced navigation menus with Svelte-specific entries. - **Tests** - Added comprehensive tests to verify Svelte Query integrations and utilities. - **Chores** - Updated dependencies and configurations to support the new Svelte ecosystem. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent e98b833 commit 999d654

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2819
-509
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
- **Contract-First Development πŸ“œ**: (Optional) Define your API contract upfront and implement it with confidence.
3333
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
3434
- **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35-
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid), Pinia Colada, and more.
35+
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
3636
- **Server Actions ⚑️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
3737
- **Standard Schema Support πŸ—‚οΈ**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
3838
- **Fast & Lightweight πŸ’¨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
@@ -56,6 +56,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
5656
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
5757
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
5858
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
59+
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
5960
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
6061
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
6162
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.

β€Žapps/content/.vitepress/config.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export default defineConfig({
129129
{ text: 'React', link: '/docs/tanstack-query/react' },
130130
{ text: 'Vue', link: '/docs/tanstack-query/vue' },
131131
{ text: 'Solid', link: '/docs/tanstack-query/solid' },
132+
{ text: 'Svelte', link: '/docs/tanstack-query/svelte' },
132133
],
133134
},
134135
{

β€Žapps/content/docs/playgrounds.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ featuring pre-configured examples accessible instantly via StackBlitz or local s
1818
| OpenAPI with Contract Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/contract-openapi?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/contract-openapi/src/router/index.ts) |
1919
| Express.js Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/expressjs?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/expressjs/src/router/index.ts) |
2020
| Solid Start Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/solid-start?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/solid-start/src/router/index.ts) |
21+
| Svelte Kit Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/svelte-kit?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/svelte-kit/src/router/index.ts) |
2122

2223
:::warning
2324
StackBlitz has own limitations, so some features may not work as expected.
@@ -35,6 +36,7 @@ npx degit unnoq/orpc/playgrounds/openapi orpc-openapi-playground
3536
npx degit unnoq/orpc/playgrounds/contract-openapi orpc-contract-openapi-playground
3637
npx degit unnoq/orpc/playgrounds/expressjs orpc-expressjs-playground
3738
npx degit unnoq/orpc/playgrounds/solid-start orpc-solid-start-playground
39+
npx degit unnoq/orpc/playgrounds/svelte-kit orpc-svelte-kit-playground
3840
```
3941

4042
For each project, set up the development environment:

β€Žapps/content/docs/tanstack-query/basic.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description: Seamlessly integrate oRPC with Tanstack Query
1313
| Vue | βœ… | βœ… |
1414
| Angular | βœ… | [Vote here](https://github.com/unnoq/orpc/issues/157) |
1515
| Solid | βœ… | βœ… |
16-
| Svelte | βœ… | [Vote here](https://github.com/unnoq/orpc/issues/136) |
16+
| Svelte | βœ… | βœ… |
1717

1818
::: warning
1919
This documentation assumes you are already familiar with [Tanstack Query](https://tanstack.com/query/latest). If you need a refresher, please review the official Tanstack Query documentation before proceeding.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Tanstack Query Integration For Svelte
3+
description: Seamlessly integrate oRPC with Tanstack Query for Svelte
4+
---
5+
6+
# Tanstack Query Integration For Svelte
7+
8+
This guide shows how to integrate oRPC with Tanstack Query for Svelte. For an introduction, please review the [Basic Guide](/docs/tanstack-query/basic) first.
9+
10+
## Installation
11+
12+
::: code-group
13+
14+
```sh [npm]
15+
npm install @orpc/svelte-query@latest @tanstack/svelte-query@latest
16+
```
17+
18+
```sh [yarn]
19+
yarn add @orpc/svelte-query@latest @tanstack/svelte-query@latest
20+
```
21+
22+
```sh [pnpm]
23+
pnpm add @orpc/svelte-query@latest @tanstack/svelte-query@latest
24+
```
25+
26+
```sh [bun]
27+
bun add @orpc/svelte-query@latest @tanstack/svelte-query@latest
28+
```
29+
30+
```sh [deno]
31+
deno install npm:@orpc/svelte-query@latest npm:@tanstack/svelte-query@latest
32+
```
33+
34+
:::
35+
36+
## Setup
37+
38+
Before you begin, ensure you have already configured a [server-side client](/docs/client/server-side) or a [client-side client](/docs/client/client-side).
39+
40+
```ts twoslash
41+
import { router } from './shared/planet'
42+
import { RouterClient } from '@orpc/server'
43+
declare const client: RouterClient<typeof router>
44+
// ---cut---
45+
import { createORPCSvelteQueryUtils } from '@orpc/svelte-query'
46+
47+
export const orpc = createORPCSvelteQueryUtils(client)
48+
49+
orpc.planet.find.queryOptions({ input: { id: 123 } })
50+
// ^|
51+
52+
//
53+
```
54+
55+
## Avoiding Query/Mutation Key Conflicts
56+
57+
Prevent key conflicts by passing a unique base key when creating your utils:
58+
59+
```ts
60+
const userORPC = createORPCSvelteQueryUtils(userClient, {
61+
path: ['user']
62+
})
63+
const postORPC = createORPCSvelteQueryUtils(postClient, {
64+
path: ['post']
65+
})
66+
```
67+
68+
## Reactivity
69+
70+
To create reactive queries, use Svelte's legacy `derived` API from `svelte/store`. With the [Tanstack Svelte v5 branch](https://github.com/TanStack/query/discussions/7413), oRPC should work out of the box.
71+
72+
```ts twoslash
73+
import type { router } from './shared/planet'
74+
import type { RouterClient } from '@orpc/server'
75+
import type { RouterUtils } from '@orpc/svelte-query'
76+
declare const orpc: RouterUtils<RouterClient<typeof router>>
77+
declare const condition: boolean
78+
// ---cut---
79+
import { createQuery } from '@tanstack/svelte-query'
80+
import { derived, writable } from 'svelte/store'
81+
82+
const id = writable(123)
83+
84+
const query = createQuery(
85+
derived(id, $id => orpc.planet.find.queryOptions({ input: { id: $id } })),
86+
)
87+
```

β€Žapps/content/index.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ features:
3737
details: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
3838
- icon: 🧩
3939
title: Framework Integrations
40-
details: Supports Tanstack Query (React, Vue, Solid), Pinia Colada, and more.
40+
details: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
4141
- icon: ⚑
4242
title: Server Actions
4343
details: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.

β€Žapps/content/package.jsonβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
"@pinia/colada": "^0.13.6",
1919
"@tanstack/react-query": "^5.66.4",
2020
"@tanstack/solid-query": "^5.66.4",
21+
"@tanstack/svelte-query": "^5.66.4",
2122
"@tanstack/vue-query": "^5.66.4",
2223
"next": "^15.1.7",
2324
"openai": "^4.85.4",
24-
"pinia": "^2.3.0"
25+
"pinia": "^2.3.0",
26+
"svelte": "^5.0.0"
2527
},
2628
"devDependencies": {
2729
"@shikijs/vitepress-twoslash": "^2.5.0",

β€Žpackage.jsonβ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
"@antfu/eslint-config": "^3.9.2",
3030
"@eslint-react/eslint-plugin": "^1.16.2",
3131
"@solidjs/testing-library": "^0.8.10",
32+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
3233
"@testing-library/jest-dom": "^6.6.3",
3334
"@testing-library/react": "^16.0.1",
35+
"@testing-library/svelte": "^5.2.7",
3436
"@testing-library/user-event": "^14.6.1",
3537
"@types/node": "^22.13.1",
3638
"@vitest/coverage-v8": "^3.0.4",
@@ -48,6 +50,11 @@
4850
"vite-plugin-solid": "^2.11.6",
4951
"vitest": "^3.0.4"
5052
},
53+
"pnpm": {
54+
"onlyBuiltDependencies": [
55+
"esbuild"
56+
]
57+
},
5158
"simple-git-hooks": {
5259
"pre-commit": "pnpm lint-staged"
5360
},

β€Žpackages/client/README.mdβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
- **Contract-First Development πŸ“œ**: (Optional) Define your API contract upfront and implement it with confidence.
3333
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
3434
- **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35-
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid), Pinia Colada, and more.
35+
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
3636
- **Server Actions ⚑️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
3737
- **Standard Schema Support πŸ—‚οΈ**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
3838
- **Fast & Lightweight πŸ’¨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
@@ -56,6 +56,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
5656
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
5757
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
5858
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
59+
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
5960
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
6061
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
6162
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.

β€Žpackages/contract/README.mdβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
- **Contract-First Development πŸ“œ**: (Optional) Define your API contract upfront and implement it with confidence.
3333
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
3434
- **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35-
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid), Pinia Colada, and more.
35+
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
3636
- **Server Actions ⚑️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
3737
- **Standard Schema Support πŸ—‚οΈ**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
3838
- **Fast & Lightweight πŸ’¨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
@@ -56,6 +56,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
5656
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
5757
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
5858
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
59+
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
5960
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
6061
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
6162
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.

0 commit comments

Comments
Β (0)