2022-02-14 12:48:52 -05:00
|
|
|
interface Product {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
price: number;
|
|
|
|
image: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
//let origin: string;
|
|
|
|
const { mode } = import.meta.env;
|
2022-02-28 17:04:03 -05:00
|
|
|
const origin = mode === 'develepment' ? `http://localhost:3000` : `http://localhost:8085`;
|
2022-02-14 12:48:52 -05:00
|
|
|
|
|
|
|
async function get<T>(endpoint: string, cb: (response: Response) => Promise<T>): Promise<T> {
|
|
|
|
const response = await fetch(`${origin}${endpoint}`);
|
2022-02-14 17:50:16 +00:00
|
|
|
if (!response.ok) {
|
2022-02-14 12:48:52 -05:00
|
|
|
// TODO make this better...
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return cb(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getProducts(): Promise<Product[]> {
|
2022-02-14 17:50:16 +00:00
|
|
|
return get<Product[]>('/api/products', async (response) => {
|
2022-02-14 12:48:52 -05:00
|
|
|
const products: Product[] = await response.json();
|
|
|
|
return products;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getProduct(id: number): Promise<Product> {
|
2022-02-14 17:50:16 +00:00
|
|
|
return get<Product>(`/api/products/${id}`, async (response) => {
|
2022-02-14 12:48:52 -05:00
|
|
|
const product: Product = await response.json();
|
|
|
|
return product;
|
|
|
|
});
|
|
|
|
}
|