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