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

posts.ts

36 lines881 Bytes

1import axios from 'redaxios'
2​
3export class NotFoundError extends Error {}
4​
5type PostType = {
6 id: string
7 title: string
8 body: string
9}
10​
11let queryURL = 'https://jsonplaceholder.typicode.com'
12​
13if (import.meta.env.VITE_NODE_ENV === 'test') {
14 queryURL = `http://localhost:${import.meta.env.VITE_EXTERNAL_PORT}`
16​
17export const fetchPosts = async () => {
18 console.info('Fetching posts...')
19 return axios
20 .get<Array<PostType>>(`${queryURL}/posts`)
21 .then((r) => r.data.slice(0, 10))
23​
24export const fetchPost = async (postId: string) => {
25 console.info(`Fetching post with id ${postId}...`)
26 const post = await axios
27 .get<PostType>(`${queryURL}/posts/${postId}`)
28 .then((r) => r.data)
29​
30 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
31 if (!post) {
32 throw new NotFoundError(`Post with id "${postId}" not found!`)
33 }
34​
35 return post