|
| 1 | +# API Prefix v1 Implementation Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document describes the implementation of the `/v1` API prefix for the Mux Backend application, as per the bootstrap requirements. All API endpoints are now served under the `/v1` path. |
| 5 | + |
| 6 | +## Changes Made |
| 7 | + |
| 8 | +### 1. Core Bootstrap Change (main.ts) |
| 9 | +**File**: `src/main.ts` |
| 10 | + |
| 11 | +The global API prefix was added to the NestJS application bootstrap process: |
| 12 | + |
| 13 | +```typescript |
| 14 | +async function bootstrap() { |
| 15 | + const app = await NestFactory.create(AppModule); |
| 16 | + app.use(requestLogger as any); |
| 17 | + |
| 18 | + // Set global API prefix for versioning |
| 19 | + app.setGlobalPrefix('v1'); |
| 20 | + |
| 21 | + await app.listen(process.env.PORT ?? 3000); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +**Impact**: All endpoints are now automatically prefixed with `/v1/` |
| 26 | + |
| 27 | +### 2. Updated Endpoint Routes |
| 28 | + |
| 29 | +All existing API routes now include the `/v1` prefix: |
| 30 | + |
| 31 | +#### Public Endpoints |
| 32 | +- `GET /v1/` - Root endpoint |
| 33 | +- `GET /v1/ready` - Readiness probe (returns 200/503 based on DB connectivity) |
| 34 | +- `GET /v1/health` - Health check endpoint |
| 35 | + |
| 36 | +#### Authentication Endpoints |
| 37 | +- `POST /v1/auth/authenticate` - User authentication and wallet creation (public) |
| 38 | + |
| 39 | +#### Resource Endpoints |
| 40 | +- `/v1/users/*` - User management |
| 41 | +- `/v1/wallets/*` - Wallet management |
| 42 | +- `/v1/payments/*` - Payment processing |
| 43 | +- `/v1/api-keys/*` - API key management |
| 44 | +- `/v1/developers/*` - Developer information |
| 45 | +- `/v1/projects/*` - Project management |
| 46 | +- `/v1/limits/*` - Rate limit configuration |
| 47 | +- `/v1/recovery/*` - Account recovery |
| 48 | +- `/v1/transactions/*` - Transaction history |
| 49 | +- `/v1/balances/*` - Balance indexing |
| 50 | +- `/v1/webhooks/*` - Webhook management |
| 51 | +- `/v1/internal/key-management/*` - Key management (internal) |
| 52 | + |
| 53 | +## Test Changes |
| 54 | + |
| 55 | +### 1. Updated Existing Tests |
| 56 | + |
| 57 | +The following E2E test files were updated to use the new `/v1` prefix: |
| 58 | + |
| 59 | +#### test/app.e2e-spec.ts |
| 60 | +- Updated root endpoint test: `GET /v1/` |
| 61 | +- Updated readiness test: `GET /v1/ready` |
| 62 | +- All path assertions now expect `/v1/` prefix |
| 63 | + |
| 64 | +#### test/auth-public-endpoint.e2e-spec.ts |
| 65 | +- Updated auth endpoint test: `POST /v1/auth/authenticate` |
| 66 | +- Verified public access without authentication still works |
| 67 | + |
| 68 | +#### test/error-handling.e2e-spec.ts |
| 69 | +- Updated all error path tests to use `/v1/` prefix |
| 70 | +- Verified error responses include correct prefixed paths |
| 71 | +- Updated all HTTP method tests (GET, POST, PUT, PATCH, DELETE) |
| 72 | + |
| 73 | +#### test/wallets.e2e-spec.ts |
| 74 | +- Updated wallet endpoint test: `GET /v1/wallets/protected` |
| 75 | +- Verified API key authentication still works with prefix |
| 76 | + |
| 77 | +### 2. New Comprehensive Test Suite |
| 78 | + |
| 79 | +Created `test/api-prefix-v1.e2e-spec.ts` to comprehensively verify: |
| 80 | + |
| 81 | +**Global Prefix Verification** |
| 82 | +- Root endpoint serves at `/v1/` |
| 83 | +- Endpoints without prefix return 404 |
| 84 | +- Public endpoints accessible without authentication |
| 85 | +- Health and readiness probes work with prefix |
| 86 | + |
| 87 | +**Controller Route Coverage** |
| 88 | +- All major controller routes respond with `/v1` prefix: |
| 89 | + - Auth routes |
| 90 | + - Users routes |
| 91 | + - Wallets routes |
| 92 | + - API keys routes |
| 93 | + - Developers routes |
| 94 | + - Projects routes |
| 95 | + |
| 96 | +**Error Handling** |
| 97 | +- Error responses include `/v1` prefix in path |
| 98 | +- 404 responses for routes without prefix |
| 99 | + |
| 100 | +**Public Endpoint Accessibility** |
| 101 | +- Root endpoint accessible without auth |
| 102 | +- Readiness probe accessible without auth |
| 103 | +- Health check accessible without auth |
| 104 | +- Auth endpoint accessible without API key |
| 105 | + |
| 106 | +**HTTP Methods** |
| 107 | +- GET, POST, PUT, PATCH, DELETE all work with prefix |
| 108 | +- Requests without prefix return 404 |
| 109 | + |
| 110 | +## Behavior Summary |
| 111 | + |
| 112 | +### Before Implementation |
| 113 | +``` |
| 114 | +GET / → 200 (Hello World!) |
| 115 | +GET /ready → 200 (readiness status) |
| 116 | +GET /health → 200 (health check) |
| 117 | +POST /auth/authenticate → processes authentication |
| 118 | +GET /users → returns users |
| 119 | +``` |
| 120 | + |
| 121 | +### After Implementation |
| 122 | +``` |
| 123 | +GET /v1/ → 200 (Hello World!) |
| 124 | +GET /v1/ready → 200 (readiness status) |
| 125 | +GET /v1/health → 200 (health check) |
| 126 | +POST /v1/auth/authenticate → processes authentication |
| 127 | +GET /v1/users → returns users |
| 128 | +
|
| 129 | +GET / → 404 (Not Found) |
| 130 | +GET /ready → 404 (Not Found) |
| 131 | +GET /health → 404 (Not Found) |
| 132 | +POST /auth/authenticate → 404 (Not Found) |
| 133 | +``` |
| 134 | + |
| 135 | +## Testing Instructions |
| 136 | + |
| 137 | +### Run E2E Tests |
| 138 | +```bash |
| 139 | +# Run all e2e tests (includes existing tests + new prefix verification tests) |
| 140 | +pnpm test:e2e |
| 141 | + |
| 142 | +# Run specific test file |
| 143 | +pnpm test:e2e -- test/api-prefix-v1.e2e-spec.ts |
| 144 | + |
| 145 | +# Run with verbose output |
| 146 | +pnpm test:e2e -- --verbose |
| 147 | + |
| 148 | +# Run with coverage |
| 149 | +pnpm test:e2e -- --coverage |
| 150 | +``` |
| 151 | + |
| 152 | +### Run Unit Tests |
| 153 | +```bash |
| 154 | +pnpm test |
| 155 | +``` |
| 156 | + |
| 157 | +### Run All Tests |
| 158 | +```bash |
| 159 | +# Run all tests (unit + e2e) |
| 160 | +pnpm test:e2e && pnpm test |
| 161 | +``` |
| 162 | + |
| 163 | +## Verification Checklist |
| 164 | + |
| 165 | +Use these steps to verify the implementation is working correctly: |
| 166 | + |
| 167 | +### 1. Start the Application |
| 168 | +```bash |
| 169 | +pnpm start:dev |
| 170 | +``` |
| 171 | + |
| 172 | +### 2. Verify Endpoints with Curl |
| 173 | + |
| 174 | +**Public Endpoints** |
| 175 | +```bash |
| 176 | +# Root endpoint |
| 177 | +curl http://localhost:3000/v1/ |
| 178 | +# Expected: Hello World! |
| 179 | + |
| 180 | +# Readiness probe |
| 181 | +curl http://localhost:3000/v1/ready |
| 182 | +# Expected: JSON with status: "ready" |
| 183 | + |
| 184 | +# Health check |
| 185 | +curl http://localhost:3000/v1/health |
| 186 | +# Expected: JSON with health status |
| 187 | +``` |
| 188 | + |
| 189 | +**Authentication Endpoint** |
| 190 | +```bash |
| 191 | +curl -X POST http://localhost:3000/v1/auth/authenticate \ |
| 192 | + -H "Content-Type: application/json" \ |
| 193 | + -d '{ |
| 194 | + "authId": "test-123", |
| 195 | + "email": "test@example.com", |
| 196 | + "displayName": "Test User", |
| 197 | + "authProvider": "CLERK", |
| 198 | + "network": "TESTNET" |
| 199 | + }' |
| 200 | +# Expected: User and wallet data (or error if validation fails) |
| 201 | +``` |
| 202 | + |
| 203 | +**Verify 404 for Non-Prefixed Routes** |
| 204 | +```bash |
| 205 | +curl http://localhost:3000/ |
| 206 | +# Expected: 404 Not Found |
| 207 | + |
| 208 | +curl http://localhost:3000/ready |
| 209 | +# Expected: 404 Not Found |
| 210 | + |
| 211 | +curl http://localhost:3000/health |
| 212 | +# Expected: 404 Not Found |
| 213 | +``` |
| 214 | + |
| 215 | +### 3. Run Tests |
| 216 | +Execute the comprehensive test suite: |
| 217 | +```bash |
| 218 | +pnpm test:e2e |
| 219 | +``` |
| 220 | + |
| 221 | +Expected outcome: All tests pass, including: |
| 222 | +- ✓ Root endpoint at `/v1/` returns content |
| 223 | +- ✓ Root endpoint without prefix returns 404 |
| 224 | +- ✓ Readiness endpoint at `/v1/ready` works |
| 225 | +- ✓ Health endpoint at `/v1/health` works |
| 226 | +- ✓ Auth endpoint at `/v1/auth/authenticate` is public |
| 227 | +- ✓ All controller routes have `/v1` prefix |
| 228 | +- ✓ Error responses include correct paths |
| 229 | +- ✓ Public endpoints remain accessible without auth |
| 230 | + |
| 231 | +## Backward Compatibility Considerations |
| 232 | + |
| 233 | +### Breaking Changes |
| 234 | +The `/v1` prefix is a **breaking change** for existing API consumers: |
| 235 | +- All existing client implementations must update their API endpoints |
| 236 | +- Old endpoints (without `/v1`) will return 404 Not Found |
| 237 | + |
| 238 | +### Migration Guide for Clients |
| 239 | +Update all API calls from: |
| 240 | +``` |
| 241 | +http://api.example.com/endpoint |
| 242 | +``` |
| 243 | +to: |
| 244 | +``` |
| 245 | +http://api.example.com/v1/endpoint |
| 246 | +``` |
| 247 | + |
| 248 | +Examples: |
| 249 | +- `https://api.example.com/auth/authenticate` → `https://api.example.com/v1/auth/authenticate` |
| 250 | +- `https://api.example.com/wallets` → `https://api.example.com/v1/wallets` |
| 251 | +- `https://api.example.com/users` → `https://api.example.com/v1/users` |
| 252 | + |
| 253 | +## Future Versioning |
| 254 | + |
| 255 | +This implementation enables future API versioning strategies: |
| 256 | +- `/v2` endpoints can be added by creating new controller prefixes |
| 257 | +- Both `/v1` and `/v2` can coexist using separate prefixes per controller |
| 258 | +- The global prefix can be made configurable via environment variables if needed |
| 259 | + |
| 260 | +Example for future versions: |
| 261 | +```typescript |
| 262 | +const apiVersion = process.env.API_VERSION ?? 'v1'; |
| 263 | +app.setGlobalPrefix(apiVersion); |
| 264 | +``` |
| 265 | + |
| 266 | +## Related Files Modified |
| 267 | + |
| 268 | +### Core Application |
| 269 | +- `src/main.ts` - Bootstrap with global prefix |
| 270 | + |
| 271 | +### Test Files |
| 272 | +- `test/app.e2e-spec.ts` - Updated to use `/v1` |
| 273 | +- `test/auth-public-endpoint.e2e-spec.ts` - Updated to use `/v1` |
| 274 | +- `test/error-handling.e2e-spec.ts` - Updated to use `/v1` |
| 275 | +- `test/wallets.e2e-spec.ts` - Updated to use `/v1` |
| 276 | +- `test/api-prefix-v1.e2e-spec.ts` - New comprehensive test suite |
| 277 | + |
| 278 | +### No Changes Required |
| 279 | +- Controllers remain unchanged (no modifications to `@Controller()` decorators) |
| 280 | +- Services remain unchanged |
| 281 | +- Middleware remains unchanged |
| 282 | +- Guards and interceptors remain unchanged |
| 283 | + |
| 284 | +## Acceptance Criteria - Met |
| 285 | + |
| 286 | +✅ **Behavior is covered by tests** |
| 287 | +- Added comprehensive test suite (api-prefix-v1.e2e-spec.ts) |
| 288 | +- Updated all existing e2e tests to verify new routes |
| 289 | +- Tests verify prefix application, public access, error handling, and HTTP methods |
| 290 | + |
| 291 | +✅ **Documented where APIs changed** |
| 292 | +- This document provides complete API endpoint changes |
| 293 | +- All endpoints now require `/v1` prefix |
| 294 | +- Migration guide provided for clients |
| 295 | + |
| 296 | +✅ **No regressions in closely related flows** |
| 297 | +- All existing functionality works with new prefix |
| 298 | +- Public endpoints remain public (no new auth requirements) |
| 299 | +- Error handling unchanged (only path representation differs) |
| 300 | +- Rate limiting, API key validation unchanged |
| 301 | + |
| 302 | +✅ **Follows existing patterns in repository** |
| 303 | +- Uses NestJS built-in `setGlobalPrefix()` method |
| 304 | +- Follows repository's testing patterns (Jest + Supertest) |
| 305 | +- Maintains existing module and security structure |
| 306 | + |
| 307 | +✅ **Handles edge cases gracefully** |
| 308 | +- Returns proper 404 for non-prefixed routes |
| 309 | +- Preserves authentication requirements |
| 310 | +- Maintains error response structure and logging |
| 311 | +- Request tracking (x-request-id) still works |
| 312 | + |
| 313 | +## Summary |
| 314 | + |
| 315 | +The `/v1` API prefix has been successfully implemented as a global prefix applied to all endpoints during application bootstrap. This provides: |
| 316 | + |
| 317 | +1. **Clear Versioning**: APIs now explicitly indicate they are v1 |
| 318 | +2. **Future Compatibility**: Enables multiple API versions to coexist |
| 319 | +3. **Professional API Design**: Follows REST API best practices |
| 320 | +4. **Full Test Coverage**: Comprehensive test suite verifies all aspects |
| 321 | +5. **No Breaking Internal Changes**: Controllers, services, and guards remain unchanged |
| 322 | +6. **Graceful Fallback**: Non-prefixed routes properly return 404 |
| 323 | + |
| 324 | +All acceptance criteria have been met, and the implementation is ready for deployment. |
0 commit comments