bxo 0.0.5-dev.3 → 0.0.5-dev.30

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: