Skip to content

fix: centralize API base URL across all frontend services (Closes #479) - #594

Open
waterWang wants to merge 2 commits into
Protocol-Guild:mainfrom
waterWang:fix/479-consistent-api-base-url
Open

fix: centralize API base URL across all frontend services (Closes #479)#594
waterWang wants to merge 2 commits into
Protocol-Guild:mainfrom
waterWang:fix/479-consistent-api-base-url

Conversation

@waterWang

Copy link
Copy Markdown

Summary

Centralizes all API base URL configuration across the frontend. Previously, 18 different hardcoded localhost fallback URLs (:3000, :3001, :4000, /api, /api/v1, etc.) were scattered across service files, causing silent failures when any non-local deployment pointed to the wrong port. This is the root cause of why mock data appears — API calls fail and silently fallback to mocks.

Changes

Single source of truth (frontend/src/utils/api.ts)

  • Exports API_BASE_URL (via VITE_API_URL env var, fallback http://localhost:4000/api)
  • Exports API_ROOT_URL (strips /api suffix for endpoints outside the API prefix)
  • Both the shared api axios instance and the 401 response interceptor now use these constants

15 service files migrated to shared api instance

File Before After
anchor.ts axios.get + localhost:3000/api api.get('/payments/...')
auditApi.ts axios.get + localhost:3001/api api.get('/audit')
benefitsApi.ts axios.get + localhost:3001/api/v1 + authHeaders() api.get('/v1/benefits/...')
cashFlowForecastApi.ts axios.get + VITE_API_BASE_URL + localhost:3001 + manual Authorization api.get('/cash-flow/...')
certificateApi.ts axios.get + VITE_API_BASE_URL + localhost:3001 api.get('/certificates/...')
contracts.ts axios.get + VITE_API_BASE_URL + localhost:3000 api.get('/contracts')
forecastApi.ts axios.get + localhost:3001/api/v1 api.get('/v1/forecast/...')
pathfinding.ts axios.get + localhost:3000/api api.get('/payments/paths')
scheduleApi.ts axios.get + localhost:3001/api/v1 api.get('/v1/schedules')
taxComplianceApi.ts axios.get + localhost:3001/api/v1 + authHeaders() api.get('/v1/taxes/...')
webhookApi.ts axios.get + localhost:4000/api/v1 + authHeaders() api.get(WEBHOOKS_URL)
withdrawal.ts axios.get + localhost:3000/api api.get('/withdrawal/...')

Non-service files

  • SocketProvider.tsx — now uses API_ROOT_URL instead of VITE_API_URL || 'localhost:3000'
  • Login.tsx — now uses API_ROOT_URL instead of VITE_BACKEND_URL || 'localhost:4000'

Removed

  • Per-file authHeaders() functions from 3 files (shared api instance auto-adds Bearer token)
  • VITE_API_BASE_URL env var — all references changed to VITE_API_URL
  • 196 lines of redundant URL configuration boilerplate

Testing

  • TypeScript compiles clean (tsc --noEmit passes)
  • Single env var VITE_API_URL used everywhere
  • Zero hardcoded localhost URLs in service files
  • Fallback URL is consistent across all services
  • Shared api instance handles auth token + 401 interceptor globally

Closes #479

Add a global axios response interceptor so expired sessions never surface as
silent spinners or empty tables. On 401 (non-auth endpoints) the interceptor:
clears the payd_auth_token from localStorage, shows a sonner toast
'Session expired, please log in again', and redirects to /login.

Registers on both the shared api instance and bare axios calls, with guards
for /auth endpoints and redirect loops. Closes Protocol-Guild#473
Replace 18 hardcoded localhost fallback URLs with a single shared
API_BASE_URL constant from utils/api.ts. Every service file now:
- Uses the shared  axios instance (with unified base URL, auth
  token interceptor, and 401 response interceptor) instead of raw
   with per-file VITE_API_URL or VITE_API_BASE_URL
- Removes per-file API_BASE_URL constants and authHeaders() helpers
- Uses relative paths that work with the shared base URL

Key changes:
- utils/api.ts exports API_BASE_URL and API_ROOT_URL as single source
  of truth, configured via VITE_API_URL env var (fallback localhost:4000/api)
- 15 service files migrated to the shared api instance
- SocketProvider and Login page use API_ROOT_URL for socket/OAuth URLs
- Renamed VITE_API_BASE_URL refs to VITE_API_URL (certificateApi,
  cashFlowForecastApi, contracts.ts)
- Removed authHeaders() functions from 3 files (the shared api instance
  auto-adds the Bearer token via request interceptor)

Closes Protocol-Guild#479
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix inconsistent API base URLs across frontend services

1 participant