-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(svelte-query): Use API wrapper to simplify SSR logic (#5322)
* Use API wrapper to simplify fetch logic * Fix prettier --------- Co-authored-by: Dominik Dorfmeister <[email protected]>
- Loading branch information
1 parent
c8799df
commit 7fd50a7
Showing
6 changed files
with
26 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Post } from './types' | ||
|
||
export const api = (customFetch = fetch) => ({ | ||
getPosts: async (limit: number) => { | ||
const response = await customFetch( | ||
'https://jsonplaceholder.typicode.com/posts', | ||
) | ||
const data = (await response.json()) as Post[] | ||
return data.filter((x) => x.id <= limit) | ||
}, | ||
getPostById: async (id: number): Promise<Post> => { | ||
const response = await customFetch( | ||
`https://jsonplaceholder.typicode.com/posts/${id}`, | ||
) | ||
const data = (await response.json()) as Post | ||
return data | ||
}, | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { getPosts } from '$lib/data' | ||
import { api } from '$lib/api' | ||
import type { PageLoad } from './$types' | ||
|
||
export const load: PageLoad = async ({ parent, fetch }) => { | ||
const { queryClient } = await parent() | ||
|
||
await queryClient.prefetchQuery({ | ||
queryKey: ['posts', 10], | ||
queryFn: () => getPosts(10, fetch), | ||
queryFn: () => api(fetch).getPosts(10), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters