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

posts.tsx

35 lines904 Bytes

1// import { notFound } from '@tanstack/react-router'
2// import { 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 = async (postId: string) => {
12 'use server'
13 console.info(`Fetching post with id ${postId}...`)
14 await new Promise((r) => setTimeout(r, 500))
15 const post = await axios
16 .get<PostType>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
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
27​
28export const fetchPosts = async () => {
29 'use server'
30 console.info('Fetching posts...')
31 await new Promise((r) => setTimeout(r, 500))
32 return axios
33 .get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts')
34 .then((r) => r.data.slice(0, 10))