vibe-gx 1.0.2 → 1.0.3

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.
Files changed (3) hide show
  1. package/README.md +109 -1
  2. package/package.json +1 -1
  3. package/vibe.d.ts +8 -0
package/README.md CHANGED
@@ -120,9 +120,40 @@ app.get("/users", async (req, res) => {
120
120
  });
121
121
  ```
122
122
 
123
- ### Streaming Uploads
123
+ ### File Uploads
124
+
125
+ Vibe supports multipart file uploads with built-in validation.
126
+
127
+ #### Basic File Upload
128
+
129
+ ```javascript
130
+ app.post("/upload", { media: { dest: "uploads" } }, (req, res) => {
131
+ return { files: req.files, body: req.body };
132
+ });
133
+ ```
134
+
135
+ #### Media Options
124
136
 
125
137
  ```javascript
138
+ app.post(
139
+ "/upload",
140
+ {
141
+ media: {
142
+ dest: "uploads", // Folder to save files
143
+ public: true, // Save inside public folder (default: true)
144
+ maxSize: 5 * 1024 * 1024, // Max file size: 5MB
145
+ allowedTypes: ["image/jpeg", "image/png", "application/pdf"],
146
+ },
147
+ },
148
+ handler,
149
+ );
150
+ ```
151
+
152
+ #### Streaming Uploads (Large Files)
153
+
154
+ ```javascript
155
+ import fs from "fs";
156
+
126
157
  app.post("/upload", { media: { streaming: true } }, (req, res) => {
127
158
  req.on("file", (name, stream, info) => {
128
159
  stream.pipe(fs.createWriteStream(`/uploads/${info.filename}`));
@@ -131,8 +162,85 @@ app.post("/upload", { media: { streaming: true } }, (req, res) => {
131
162
  });
132
163
  ```
133
164
 
165
+ #### Uploaded File Object
166
+
167
+ ```javascript
168
+ // req.files contains:
169
+ [
170
+ {
171
+ filename: "image-a7x92b.png", // Saved filename
172
+ originalName: "photo.png", // Original filename
173
+ type: "image/png", // MIME type
174
+ filePath: "/uploads/image.png", // Full path
175
+ size: 102400, // Size in bytes
176
+ },
177
+ ];
178
+ ```
179
+
134
180
  ---
135
181
 
182
+ ### Interceptors (Middleware)
183
+
184
+ Interceptors run before your handler. Return `false` to stop execution.
185
+
186
+ #### Single Interceptor
187
+
188
+ ```javascript
189
+ const authCheck = (req, res) => {
190
+ if (!req.headers.authorization) {
191
+ res.unauthorized("Token required");
192
+ return false;
193
+ }
194
+ req.user = { id: 1 };
195
+ return true;
196
+ };
197
+
198
+ app.get("/protected", { intercept: authCheck }, (req) => {
199
+ return { user: req.user };
200
+ });
201
+ ```
202
+
203
+ #### Multiple Interceptors
204
+
205
+ ```javascript
206
+ app.get(
207
+ "/admin",
208
+ {
209
+ intercept: [authCheck, adminCheck, rateLimiter],
210
+ },
211
+ handler,
212
+ );
213
+ ```
214
+
215
+ #### Global Interceptors
216
+
217
+ ```javascript
218
+ // Applies to ALL routes
219
+ app.plugin((req, res) => {
220
+ console.log(`${req.method} ${req.url}`);
221
+ });
222
+ ```
223
+
224
+ ---
225
+
226
+ ### Route Options
227
+
228
+ ```javascript
229
+ app.post(
230
+ "/path",
231
+ {
232
+ intercept: authMiddleware, // Middleware function(s)
233
+ media: {
234
+ // File upload config
235
+ dest: "uploads",
236
+ maxSize: 10 * 1024 * 1024,
237
+ allowedTypes: ["image/*"],
238
+ },
239
+ },
240
+ handler,
241
+ );
242
+ ```
243
+
136
244
  ## 🛠️ API Reference
137
245
 
138
246
  ### Application
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vibe-gx",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A lightweight, regex-based Node.js web framework built for speed and simplicity.",
5
5
  "type": "module",
6
6
  "main": "vibe.js",
package/vibe.d.ts CHANGED
@@ -36,6 +36,8 @@ export interface MediaOptions {
36
36
  maxSize?: number;
37
37
  /** Allowed MIME types (e.g., ["image/png", "image/jpeg"]) */
38
38
  allowedTypes?: string[];
39
+ /** Enable streaming mode for large files. Use req.on('file', ...) */
40
+ streaming?: boolean;
39
41
  }
40
42
 
41
43
  /**
@@ -89,7 +91,13 @@ export interface VibeResponse extends ServerResponse {
89
91
  json: (data: any) => void;
90
92
  send: (data: string | number | boolean | object) => void;
91
93
  status: (code: number) => VibeResponse;
94
+ /** Send a file from the public folder */
92
95
  sendFile: (filePath: string) => void;
96
+ /** Send any file by absolute path */
97
+ sendAbsoluteFile: (
98
+ absolutePath: string,
99
+ opts?: { download?: boolean; filename?: string },
100
+ ) => void;
93
101
  sendHtml: (filename: string) => void;
94
102
  redirect: (url: string, code?: number) => void;
95
103