elit 3.3.7 → 3.3.8
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/dist/cli.js +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +23 -1
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +23 -1
- package/dist/server.mjs.map +1 -1
- package/package.json +1 -1
- package/src/server.ts +31 -1
package/package.json
CHANGED
package/src/server.ts
CHANGED
|
@@ -288,10 +288,40 @@ export class ServerRouter {
|
|
|
288
288
|
// Attach params to req for Express-like compatibility
|
|
289
289
|
(req as ElitRequest).params = params;
|
|
290
290
|
|
|
291
|
+
// Add Express-like response helpers to res object
|
|
292
|
+
let _statusCode = 200;
|
|
293
|
+
const elitRes = res as ElitResponse;
|
|
294
|
+
|
|
295
|
+
// Implement status() method
|
|
296
|
+
elitRes.status = function(code: number): ElitResponse {
|
|
297
|
+
_statusCode = code;
|
|
298
|
+
return this;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// Implement json() method
|
|
302
|
+
elitRes.json = function(data: any, statusCode?: number): ElitResponse {
|
|
303
|
+
const code = statusCode !== undefined ? statusCode : _statusCode;
|
|
304
|
+
this.writeHead(code, { 'Content-Type': 'application/json' });
|
|
305
|
+
this.end(JSON.stringify(data));
|
|
306
|
+
return this;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
// Implement send() method
|
|
310
|
+
elitRes.send = function(data: any): ElitResponse {
|
|
311
|
+
if (typeof data === 'string') {
|
|
312
|
+
this.writeHead(_statusCode, { 'Content-Type': 'text/html' });
|
|
313
|
+
this.end(data);
|
|
314
|
+
} else {
|
|
315
|
+
this.writeHead(_statusCode, { 'Content-Type': 'application/json' });
|
|
316
|
+
this.end(JSON.stringify(data));
|
|
317
|
+
}
|
|
318
|
+
return this;
|
|
319
|
+
};
|
|
320
|
+
|
|
291
321
|
// Add Express-like response helpers to context
|
|
292
322
|
const ctx: ServerRouteContext = {
|
|
293
323
|
req: req as ElitRequest,
|
|
294
|
-
res:
|
|
324
|
+
res: elitRes,
|
|
295
325
|
params,
|
|
296
326
|
query,
|
|
297
327
|
body,
|