chore: initial import
This commit is contained in:
48
frontend/lib/api.js
Normal file
48
frontend/lib/api.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const API_BASE = process.env.NEXT_PUBLIC_API_URL?.replace(/\/$/, '') || 'http://localhost:8000';
|
||||
|
||||
async function request(path, init = {}) {
|
||||
const response = await fetch(`${API_BASE}${path}`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(init.headers || {}),
|
||||
},
|
||||
...init,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Request failed: ${response.status} ${text}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export function buildMediaUrl(path) {
|
||||
if (!path) return null;
|
||||
if (path.startsWith('http')) return path;
|
||||
return `${API_BASE}${path}`;
|
||||
}
|
||||
|
||||
export async function fetchProducts() {
|
||||
return request('/api/commerce/products/');
|
||||
}
|
||||
|
||||
export async function fetchProduct(slug) {
|
||||
if (!slug) throw new Error('Product slug is required');
|
||||
return request(`/api/commerce/products/${slug}/`);
|
||||
}
|
||||
|
||||
export async function fetchCategories() {
|
||||
return request('/api/commerce/categories/');
|
||||
}
|
||||
|
||||
export async function fetchDeliveryZones() {
|
||||
return request('/api/commerce/delivery-zones/');
|
||||
}
|
||||
|
||||
export async function submitOrder(payload) {
|
||||
return request('/api/commerce/orders/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user