diff --git a/examples/svelte/ssr/src/lib/Post.svelte b/examples/svelte/ssr/src/lib/Post.svelte index f89fa9b724..774a905e87 100644 --- a/examples/svelte/ssr/src/lib/Post.svelte +++ b/examples/svelte/ssr/src/lib/Post.svelte @@ -1,11 +1,11 @@
diff --git a/examples/svelte/ssr/src/lib/Posts.svelte b/examples/svelte/ssr/src/lib/Posts.svelte index 198d7d3cf2..9983e36b15 100644 --- a/examples/svelte/ssr/src/lib/Posts.svelte +++ b/examples/svelte/ssr/src/lib/Posts.svelte @@ -1,13 +1,13 @@
@@ -24,7 +24,7 @@ href={resolve('/[postId]', { postId: String(post.id) })} style={// We can use the queryCache here to show bold links for // ones that are cached - client.getQueryData(postQueryOptions(post.id).queryKey) + client.getQueryData(postsQueries.detail(post.id).queryKey) ? 'font-weight: bold; color: indianred' : 'cursor: pointer'} > diff --git a/examples/svelte/ssr/src/lib/queries.ts b/examples/svelte/ssr/src/lib/queries.ts deleted file mode 100644 index 6a0843bfbe..0000000000 --- a/examples/svelte/ssr/src/lib/queries.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { queryOptions } from '@tanstack/svelte-query' -import { api } from './api' - -export const postsQueryOptions = (limit: number, customFetch?: typeof fetch) => - queryOptions({ - queryKey: ['posts', limit], - queryFn: () => api(customFetch).getPosts(limit), - }) - -export const postQueryOptions = (id: number, customFetch?: typeof fetch) => - queryOptions({ - queryKey: ['post', id], - queryFn: () => api(customFetch).getPostById(id), - }) diff --git a/examples/svelte/ssr/src/lib/api.ts b/examples/svelte/ssr/src/lib/services/posts/api.ts similarity index 85% rename from examples/svelte/ssr/src/lib/api.ts rename to examples/svelte/ssr/src/lib/services/posts/api.ts index 347a928695..b91e404e82 100644 --- a/examples/svelte/ssr/src/lib/api.ts +++ b/examples/svelte/ssr/src/lib/services/posts/api.ts @@ -1,6 +1,6 @@ import type { Post } from './types' -export const api = (customFetch = fetch) => ({ +export const postsAPI = (customFetch = fetch) => ({ getPosts: async (limit: number) => { const response = await customFetch( 'https://jsonplaceholder.typicode.com/posts', @@ -9,7 +9,7 @@ export const api = (customFetch = fetch) => ({ const data = (await response.json()) as Array return data.filter((x) => x.id <= limit) }, - getPostById: async (id: number): Promise => { + getPost: async (id: number): Promise => { const response = await customFetch( `https://jsonplaceholder.typicode.com/posts/${id}`, ) diff --git a/examples/svelte/ssr/src/lib/services/posts/queries.ts b/examples/svelte/ssr/src/lib/services/posts/queries.ts new file mode 100644 index 0000000000..5945ce2e41 --- /dev/null +++ b/examples/svelte/ssr/src/lib/services/posts/queries.ts @@ -0,0 +1,16 @@ +import { queryOptions } from '@tanstack/svelte-query' +import { postsAPI } from './api' + +export const postsQueries = { + all: () => ['posts'] as const, + list: (limit: number, customFetch?: typeof fetch) => + queryOptions({ + queryKey: [...postsQueries.all(), 'list', limit] as const, + queryFn: () => postsAPI(customFetch).getPosts(limit), + }), + detail: (id: number, customFetch?: typeof fetch) => + queryOptions({ + queryKey: [...postsQueries.all(), 'detail', id] as const, + queryFn: () => postsAPI(customFetch).getPost(id), + }), +} diff --git a/examples/svelte/ssr/src/lib/types.ts b/examples/svelte/ssr/src/lib/services/posts/types.ts similarity index 100% rename from examples/svelte/ssr/src/lib/types.ts rename to examples/svelte/ssr/src/lib/services/posts/types.ts diff --git a/examples/svelte/ssr/src/routes/+page.ts b/examples/svelte/ssr/src/routes/+page.ts index 69ff70073b..ed3ddab97a 100644 --- a/examples/svelte/ssr/src/routes/+page.ts +++ b/examples/svelte/ssr/src/routes/+page.ts @@ -1,9 +1,9 @@ import { noop } from '@tanstack/svelte-query' import type { PageLoad } from './$types' -import { postsQueryOptions } from '$lib/queries' +import { postsQueries } from '$lib/services/posts/queries' export const load: PageLoad = async ({ parent, fetch }) => { const { queryClient } = await parent() - await queryClient.query(postsQueryOptions(10, fetch)).catch(noop) + await queryClient.query(postsQueries.list(10, fetch)).catch(noop) } diff --git a/examples/svelte/ssr/src/routes/[postId]/+page.ts b/examples/svelte/ssr/src/routes/[postId]/+page.ts index b6048618c6..fea5f25b04 100644 --- a/examples/svelte/ssr/src/routes/[postId]/+page.ts +++ b/examples/svelte/ssr/src/routes/[postId]/+page.ts @@ -1,13 +1,13 @@ import { noop } from '@tanstack/svelte-query' import type { PageLoad } from './$types' -import { postQueryOptions } from '$lib/queries' +import { postsQueries } from '$lib/services/posts/queries' export const load: PageLoad = async ({ parent, fetch, params }) => { const { queryClient } = await parent() const postId = parseInt(params.postId) - await queryClient.query(postQueryOptions(postId, fetch)).catch(noop) + await queryClient.query(postsQueries.detail(postId, fetch)).catch(noop) return { postId } }