API Constants

Centralized API endpoints and configuration constants.

API Endpoints

// src/api/constants.ts
export const API_ENDPOINTS = {
  // Authentication
  AUTH: {
    LOGIN: '/auth/login',
    REGISTER: '/auth/register',
    LOGOUT: '/auth/logout',
    REFRESH: '/auth/refresh',
    VERIFY_PHONE: '/auth/verify-phone',
    FORGOT_PASSWORD: '/auth/forgot-password',
  },
  
  // User Management
  USERS: {
    PROFILE: '/users/profile',
    UPDATE_PROFILE: '/users/profile',
    UPLOAD_AVATAR: '/users/avatar',
    PREFERENCES: '/users/preferences',
  },
  
  // Rides
  RIDES: {
    SEARCH: '/rides/search',
    CREATE: '/rides',
    BY_ID: (id: string) => `/rides/${id}`,
    BOOK: (id: string) => `/rides/${id}/book`,
    CANCEL: (id: string) => `/rides/${id}/cancel`,
  },
  
  // Payments
  PAYMENTS: {
    METHODS: '/payments/methods',
    CREATE_INTENT: '/payments/create-intent',
    CONFIRM: '/payments/confirm',
    WALLET: '/payments/wallet',
  },
} as const

HTTP Status Codes

export const HTTP_STATUS = {
  OK: 200,
  CREATED: 201,
  NO_CONTENT: 204,
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  UNPROCESSABLE_ENTITY: 422,
  INTERNAL_SERVER_ERROR: 500,
  SERVICE_UNAVAILABLE: 503,
} as const

Application Constants

User Types and Roles

// src/constants/user.ts
export const USER_ROLES = {
  PASSENGER: 'passenger',
  DRIVER: 'driver',
  ADMIN: 'admin',
} as const

export const USER_STATUS = {
  ACTIVE: 'active',
  INACTIVE: 'inactive',
  SUSPENDED: 'suspended',
  PENDING_VERIFICATION: 'pending_verification',
} as const

export const VERIFICATION_STATUS = {
  PENDING: 'pending',
  VERIFIED: 'verified',
  REJECTED: 'rejected',
} as const

Ride Constants

// src/constants/rides.ts
export const RIDE_STATUS = {
  AVAILABLE: 'available',
  BOOKED: 'booked',
  IN_PROGRESS: 'in_progress',
  COMPLETED: 'completed',
  CANCELLED: 'cancelled',
} as const

export const BOOKING_STATUS = {
  PENDING: 'pending',
  CONFIRMED: 'confirmed',
  CANCELLED: 'cancelled',
  COMPLETED: 'completed',
} as const

export const VEHICLE_TYPES = {
  SEDAN: 'sedan',
  SUV: 'suv',
  HATCHBACK: 'hatchback',
  COUPE: 'coupe',
  MINIVAN: 'minivan',
} as const

export const RIDE_LIMITS = {
  MAX_PASSENGERS: 8,
  MIN_PASSENGERS: 1,
  MAX_SEARCH_RADIUS: 100, // km
  MIN_ADVANCE_BOOKING: 30, // minutes
  MAX_ADVANCE_BOOKING: 30, // days
} as const

UI Constants

Breakpoints and Responsive

// src/constants/ui.ts
export const BREAKPOINTS = {
  SM: '640px',
  MD: '768px',
  LG: '1024px',
  XL: '1280px',
  '2XL': '1536px',
} as const

export const Z_INDEX = {
  DROPDOWN: 1000,
  STICKY: 1020,
  FIXED: 1030,
  MODAL_BACKDROP: 1040,
  MODAL: 1050,
  POPOVER: 1060,
  TOOLTIP: 1070,
  TOAST: 1080,
} as const

export const ANIMATION_DURATION = {
  FAST: 150,
  NORMAL: 300,
  SLOW: 500,
} as const

Form Validation

// src/constants/validation.ts
export const VALIDATION_RULES = {
  PASSWORD: {
    MIN_LENGTH: 8,
    MAX_LENGTH: 128,
    REQUIRE_UPPERCASE: true,
    REQUIRE_LOWERCASE: true,
    REQUIRE_NUMBER: true,
    REQUIRE_SPECIAL_CHAR: false,
  },
  
  PHONE: {
    MIN_LENGTH: 10,
    MAX_LENGTH: 15,
    PATTERN: /^\+[1-9]\d{1,14}$/,
  },
  
  FILE_UPLOAD: {
    MAX_SIZE: 5 * 1024 * 1024, // 5MB
    ALLOWED_TYPES: ['image/jpeg', 'image/png', 'image/webp'],
    MAX_FILES: 5,
  },
  
  TEXT_FIELDS: {
    NAME: {
      MIN_LENGTH: 2,
      MAX_LENGTH: 50,
    },
    DESCRIPTION: {
      MIN_LENGTH: 10,
      MAX_LENGTH: 500,
    },
  },
} as const

Business Logic Constants

Payment and Pricing

