Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

posts.tsx

33 lines859 Bytes

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
26​
27export const fetchPosts = async () => {
28 console.info('Fetching posts...')
29 await new Promise((r) => setTimeout(r, 500))
30 return axios
31 .get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts')
32 .then((r) => r.data.slice(0, 10))