api.ts 775 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { Recipe } from "@/types/recipes";
  2. async function api<T>(url: string): Promise<T> {
  3. const response = await fetch(url);
  4. if (!response.ok) {
  5. throw new Error(response.statusText);
  6. }
  7. return await response.json();
  8. }
  9. async function mockAPI<T>(data: T): Promise<T> {
  10. return new Promise((resolve) => {
  11. resolve(data);
  12. })
  13. }
  14. export const getAllRecipes = () => api<Recipe[]>(`${process.env.API_RECIPES}/all/`)
  15. type Tag = {
  16. name: string,
  17. };
  18. export const getAllTags = () => mockAPI<Tag[]>(
  19. [
  20. { name: 'Dinner' },
  21. { name: 'Snack' },
  22. { name: 'Dessert' },
  23. { name: 'Lunch' }
  24. ]
  25. );
  26. export const getRecipe = (id: string) => api<Recipe>(`${process.env.API_RECIPES}/id/${id}/`);
  27. export default {
  28. getAllRecipes,
  29. getAllTags,
  30. getRecipe,
  31. };