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