// src/constants/payments.ts
export const PAYMENT_METHODS = {
  CREDIT_CARD: 'credit_card',
  DEBIT_CARD: 'debit_card',
  PAYPAL: 'paypal',
  WALLET: 'wallet',
  CASH: 'cash',
} as const

export const CURRENCY = {
  USD: 'USD',
  EUR: 'EUR',
  GBP: 'GBP',
} as const

export const PRICING = {
  BASE_RATE: 0.5, // per km
  TIME_RATE: 0.2, // per minute
  BOOKING_FEE: 2.0,
  PLATFORM_FEE_PERCENTAGE: 0.05, // 5%
  CANCELLATION_FEE: 5.0,
} as const

Notification Types

// src/constants/notifications.ts
export const NOTIFICATION_TYPES = {
  RIDE_BOOKED: 'ride_booked',
  RIDE_CANCELLED: 'ride_cancelled',
  RIDE_REMINDER: 'ride_reminder',
  PAYMENT_SUCCESS: 'payment_success',
  PAYMENT_FAILED: 'payment_failed',
  MESSAGE_RECEIVED: 'message_received',
  DRIVER_ARRIVED: 'driver_arrived',
} as const

export const NOTIFICATION_PRIORITY = {
  LOW: 'low',
  NORMAL: 'normal',
  HIGH: 'high',
  URGENT: 'urgent',
} as const

Error Constants

Error Codes and Messages

// src/constants/errors.ts
export const ERROR_CODES = {
  // Authentication
  INVALID_CREDENTIALS: 'AUTH_INVALID_CREDENTIALS',
  TOKEN_EXPIRED: 'AUTH_TOKEN_EXPIRED',
  UNAUTHORIZED: 'AUTH_UNAUTHORIZED',
  
  // Validation
  VALIDATION_FAILED: 'VALIDATION_FAILED',
  REQUIRED_FIELD: 'REQUIRED_FIELD',
  INVALID_FORMAT: 'INVALID_FORMAT',
  
  // Business Logic
  RIDE_NOT_AVAILABLE: 'RIDE_NOT_AVAILABLE',
  INSUFFICIENT_BALANCE: 'INSUFFICIENT_BALANCE',
  BOOKING_LIMIT_EXCEEDED: 'BOOKING_LIMIT_EXCEEDED',
  
  // System
  NETWORK_ERROR: 'NETWORK_ERROR',
  SERVER_ERROR: 'SERVER_ERROR',
  UNKNOWN_ERROR: 'UNKNOWN_ERROR',
} as const

export const ERROR_MESSAGES = {
  [ERROR_CODES.INVALID_CREDENTIALS]: 'Invalid email or password',
  [ERROR_CODES.TOKEN_EXPIRED]: 'Your session has expired. Please sign in again.',
  [ERROR_CODES.RIDE_NOT_AVAILABLE]: 'This ride is no longer available',
  [ERROR_CODES.INSUFFICIENT_BALANCE]: 'Insufficient wallet balance',
  [ERROR_CODES.NETWORK_ERROR]: 'Network connection error. Please try again.',
} as const

Feature Flags

Feature Toggle Constants

// src/constants/features.ts
export const FEATURE_FLAGS = {
  ENABLE_CHAT: 'enable_chat',
  ENABLE_PAYMENTS: 'enable_payments',
  ENABLE_PUSH_NOTIFICATIONS: 'enable_push_notifications',
  ENABLE_GEOLOCATION: 'enable_geolocation',
  ENABLE_SOCIAL_LOGIN: 'enable_social_login',
  ENABLE_DRIVER_VERIFICATION: 'enable_driver_verification',
  ENABLE_RATING_SYSTEM: 'enable_rating_system',
} as const

export const DEFAULT_FEATURES = {
  [FEATURE_FLAGS.ENABLE_CHAT]: true,
  [FEATURE_FLAGS.ENABLE_PAYMENTS]: true,
  [FEATURE_FLAGS.ENABLE_PUSH_NOTIFICATIONS]: false,
  [FEATURE_FLAGS.ENABLE_GEOLOCATION]: true,
  [FEATURE_FLAGS.ENABLE_SOCIAL_LOGIN]: false,
  [FEATURE_FLAGS.ENABLE_DRIVER_VERIFICATION]: true,
  [FEATURE_FLAGS.ENABLE_RATING_SYSTEM]: true,
} as const

Usage Guidelines

Best Practices

  • Use TypeScript's `const assertions` for type safety
  • Group related constants in separate files
  • Use UPPER_CASE for constant values
  • Create enums for related values
  • Document complex constants with comments

Type Safety

Using Constants with Types

// Create types from constants
export type UserRole = typeof USER_ROLES[keyof typeof USER_ROLES]
export type RideStatus = typeof RIDE_STATUS[keyof typeof RIDE_STATUS]
export type PaymentMethod = typeof PAYMENT_METHODS[keyof typeof PAYMENT_METHODS]

// Usage in functions
function updateRideStatus(rideId: string, status: RideStatus) {
  // TypeScript will enforce valid status values
}

// Usage in components
interface RideCardProps {
  status: RideStatus
  paymentMethod: PaymentMethod
}