Skip to content

Commit 7167aba

Browse files
authored
Add procedure extensions (#51)
* feat: add procedure extensions * docs: add reference to example app in README * bump version
1 parent e2d66e6 commit 7167aba

File tree

5 files changed

+155
-111
lines changed

5 files changed

+155
-111
lines changed

@example/src/routes/(app)/client-only/+page.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
import { page } from '$app/stores';
88
import { trpc } from '$lib/trpc/client';
99
import { writable } from 'svelte/store';
10-
import type { InferProcedureOpts } from 'trpc-svelte-query-adapter';
1110
1211
const api = trpc($page);
1312
const utils = api.createUtils();
1413
1514
let todoInput: HTMLInputElement;
1615
1716
const filter = writable<string | undefined>();
18-
const opts = writable({
19-
refetchInterval: Infinity,
20-
} satisfies InferProcedureOpts<typeof api.todos.get.createQuery>);
21-
17+
const opts = writable(
18+
api.todos.get.createQuery.opts({
19+
refetchInterval: Infinity,
20+
})
21+
);
2222
const todos = api.todos.get.createQuery(filter, opts);
2323
2424
const popularTodos = api.todos.getPopular.createInfiniteQuery(

@example/src/routes/(app)/ssr/+page.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import { X, Plus } from 'phosphor-svelte';
55
import { writable } from 'svelte/store';
66
import { debounce } from '$lib/utils';
7-
import type { InferProcedureOpts } from 'trpc-svelte-query-adapter';
87
98
export let data;
109
@@ -14,9 +13,11 @@
1413
let todoInput: HTMLInputElement;
1514
1615
const filter = writable<string | undefined>();
17-
const opts = writable({
18-
refetchInterval: Infinity,
19-
} satisfies InferProcedureOpts<typeof api.todos.get.createQuery>);
16+
const opts = writable(
17+
api.todos.get.createQuery.opts({
18+
refetchInterval: Infinity,
19+
})
20+
);
2021
2122
const todos = data.todos(filter, opts);
2223

@lib/README.md

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ The following instructions assume the `tRPC` router to have the following proced
5353

5454
```typescript
5555
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+
}),
6565
});
6666

6767
export type Router = typeof router;
@@ -81,12 +81,12 @@ import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
8181
import { svelteQueryWrapper } from 'trpc-svelte-query-adapter';
8282

8383
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+
],
9090
});
9191

9292
export const trpc = svelteQueryWrapper<Router>({ client });
@@ -122,20 +122,20 @@ Here is an example of what that might look like:
122122
import type { QueryClient } from '@tanstack/svelte-query';
123123

124124
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+
],
131131
});
132132

133133
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+
}
139139
```
140140

141141
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
166166
let browserClient: ReturnType<typeof createTRPCClient<Router>>;
167167

168168
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;
174174
}
175175
```
176176

@@ -183,14 +183,14 @@ import type { QueryClient } from '@tanstack/svelte-query';
183183
let browserClient: ReturnType<typeof svelteQueryWrapper<Router>>;
184184

185185
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;
194194
}
195195
```
196196

@@ -214,16 +214,17 @@ import { trpc } from '$lib/trpc/client';
214214
import type { PageLoad } from './$types';
215215

216216
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;
227228
```
228229

229230
Then, in the component:
@@ -319,6 +320,8 @@ You can also optionally pass new inputs to the queries and infinite queries from
319320
</div>
320321
```
321322

323+
For more usage examples, you can refer to the [example app provided in the repo](/@example).
324+
322325
[npm-url]: https://npmjs.org/package/trpc-svelte-query-adapter
323326
[npm-image]: https://img.shields.io/npm/v/trpc-svelte-query-adapter.svg
324327
[license-url]: LICENSE

@lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "trpc-svelte-query-adapter",
3-
"version": "2.3.9",
3+
"version": "2.3.10",
44
"description": "A simple adapter to use `@tanstack/svelte-query` with trpc, similar to `@trpc/react-query`.",
55
"keywords": [
66
"trpc",

0 commit comments

Comments
 (0)