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 .validator((postId: string) => postId)
13 .handler(async ({ data }) => {
14 console.info(`Fetching post with id ${data}...`)
15 const post = await axios
16 .get<PostType>(`https://jsonplaceholder.typicode.com/posts/${data}`)
17 .then((r) => r.data)
18 .catch((err) => {
19 if (err.status === 404) {
20 throw notFound()
21 }
22 throw err
23 })
24β
25 return post
26 })
27β
28export const fetchPosts = createServerFn({ method: 'GET' }).handler(
29 async () => {
30 console.info('Fetching posts...')
31 return axios
32 .get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts')
33 .then((r) => r.data.slice(0, 10))
34 },
35)