Skip to content

Commit 6a82197

Browse files
authored
doc: updates to sveltekit adapter and svelte-query integration docs (#543)
1 parent b105e22 commit 6a82197

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

docs/reference/server-adapters/sveltekit.mdx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,68 @@ import AdapterOptions from './_options.mdx';
1919

2020
### Mounting the API
2121

22-
You can mount the API by creating SvelteKit server hooks like:
22+
There are two ways to mount the ZenStack API in SvelteKit: as API routes or server hooks (deprecated).
23+
24+
#### API Routes
25+
26+
You can mount the services at specific path as [API routes](https://svelte.dev/tutorial/kit/get-handlers) like the following:
27+
28+
```ts title='/src/routes/api/model/[...path]/+server.ts'
29+
import { SvelteKitRouteHandler } from "@zenstackhq/server/sveltekit";
30+
import { RPCApiHandler } from '@zenstackhq/server/api';
31+
import { getSessionUser } from '~/auth';
32+
import { db } from '~/db';
33+
import { schema } from '~/zenstack/schema';
34+
35+
const handler = SvelteKitRouteHandler({
36+
apiHandler: new RPCApiHandler({ schema }),
37+
// getSessionUser extracts the current session user from the request, its
38+
// implementation depends on your auth solution
39+
getClient: (event) => db.$setAuth(getSessionUser(event)),
40+
});
41+
42+
export const GET = handler;
43+
export const POST = handler;
44+
export const PUT = handler;
45+
export const DELETE = handler;
46+
export const PATCH = handler;
47+
```
48+
49+
Please note that the wildcard route parameter must be named `path` as expected by the adapter implementation.
50+
51+
The API router handler takes the following options to initialize:
52+
53+
<AdapterOptions getClient='(event: RequestEvent) => ClientContract<Schema> | Promise<ClientContract<Schema>>' />
54+
55+
#### Server Hooks
56+
57+
:::warning
58+
The server hooks method is deprecated and will be removed in a future release. Use API routes instead.
59+
:::
60+
61+
You can mount the API by creating SvelteKit [server hooks](https://svelte.dev/tutorial/kit/handle) like:
2362

2463
```ts title='/src/hooks.server.ts'
2564
import { SvelteKitHandler } from '@zenstackhq/server/sveltekit';
2665
import { RPCApiHandler } from '@zenstackhq/server/api';
2766
import { getSessionUser } from '~/auth';
28-
import { client } from '~/db';
67+
import { db } from '~/db';
2968
import { schema } from '~/zenstack/schema';
3069

3170
export const handle = SvelteKitHandler({
3271
apiHandler: new RPCApiHandler({ schema }),
3372
prefix: '/api/model',
3473
// getSessionUser extracts the current session user from the request, its
3574
// implementation depends on your auth solution
36-
getClient: (event) => client.$setAuth(getSessionUser(event)),
75+
getClient: (event) => db.$setAuth(getSessionUser(event)),
3776
});
3877
```
3978

4079
:::tip
4180
You can use the [sequence helper](https://svelte.dev/docs/kit/@sveltejs-kit-hooks#sequence) to compose multiple server hooks.
4281
:::
4382

44-
The SvelteKit hooks handler takes the following options to initialize:
83+
The hooks handler takes the following options to initialize:
4584

4685
- prefix
4786

docs/service/client-sdk/tanstack-query/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import OptimisticBehavior from './_optimistic-behavior.md';
1010
import OptimisticLimitation from './_optimistic-limitation.md';
1111
import FineGrainedOptimistic from './_fine-grained-optimistic.md';
1212
import Invalidation from './_invalidation.md';
13+
import PreviewFeature from '../../../_components/PreviewFeature.tsx'
1314

1415
# TanStack Query
1516

@@ -50,6 +51,8 @@ The integration provides the following features
5051
- `@tanstack/svelte-query`: v6+
5152
- `svelte` v5.25.0+
5253

54+
<PreviewFeature name="Svelte support" />
55+
5356
:::warning
5457

5558
`@tanstack/svelte-query` v6 leverages Svelte 5's [runes](https://svelte.dev/docs/svelte/what-are-runes) reactivity system. ZenStack is not compatible with prior versions that use Svelte stores.

0 commit comments

Comments
 (0)