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

posts.tsx

33 lines772 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​
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
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))