blob: 59619ade661c72d14f8898d7fafc69b19acfadf3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
interface Product {
id: number;
name: string;
price: number;
image: string;
}
//let origin: string;
const { mode } = import.meta.env;
const origin = mode === 'develepment' ? `http://localhost:3000` : `http://localhost:8085`;
async function get<T>(endpoint: string, cb: (response: Response) => Promise<T>): Promise<T> {
const response = await fetch(`${origin}${endpoint}`);
if (!response.ok) {
// TODO make this better...
return null;
}
return cb(response);
}
export async function getProducts(): Promise<Product[]> {
return get<Product[]>('/api/products', async (response) => {
const products: Product[] = await response.json();
return products;
});
}
export async function getProduct(id: number): Promise<Product> {
return get<Product>(`/api/products/${id}`, async (response) => {
const product: Product = await response.json();
return product;
});
}
|