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

posts.tsx

36 lines842 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​
10let queryURL = 'https://jsonplaceholder.typicode.com'
11​
12if (import.meta.env.VITE_NODE_ENV === 'test') {
13 queryURL = `http://localhost:${import.meta.env.VITE_EXTERNAL_PORT}`
15​
16export const fetchPost = async (postId: string) => {
17 console.info(`Fetching post with id ${postId}...`)
18 const post = await axios
19 .get<PostType>(`${queryURL}/posts/${postId}`)
20 .then((r) => r.data)
21 .catch((err) => {
22 if (err.status === 404) {
23 throw notFound()
24 }
25 throw err
26 })
27​
28 return post
30​
31export const fetchPosts = async () => {
32 console.info('Fetching posts...')
33 return axios
34 .get<Array<PostType>>(`${queryURL}/posts`)
35 .then((r) => r.data.slice(0, 10))