Files
ember-market-frontend/lib
NotII 29ec1be68c Refactor API URLs and add environment config example
Replaces hardcoded production API URLs with localhost defaults for local development in both server and client code. Updates Dockerfile to require API URLs via deployment environment variables. Improves ChatTable to use a batch endpoint for chats and unread counts, with backward compatibility. Adds an env.example file to document required environment variables. Updates next.config.mjs to use environment variables for backend API rewrites and image domains.
2025-09-01 15:35:10 +01:00
..
2025-04-08 01:32:00 +01:00
2025-06-29 04:13:50 +01:00
2025-04-07 19:25:24 +01:00

API & Utilities Organization

This directory contains the API client and utility functions used throughout the application.

Directory Structure

lib/
├─ api.ts                # Main API entry point
├─ api-client.ts         # Client-side API functions
├─ server-api.ts         # Server-side API functions
├─ services/             # Service modules
│  ├─ index.ts           # Service re-exports
│  ├─ product-service.ts # Product API
│  ├─ shipping-service.ts # Shipping API
│  └─ stats-service.ts   # Statistics API
├─ utils/                # Utility functions
│  └─ index.ts           # Utility re-exports
├─ types.ts              # Common TypeScript types
├─ utils.ts              # General utilities
├─ auth-utils.ts         # Authentication utilities
└─ styles.ts             # Styling utilities

API Structure

  • api.ts - The main API entry point that re-exports all API functionality
  • api-client.ts - Client-side API functions and types
  • server-api.ts - Server-side API functions (for server components in the app/ directory)

How to Use

In Client Components

// Import what you need from the main API module
import { clientFetch, getCustomers, getProducts } from '@/lib/api';

// Example usage
const customers = await getCustomers(1, 25);
const products = await getProducts(1, 10);

In Server Components (app/ directory only)

// Server functions only work in Server Components in the app/ directory
import { getCustomersServer } from '@/lib/api';

// In a Server Component
export default async function Page() {
  const customers = await getCustomersServer(1, 25);
  // ...
}

Server Components Compatibility

The server-side API functions (fetchServer, getCustomersServer, etc.) can only be used in Server Components within the app/ directory. They will throw an error if used in:

  • Client Components
  • The pages/ directory
  • Any code that runs in the browser

This is because they rely on Next.js server-only features like the cookies() function from next/headers.

Utilities

For utilities, you can either import specific functions or use the utils index:

// Import specific utilities
import { cn } from '@/lib/utils';
import { getAuthToken } from '@/lib/auth-utils';

// Or import from the utils index
import { cn, getAuthToken } from '@/lib/utils';

Backward Compatibility

For backward compatibility, many functions are also re-exported from their original locations.