import React, { useState } from 'react' import { del, get, post } from '../../src' import { createClientHook } from '../../src/react' const useApi = createClientHook({ products: get>('/products'), addProduct: post<{ title: string }, { id: number; title: string }>( '/products', ), deleteProduct: del('/products/:id'), }) export function App() { const api = useApi({ baseUrl: 'https://fakestoreapi.com', onError: () => { return 'strigi' }, }) const [products, setProducts] = useState< Array<{ id: number; title: string }> >([]) const [loading, setLoading] = useState(false) const [newTitle, setNewTitle] = useState('') const fetchProducts = async () => { setLoading(true) const data = await api.products().json() setProducts(data) setLoading(false) } const handleAdd = async () => { if (!newTitle) return setLoading(true) const product = { title: newTitle } await api.addProduct(product) setNewTitle('') await fetchProducts() setLoading(false) } const handleDelete = async (id: number) => { setLoading(true) await api.deleteProduct({ params: { id } }) await fetchProducts() setLoading(false) } React.useEffect(() => { fetchProducts() }, []) return (

kypi Playground (Fake Store API)

setNewTitle(e.target.value)} placeholder="New product title" />
{loading &&

Loading...

}
    {products.map((p) => (
  • {p.title}
  • ))}
) }