|
4 | 4 | [![License][license-image]][license-url]
|
5 | 5 | [![Last commit][last-commit-image]][repo-url]
|
6 | 6 |
|
7 |
| -> **NOTE:** The README on [npmjs](https://npmjs.com/trpc-svelte-query-adapter) might not be fully up to date. Please refer to the [README on the Github Repo](https://github.com/vishalbalaji/trpc-svelte-query-adapter/#readme) for the latest setup instructions. |
| 7 | +> [!NOTE] |
| 8 | +> The README on [npmjs](https://npmjs.com/trpc-svelte-query-adapter) might not be fully up to date. Please refer to |
| 9 | +> the [README on the Github Repo](https://github.com/vishalbalaji/trpc-svelte-query-adapter/#readme) for the latest setup instructions. |
8 | 10 |
|
9 |
| -This package provides an adapter to call `tRPC` procedures wrapped with <code>[@tanstack/svelte-query](https://tanstack.com/query/latest/docs/svelte/overview)</code>, similar to <code>[@trpc/react-query](https://trpc.io/docs/react-query)</code>. This is made possible using <code>[proxy-deep](https://www.npmjs.com/package/proxy-deep)</code>. |
| 11 | +An adapter to call `tRPC` procedures wrapped with <code>[@tanstack/svelte-query](https://tanstack.com/query/latest/docs/svelte/overview)</code>, similar to <code>[@trpc/react-query](https://trpc.io/docs/react-query)</code>. This is made possible using <code>[proxy-deep](https://www.npmjs.com/package/proxy-deep)</code>. |
10 | 12 |
|
11 | 13 | ## Installation
|
12 | 14 |
|
13 |
| -```console |
| 15 | +```sh |
14 | 16 | # npm
|
15 | 17 | npm install trpc-svelte-query-adapter @trpc/client @trpc/server @tanstack/svelte-query
|
16 | 18 |
|
@@ -202,7 +204,8 @@ By default, these 3 procedures will pre-fetch the data required to pre-render th
|
202 | 204 |
|
203 | 205 | These procedures can be used as such:
|
204 | 206 |
|
205 |
| -> **NOTE:** [Gotta await top-level promises to pre-fetch data from SvelteKit v2](https://kit.svelte.dev/docs/migrating-to-sveltekit-2#top-level-promises-are-no-longer-awaited). |
| 207 | +> [!NOTE] |
| 208 | +> [Gotta await top-level promises to pre-fetch data from SvelteKit v2](https://kit.svelte.dev/docs/migrating-to-sveltekit-2#top-level-promises-are-no-longer-awaited). |
206 | 209 |
|
207 | 210 | ```typescript
|
208 | 211 | // +page.ts
|
@@ -258,58 +261,62 @@ Then, in the component:
|
258 | 261 | {/each}
|
259 | 262 | ```
|
260 | 263 |
|
261 |
| -You can also optionally pass new inputs to the queries and infinite queries from the client side(see [#34](/../../issues/34)) like so: |
| 264 | +You can also optionally pass new inputs to the queries and infinite queries from the client side(see [#34](/../../issues/34), [#47]( /../../issues/47 )) like so: |
262 | 265 |
|
263 | 266 | ```svelte
|
264 | 267 | <script lang="ts">
|
265 | 268 | import { page } from "$app/stores";
|
266 | 269 | import type { PageData } from "./$types";
|
267 | 270 |
|
| 271 | + import { derived, writable } from '@svelte/store'; |
| 272 | +
|
268 | 273 | export let data: PageData;
|
269 | 274 |
|
270 |
| - let name = 'foo'; |
271 |
| - let newNames: string[] = []; |
| 275 | + const name = writable('foo'); |
| 276 | + const newNames = writable<string[]>([]); |
272 | 277 |
|
273 |
| - $: foo = data.foo(name); // `$` label to make the query reactive to the input |
| 278 | + const foo = data.foo($name); |
274 | 279 |
|
275 | 280 | // You can also access the default input if you pass in a callback as the new input:
|
276 |
| - // $: foo = data.foo((old) => old + name); |
| 281 | + // const foo = data.foo((old) => derived(name, ($name) => old + name)); |
277 | 282 |
|
278 |
| - $: queries = data.queries((t, old) => [...old, ...newNames.map((name) => t.greeting(name))]); |
| 283 | + const queries = data.queries((t, old) => derived(newNames, ($newNames) => [...old, ...$newNames.map((name) => t.greeting(name))])); |
279 | 284 | </script>
|
280 | 285 |
|
281 |
| -{#if $foo.isPending} |
282 |
| - Loading... |
283 |
| -{:else if $foo.isError} |
284 |
| - {$foo.error} |
285 |
| -{:else if $foo.data} |
286 |
| - {$foo.data} |
287 |
| -{/if} |
288 |
| -
|
289 |
| -<br /> |
290 |
| -<input bind:value={name} /> |
291 |
| -
|
292 |
| -<br /><br /> |
293 |
| -
|
294 |
| -{#each $queries as query} |
295 |
| - {#if query.isPending} |
| 286 | +<div> |
| 287 | + {#if $foo.isPending} |
296 | 288 | Loading...
|
297 |
| - {:else if query.isError} |
298 |
| - {query.error.message} |
299 |
| - {:else if query.data} |
300 |
| - {query.data} |
| 289 | + {:else if $foo.isError} |
| 290 | + {$foo.error} |
| 291 | + {:else if $foo.data} |
| 292 | + {$foo.data} |
301 | 293 | {/if}
|
302 |
| - <br /> |
303 |
| -{/each} |
| 294 | + <input bind:value={$name} /> |
| 295 | +</div> |
| 296 | +
|
| 297 | +<br /> |
304 | 298 |
|
305 |
| -<form on:submit|preventDefault={(e) => { |
306 |
| - const data = new FormData(e.currentTarget).get('name'); |
307 |
| - if (typeof data === 'string') newNames.push(data); |
308 |
| - newNames = newNames; |
309 |
| -}}> |
310 |
| - <input name="name" /> |
311 |
| - <button type="submit">Submit</button> |
312 |
| -</form> |
| 299 | +<div> |
| 300 | + {#each $queries as query} |
| 301 | + {#if query.isPending} |
| 302 | + Loading... |
| 303 | + {:else if query.isError} |
| 304 | + {query.error.message} |
| 305 | + {:else if query.data} |
| 306 | + {query.data} |
| 307 | + {/if} |
| 308 | + <br /> |
| 309 | + {/each} |
| 310 | +
|
| 311 | + <form on:submit|preventDefault={(e) => { |
| 312 | + const data = new FormData(e.currentTarget).get('name'); |
| 313 | + if (typeof data === 'string') $newNames.push(data); |
| 314 | + $newNames = $newNames; |
| 315 | + }}> |
| 316 | + <input name="name" /> |
| 317 | + <button type="submit">Submit</button> |
| 318 | + </form> |
| 319 | +</div> |
313 | 320 | ```
|
314 | 321 |
|
315 | 322 | [npm-url]: https://npmjs.org/package/trpc-svelte-query-adapter
|
|
0 commit comments