fix
This commit is contained in:
47
lib/api.ts
47
lib/api.ts
@@ -54,6 +54,14 @@ export {
|
||||
type PlatformStats,
|
||||
} from './services/stats-service';
|
||||
|
||||
// Re-export server API functions
|
||||
export {
|
||||
fetchServer,
|
||||
getCustomersServer,
|
||||
getCustomerDetailsServer,
|
||||
getPlatformStatsServer
|
||||
} from './server-api';
|
||||
|
||||
// Get clientFetch first so we can use it in the compatibility functions
|
||||
import { clientFetch } from './api-client';
|
||||
import { getProductDetails, updateProduct } from './services/product-service';
|
||||
@@ -96,41 +104,4 @@ export const saveProductData = async (productData: any, productId: string, token
|
||||
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