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β
13const queryURL = 'https://jsonplaceholder.typicode.com'
14β
15export class PostNotFoundError extends Error {}
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}