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

posts.ts

32 lines762 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​
11const queryURL = 'https://jsonplaceholder.typicode.com'
12​
13export const fetchPosts = async () => {
14 console.info('Fetching posts...')
15 return axios
16 .get<Array<PostType>>(`${queryURL}/posts`)
17 .then((r) => r.data.slice(0, 10))
19​
20export const fetchPost = async (postId: string) => {
21 console.info(`Fetching post with id ${postId}...`)
22 const post = await axios
23 .get<PostType>(`${queryURL}/posts/${postId}`)
24 .then((r) => r.data)
25​
26 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27 if (!post) {
28 throw new NotFoundError(`Post with id "${postId}" not found!`)
29 }
30​
31 return post