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