Open History to see the last commit
1import { notFound } from '@tanstack/solid-router'
2import axios from 'redaxios'
3β
4export type PostType = {
5 id: string
6 title: string
7 body: string
8}
9β
10let queryURL = 'https://jsonplaceholder.typicode.com'
11β
12if (import.meta.env.VITE_NODE_ENV === 'test') {
13 queryURL = `http://localhost:${import.meta.env.VITE_EXTERNAL_PORT}`
14}
15β
16export const fetchPost = async (postId: string) => {
17 console.info(`Fetching post with id ${postId}...`)
18 await new Promise((r) => setTimeout(r, 500))
19 const post = await axios
20 .get<PostType>(`${queryURL}/posts/${postId}`)
21 .then((r) => r.data)
22 .catch((err) => {
23 if (err.status === 404) {
24 throw notFound()
25 }
26 throw err
27 })
28β
29 return post
30}
31β
32export const fetchPosts = async () => {
33 console.info('Fetching posts...')
34 await new Promise((r) => setTimeout(r, 500))
35 return axios
36 .get<Array<PostType>>(`${queryURL}/posts`)
37 .then((r) => r.data.slice(0, 10))
38}