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β
11export const fetchPosts = async () => {
12 console.info('Fetching posts...')
13 await new Promise((r) => setTimeout(r, 500))
14 return axios
15 .get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts')
16 .then((r) => r.data.slice(0, 10))
17}
18β
19export const fetchPost = async (postId: string) => {
20 console.info(`Fetching post with id ${postId}...`)
21 await new Promise((r) => setTimeout(r, 500))
22 const post = await axios
23 .get<PostType>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
24 .then((r) => r.data)
25β
26 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27 if (!post) {
28 throw new NotFoundError(`Post with id "${postId}" not found!`)
29 }
30β
31 return post
32}