weewoo
This commit is contained in:
136
lib/api.ts
Normal file
136
lib/api.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
// Re-export client API functions
|
||||
export {
|
||||
// Core client API functions
|
||||
clientFetch,
|
||||
fetchClient,
|
||||
getAuthToken,
|
||||
getCookie,
|
||||
|
||||
// Customer API
|
||||
getCustomers,
|
||||
getCustomerDetails,
|
||||
|
||||
// Types
|
||||
type CustomerStats,
|
||||
type CustomerResponse,
|
||||
} from './api-client';
|
||||
|
||||
// Re-export product services
|
||||
export {
|
||||
getProducts,
|
||||
getProductDetails,
|
||||
createProduct,
|
||||
updateProduct,
|
||||
deleteProduct,
|
||||
uploadProductImage,
|
||||
getProductStock,
|
||||
updateProductStock,
|
||||
|
||||
// Types
|
||||
type Product,
|
||||
type ProductsResponse,
|
||||
type StockData,
|
||||
} from './services/product-service';
|
||||
|
||||
// Re-export shipping services
|
||||
export {
|
||||
getShippingOptions,
|
||||
getShippingOption,
|
||||
createShippingOption,
|
||||
updateShippingOption,
|
||||
deleteShippingOption,
|
||||
|
||||
// Types
|
||||
type ShippingOption,
|
||||
type ShippingOptionsResponse,
|
||||
} from './services/shipping-service';
|
||||
|
||||
// Re-export stats services
|
||||
export {
|
||||
getPlatformStats,
|
||||
getVendorStats,
|
||||
|
||||
// Types
|
||||
type PlatformStats,
|
||||
} from './services/stats-service';
|
||||
|
||||
// Get clientFetch first so we can use it in the compatibility functions
|
||||
import { clientFetch } from './api-client';
|
||||
import { getProductDetails, updateProduct } from './services/product-service';
|
||||
import { getPlatformStats } from './services/stats-service';
|
||||
|
||||
// Add missing functions for backward compatibility
|
||||
// These are functions from the old style that we need to maintain compatibility
|
||||
export const fetchData = async (endpoint: string, options: any = {}) => {
|
||||
console.warn('fetchData is deprecated, use clientFetch instead');
|
||||
return clientFetch(endpoint, options);
|
||||
};
|
||||
|
||||
export const apiRequest = async (endpoint: string, method = 'GET', data: any = null, token: string | null = null) => {
|
||||
console.warn('apiRequest is deprecated, use clientFetch instead');
|
||||
const options: RequestInit & { headers: Record<string, string> } = {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
};
|
||||
|
||||
if (token) {
|
||||
options.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return clientFetch(endpoint, options);
|
||||
};
|
||||
|
||||
// Product-specific compatibility functions
|
||||
export const fetchProductData = async (productId: string, token?: string) => {
|
||||
console.warn('fetchProductData is deprecated, use getProductDetails instead');
|
||||
return getProductDetails(productId);
|
||||
};
|
||||
|
||||
export const saveProductData = async (productData: any, productId: string, token?: string) => {
|
||||
console.warn('saveProductData is deprecated, use updateProduct instead');
|
||||
return updateProduct(productId, productData);
|
||||
};
|
||||
|
||||
// Stats compatibility function
|
||||
export const fetchPlatformStats = async () => {
|
||||
console.warn('fetchPlatformStats is deprecated, use getPlatformStats instead');
|
||||
return getPlatformStats();
|
||||
};
|
||||
|
||||
// Server API functions are conditionally exported
|
||||
// They are only usable in Server Components in the app/ directory
|
||||
// Dynamically check if we can use server components
|
||||
let canUseServerComponents = false;
|
||||
try {
|
||||
// Check if next/headers is available
|
||||
require('next/headers');
|
||||
canUseServerComponents = true;
|
||||
} catch (e) {
|
||||
// We're not in a Server Component context
|
||||
// This is normal in Client Components and pages/ directory
|
||||
}
|
||||
|
||||
// Handle server API functions
|
||||
// Define function types first for TypeScript
|
||||
type ServerFetchFn = <T>(url: string, options?: RequestInit) => Promise<T>;
|
||||
type CustomerServerFn = (options?: any) => Promise<any>;
|
||||
type CustomerDetailServerFn = (id: string, options?: any) => Promise<any>;
|
||||
type PlatformStatsServerFn = () => Promise<any>;
|
||||
|
||||
// Export the functions for use in server components
|
||||
export const fetchServer: ServerFetchFn = canUseServerComponents
|
||||
? require('./server-api').fetchServer
|
||||
: (() => { throw new Error('fetchServer can only be used in Server Components'); }) as any;
|
||||
|
||||
export const getCustomersServer: CustomerServerFn = canUseServerComponents
|
||||
? require('./server-api').getCustomersServer
|
||||
: (() => { throw new Error('getCustomersServer can only be used in Server Components'); }) as any;
|
||||
|
||||
export const getCustomerDetailsServer: CustomerDetailServerFn = canUseServerComponents
|
||||
? require('./server-api').getCustomerDetailsServer
|
||||
: (() => { throw new Error('getCustomerDetailsServer can only be used in Server Components'); }) as any;
|
||||
|
||||
export const getPlatformStatsServer: PlatformStatsServerFn = canUseServerComponents
|
||||
? require('./server-api').getPlatformStatsServer
|
||||
: (() => { throw new Error('getPlatformStatsServer can only be used in Server Components'); }) as any;
|
||||
Reference in New Issue
Block a user