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β
11const queryURL = 'https://jsonplaceholder.typicode.com'
12β
13export const fetchPosts = async () => {
14 console.info('Fetching posts...')
15 return axios
16 .get<Array<PostType>>(`${queryURL}/posts`)
17 .then((r) => r.data.slice(0, 10))
18}
19β
20export const fetchPost = async (postId: string) => {
21 console.info(`Fetching post with id ${postId}...`)
22 const post = await axios
23 .get<PostType>(`${queryURL}/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}