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