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

api.ts

18 lines632 Bytes

1import type { Post } from './types'
2​
3export const postsAPI = {
4 getPosts: async (limit: number) => {
5 const response = await fetch('https://jsonplaceholder.typicode.com/posts')
6 if (!response.ok) throw new Error('Failed to fetch posts')
7 const data = (await response.json()) as Array<Post>
8 return data.filter((x) => x.id <= limit)
9 },
10 getPost: async (id: number): Promise<Post> => {
11 const response = await fetch(
12 `https://jsonplaceholder.typicode.com/posts/${id}`,
13 )
14 if (!response.ok) throw new Error('Failed to fetch post')
15 const data = (await response.json()) as Post
16 return data
17 },