Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

posts.tsx

33 lines881 Bytes

1import { queryOptions } from '@tanstack/solid-query'
2import axios from 'redaxios'
3​
4export type PostType = {
5 id: string
6 title: string
7 body: string
8}
9​
10export const postsQueryOptions = () =>
11 queryOptions({
12 queryKey: ['posts'],
13 queryFn: async () => {
14 console.info('Fetching posts...')
15 await new Promise((r) => setTimeout(r, 500))
16 return axios
17 .get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts')
18 .then((r) => r.data.slice(0, 10))
19 },
20 })
21​
22export const postQueryOptions = (postId: string) =>
23 queryOptions({
24 queryKey: ['posts', postId],
25 queryFn: async () => {
26 console.info(`Fetching post with id ${postId}...`)
27 await new Promise((r) => setTimeout(r, 500))
28​
29 return axios
30 .get<PostType>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
31 .then((r) => r.data)
32 },
33 })