@valentine-efagene/qshelter-common 1.0.0 β†’ 1.0.1

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.
@@ -0,0 +1,58 @@
1
+ import { Injectable, NestMiddleware, Logger } from '@nestjs/common';
2
+ import { Request, Response, NextFunction } from 'express';
3
+
4
+ @Injectable()
5
+ export class AccessLoggerMiddleware implements NestMiddleware {
6
+ private readonly logger = new Logger('HTTP');
7
+
8
+ use = (request: Request, response: Response, next: NextFunction): void => {
9
+ const { ip, method, originalUrl: url } = request;
10
+ const userAgent = request.get('user-agent') || '';
11
+ const startTime = Date.now();
12
+
13
+ // πŸ‘‡ capture body but don’t log yet
14
+ const originalSend = response.send;
15
+ let responseBody: any;
16
+
17
+ response.send = function (body?: any): Response {
18
+ responseBody = body;
19
+ return originalSend.call(this, body);
20
+ };
21
+
22
+ response.on('finish', () => {
23
+ const { statusCode } = response;
24
+ const contentLength = response.get('content-length');
25
+ const responseTime = Date.now() - startTime;
26
+
27
+ const message = {
28
+ method,
29
+ url,
30
+ statusCode,
31
+ contentLength,
32
+ responseTime: `${responseTime}ms`,
33
+ userAgent,
34
+ ip,
35
+ timestamp: new Date().toISOString(),
36
+ };
37
+
38
+ if (statusCode >= 500) {
39
+ this.logger.error(JSON.stringify(message));
40
+
41
+ // πŸ‘‡ only log the response body here
42
+ try {
43
+ if (typeof responseBody === 'object') {
44
+ console.log('Response body:', JSON.stringify(responseBody, null, 2));
45
+ } else {
46
+ console.log('Response body:', responseBody);
47
+ }
48
+ } catch (err) {
49
+ console.log('Response body (unserializable):', responseBody);
50
+ }
51
+ } else {
52
+ this.logger.log(JSON.stringify(message));
53
+ }
54
+ });
55
+
56
+ next();
57
+ };
58
+ }
@@ -0,0 +1,13 @@
1
+ import { Injectable, NestMiddleware } from '@nestjs/common';
2
+ import { Request, Response, NextFunction } from 'express';
3
+
4
+ @Injectable()
5
+ export default class AuthenticationMiddleware implements NestMiddleware {
6
+ use(req: Request, _res: Response, next: NextFunction) {
7
+ // No-op authentication middleware for now. Consumers should attach
8
+ // a real implementation or use a guard. This prevents build-time errors
9
+ // where the middleware is referenced.
10
+ // It preserves any existing req.user if already set by upstream middleware.
11
+ return next();
12
+ }
13
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@valentine-efagene/qshelter-common",
3
- "version": "1.0.0",
4
- "description": "Shared common utilities, DTOs, guards, middleware, decorators, and pagination for QShelter",
3
+ "version": "1.0.1",
4
+ "description": "Shared common utilities, DTOs, entities, guards, middleware, decorators, and pagination for QShelter",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {