Open History to see the last commit
1import { notFound } from '@tanstack/react-router'
2import { createServerFn } from '@tanstack/react-start'
3import axios from 'redaxios'
4β
5export type PostType = {
6 id: string
7 title: string
8 body: string
9}
10β
11export const fetchPost = createServerFn({ method: 'GET' })
12 .inputValidator((postId: string) => postId)
13 .handler(async ({ data: postId }) => {
14 console.info(`Fetching post with id ${postId}...`)
15 const post = await axios
16 .get<PostType>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
17 .then((r) => r.data)
18 .catch((err) => {
19 console.error(err)
20 if (err.status === 404) {
21 throw notFound()
22 }
23 throw err
24 })
25β
26 return post
27 })
28β
29export const fetchPosts = createServerFn({ method: 'GET' }).handler(
30 async () => {
31 console.info('Fetching posts...')
32 await new Promise((r) => setTimeout(r, 1000))
33 return axios
34 .get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts')
35 .then((r) => r.data.slice(0, 10))
36 },
37)