Skip to content

🔗 Quick Win 3: Decouple API Path Prefix from Logic #574

Description

@arii
Category Value
Labels maintainability, architecture, typescript
Effort Low

Description

The API URL helper in utils/urls.ts currently hardcodes the /api/ path segment. This violates the DRY (Don't Repeat Yourself) principle and makes it difficult to globally change the API version/prefix (e.g., to /v1/api/) without modifying multiple functions.

Action

Extract the API path prefix into an exported constant within utils/urls.ts for centralized control.

Code Fix (File: utils/urls.ts)

// File: utils/urls.ts (URL Configuration Helper)
// ... existing getBaseURL and getWebSocketURL ...

// === QUICK WIN 3: API CONSTANT EXTRACTION ===
export const API_PREFIX = '/api/' // Define the standardized prefix once
// ============================================

export const getAPIURL = (endpoint: string): string => {
  // Normalize endpoint to remove leading slash
  const cleanEndpoint = endpoint.replace(/^\//, '')
  
  if (typeof window !== 'undefined') {
    // Client-side: use current origin
    return `${window.location.origin}${API_PREFIX}${cleanEndpoint}` 
  }

  // Server-side: use environment variable for internal API calls
  const baseUrl = getBaseURL()
  return `${baseUrl}${API_PREFIX}${cleanEndpoint}`
}

This change requires checking and fixing all other usages of the string '/api/' throughout your codebase to use the new API_PREFIX constant.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions