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

api.ts

18 lines558 Bytes

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