@@ -53,15 +53,15 @@ The following instructions assume the `tRPC` router to have the following proced
53
53
54
54
``` typescript
55
55
export const router = t .router ({
56
- greeting: t .procedure
57
- .input ((name : unknown ) => {
58
- if (typeof name === ' string' ) return name ;
59
-
60
- throw new Error (` Invalid input: ${typeof name } ` );
61
- })
62
- .query (async ({ input }) => {
63
- return ` Hello, ${input } from tRPC v10 @ ${new Date ().toLocaleTimeString ()} ` ;
64
- })
56
+ greeting: t .procedure
57
+ .input ((name : unknown ) => {
58
+ if (typeof name === ' string' ) return name ;
59
+
60
+ throw new Error (` Invalid input: ${typeof name } ` );
61
+ })
62
+ .query (async ({ input }) => {
63
+ return ` Hello, ${input } from tRPC v10 @ ${new Date ().toLocaleTimeString ()} ` ;
64
+ }),
65
65
});
66
66
67
67
export type Router = typeof router ;
@@ -81,12 +81,12 @@ import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
81
81
import { svelteQueryWrapper } from ' trpc-svelte-query-adapter' ;
82
82
83
83
const client = createTRPCProxyClient <Router >({
84
- links: [
85
- httpBatchLink ({
86
- // Replace this URL with that of your tRPC server
87
- url: ' http://localhost:5000/api/v1/trpc/' ,
88
- }),
89
- ],
84
+ links: [
85
+ httpBatchLink ({
86
+ // Replace this URL with that of your tRPC server
87
+ url: ' http://localhost:5000/api/v1/trpc/' ,
88
+ }),
89
+ ],
90
90
});
91
91
92
92
export const trpc = svelteQueryWrapper <Router >({ client });
@@ -122,20 +122,20 @@ Here is an example of what that might look like:
122
122
import type { QueryClient } from ' @tanstack/svelte-query' ;
123
123
124
124
const client = createTRPCProxyClient <Router >({
125
- links: [
126
- httpBatchLink ({
127
- // Replace this URL with that of your tRPC server
128
- url: ' http://localhost:5000/api/v1/trpc/' ,
129
- }),
130
- ],
125
+ links: [
126
+ httpBatchLink ({
127
+ // Replace this URL with that of your tRPC server
128
+ url: ' http://localhost:5000/api/v1/trpc/' ,
129
+ }),
130
+ ],
131
131
});
132
132
133
133
export function trpc(queryClient ? : QueryClient ) {
134
- return svelteQueryWrapper <Router >({
135
- client ,
136
- queryClient
137
- });
138
- };
134
+ return svelteQueryWrapper <Router >({
135
+ client ,
136
+ queryClient ,
137
+ });
138
+ }
139
139
```
140
140
141
141
Which can then be used in a component as such:
@@ -166,11 +166,11 @@ The main thing that needs to passed in to `svelteQueryWrapper` is the `tRPC` cli
166
166
let browserClient: ReturnType <typeof createTRPCClient <Router >>;
167
167
168
168
export function trpc(init ? : TRPCClientInit ) {
169
- const isBrowser = typeof window !== ' undefined' ;
170
- if (isBrowser && browserClient ) return browserClient ;
171
- const client = createTRPCClient <Router >({ init });
172
- if (isBrowser ) browserClient = client ;
173
- return client ;
169
+ const isBrowser = typeof window !== ' undefined' ;
170
+ if (isBrowser && browserClient ) return browserClient ;
171
+ const client = createTRPCClient <Router >({ init });
172
+ if (isBrowser ) browserClient = client ;
173
+ return client ;
174
174
}
175
175
```
176
176
@@ -183,14 +183,14 @@ import type { QueryClient } from '@tanstack/svelte-query';
183
183
let browserClient: ReturnType <typeof svelteQueryWrapper <Router >>;
184
184
185
185
export function trpc(init ? : TRPCClientInit , queryClient ? : QueryClient ) {
186
- const isBrowser = typeof window !== ' undefined' ;
187
- if (isBrowser && browserClient ) return browserClient ;
188
- const client = svelteQueryWrapper <Router >({
189
- client: createTRPCClient <Router >({ init }),
190
- queryClient
191
- });
192
- if (isBrowser ) browserClient = client ;
193
- return client ;
186
+ const isBrowser = typeof window !== ' undefined' ;
187
+ if (isBrowser && browserClient ) return browserClient ;
188
+ const client = svelteQueryWrapper <Router >({
189
+ client: createTRPCClient <Router >({ init }),
190
+ queryClient ,
191
+ });
192
+ if (isBrowser ) browserClient = client ;
193
+ return client ;
194
194
}
195
195
```
196
196
@@ -214,16 +214,17 @@ import { trpc } from '$lib/trpc/client';
214
214
import type { PageLoad } from ' ./$types' ;
215
215
216
216
export const load = (async (event ) => {
217
- const { queryClient } = await event .parent ();
218
- const client = trpc (event , queryClient );
219
-
220
- return {
221
- foo: await client .greeting .createServerQuery (' foo' ),
222
- queries: await client .createServerQueries ((t ) =>
223
- [' bar' , ' baz' ].map ((name ) => t .greeting (name , { ssr: name !== ' baz' })), // pre-fetching disabled for the `baz` query.
224
- ),
225
- };
226
- }) satisfies PageLoad
217
+ const { queryClient } = await event .parent ();
218
+ const client = trpc (event , queryClient );
219
+
220
+ return {
221
+ foo: await client .greeting .createServerQuery (' foo' ),
222
+ queries: await client .createServerQueries (
223
+ (t ) =>
224
+ [' bar' , ' baz' ].map ((name ) => t .greeting (name , { ssr: name !== ' baz' })) // pre-fetching disabled for the `baz` query.
225
+ ),
226
+ };
227
+ }) satisfies PageLoad ;
227
228
```
228
229
229
230
Then, in the component:
@@ -319,6 +320,8 @@ You can also optionally pass new inputs to the queries and infinite queries from
319
320
</div>
320
321
```
321
322
323
+ For more usage examples, you can refer to the [ example app provided in the repo] ( /@example ) .
324
+
322
325
[ npm-url ] : https://npmjs.org/package/trpc-svelte-query-adapter
323
326
[ npm-image ] : https://img.shields.io/npm/v/trpc-svelte-query-adapter.svg
324
327
[ license-url ] : LICENSE
0 commit comments