feat(server-time): enhance middleware configuration and import context

- Updated server-time middleware to import RequestContext for improved request handling.
- Modified ServerTimeModule to conditionally apply middleware based on the environment, using apiRoutePrefix for production paths.
- Adjusted snapshot tests to reflect changes in the note controller's response structure, adding is_published field.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-07-03 16:17:56 +08:00
parent 0a8ab1e120
commit a4635519b8
3 changed files with 9 additions and 4 deletions

View File

@@ -1,5 +1,7 @@
import type { IncomingMessage, ServerResponse } from 'node:http' import type { IncomingMessage, ServerResponse } from 'node:http'
import { RequestContext } from '~/common/contexts/request.context'
export async function trackResponseTimeMiddleware( export async function trackResponseTimeMiddleware(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,

View File

@@ -2,6 +2,8 @@ import type { MiddlewareConsumer, NestModule } from '@nestjs/common'
import { Module, RequestMethod } from '@nestjs/common' import { Module, RequestMethod } from '@nestjs/common'
import { apiRoutePrefix } from '~/common/decorators/api-controller.decorator'
import { ServerTimeController } from './server-time.controller' import { ServerTimeController } from './server-time.controller'
import { trackResponseTimeMiddleware } from './server-time.middleware' import { trackResponseTimeMiddleware } from './server-time.middleware'
@@ -10,8 +12,9 @@ import { trackResponseTimeMiddleware } from './server-time.middleware'
}) })
export class ServerTimeModule implements NestModule { export class ServerTimeModule implements NestModule {
configure(consumer: MiddlewareConsumer) { configure(consumer: MiddlewareConsumer) {
consumer consumer.apply(trackResponseTimeMiddleware).forRoutes({
.apply(trackResponseTimeMiddleware) path: isDev ? '/server-time' : `${apiRoutePrefix}/server-time`,
.forRoutes({ path: '/server-time', method: RequestMethod.ALL }) method: RequestMethod.ALL,
})
} }
} }

View File

@@ -11,8 +11,8 @@ exports[`NoteController (e2e) > GET /latest 1`] = `
"read": 0, "read": 0,
}, },
"created": "2021-03-20T00:00:00.000Z", "created": "2021-03-20T00:00:00.000Z",
"hide": false,
"images": [], "images": [],
"is_published": true,
"modified": null, "modified": null,
"nid": 20, "nid": 20,
"text": "Content 20", "text": "Content 20",