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