bxo 0.0.5-dev.50 โ 0.0.5-dev.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/REFACTOR_README.md +209 -0
- package/index.ts +4 -1554
- package/package.json +9 -1
- package/src/core/bxo.ts +437 -0
- package/src/handlers/request-handler.ts +229 -0
- package/src/index.ts +54 -0
- package/src/types/index.ts +170 -0
- package/src/utils/context-factory.ts +158 -0
- package/src/utils/helpers.ts +40 -0
- package/src/utils/index.ts +258 -0
- package/src/utils/response-handler.ts +216 -0
- package/src/utils/route-matcher.ts +191 -0
- package/tests/README.md +359 -0
- package/tests/integration/bxo.test.ts +413 -0
- package/tests/run-tests.ts +44 -0
- package/tests/unit/context-factory.test.ts +386 -0
- package/tests/unit/helpers.test.ts +253 -0
- package/tests/unit/response-handler.test.ts +301 -0
- package/tests/unit/route-matcher.test.ts +181 -0
- package/tests/unit/utils.test.ts +310 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# BXO Framework - Refactored Structure
|
|
2
|
+
|
|
3
|
+
The BXO framework has been completely refactored to be more modular, maintainable, and easier to understand. The functionality remains exactly the same, but the code is now organized into logical modules.
|
|
4
|
+
|
|
5
|
+
## ๐๏ธ New Directory Structure
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
src/
|
|
9
|
+
โโโ core/ # Core BXO class and main logic
|
|
10
|
+
โโโ types/ # TypeScript type definitions
|
|
11
|
+
โโโ utils/ # Utility functions and helpers
|
|
12
|
+
โโโ handlers/ # Request handling logic
|
|
13
|
+
โโโ plugins/ # Plugin system (existing)
|
|
14
|
+
โโโ middleware/ # Middleware system (existing)
|
|
15
|
+
โโโ index.ts # Main export file
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## ๐ Module Breakdown
|
|
19
|
+
|
|
20
|
+
### `src/types/index.ts`
|
|
21
|
+
- All TypeScript interfaces and types
|
|
22
|
+
- Zod schema type utilities
|
|
23
|
+
- Context, Route, and Plugin interfaces
|
|
24
|
+
- File upload type definitions
|
|
25
|
+
|
|
26
|
+
### `src/utils/index.ts`
|
|
27
|
+
- Core utility functions for parsing requests
|
|
28
|
+
- Validation helpers
|
|
29
|
+
- Cookie handling utilities
|
|
30
|
+
- File upload utilities
|
|
31
|
+
|
|
32
|
+
### `src/utils/route-matcher.ts`
|
|
33
|
+
- Route matching logic for HTTP routes
|
|
34
|
+
- WebSocket route matching
|
|
35
|
+
- Wildcard and parameter handling
|
|
36
|
+
|
|
37
|
+
### `src/utils/context-factory.ts`
|
|
38
|
+
- Context object creation
|
|
39
|
+
- Validation integration
|
|
40
|
+
- Cookie management
|
|
41
|
+
|
|
42
|
+
### `src/utils/response-handler.ts`
|
|
43
|
+
- Response processing and formatting
|
|
44
|
+
- Error response creation
|
|
45
|
+
- File and Bun.file handling
|
|
46
|
+
|
|
47
|
+
### `src/utils/helpers.ts`
|
|
48
|
+
- Helper functions (error, file, redirect)
|
|
49
|
+
- Cookie options utilities
|
|
50
|
+
|
|
51
|
+
### `src/handlers/request-handler.ts`
|
|
52
|
+
- Main request processing logic
|
|
53
|
+
- Middleware and hook execution
|
|
54
|
+
- WebSocket upgrade handling
|
|
55
|
+
- Error handling
|
|
56
|
+
|
|
57
|
+
### `src/core/bxo.ts`
|
|
58
|
+
- Main BXO class
|
|
59
|
+
- Route registration methods
|
|
60
|
+
- Server lifecycle management
|
|
61
|
+
- Plugin system integration
|
|
62
|
+
|
|
63
|
+
## ๐ Migration Guide
|
|
64
|
+
|
|
65
|
+
### Before (Old Structure)
|
|
66
|
+
```typescript
|
|
67
|
+
import BXO from './index';
|
|
68
|
+
|
|
69
|
+
const app = new BXO();
|
|
70
|
+
app.get('/', () => 'Hello World');
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### After (New Structure)
|
|
74
|
+
```typescript
|
|
75
|
+
import BXO from './index'; // Still works exactly the same!
|
|
76
|
+
|
|
77
|
+
const app = new BXO();
|
|
78
|
+
app.get('/', () => 'Hello World');
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**No changes needed in your existing code!** The API is completely backward compatible.
|
|
82
|
+
|
|
83
|
+
## โจ Benefits of Refactoring
|
|
84
|
+
|
|
85
|
+
### 1. **Better Separation of Concerns**
|
|
86
|
+
- Each module has a single responsibility
|
|
87
|
+
- Easier to understand what each part does
|
|
88
|
+
- Clear boundaries between different functionalities
|
|
89
|
+
|
|
90
|
+
### 2. **Improved Maintainability**
|
|
91
|
+
- Smaller, focused files are easier to modify
|
|
92
|
+
- Changes to one feature don't affect others
|
|
93
|
+
- Better testability with isolated modules
|
|
94
|
+
|
|
95
|
+
### 3. **Enhanced Readability**
|
|
96
|
+
- Code is organized logically
|
|
97
|
+
- Related functionality is grouped together
|
|
98
|
+
- Easier to find specific features
|
|
99
|
+
|
|
100
|
+
### 4. **Better Type Safety**
|
|
101
|
+
- Types are centralized and well-defined
|
|
102
|
+
- Easier to maintain type consistency
|
|
103
|
+
- Better IntelliSense support
|
|
104
|
+
|
|
105
|
+
### 5. **Easier Testing**
|
|
106
|
+
- Individual modules can be tested in isolation
|
|
107
|
+
- Mocking is simpler with clear interfaces
|
|
108
|
+
- Unit tests are more focused
|
|
109
|
+
|
|
110
|
+
## ๐งช Testing the Refactored Structure
|
|
111
|
+
|
|
112
|
+
Run the test file to verify everything works:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
bun run test-refactored.ts
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## ๐ Usage Examples
|
|
119
|
+
|
|
120
|
+
### Basic Usage (Unchanged)
|
|
121
|
+
```typescript
|
|
122
|
+
import BXO, { z } from './index';
|
|
123
|
+
|
|
124
|
+
const app = new BXO();
|
|
125
|
+
|
|
126
|
+
app.get('/', () => 'Hello World');
|
|
127
|
+
|
|
128
|
+
app.post('/users', async (ctx) => {
|
|
129
|
+
return { message: 'User created' };
|
|
130
|
+
}, {
|
|
131
|
+
body: z.object({
|
|
132
|
+
name: z.string(),
|
|
133
|
+
email: z.string().email()
|
|
134
|
+
})
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
app.start(3000);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### File Uploads (Unchanged)
|
|
141
|
+
```typescript
|
|
142
|
+
import { isFileUpload, saveUploadedFile } from './index';
|
|
143
|
+
|
|
144
|
+
app.post('/upload', async (ctx) => {
|
|
145
|
+
if (ctx.body.avatar && isFileUpload(ctx.body.avatar)) {
|
|
146
|
+
await saveUploadedFile(ctx.body.avatar, './uploads/avatar.jpg');
|
|
147
|
+
}
|
|
148
|
+
return { message: 'Upload successful' };
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### WebSockets (Unchanged)
|
|
153
|
+
```typescript
|
|
154
|
+
app.ws('/chat', {
|
|
155
|
+
onOpen: (ws) => console.log('Connected'),
|
|
156
|
+
onMessage: (ws, message) => ws.send(`Echo: ${message}`)
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## ๐ง Development
|
|
161
|
+
|
|
162
|
+
### Adding New Features
|
|
163
|
+
1. **New utility functions**: Add to `src/utils/`
|
|
164
|
+
2. **New types**: Add to `src/types/`
|
|
165
|
+
3. **New handlers**: Add to `src/handlers/`
|
|
166
|
+
4. **New core features**: Add to `src/core/`
|
|
167
|
+
|
|
168
|
+
### Modifying Existing Features
|
|
169
|
+
- **Route matching**: Edit `src/utils/route-matcher.ts`
|
|
170
|
+
- **Request parsing**: Edit `src/utils/index.ts`
|
|
171
|
+
- **Response handling**: Edit `src/utils/response-handler.ts`
|
|
172
|
+
- **Context creation**: Edit `src/utils/context-factory.ts`
|
|
173
|
+
|
|
174
|
+
## ๐ฆ Build and Distribution
|
|
175
|
+
|
|
176
|
+
The main `index.ts` file now simply re-exports everything from the new structure:
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
// Re-export everything from the refactored source
|
|
180
|
+
export * from './src/index';
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
This means:
|
|
184
|
+
- โ
**Zero breaking changes** for existing users
|
|
185
|
+
- โ
**Same import syntax** as before
|
|
186
|
+
- โ
**All functionality preserved**
|
|
187
|
+
- โ
**Better internal organization**
|
|
188
|
+
|
|
189
|
+
## ๐ฏ Future Improvements
|
|
190
|
+
|
|
191
|
+
With this new structure, we can now easily:
|
|
192
|
+
|
|
193
|
+
1. **Add new modules** without cluttering the main file
|
|
194
|
+
2. **Implement better testing** with isolated components
|
|
195
|
+
3. **Add new features** in dedicated modules
|
|
196
|
+
4. **Improve performance** by optimizing specific modules
|
|
197
|
+
5. **Better documentation** with focused module descriptions
|
|
198
|
+
|
|
199
|
+
## ๐ค Contributing
|
|
200
|
+
|
|
201
|
+
When contributing to the framework:
|
|
202
|
+
|
|
203
|
+
1. **Identify the right module** for your changes
|
|
204
|
+
2. **Keep modules focused** on their specific responsibility
|
|
205
|
+
3. **Update types** if adding new interfaces
|
|
206
|
+
4. **Add tests** for new functionality
|
|
207
|
+
5. **Update documentation** for new features
|
|
208
|
+
|
|
209
|
+
The refactored structure makes it much easier to contribute and maintain the codebase!
|