Open History to see the last commit
1import axios from 'redaxios'
2β
3export class NotFoundError extends Error {}
4β
5type PostType = {
6 id: string
7 title: string
8 body: string
9}
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 fetchPosts = async () => {
18 console.info('Fetching posts...')
19 return axios
20 .get<Array<PostType>>(`${queryURL}/posts`)
21 .then((r) => r.data.slice(0, 10))
22}
23β
24export const fetchPost = async (postId: string) => {
25 console.info(`Fetching post with id ${postId}...`)
26 const post = await axios
27 .get<PostType>(`${queryURL}/posts/${postId}`)
28 .then((r) => r.data)
29β
30 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
31 if (!post) {
32 throw new NotFoundError(`Post with id "${postId}" not found!`)
33 }
34β
35 return post
36}