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

posts.tsx

32 lines723 Bytes

1import { notFound } from '@tanstack/react-router'
2import axios from 'redaxios'
3​
4export type PostType = {
5 id: string
6 title: string
7 body: string
8}
9​
10const queryURL = 'https://jsonplaceholder.typicode.com'
11​
12export const fetchPost = async (postId: string) => {
13 console.info(`Fetching post with id ${postId}...`)
14 const post = await axios
15 .get<PostType>(`${queryURL}/posts/${postId}`)
16 .then((r) => r.data)
17 .catch((err) => {
18 if (err.status === 404) {
19 throw notFound()
20 }
21 throw err
22 })
23​
24 return post
26​
27export const fetchPosts = async () => {
28 console.info('Fetching posts...')
29 return axios
30 .get<Array<PostType>>(`${queryURL}/posts`)
31 .then((r) => r.data.slice(0, 10))