bxo 0.0.5-dev.11 โ 0.0.5-dev.14
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/example.ts +43 -22
- package/index.ts +23 -2
- package/package.json +1 -1
package/example.ts
CHANGED
@@ -1,13 +1,39 @@
|
|
1
1
|
import BXO, { z } from './index';
|
2
2
|
import { cors, logger, auth, rateLimit, createJWT } from './plugins';
|
3
3
|
|
4
|
+
// Create a simple API plugin that defines its own routes
|
5
|
+
function createApiPlugin(): BXO {
|
6
|
+
const apiPlugin = new BXO();
|
7
|
+
|
8
|
+
apiPlugin
|
9
|
+
.get('/api/info', async (ctx) => {
|
10
|
+
return {
|
11
|
+
name: 'BXO API Plugin',
|
12
|
+
version: '1.0.0',
|
13
|
+
endpoints: ['/api/info', '/api/ping', '/api/time']
|
14
|
+
};
|
15
|
+
})
|
16
|
+
.get('/api/ping', async (ctx) => {
|
17
|
+
return { ping: 'pong', timestamp: Date.now() };
|
18
|
+
})
|
19
|
+
.get('/api/time', async (ctx) => {
|
20
|
+
return { time: new Date().toISOString() };
|
21
|
+
})
|
22
|
+
.post('/api/echo', async (ctx) => {
|
23
|
+
return { echo: ctx.body };
|
24
|
+
}, {
|
25
|
+
body: z.object({
|
26
|
+
message: z.string()
|
27
|
+
})
|
28
|
+
});
|
29
|
+
|
30
|
+
return apiPlugin;
|
31
|
+
}
|
32
|
+
|
4
33
|
// Create the app instance
|
5
34
|
const app = new BXO();
|
6
35
|
|
7
|
-
//
|
8
|
-
app.enableHotReload([process.cwd(), './']); // Watch current directory
|
9
|
-
|
10
|
-
// Add plugins
|
36
|
+
// Add plugins (including our new API plugin)
|
11
37
|
app
|
12
38
|
.use(logger({ format: 'simple' }))
|
13
39
|
.use(cors({
|
@@ -22,8 +48,9 @@ app
|
|
22
48
|
.use(auth({
|
23
49
|
type: 'jwt',
|
24
50
|
secret: 'your-secret-key',
|
25
|
-
exclude: ['/', '/login', '/health']
|
26
|
-
}))
|
51
|
+
exclude: ['/', '/login', '/health', '/api/*']
|
52
|
+
}))
|
53
|
+
.use(createApiPlugin()); // Add our plugin with actual routes
|
27
54
|
|
28
55
|
// Add simplified lifecycle hooks
|
29
56
|
app
|
@@ -39,12 +66,6 @@ app
|
|
39
66
|
.onAfterStop(() => {
|
40
67
|
console.log('โ
Server fully stopped!');
|
41
68
|
})
|
42
|
-
.onBeforeRestart(() => {
|
43
|
-
console.log('๐ง Preparing to restart server...');
|
44
|
-
})
|
45
|
-
.onAfterRestart(() => {
|
46
|
-
console.log('โ
Server restart completed!');
|
47
|
-
})
|
48
69
|
.onRequest((ctx) => {
|
49
70
|
console.log(`๐จ Processing ${ctx.request.method} ${ctx.request.url}`);
|
50
71
|
})
|
@@ -116,13 +137,6 @@ app
|
|
116
137
|
return { message: 'This is protected', user: ctx.user };
|
117
138
|
})
|
118
139
|
|
119
|
-
// Server control endpoints
|
120
|
-
.post('/restart', async (ctx) => {
|
121
|
-
// Restart the server
|
122
|
-
setTimeout(() => app.restart(3000), 100);
|
123
|
-
return { message: 'Server restart initiated' };
|
124
|
-
})
|
125
|
-
|
126
140
|
.get('/status', async (ctx) => {
|
127
141
|
return {
|
128
142
|
...app.getServerInfo(),
|
@@ -162,12 +176,12 @@ console.log(`
|
|
162
176
|
๐ฆ BXO Framework with Hot Reload
|
163
177
|
|
164
178
|
โจ Features Enabled:
|
165
|
-
- ๐ Hot reload (edit any .ts/.js file to restart)
|
166
179
|
- ๐ฃ Full lifecycle hooks (before/after pattern)
|
167
180
|
- ๐ JWT authentication
|
168
181
|
- ๐ Rate limiting
|
169
182
|
- ๐ CORS support
|
170
183
|
- ๐ Request logging
|
184
|
+
- ๐ API Plugin with routes
|
171
185
|
|
172
186
|
๐งช Try these endpoints:
|
173
187
|
- GET /simple
|
@@ -177,7 +191,14 @@ console.log(`
|
|
177
191
|
- POST /login (with JSON body: {"username": "admin", "password": "password"})
|
178
192
|
- GET /protected (requires Bearer token from /login)
|
179
193
|
- GET /status (server statistics)
|
180
|
-
|
194
|
+
|
195
|
+
๐ API Plugin endpoints:
|
196
|
+
- GET /api/info (plugin information)
|
197
|
+
- GET /api/ping (ping pong)
|
198
|
+
- GET /api/time (current time)
|
199
|
+
- POST /api/echo (echo message: {"message": "hello"})
|
181
200
|
|
182
201
|
๐ก Edit this file and save to see hot reload in action!
|
183
|
-
`);
|
202
|
+
`);
|
203
|
+
|
204
|
+
console.log(app.routes)
|
package/index.ts
CHANGED
@@ -231,9 +231,28 @@ export default class BXO {
|
|
231
231
|
return this;
|
232
232
|
}
|
233
233
|
|
234
|
+
// Helper methods to get all routes including plugin routes
|
235
|
+
private getAllRoutes(): Route[] {
|
236
|
+
const allRoutes = [...this._routes];
|
237
|
+
for (const plugin of this.plugins) {
|
238
|
+
allRoutes.push(...plugin._routes);
|
239
|
+
}
|
240
|
+
return allRoutes;
|
241
|
+
}
|
242
|
+
|
243
|
+
private getAllWSRoutes(): WSRoute[] {
|
244
|
+
const allWSRoutes = [...this._wsRoutes];
|
245
|
+
for (const plugin of this.plugins) {
|
246
|
+
allWSRoutes.push(...plugin._wsRoutes);
|
247
|
+
}
|
248
|
+
return allWSRoutes;
|
249
|
+
}
|
250
|
+
|
234
251
|
// Route matching utility
|
235
252
|
private matchRoute(method: string, pathname: string): { route: Route; params: Record<string, string> } | null {
|
236
|
-
|
253
|
+
const allRoutes = this.getAllRoutes();
|
254
|
+
|
255
|
+
for (const route of allRoutes) {
|
237
256
|
if (route.method !== method) continue;
|
238
257
|
|
239
258
|
const routeSegments = route.path.split('/').filter(Boolean);
|
@@ -297,7 +316,9 @@ export default class BXO {
|
|
297
316
|
|
298
317
|
// WebSocket route matching utility
|
299
318
|
private matchWSRoute(pathname: string): { route: WSRoute; params: Record<string, string> } | null {
|
300
|
-
|
319
|
+
const allWSRoutes = this.getAllWSRoutes();
|
320
|
+
|
321
|
+
for (const route of allWSRoutes) {
|
301
322
|
const routeSegments = route.path.split('/').filter(Boolean);
|
302
323
|
const pathSegments = pathname.split('/').filter(Boolean);
|
303
324
|
|