Open History to see the last commit
1import axios from 'redaxios'
2import { queryOptions } from '@tanstack/solid-query'
3β
4export class NotFoundError extends Error {}
5β
6type PostType = {
7 id: string
8 title: string
9 body: string
10}
11β
12let queryURL = 'https://jsonplaceholder.typicode.com'
13β
14if (import.meta.env.VITE_NODE_ENV === 'test') {
15 queryURL = `http://localhost:${import.meta.env.VITE_EXTERNAL_PORT}`
16}
17β
18const fetchPosts = async () => {
19 console.info('Fetching posts...')
20 await new Promise((r) => setTimeout(r, 500))
21 return axios
22 .get<Array<PostType>>(`${queryURL}/posts`)
23 .then((r) => r.data.slice(0, 10))
24}
25β
26const fetchPost = async (postId: string) => {
27 console.info(`Fetching post with id ${postId}...`)
28 await new Promise((r) => setTimeout(r, 500))
29 const post = await axios
30 .get<PostType>(`${queryURL}/posts/${postId}`)
31 .then((r) => r.data)
32β
33 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
34 if (!post) {
35 throw new NotFoundError(`Post with id "${postId}" not found!`)
36 }
37β
38 return post
39}
40β
41export const postQueryOptions = (postId: string) =>
42 queryOptions({
43 queryKey: ['posts', { postId }],
44 queryFn: () => fetchPost(postId),
45 })
46β
47export const postsQueryOptions = queryOptions({
48 queryKey: ['posts'],
49 queryFn: () => fetchPosts(),
50})