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