66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import { ProductResponse, CategoryResponse, BrandResponse } from './catalogService';
|
|
|
|
const API_URL =
|
|
process.env.API_URL ||
|
|
process.env.NEXT_PUBLIC_API_URL ||
|
|
'https://ifixkartbe.trionixsolution.com';
|
|
|
|
async function serverFetch<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
|
|
const res = await fetch(`${API_URL}${endpoint}`, {
|
|
...options,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...options.headers,
|
|
},
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`Catalog Server API call failed: ${res.status} ${res.statusText} at ${endpoint}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
export const catalogServerService = {
|
|
async getProducts(): Promise<ProductResponse[]> {
|
|
const data = await serverFetch<any>('/api/v1/catalog/products?page=1&limit=48', {
|
|
next: { revalidate: 30, tags: ['products'] }
|
|
});
|
|
return Array.isArray(data) ? data : data?.products || [];
|
|
},
|
|
|
|
async getProductDetail(slug: string): Promise<{
|
|
product: ProductResponse;
|
|
reviews: any[];
|
|
related_products: ProductResponse[];
|
|
compatible_devices: any[];
|
|
specifications: any[];
|
|
}> {
|
|
return serverFetch<{
|
|
product: ProductResponse;
|
|
reviews: any[];
|
|
related_products: ProductResponse[];
|
|
compatible_devices: any[];
|
|
specifications: any[];
|
|
}>(`/api/v1/catalog/products/detail/${slug}`, {
|
|
next: { revalidate: 60, tags: [`product-${slug}`] }
|
|
});
|
|
},
|
|
|
|
async getCategories(): Promise<CategoryResponse[]> {
|
|
return serverFetch<CategoryResponse[]>('/api/v1/catalog/categories/all', {
|
|
next: { revalidate: 300, tags: ['categories'] }
|
|
});
|
|
},
|
|
|
|
async getBrands(): Promise<BrandResponse[]> {
|
|
return serverFetch<BrandResponse[]>('/api/v1/catalog/brands/all', {
|
|
next: { revalidate: 300, tags: ['brands'] }
|
|
});
|
|
},
|
|
|
|
async fetchLayoutConfigOnServer(): Promise<any> {
|
|
return serverFetch('/api/v1/storefront/layout/home', {
|
|
cache: 'no-store'
|
|
});
|
|
}
|
|
};
|