|
| 1 | +# Schema Deduplication - Changes Summary |
| 2 | + |
| 3 | +## Issue |
| 4 | +@hotlong requested evaluation of whether the new HTTP server and REST API Zod schemas duplicate existing definitions in `spec/api`. |
| 5 | + |
| 6 | +## Analysis Results |
| 7 | + |
| 8 | +### Identified Duplications |
| 9 | + |
| 10 | +1. **CORS Configuration** |
| 11 | + - Found in: `api/router.zod.ts` and `system/http-server.zod.ts` |
| 12 | + - Issue: Similar structure with minor differences (field names: "origin" vs "origins", "dir" vs "directory") |
| 13 | + |
| 14 | +2. **Rate Limiting** |
| 15 | + - Found in: `api/endpoint.zod.ts` and `system/http-server.zod.ts` |
| 16 | + - Issue: Nearly identical schema, one exported, one inline |
| 17 | + |
| 18 | +3. **Static File Serving** |
| 19 | + - Found in: `api/router.zod.ts` and `system/http-server.zod.ts` |
| 20 | + - Issue: Same structure with field name differences |
| 21 | + |
| 22 | +### Resolution |
| 23 | + |
| 24 | +Created a new shared schema file to consolidate duplicated HTTP-related schemas: |
| 25 | + |
| 26 | +**New File:** `packages/spec/src/shared/http.zod.ts` |
| 27 | + |
| 28 | +This file now contains: |
| 29 | +- `CorsConfigSchema` - Unified CORS configuration |
| 30 | +- `RateLimitConfigSchema` - Unified rate limiting configuration |
| 31 | +- `StaticMountSchema` - Unified static file serving configuration |
| 32 | + |
| 33 | +### Files Modified |
| 34 | + |
| 35 | +1. **`packages/spec/src/shared/http.zod.ts`** (NEW) |
| 36 | + - Created new shared HTTP schemas |
| 37 | + - Comprehensive JSDoc documentation |
| 38 | + - Examples for each schema |
| 39 | + |
| 40 | +2. **`packages/spec/src/shared/index.ts`** |
| 41 | + - Added export for `http.zod.ts` |
| 42 | + |
| 43 | +3. **`packages/spec/src/system/http-server.zod.ts`** |
| 44 | + - Removed inline CORS, rate limit, and static schemas |
| 45 | + - Now imports from `shared/http.zod.ts` |
| 46 | + - Standardized field names: "origins" and "directory" |
| 47 | + |
| 48 | +4. **`packages/spec/src/api/router.zod.ts`** |
| 49 | + - Removed inline CORS and static mount schemas |
| 50 | + - Now imports from `shared/http.zod.ts` |
| 51 | + - Standardized field names to match shared schemas |
| 52 | + |
| 53 | +5. **`packages/spec/src/api/endpoint.zod.ts`** |
| 54 | + - Deprecated local `RateLimitSchema` |
| 55 | + - Now re-exports from `shared/http.zod.ts` for backward compatibility |
| 56 | + |
| 57 | +## Benefits |
| 58 | + |
| 59 | +1. ✅ **Eliminated Duplication** - Single source of truth for common HTTP schemas |
| 60 | +2. ✅ **Consistency** - Standardized field names across all uses |
| 61 | +3. ✅ **Maintainability** - Changes to HTTP schemas now only need to be made in one place |
| 62 | +4. ✅ **Documentation** - Centralized documentation for shared schemas |
| 63 | +5. ✅ **Backward Compatibility** - Old `RateLimitSchema` still available (deprecated) |
| 64 | + |
| 65 | +## Breaking Changes |
| 66 | + |
| 67 | +### Field Name Changes (Minor) |
| 68 | + |
| 69 | +**CORS Configuration:** |
| 70 | +- `origin` → `origins` (more semantically correct for multiple origins) |
| 71 | + |
| 72 | +**Static Mounts:** |
| 73 | +- `dir` → `directory` (more explicit and clear) |
| 74 | + |
| 75 | +These changes standardize the naming convention and improve clarity. If there are existing implementations depending on the old field names, they should be updated to use the new names. |
| 76 | + |
| 77 | +## Migration Guide |
| 78 | + |
| 79 | +### For CORS Configuration |
| 80 | +```typescript |
| 81 | +// Before (router.zod.ts) |
| 82 | +cors: { |
| 83 | + origin: 'http://localhost:3000' |
| 84 | +} |
| 85 | + |
| 86 | +// After (shared/http.zod.ts) |
| 87 | +cors: { |
| 88 | + origins: 'http://localhost:3000' // Note: 'origins' instead of 'origin' |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### For Static Mounts |
| 93 | +```typescript |
| 94 | +// Before (router.zod.ts) |
| 95 | +staticMounts: [{ |
| 96 | + path: '/static', |
| 97 | + dir: './public' |
| 98 | +}] |
| 99 | + |
| 100 | +// After (shared/http.zod.ts) |
| 101 | +staticMounts: [{ |
| 102 | + path: '/static', |
| 103 | + directory: './public' // Note: 'directory' instead of 'dir' |
| 104 | +}] |
| 105 | +``` |
| 106 | + |
| 107 | +### For Rate Limiting |
| 108 | +```typescript |
| 109 | +// Before (endpoint.zod.ts or http-server.zod.ts) |
| 110 | +import { RateLimitSchema } from './endpoint.zod'; |
| 111 | + |
| 112 | +// After (shared/http.zod.ts) |
| 113 | +import { RateLimitConfigSchema } from '../shared/http.zod'; |
| 114 | +// Or continue using RateLimitSchema (deprecated) from endpoint.zod.ts |
| 115 | +``` |
| 116 | + |
| 117 | +## Next Steps |
| 118 | + |
| 119 | +1. Update any existing code that uses `origin` to use `origins` |
| 120 | +2. Update any existing code that uses `dir` to use `directory` |
| 121 | +3. Consider adding migration guide to CHANGELOG.md |
| 122 | +4. Run tests to ensure no breaking changes in actual usage |
| 123 | + |
| 124 | +## Notes |
| 125 | + |
| 126 | +- The `HttpMethod` enum was already being imported from `router.zod.ts` in the new files (no duplication) |
| 127 | +- All changes follow the Zod-first architecture pattern |
| 128 | +- Maintained backward compatibility where possible |
0 commit comments