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

posts.js

27 lines697 Bytes

1import axios from 'redaxios'
2​
3export class NotFoundError extends Error {}
4​
5let queryURL = 'https://jsonplaceholder.typicode.com'
6​
7if (import.meta.env.VITE_NODE_ENV === 'test') {
8 queryURL = `http://localhost:${import.meta.env.VITE_EXTERNAL_PORT}`
9}
10​
11export const fetchPosts = async () => {
12 console.info('Fetching posts...')
13 return axios.get(`${queryURL}/posts`).then((r) => r.data.slice(0, 10))
15​
16export const fetchPost = async (postId) => {
17 console.info(`Fetching post with id ${postId}...`)
18 const post = await axios
19 .get(`${queryURL}/posts/${postId}`)
20 .then((r) => r.data)
21​
22 if (!post) {
23 throw new NotFoundError(`Post with id "${postId}" not found!`)
24 }
25​
26 return post