|
| 1 | +# Express API Refactoring Todo |
| 2 | + |
| 3 | +## Phase 1: Add Programmatic API (Foundation) |
| 4 | + |
| 5 | +### 1.1 Create Configuration Interface |
| 6 | +- [x] Create `src/types.ts` with `ExpressServerConfig` interface |
| 7 | +- [x] Define configuration structure for CouchDB, ports, CORS, etc. |
| 8 | +- [x] Add TypeScript types for all configuration options |
| 9 | + |
| 10 | +**Summary**: Created comprehensive type definitions including: |
| 11 | +- `ExpressServerConfig` - Main interface for programmatic usage with CouchDB config, optional port, version, nodeEnv, CORS settings |
| 12 | +- `EnvironmentConfig` - Internal bridge type for existing env var usage |
| 13 | +- `ServerStartResult` - Return type for server startup with port/URL info |
| 14 | +- Full JSDoc documentation for all interfaces and properties |
| 15 | + |
| 16 | +### 1.2 Extract App Creation Logic |
| 17 | +- [x] **Keep existing `src/app.ts` unchanged** (platform usage) |
| 18 | +- [x] Create `src/app-factory.ts` with shared Express app creation logic |
| 19 | +- [x] Extract common app setup code from existing `src/app.ts` |
| 20 | +- [x] Ensure both standalone and programmatic modes use same logic |
| 21 | + |
| 22 | +**Summary**: Created `app-factory.ts` with shared Express app creation logic: |
| 23 | +- `createExpressApp(config)` - Accepts both ExpressServerConfig and EnvironmentConfig |
| 24 | +- `initializeServices()` - Extracted async initialization logic for background services |
| 25 | +- Type guards to handle dual configuration formats (programmatic vs env vars) |
| 26 | +- Config conversion utilities to bridge between formats |
| 27 | +- All routes and middleware extracted from original app.ts |
| 28 | +- VueClientRequest interface moved to factory for shared usage |
| 29 | + |
| 30 | +### 1.2b Refactor Existing App to Use Factory |
| 31 | +- [x] Refactor `src/app.ts` to use `createExpressApp()` from app-factory |
| 32 | +- [x] Replace duplicated middleware/routes with factory function call |
| 33 | +- [x] Use `initializeServices()` instead of inline init() function |
| 34 | +- [x] Ensure existing platform behavior unchanged |
| 35 | +- [x] Test that `yarn dev` still works correctly |
| 36 | + |
| 37 | +**Summary**: Completely refactored standalone app.ts to eliminate duplication: |
| 38 | +- Reduced from ~264 lines to ~32 lines (87% reduction) |
| 39 | +- Uses `createExpressApp(ENV)` with environment config |
| 40 | +- Uses `initializeServices()` for background initialization |
| 41 | +- Preserves original port (3000), logging, and error handling behavior |
| 42 | +- Both modes now guaranteed to use identical Express configuration and routes |
| 43 | + |
| 44 | +**Note**: This eliminates code duplication and ensures both standalone and programmatic modes use identical logic. |
| 45 | + |
| 46 | +### 1.3 Révisée: Create Public API with Factory Functions |
| 47 | +- [x] Create `src/index.ts` as main package entry point |
| 48 | +- [x] Export `createExpressApp` and `initializeServices` functions |
| 49 | +- [x] Export type definitions (`ExpressServerConfig`, `VueClientRequest`, etc.) |
| 50 | +- [x] Update `package.json` main/types entries to point to index |
| 51 | +- [x] Add exports map for subpath compatibility (`./app` for standalone usage) |
| 52 | +- [x] Test build and verify API exports work correctly |
| 53 | + |
| 54 | +**Summary**: Created clean factory-based public API: |
| 55 | +- Main entry: `src/index.ts` with `createExpressApp()` and `initializeServices()` |
| 56 | +- Type exports: `ExpressServerConfig`, `VueClientRequest`, `AppConfig`, `ServerStartResult` |
| 57 | +- Backwards compatibility: `./app` export for direct standalone server access |
| 58 | +- Package.json updated: main/types point to dist/index.js/d.ts |
| 59 | +- Build successful, dev mode still works correctly |
| 60 | + |
| 61 | +### 1.4 Add Dual Configuration Support |
| 62 | +- [x] **Keep existing `src/utils/env.ts` unchanged** (platform usage) |
| 63 | +- [x] Add config object support to app factory |
| 64 | +- [x] Create utility to convert between env vars and config objects |
| 65 | +- [x] Ensure both configuration methods work with same app logic |
| 66 | + |
| 67 | +**Summary**: Dual configuration already implemented in app-factory.ts: |
| 68 | +- `AppConfig` type accepts both `ExpressServerConfig` and `EnvironmentConfig` |
| 69 | +- Type guards (`isExpressServerConfig()`) distinguish between formats |
| 70 | +- `convertToEnvConfig()` utility bridges programmatic → environment format |
| 71 | +- Both standalone (env vars) and programmatic (config objects) modes supported |
| 72 | + |
| 73 | +## Phase 2: Package Structure (Exports) ✅ COMPLETED |
| 74 | + |
| 75 | +### 2.1 Create Main Export File |
| 76 | +- [x] Create `src/index.ts` as main package entry point |
| 77 | +- [x] Export factory functions (`createExpressApp`, `initializeServices`) |
| 78 | +- [x] Export type definitions (`ExpressServerConfig`, `VueClientRequest`, etc.) |
| 79 | +- [x] Export utility types for both programmatic and environment usage |
| 80 | + |
| 81 | +### 2.2 Update Package Configuration |
| 82 | +- [x] Update `package.json` main entry to `dist/index.js` |
| 83 | +- [x] Update `package.json` types entry to `dist/index.d.ts` |
| 84 | +- [x] Add proper exports map for subpath exports |
| 85 | +- [x] Ensure backwards compatibility exports for `./app` |
| 86 | + |
| 87 | +### 2.3 Platform Compatibility |
| 88 | +- [x] Verify existing `src/app.ts` standalone execution still works |
| 89 | +- [x] Preserve current `yarn dev` behavior for monorepo usage |
| 90 | +- [x] Test that existing platform Express usage unchanged |
| 91 | + |
| 92 | +**Phase 2 Summary**: Package structure complete with clean exports and full backwards compatibility. |
| 93 | + |
| 94 | +## Phase 3: CLI Integration (Remove Embedding) |
| 95 | + |
| 96 | +### 3.1 Update CLI Dependencies |
| 97 | +- [ ] Add `@vue-skuilder/express` to CLI's `package.json` dependencies |
| 98 | +- [ ] Remove Express from devDependencies (it was in there for embedding) |
| 99 | +- [ ] Update CLI's TypeScript imports to use new API |
| 100 | + |
| 101 | +### 3.2 Refactor Studio Command |
| 102 | +- [ ] Replace `ExpressManager` embedded approach with direct import |
| 103 | +- [ ] Update `src/commands/studio.ts` to use `SkuilderExpressServer` |
| 104 | +- [ ] Pass CouchDB configuration dynamically to Express server |
| 105 | +- [ ] Handle port assignment and URL reporting |
| 106 | + |
| 107 | +### 3.3 Remove Embedding Infrastructure |
| 108 | +- [ ] Remove `embed:express` script from CLI's `package.json` |
| 109 | +- [ ] Remove `build:express` script from CLI's `package.json` |
| 110 | +- [ ] Update main build script to exclude Express embedding |
| 111 | +- [ ] Clean up `ExpressManager.js` utility (if no longer needed) |
| 112 | + |
| 113 | +## Phase 4: Testing & Validation |
| 114 | + |
| 115 | +### 4.1 Monorepo Testing |
| 116 | +- [ ] Test Express server directly: `yarn workspace @vue-skuilder/express dev` |
| 117 | +- [ ] Test CLI studio command in monorepo: `yarn studio` |
| 118 | +- [ ] Verify CouchDB integration still works |
| 119 | +- [ ] Verify all Express endpoints respond correctly |
| 120 | + |
| 121 | +### 4.2 External Package Testing |
| 122 | +- [ ] Build and publish packages locally for testing |
| 123 | +- [ ] Test CLI via `npx` in external project directory |
| 124 | +- [ ] Verify `yarn studio` works without embedding errors |
| 125 | +- [ ] Test that all Express dependencies resolve correctly |
| 126 | + |
| 127 | +### 4.3 Edge Case Testing |
| 128 | +- [ ] Test multiple concurrent studio sessions (port conflicts) |
| 129 | +- [ ] Test Express server shutdown and cleanup |
| 130 | +- [ ] Test error handling for invalid configurations |
| 131 | +- [ ] Verify memory leaks don't occur with start/stop cycles |
| 132 | + |
| 133 | +## Phase 5: Documentation & Cleanup |
| 134 | + |
| 135 | +### 5.1 Update Documentation |
| 136 | +- [ ] Update Express package `CLAUDE.md` with new API usage |
| 137 | +- [ ] Update CLI package `CLAUDE.md` with Express integration changes |
| 138 | +- [ ] Document migration path for any external users |
| 139 | +- [ ] Add JSDoc comments to public API methods |
| 140 | + |
| 141 | +### 5.2 Code Cleanup |
| 142 | +- [ ] Remove old embedded Express assets from CLI dist |
| 143 | +- [ ] Clean up any unused utility files |
| 144 | +- [ ] Update TypeScript build configs if needed |
| 145 | +- [ ] Run linting and fix any issues |
| 146 | + |
| 147 | +### 5.3 Version Management |
| 148 | +- [ ] Coordinate version bumps for both Express and CLI packages |
| 149 | +- [ ] Update `vtag.js` if needed for new dependency relationship |
| 150 | +- [ ] Test that version transformations still work correctly |
| 151 | + |
| 152 | +## Success Criteria |
| 153 | + |
| 154 | +- [ ] CLI can be used via `npx @vue-skuilder/cli studio` without dependency errors |
| 155 | +- [ ] Express server starts and stops programmatically via CLI |
| 156 | +- [ ] All existing Express API endpoints work correctly |
| 157 | +- [ ] Monorepo development workflow unchanged |
| 158 | +- [ ] No increase in CLI package size (should decrease) |
| 159 | +- [ ] Express server supports concurrent sessions with different ports |
| 160 | +- [ ] Studio mode workflow functions end-to-end |
0 commit comments