From ee363ed7650f652efc1a07786b6fb679bf57544f Mon Sep 17 00:00:00 2001 From: Fadhlan Date: Thu, 2 Jul 2026 20:18:58 +0700 Subject: [PATCH] fix: allow API base URL override --- README.md | 6 +++++- src/constants/config.test.ts | 30 ++++++++++++++++++++++++++++++ src/constants/config.ts | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/constants/config.test.ts diff --git a/README.md b/README.md index 65a8119..a5a01b9 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,11 @@ npm install npm run dev ``` -The app runs against the live testnet API by default. +The app runs against the live testnet API by default. To use a different backend, set `VITE_API_BASE_URL`: + +```bash +VITE_API_BASE_URL=http://localhost:3000/api/v1 npm run dev +``` No local backend setup is required to start contributing to the frontend. diff --git a/src/constants/config.test.ts b/src/constants/config.test.ts new file mode 100644 index 0000000..a6f29f8 --- /dev/null +++ b/src/constants/config.test.ts @@ -0,0 +1,30 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' + +const DEFAULT_API_BASE_URL = 'https://stepfi-api.onrender.com/api/v1' + +async function importConfig() { + vi.resetModules() + return import('./config') +} + +describe('config', () => { + afterEach(() => { + vi.unstubAllEnvs() + }) + + it('uses the live testnet API by default', async () => { + vi.stubEnv('VITE_API_BASE_URL', undefined) + + const { API_BASE_URL } = await importConfig() + + expect(API_BASE_URL).toBe(DEFAULT_API_BASE_URL) + }) + + it('uses VITE_API_BASE_URL when provided', async () => { + vi.stubEnv('VITE_API_BASE_URL', 'http://localhost:3000/api/v1') + + const { API_BASE_URL } = await importConfig() + + expect(API_BASE_URL).toBe('http://localhost:3000/api/v1') + }) +}) diff --git a/src/constants/config.ts b/src/constants/config.ts index 36cdc80..22334f3 100644 --- a/src/constants/config.ts +++ b/src/constants/config.ts @@ -1,5 +1,5 @@ export const API_BASE_URL = - 'https://stepfi-api.onrender.com/api/v1' + import.meta.env.VITE_API_BASE_URL ?? 'https://stepfi-api.onrender.com/api/v1' export const STELLAR_NETWORK = 'TESTNET'