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

posts.tsx

32 lines810 Bytes

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