bxo 0.0.5-dev.6 → 0.0.5-dev.61

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/README.md CHANGED
@@ -20,7 +20,7 @@ BXO is a fast, lightweight, and fully type-safe web framework built specifically
20
20
  ### Installation
21
21
 
22
22
  ```bash
23
- bun add zod
23
+ bun add bxo
24
24
  ```
25
25
 
26
26
  ### Basic Usage
@@ -93,11 +93,86 @@ interface Context<TConfig> {
93
93
  status?: number;
94
94
  headers?: Record<string, string>;
95
95
  };
96
+ status: (code: number, data?: any) => any; // Type-safe status method
96
97
  user?: any; // Added by auth plugin
97
98
  [key: string]: any; // Extended by plugins
98
99
  }
99
100
  ```
100
101
 
102
+ ### Type-Safe Status Method
103
+
104
+ BXO provides a type-safe `ctx.status()` method similar to Elysia, which allows you to set HTTP status codes and return data in one call:
105
+
106
+ ```typescript
107
+ // Simple usage
108
+ app.get('/hello', (ctx) => {
109
+ return ctx.status(200, { message: 'Hello World' });
110
+ });
111
+
112
+ // With response validation
113
+ app.get('/user/:id', (ctx) => {
114
+ const userId = ctx.params.id;
115
+
116
+ if (userId === 'not-found') {
117
+ // TypeScript suggests 404 as valid status
118
+ return ctx.status(404, { error: 'User not found' });
119
+ }
120
+
121
+ // TypeScript suggests 200 as valid status
122
+ return ctx.status(200, { user: { id: userId, name: 'John Doe' } });
123
+ }, {
124
+ response: {
125
+ 200: z.object({
126
+ user: z.object({
127
+ id: z.string(),
128
+ name: z.string()
129
+ })
130
+ }),
131
+ 404: z.object({
132
+ error: z.string()
133
+ })
134
+ }
135
+ });
136
+
137
+ // POST with validation and status responses
138
+ app.post('/users', (ctx) => {
139
+ const { name, email } = ctx.body;
140
+
141
+ if (!name || !email) {
142
+ return ctx.status(400, { error: 'Missing required fields' });
143
+ }
144
+
145
+ return ctx.status(201, {
146
+ success: true,
147
+ user: { id: 1, name, email }
148
+ });
149
+ }, {
150
+ body: z.object({
151
+ name: z.string(),
152
+ email: z.string().email()
153
+ }),
154
+ response: {
155
+ 201: z.object({
156
+ success: z.boolean(),
157
+ user: z.object({
158
+ id: z.number(),
159
+ name: z.string(),
160
+ email: z.string()
161
+ })
162
+ }),
163
+ 400: z.object({
164
+ error: z.string()
165
+ })
166
+ }
167
+ });
168
+ ```
169
+
170
+ **Key Features:**
171
+ - **Type Safety**: Status codes are suggested based on your response configuration
172
+ - **Data Validation**: Return data is validated against the corresponding schema
173
+ - **Autocomplete**: TypeScript provides autocomplete for valid status codes
174
+ - **Return Type Inference**: Return types are properly inferred from schemas
175
+
101
176
  ### Validation Configuration
102
177
 
103
178
  Each route can specify validation schemas for different parts of the request:
@@ -128,7 +203,7 @@ app.post('/api/users/:id', async (ctx) => {
128
203
 
129
204
  ## 🔌 Plugin System
130
205
 
131
- BXO has a powerful plugin system with lifecycle hooks. Plugins are separate modules imported from `./plugins`.
206
+ BXO has a powerful plugin system with lifecycle hooks. Plugins can be either middleware-style plugins or full BXO instances. Middleware plugins are separate modules imported from `./plugins` and are executed in the order they are added.
132
207
 
133
208
  ### Available Plugins
134
209
 
@@ -146,6 +221,13 @@ app.use(cors({
146
221
  }));
147
222
  ```
148
223
 
224
+ **Features:**
225
+ - **Origin Header Support**: Standard CORS origin header handling
226
+ - **Referer Header Fallback**: Automatically falls back to Referer header when Origin is not present
227
+ - **Origin Precedence**: When both Origin and Referer are present, Origin takes precedence
228
+ - **Invalid URL Handling**: Gracefully handles invalid Referer URLs
229
+ - **Flexible Origin Configuration**: Supports string, array, boolean, or wildcard (`*`) origins
230
+
149
231
  #### Logger Plugin
150
232
 
151
233
  ```typescript
@@ -200,6 +282,8 @@ app.use(rateLimit({
200
282
 
201
283
  ### Creating Custom Plugins
202
284
 
285
+ #### Middleware-Style Plugins (Recommended)
286
+
203
287
  ```typescript
204
288
  const customPlugin = {
205
289
  name: 'custom',
@@ -220,6 +304,19 @@ const customPlugin = {
220
304
  app.use(customPlugin);
221
305
  ```
222
306
 
307
+ #### Full BXO Instance Plugins
308
+
309
+ You can also use full BXO instances as plugins, which will merge their routes and hooks:
310
+
311
+ ```typescript
312
+ const pluginApp = new BXO();
313
+ pluginApp.get('/plugin-route', async (ctx) => {
314
+ return { message: 'From plugin' };
315
+ });
316
+
317
+ app.use(pluginApp);
318
+ ```
319
+
223
320
  ## 🎣 Lifecycle Hooks
224
321
 
225
322
  BXO provides comprehensive lifecycle hooks with a consistent before/after pattern for both server and request lifecycle: