import ky from 'ky';
import type { Book } from './Book';

const booksAPI = ky.create({
  prefixUrl: `${import.meta.env.VITE_API_SERVER}/books`,
});

export async function getAllBooks(): Promise<Book[]> {
  return booksAPI.get<Book[]>('').json();
}

export async function deleteBook(id: number): Promise<void> {
  await booksAPI.delete(`${id}`);
}

export async function editBook(book: Book): Promise<Book> {
  return booksAPI
    .put<Book>(`${book.id}`, {
      json: book,
    })
    .json();
}
