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