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

posts.tsx

34 lines817 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​
10const queryURL = 'https://jsonplaceholder.typicode.com'
11​
12export const fetchPost = async (postId: string) => {
13 console.info(`Fetching post with id ${postId}...`)
14 await new Promise((r) => setTimeout(r, 500))
15 const post = await axios
16 .get<PostType>(`${queryURL}/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 console.info('Fetching posts...')
30 await new Promise((r) => setTimeout(r, 500))
31 return axios
32 .get<Array<PostType>>(`${queryURL}/posts`)
33 .then((r) => r.data.slice(0, 10))