@dangao/bun-server 1.8.2 → 1.8.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.
- package/dist/controller/controller.d.ts.map +1 -1
- package/dist/core/application.d.ts.map +1 -1
- package/dist/error/handler.d.ts.map +1 -1
- package/dist/index.js +98 -21
- package/dist/middleware/builtin/error-handler.d.ts.map +1 -1
- package/dist/middleware/builtin/logger.d.ts.map +1 -1
- package/dist/router/decorators.d.ts +10 -10
- package/dist/router/decorators.d.ts.map +1 -1
- package/dist/router/router.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/controller/controller.ts +10 -2
- package/src/core/application.ts +11 -0
- package/src/error/handler.ts +17 -0
- package/src/middleware/builtin/error-handler.ts +10 -0
- package/src/middleware/builtin/logger.ts +17 -0
- package/src/router/decorators.ts +15 -15
- package/src/router/router.ts +18 -1
- package/tests/controller/path-combination.test.ts +135 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/controller/controller.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAS5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/controller/controller.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAS5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAShD;;GAEG;AACH,eAAO,MAAM,uBAAuB,eAAuB,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;CAC9B;AAED;;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AwB,CAAC,YAAY,EAAE,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI;IAIzE;;;OAGG;IACI,iBAAiB,CAAC,SAAS,EAAE,oBAAoB,GAAG,IAAI;IAK/D;;;OAGG;IACI,SAAS,IAAI,SAAS,GAAG,SAAS;IAIzC;;;OAGG;YACW,gBAAgB;IAsB9B;;;OAGG;YACW,kBAAkB;IAwBhC;;;OAGG;IACI,YAAY;IAInB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IA2B7B;;OAEG;IACH,OAAO,CAAC,oBAAoB;CAS7B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/error/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/error/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAO/C;;GAEG;AACH,wBAAsB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsErF"}
|
package/dist/index.js
CHANGED
|
@@ -331,6 +331,8 @@ class Route {
|
|
|
331
331
|
var init_route = () => {};
|
|
332
332
|
|
|
333
333
|
// src/router/router.ts
|
|
334
|
+
import { LoggerManager as LoggerManager3 } from "@dangao/logsmith";
|
|
335
|
+
|
|
334
336
|
class Router {
|
|
335
337
|
routes = [];
|
|
336
338
|
staticRoutes = new Map;
|
|
@@ -400,11 +402,20 @@ class Router {
|
|
|
400
402
|
async preHandle(context) {
|
|
401
403
|
const method = context.method;
|
|
402
404
|
const path = this.normalizePath(context.path);
|
|
405
|
+
const logger = LoggerManager3.getLogger();
|
|
403
406
|
const result = this.findRouteWithMatch(method, path);
|
|
404
407
|
if (!result) {
|
|
408
|
+
logger.debug("[Router] Route not matched", { method, path });
|
|
405
409
|
return;
|
|
406
410
|
}
|
|
407
411
|
const { route, match } = result;
|
|
412
|
+
logger.debug("[Router] Route matched", {
|
|
413
|
+
method,
|
|
414
|
+
path,
|
|
415
|
+
routePath: route.path,
|
|
416
|
+
controller: route.controllerClass?.name,
|
|
417
|
+
methodName: route.methodName
|
|
418
|
+
});
|
|
408
419
|
if (match.matched) {
|
|
409
420
|
context.params = match.params;
|
|
410
421
|
}
|
|
@@ -418,11 +429,17 @@ class Router {
|
|
|
418
429
|
async handle(context) {
|
|
419
430
|
const method = context.method;
|
|
420
431
|
const path = this.normalizePath(context.path);
|
|
432
|
+
const logger = LoggerManager3.getLogger();
|
|
421
433
|
const result = this.findRouteWithMatch(method, path);
|
|
422
434
|
if (!result) {
|
|
423
435
|
return;
|
|
424
436
|
}
|
|
425
437
|
const { route, match } = result;
|
|
438
|
+
logger.debug("[Router] Executing handler", {
|
|
439
|
+
path: route.path,
|
|
440
|
+
controller: route.controllerClass?.name,
|
|
441
|
+
methodName: route.methodName
|
|
442
|
+
});
|
|
426
443
|
if (match.matched) {
|
|
427
444
|
context.params = match.params;
|
|
428
445
|
}
|
|
@@ -503,7 +520,7 @@ var init_types = __esm(() => {
|
|
|
503
520
|
|
|
504
521
|
// src/di/container.ts
|
|
505
522
|
import"reflect-metadata";
|
|
506
|
-
import { LoggerManager as
|
|
523
|
+
import { LoggerManager as LoggerManager4 } from "@dangao/logsmith";
|
|
507
524
|
|
|
508
525
|
class Container {
|
|
509
526
|
parent;
|
|
@@ -621,7 +638,7 @@ class Container {
|
|
|
621
638
|
resolveInternal(token) {
|
|
622
639
|
const tokenType = typeof token;
|
|
623
640
|
const tokenString = String(token);
|
|
624
|
-
const logger =
|
|
641
|
+
const logger = LoggerManager4.getLogger();
|
|
625
642
|
if (tokenString.includes("Level2") || tokenString.includes("Dependency") || tokenString.includes("Service")) {
|
|
626
643
|
logger.debug(`[DI Debug] resolveInternal: token=${tokenString}, tokenType=${tokenType}`);
|
|
627
644
|
}
|
|
@@ -1121,7 +1138,7 @@ var init_param_binder = __esm(() => {
|
|
|
1121
1138
|
|
|
1122
1139
|
// src/router/decorators.ts
|
|
1123
1140
|
import"reflect-metadata";
|
|
1124
|
-
function createRouteDecorator(method, path) {
|
|
1141
|
+
function createRouteDecorator(method, path = "") {
|
|
1125
1142
|
return function(target, propertyKey, descriptor) {
|
|
1126
1143
|
const constructor = target.constructor;
|
|
1127
1144
|
if (constructor && typeof constructor === "function") {
|
|
@@ -1147,26 +1164,26 @@ function createRouteDecorator(method, path) {
|
|
|
1147
1164
|
}
|
|
1148
1165
|
existingRoutes.push({
|
|
1149
1166
|
method,
|
|
1150
|
-
path,
|
|
1167
|
+
path: path ?? "",
|
|
1151
1168
|
handler: descriptor.value,
|
|
1152
1169
|
propertyKey: propertyKeyStr || undefined
|
|
1153
1170
|
});
|
|
1154
1171
|
Reflect.defineMetadata(ROUTE_METADATA_KEY, existingRoutes, target);
|
|
1155
1172
|
};
|
|
1156
1173
|
}
|
|
1157
|
-
function GET(path) {
|
|
1174
|
+
function GET(path = "") {
|
|
1158
1175
|
return createRouteDecorator("GET", path);
|
|
1159
1176
|
}
|
|
1160
|
-
function POST(path) {
|
|
1177
|
+
function POST(path = "") {
|
|
1161
1178
|
return createRouteDecorator("POST", path);
|
|
1162
1179
|
}
|
|
1163
|
-
function PUT(path) {
|
|
1180
|
+
function PUT(path = "") {
|
|
1164
1181
|
return createRouteDecorator("PUT", path);
|
|
1165
1182
|
}
|
|
1166
|
-
function DELETE(path) {
|
|
1183
|
+
function DELETE(path = "") {
|
|
1167
1184
|
return createRouteDecorator("DELETE", path);
|
|
1168
1185
|
}
|
|
1169
|
-
function PATCH(path) {
|
|
1186
|
+
function PATCH(path = "") {
|
|
1170
1187
|
return createRouteDecorator("PATCH", path);
|
|
1171
1188
|
}
|
|
1172
1189
|
var ROUTE_METADATA_KEY;
|
|
@@ -1330,10 +1347,10 @@ var init_decorators4 = __esm(() => {
|
|
|
1330
1347
|
});
|
|
1331
1348
|
|
|
1332
1349
|
// src/middleware/builtin/logger.ts
|
|
1333
|
-
import { LoggerManager as
|
|
1350
|
+
import { LoggerManager as LoggerManager5 } from "@dangao/logsmith";
|
|
1334
1351
|
function createLoggerMiddleware(options = {}) {
|
|
1335
1352
|
const log = options.logger ?? ((message, details) => {
|
|
1336
|
-
const logger =
|
|
1353
|
+
const logger = LoggerManager5.getLogger();
|
|
1337
1354
|
if (details) {
|
|
1338
1355
|
logger.info(message, details);
|
|
1339
1356
|
} else {
|
|
@@ -1349,12 +1366,20 @@ function createLoggerMiddleware(options = {}) {
|
|
|
1349
1366
|
} finally {
|
|
1350
1367
|
const status = response?.status ?? context.statusCode ?? 200;
|
|
1351
1368
|
log(`${prefix} ${context.method} ${context.path} ${status}`);
|
|
1369
|
+
if (status >= 400) {
|
|
1370
|
+
const logger = LoggerManager5.getLogger();
|
|
1371
|
+
logger.debug(`${prefix} Error response`, {
|
|
1372
|
+
method: context.method,
|
|
1373
|
+
path: context.path,
|
|
1374
|
+
statusCode: status
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1352
1377
|
}
|
|
1353
1378
|
};
|
|
1354
1379
|
}
|
|
1355
1380
|
function createRequestLoggingMiddleware(options = {}) {
|
|
1356
1381
|
const log = options.logger ?? ((message, details) => {
|
|
1357
|
-
const logger =
|
|
1382
|
+
const logger = LoggerManager5.getLogger();
|
|
1358
1383
|
logger.info(message, details);
|
|
1359
1384
|
});
|
|
1360
1385
|
const prefix = options.prefix ?? "[Request]";
|
|
@@ -1372,6 +1397,15 @@ function createRequestLoggingMiddleware(options = {}) {
|
|
|
1372
1397
|
} catch (error) {
|
|
1373
1398
|
const duration = performance.now() - start;
|
|
1374
1399
|
log(`${prefix} ${context.method} ${context.path} error ${duration.toFixed(2)}ms`, error instanceof Error ? { error: error.message } : undefined);
|
|
1400
|
+
if (error instanceof Error) {
|
|
1401
|
+
LoggerManager5.getLogger().debug(`${prefix} Request error details`, {
|
|
1402
|
+
method: context.method,
|
|
1403
|
+
path: context.path,
|
|
1404
|
+
durationMs: duration.toFixed(2),
|
|
1405
|
+
message: error.message,
|
|
1406
|
+
stack: error.stack
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
1375
1409
|
throw error;
|
|
1376
1410
|
}
|
|
1377
1411
|
};
|
|
@@ -2449,6 +2483,7 @@ var exports_handler = {};
|
|
|
2449
2483
|
__export(exports_handler, {
|
|
2450
2484
|
handleError: () => handleError
|
|
2451
2485
|
});
|
|
2486
|
+
import { LoggerManager as LoggerManager6 } from "@dangao/logsmith";
|
|
2452
2487
|
async function handleError(error, context) {
|
|
2453
2488
|
const registry = ExceptionFilterRegistry.getInstance();
|
|
2454
2489
|
const filterResponse = await registry.execute(error, context);
|
|
@@ -2457,6 +2492,15 @@ async function handleError(error, context) {
|
|
|
2457
2492
|
}
|
|
2458
2493
|
if (error instanceof HttpException) {
|
|
2459
2494
|
context.setStatus(error.status);
|
|
2495
|
+
if (error.status >= 400) {
|
|
2496
|
+
LoggerManager6.getLogger().debug("HttpException", {
|
|
2497
|
+
method: context.method,
|
|
2498
|
+
path: context.path,
|
|
2499
|
+
status: error.status,
|
|
2500
|
+
code: error.code,
|
|
2501
|
+
message: error.message
|
|
2502
|
+
});
|
|
2503
|
+
}
|
|
2460
2504
|
let errorMessage = error.message;
|
|
2461
2505
|
if (error.code) {
|
|
2462
2506
|
const acceptLanguage = context.getHeader("accept-language");
|
|
@@ -2483,7 +2527,14 @@ async function handleError(error, context) {
|
|
|
2483
2527
|
});
|
|
2484
2528
|
}
|
|
2485
2529
|
const message = error instanceof Error ? error.message : String(error);
|
|
2530
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
2486
2531
|
context.setStatus(500);
|
|
2532
|
+
LoggerManager6.getLogger().debug("Internal error (500)", {
|
|
2533
|
+
method: context.method,
|
|
2534
|
+
path: context.path,
|
|
2535
|
+
message,
|
|
2536
|
+
stack
|
|
2537
|
+
});
|
|
2487
2538
|
return context.createResponse({
|
|
2488
2539
|
error: "Internal Server Error",
|
|
2489
2540
|
details: message
|
|
@@ -2504,24 +2555,32 @@ var init_error = __esm(() => {
|
|
|
2504
2555
|
});
|
|
2505
2556
|
|
|
2506
2557
|
// src/middleware/builtin/error-handler.ts
|
|
2507
|
-
import { LoggerManager as
|
|
2558
|
+
import { LoggerManager as LoggerManager7 } from "@dangao/logsmith";
|
|
2508
2559
|
function createErrorHandlingMiddleware(options = {}) {
|
|
2509
2560
|
const log = options.logger ?? ((error, context) => {
|
|
2510
|
-
|
|
2561
|
+
LoggerManager7.getLogger().error("[Error]", { ...context, error });
|
|
2511
2562
|
});
|
|
2512
2563
|
const expose = options.exposeError ?? false;
|
|
2513
2564
|
const defaultStatus = options.statusCode ?? 500;
|
|
2514
2565
|
return async (context, next) => {
|
|
2515
|
-
const logger =
|
|
2566
|
+
const logger = LoggerManager7.getLogger();
|
|
2516
2567
|
try {
|
|
2517
2568
|
return await next();
|
|
2518
2569
|
} catch (error) {
|
|
2570
|
+
const routeHandler = context.routeHandler;
|
|
2519
2571
|
log(error, { method: context.method, path: context.path });
|
|
2520
2572
|
logger.error("Unhandled error", {
|
|
2521
2573
|
method: context.method,
|
|
2522
2574
|
path: context.path,
|
|
2523
2575
|
error
|
|
2524
2576
|
});
|
|
2577
|
+
logger.debug("Unhandled error details", {
|
|
2578
|
+
method: context.method,
|
|
2579
|
+
path: context.path,
|
|
2580
|
+
routeHandler: routeHandler ? `${routeHandler.controller?.name ?? "unknown"}.${routeHandler.method ?? "unknown"}` : undefined,
|
|
2581
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
2582
|
+
stack: error instanceof Error ? error.stack : undefined
|
|
2583
|
+
});
|
|
2525
2584
|
if (error instanceof Response) {
|
|
2526
2585
|
return error;
|
|
2527
2586
|
}
|
|
@@ -3028,7 +3087,7 @@ var init_permission_interceptor = __esm(() => {
|
|
|
3028
3087
|
|
|
3029
3088
|
// src/extensions/logger-extension.ts
|
|
3030
3089
|
import {
|
|
3031
|
-
LoggerManager as
|
|
3090
|
+
LoggerManager as LoggerManager8,
|
|
3032
3091
|
LogLevel,
|
|
3033
3092
|
SimpleLogger
|
|
3034
3093
|
} from "@dangao/logsmith";
|
|
@@ -3043,7 +3102,7 @@ class LoggerExtension {
|
|
|
3043
3102
|
}
|
|
3044
3103
|
register(container) {
|
|
3045
3104
|
const logger = new SimpleLogger(this.options);
|
|
3046
|
-
|
|
3105
|
+
LoggerManager8.setLogger(logger);
|
|
3047
3106
|
container.registerInstance(SimpleLogger, logger);
|
|
3048
3107
|
container.registerInstance(LOGGER_TOKEN, logger);
|
|
3049
3108
|
}
|
|
@@ -3113,7 +3172,7 @@ var init_logger_module = __esm(() => {
|
|
|
3113
3172
|
});
|
|
3114
3173
|
|
|
3115
3174
|
// src/extensions/index.ts
|
|
3116
|
-
import { LoggerManager as
|
|
3175
|
+
import { LoggerManager as LoggerManager9, LogLevel as LogLevel2, SimpleLogger as SimpleLogger2 } from "@dangao/logsmith";
|
|
3117
3176
|
var init_extensions = __esm(() => {
|
|
3118
3177
|
init_logger_extension();
|
|
3119
3178
|
init_logger_module();
|
|
@@ -3230,6 +3289,7 @@ __export(exports_controller, {
|
|
|
3230
3289
|
CONTROLLER_METADATA_KEY: () => CONTROLLER_METADATA_KEY
|
|
3231
3290
|
});
|
|
3232
3291
|
import"reflect-metadata";
|
|
3292
|
+
import { LoggerManager as LoggerManager10 } from "@dangao/logsmith";
|
|
3233
3293
|
function Controller(path = "") {
|
|
3234
3294
|
return function(target) {
|
|
3235
3295
|
Reflect.defineMetadata(CONTROLLER_METADATA_KEY, { path, target }, target);
|
|
@@ -3327,6 +3387,13 @@ class ControllerRegistry {
|
|
|
3327
3387
|
}
|
|
3328
3388
|
return context.createResponse(responseData);
|
|
3329
3389
|
} catch (error) {
|
|
3390
|
+
LoggerManager10.getLogger().debug("Controller handler error", {
|
|
3391
|
+
controller: controllerClass.name,
|
|
3392
|
+
method: propertyKey,
|
|
3393
|
+
path: context.path,
|
|
3394
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
3395
|
+
stack: error instanceof Error ? error.stack : undefined
|
|
3396
|
+
});
|
|
3330
3397
|
const { handleError: handleError2 } = await Promise.resolve().then(() => (init_handler(), exports_handler));
|
|
3331
3398
|
return await handleError2(error, context);
|
|
3332
3399
|
}
|
|
@@ -3343,7 +3410,7 @@ class ControllerRegistry {
|
|
|
3343
3410
|
if (!base) {
|
|
3344
3411
|
base = "/";
|
|
3345
3412
|
}
|
|
3346
|
-
const method = methodPath.replace(/^\/+/, "");
|
|
3413
|
+
const method = (methodPath ?? "").replace(/^\/+/, "");
|
|
3347
3414
|
if (!method) {
|
|
3348
3415
|
return base === "/" ? "/" : base;
|
|
3349
3416
|
}
|
|
@@ -5338,7 +5405,7 @@ CacheModule = __legacyDecorateClassTS([
|
|
|
5338
5405
|
})
|
|
5339
5406
|
], CacheModule);
|
|
5340
5407
|
// src/core/application.ts
|
|
5341
|
-
import { LoggerManager as
|
|
5408
|
+
import { LoggerManager as LoggerManager11 } from "@dangao/logsmith";
|
|
5342
5409
|
|
|
5343
5410
|
class Application {
|
|
5344
5411
|
server;
|
|
@@ -5433,6 +5500,12 @@ class Application {
|
|
|
5433
5500
|
}
|
|
5434
5501
|
}
|
|
5435
5502
|
async handleRequest(context) {
|
|
5503
|
+
const logger = LoggerManager11.getLogger();
|
|
5504
|
+
logger.debug("[Request] Incoming", {
|
|
5505
|
+
method: context.method,
|
|
5506
|
+
path: context.path,
|
|
5507
|
+
url: context.url?.href
|
|
5508
|
+
});
|
|
5436
5509
|
return await contextStore.run(context, async () => {
|
|
5437
5510
|
if (["POST", "PUT", "PATCH"].includes(context.method)) {
|
|
5438
5511
|
await context.getBody();
|
|
@@ -5445,6 +5518,10 @@ class Application {
|
|
|
5445
5518
|
if (response) {
|
|
5446
5519
|
return response;
|
|
5447
5520
|
}
|
|
5521
|
+
logger.debug("[Router] No route matched", {
|
|
5522
|
+
method: context.method,
|
|
5523
|
+
path: context.path
|
|
5524
|
+
});
|
|
5448
5525
|
context.setStatus(404);
|
|
5449
5526
|
return context.createResponse({ error: "Not Found" });
|
|
5450
5527
|
});
|
|
@@ -5524,7 +5601,7 @@ class Application {
|
|
|
5524
5601
|
if (this.signalHandlersInstalled) {
|
|
5525
5602
|
return;
|
|
5526
5603
|
}
|
|
5527
|
-
const logger =
|
|
5604
|
+
const logger = LoggerManager11.getLogger();
|
|
5528
5605
|
const shutdownHandler = async (signal) => {
|
|
5529
5606
|
logger.info(`Received ${signal}, starting graceful shutdown...`);
|
|
5530
5607
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../../../src/middleware/builtin/error-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAOhD,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAE7E;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,GAAE,mBAAwB,GAChC,UAAU,
|
|
1
|
+
{"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../../../src/middleware/builtin/error-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAOhD,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAE7E;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,GAAE,mBAAwB,GAChC,UAAU,CAsEZ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/middleware/builtin/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAEtE;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE,uBAA4B,GACpC,UAAU,
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/middleware/builtin/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAEtE;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE,uBAA4B,GACpC,UAAU,CA8BZ;AAED,MAAM,WAAW,qBAAsB,SAAQ,uBAAuB;IACpE;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;GAEG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,GAAE,qBAA0B,GAClC,UAAU,CA2CZ"}
|
|
@@ -19,27 +19,27 @@ export interface RouteMetadata {
|
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* GET 路由装饰器
|
|
22
|
-
* @param path -
|
|
22
|
+
* @param path - 路由路径(可选,默认 '',即 @GET() 映射到控制器基础路径或 /)
|
|
23
23
|
*/
|
|
24
|
-
export declare function GET(path
|
|
24
|
+
export declare function GET(path?: string): (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
25
25
|
/**
|
|
26
26
|
* POST 路由装饰器
|
|
27
|
-
* @param path -
|
|
27
|
+
* @param path - 路由路径(可选,默认 '')
|
|
28
28
|
*/
|
|
29
|
-
export declare function POST(path
|
|
29
|
+
export declare function POST(path?: string): (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
30
30
|
/**
|
|
31
31
|
* PUT 路由装饰器
|
|
32
|
-
* @param path -
|
|
32
|
+
* @param path - 路由路径(可选,默认 '')
|
|
33
33
|
*/
|
|
34
|
-
export declare function PUT(path
|
|
34
|
+
export declare function PUT(path?: string): (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
35
35
|
/**
|
|
36
36
|
* DELETE 路由装饰器
|
|
37
|
-
* @param path -
|
|
37
|
+
* @param path - 路由路径(可选,默认 '')
|
|
38
38
|
*/
|
|
39
|
-
export declare function DELETE(path
|
|
39
|
+
export declare function DELETE(path?: string): (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
40
40
|
/**
|
|
41
41
|
* PATCH 路由装饰器
|
|
42
|
-
* @param path -
|
|
42
|
+
* @param path - 路由路径(可选,默认 '')
|
|
43
43
|
*/
|
|
44
|
-
export declare function PATCH(path
|
|
44
|
+
export declare function PATCH(path?: string): (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
45
45
|
//# sourceMappingURL=decorators.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../../src/router/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG1C;;GAEG;AACH,eAAO,MAAM,kBAAkB,eAAkB,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,UAAU,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,YAAY,CAAC;IACtB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AA8DD;;;GAGG;AACH,wBAAgB,GAAG,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../../src/router/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG1C;;GAEG;AACH,eAAO,MAAM,kBAAkB,eAAkB,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,UAAU,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,YAAY,CAAC;IACtB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AA8DD;;;GAGG;AACH,wBAAgB,GAAG,CAAC,IAAI,GAAE,MAAW,YAzDV,GAAG,eAAe,MAAM,GAAG,MAAM,cAAc,kBAAkB,UA2D3F;AAED;;;GAGG;AACH,wBAAgB,IAAI,CAAC,IAAI,GAAE,MAAW,YAjEX,GAAG,eAAe,MAAM,GAAG,MAAM,cAAc,kBAAkB,UAmE3F;AAED;;;GAGG;AACH,wBAAgB,GAAG,CAAC,IAAI,GAAE,MAAW,YAzEV,GAAG,eAAe,MAAM,GAAG,MAAM,cAAc,kBAAkB,UA2E3F;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,GAAE,MAAW,YAjFb,GAAG,eAAe,MAAM,GAAG,MAAM,cAAc,kBAAkB,UAmF3F;AAED;;;GAGG;AACH,wBAAgB,KAAK,CAAC,IAAI,GAAE,MAAW,YAzFZ,GAAG,eAAe,MAAM,GAAG,MAAM,cAAc,kBAAkB,UA2F3F"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/router/router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACpE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/router/router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACpE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD;;;GAGG;AACH,qBAAa,MAAM;IACjB;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;IACtC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA4B;IACzD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAE7C;;;OAG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AAI,CAAC;IAgCvD;;;;OAIG;IACU,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAmCpE;;;OAGG;IACI,SAAS,IAAI,SAAS,KAAK,EAAE;IAIpC;;OAEG;IACI,KAAK,IAAI,IAAI;CAMrB"}
|
package/package.json
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
InterceptorChain,
|
|
16
16
|
scanInterceptorMetadata,
|
|
17
17
|
} from '../interceptor';
|
|
18
|
+
import { LoggerManager } from '@dangao/logsmith';
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* 控制器元数据键
|
|
@@ -212,6 +213,13 @@ export class ControllerRegistry {
|
|
|
212
213
|
// 创建响应
|
|
213
214
|
return context.createResponse(responseData);
|
|
214
215
|
} catch (error) {
|
|
216
|
+
LoggerManager.getLogger().debug('Controller handler error', {
|
|
217
|
+
controller: controllerClass.name,
|
|
218
|
+
method: propertyKey,
|
|
219
|
+
path: context.path,
|
|
220
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
221
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
222
|
+
});
|
|
215
223
|
// 使用全局错误处理器,确保错误码和国际化正确应用
|
|
216
224
|
const { handleError } = await import('../error/handler');
|
|
217
225
|
return await handleError(error, context);
|
|
@@ -258,8 +266,8 @@ export class ControllerRegistry {
|
|
|
258
266
|
base = '/';
|
|
259
267
|
}
|
|
260
268
|
|
|
261
|
-
// 规范化 methodPath
|
|
262
|
-
const method = methodPath.replace(/^\/+/, '');
|
|
269
|
+
// 规范化 methodPath:移除前导斜杠(支持 undefined,如 @GET() 无参)
|
|
270
|
+
const method = (methodPath ?? '').replace(/^\/+/, '');
|
|
263
271
|
|
|
264
272
|
if (!method) {
|
|
265
273
|
// 如果方法路径为空,返回基础路径
|
package/src/core/application.ts
CHANGED
|
@@ -242,6 +242,13 @@ export class Application {
|
|
|
242
242
|
* @returns 响应对象
|
|
243
243
|
*/
|
|
244
244
|
private async handleRequest(context: Context): Promise<Response> {
|
|
245
|
+
const logger = LoggerManager.getLogger();
|
|
246
|
+
logger.debug('[Request] Incoming', {
|
|
247
|
+
method: context.method,
|
|
248
|
+
path: context.path,
|
|
249
|
+
url: context.url?.href,
|
|
250
|
+
});
|
|
251
|
+
|
|
245
252
|
// 使用 AsyncLocalStorage 包裹请求处理,确保所有中间件和控制器都在请求上下文中执行
|
|
246
253
|
return await contextStore.run(context, async () => {
|
|
247
254
|
// 对于 POST、PUT、PATCH 请求,提前解析 body 并缓存
|
|
@@ -265,6 +272,10 @@ export class Application {
|
|
|
265
272
|
return response;
|
|
266
273
|
}
|
|
267
274
|
|
|
275
|
+
logger.debug('[Router] No route matched', {
|
|
276
|
+
method: context.method,
|
|
277
|
+
path: context.path,
|
|
278
|
+
});
|
|
268
279
|
context.setStatus(404);
|
|
269
280
|
return context.createResponse({ error: 'Not Found' });
|
|
270
281
|
});
|
package/src/error/handler.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { HttpException } from './http-exception';
|
|
|
3
3
|
import { ExceptionFilterRegistry } from './filter';
|
|
4
4
|
import { ValidationError } from '../validation';
|
|
5
5
|
import { ErrorMessageI18n } from './i18n';
|
|
6
|
+
import { LoggerManager } from '@dangao/logsmith';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* 全局错误处理
|
|
@@ -16,6 +17,15 @@ export async function handleError(error: unknown, context: Context): Promise<Res
|
|
|
16
17
|
|
|
17
18
|
if (error instanceof HttpException) {
|
|
18
19
|
context.setStatus(error.status);
|
|
20
|
+
if (error.status >= 400) {
|
|
21
|
+
LoggerManager.getLogger().debug('HttpException', {
|
|
22
|
+
method: context.method,
|
|
23
|
+
path: context.path,
|
|
24
|
+
status: error.status,
|
|
25
|
+
code: error.code,
|
|
26
|
+
message: error.message,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
19
29
|
|
|
20
30
|
// 如果异常有错误码,尝试国际化消息
|
|
21
31
|
let errorMessage = error.message;
|
|
@@ -56,7 +66,14 @@ export async function handleError(error: unknown, context: Context): Promise<Res
|
|
|
56
66
|
}
|
|
57
67
|
|
|
58
68
|
const message = error instanceof Error ? error.message : String(error);
|
|
69
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
59
70
|
context.setStatus(500);
|
|
71
|
+
LoggerManager.getLogger().debug('Internal error (500)', {
|
|
72
|
+
method: context.method,
|
|
73
|
+
path: context.path,
|
|
74
|
+
message,
|
|
75
|
+
stack,
|
|
76
|
+
});
|
|
60
77
|
return context.createResponse({
|
|
61
78
|
error: 'Internal Server Error',
|
|
62
79
|
details: process.env.NODE_ENV === 'production' ? undefined : message,
|
|
@@ -41,12 +41,22 @@ export function createErrorHandlingMiddleware(
|
|
|
41
41
|
try {
|
|
42
42
|
return await next();
|
|
43
43
|
} catch (error) {
|
|
44
|
+
const routeHandler = (context as { routeHandler?: { controller?: { name?: string }; method?: string } }).routeHandler;
|
|
44
45
|
log(error, { method: context.method, path: context.path });
|
|
45
46
|
logger.error("Unhandled error", {
|
|
46
47
|
method: context.method,
|
|
47
48
|
path: context.path,
|
|
48
49
|
error,
|
|
49
50
|
});
|
|
51
|
+
logger.debug("Unhandled error details", {
|
|
52
|
+
method: context.method,
|
|
53
|
+
path: context.path,
|
|
54
|
+
routeHandler: routeHandler
|
|
55
|
+
? `${routeHandler.controller?.name ?? 'unknown'}.${routeHandler.method ?? 'unknown'}`
|
|
56
|
+
: undefined,
|
|
57
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
58
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
59
|
+
});
|
|
50
60
|
|
|
51
61
|
if (error instanceof Response) {
|
|
52
62
|
return error as Response;
|
|
@@ -38,6 +38,14 @@ export function createLoggerMiddleware(
|
|
|
38
38
|
} finally {
|
|
39
39
|
const status = response?.status ?? context.statusCode ?? 200;
|
|
40
40
|
log(`${prefix} ${context.method} ${context.path} ${status}`);
|
|
41
|
+
if (status >= 400) {
|
|
42
|
+
const logger = LoggerManager.getLogger();
|
|
43
|
+
logger.debug(`${prefix} Error response`, {
|
|
44
|
+
method: context.method,
|
|
45
|
+
path: context.path,
|
|
46
|
+
statusCode: status,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
41
49
|
}
|
|
42
50
|
};
|
|
43
51
|
}
|
|
@@ -85,6 +93,15 @@ export function createRequestLoggingMiddleware(
|
|
|
85
93
|
}ms`,
|
|
86
94
|
error instanceof Error ? { error: error.message } : undefined,
|
|
87
95
|
);
|
|
96
|
+
if (error instanceof Error) {
|
|
97
|
+
LoggerManager.getLogger().debug(`${prefix} Request error details`, {
|
|
98
|
+
method: context.method,
|
|
99
|
+
path: context.path,
|
|
100
|
+
durationMs: duration.toFixed(2),
|
|
101
|
+
message: error.message,
|
|
102
|
+
stack: error.stack,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
88
105
|
throw error;
|
|
89
106
|
}
|
|
90
107
|
};
|
package/src/router/decorators.ts
CHANGED
|
@@ -25,10 +25,10 @@ export interface RouteMetadata {
|
|
|
25
25
|
/**
|
|
26
26
|
* 路由装饰器工厂
|
|
27
27
|
* @param method - HTTP 方法
|
|
28
|
-
* @param path -
|
|
28
|
+
* @param path - 路由路径(可选,默认 '',即 @GET() 等价于根路径)
|
|
29
29
|
* @returns 方法装饰器
|
|
30
30
|
*/
|
|
31
|
-
function createRouteDecorator(method: HttpMethod, path: string) {
|
|
31
|
+
function createRouteDecorator(method: HttpMethod, path: string = '') {
|
|
32
32
|
return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
|
|
33
33
|
// 注意:装饰器应用顺序问题
|
|
34
34
|
// 方法装饰器(@GET)在类装饰器(@Controller)之前应用
|
|
@@ -72,9 +72,9 @@ function createRouteDecorator(method: HttpMethod, path: string) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
existingRoutes.push({
|
|
76
|
-
method,
|
|
77
|
-
path,
|
|
75
|
+
existingRoutes.push({
|
|
76
|
+
method,
|
|
77
|
+
path: path ?? '',
|
|
78
78
|
handler: descriptor.value as RouteHandler,
|
|
79
79
|
propertyKey: propertyKeyStr || undefined,
|
|
80
80
|
});
|
|
@@ -84,40 +84,40 @@ function createRouteDecorator(method: HttpMethod, path: string) {
|
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* GET 路由装饰器
|
|
87
|
-
* @param path -
|
|
87
|
+
* @param path - 路由路径(可选,默认 '',即 @GET() 映射到控制器基础路径或 /)
|
|
88
88
|
*/
|
|
89
|
-
export function GET(path: string) {
|
|
89
|
+
export function GET(path: string = '') {
|
|
90
90
|
return createRouteDecorator('GET', path);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
94
|
* POST 路由装饰器
|
|
95
|
-
* @param path -
|
|
95
|
+
* @param path - 路由路径(可选,默认 '')
|
|
96
96
|
*/
|
|
97
|
-
export function POST(path: string) {
|
|
97
|
+
export function POST(path: string = '') {
|
|
98
98
|
return createRouteDecorator('POST', path);
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
102
|
* PUT 路由装饰器
|
|
103
|
-
* @param path -
|
|
103
|
+
* @param path - 路由路径(可选,默认 '')
|
|
104
104
|
*/
|
|
105
|
-
export function PUT(path: string) {
|
|
105
|
+
export function PUT(path: string = '') {
|
|
106
106
|
return createRouteDecorator('PUT', path);
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
110
|
* DELETE 路由装饰器
|
|
111
|
-
* @param path -
|
|
111
|
+
* @param path - 路由路径(可选,默认 '')
|
|
112
112
|
*/
|
|
113
|
-
export function DELETE(path: string) {
|
|
113
|
+
export function DELETE(path: string = '') {
|
|
114
114
|
return createRouteDecorator('DELETE', path);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
118
|
* PATCH 路由装饰器
|
|
119
|
-
* @param path -
|
|
119
|
+
* @param path - 路由路径(可选,默认 '')
|
|
120
120
|
*/
|
|
121
|
-
export function PATCH(path: string) {
|
|
121
|
+
export function PATCH(path: string = '') {
|
|
122
122
|
return createRouteDecorator('PATCH', path);
|
|
123
123
|
}
|
package/src/router/router.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { Constructor } from '../core/types';
|
|
|
3
3
|
import { Route } from './route';
|
|
4
4
|
import type { HttpMethod, RouteHandler, RouteMatch } from './types';
|
|
5
5
|
import type { Middleware } from '../middleware';
|
|
6
|
+
import { LoggerManager } from '@dangao/logsmith';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* 路由器类
|
|
@@ -168,14 +169,23 @@ export class Router {
|
|
|
168
169
|
public async preHandle(context: Context): Promise<void> {
|
|
169
170
|
const method = context.method as HttpMethod;
|
|
170
171
|
const path = this.normalizePath(context.path);
|
|
172
|
+
const logger = LoggerManager.getLogger();
|
|
171
173
|
|
|
172
174
|
// 使用 findRouteWithMatch 避免重复匹配
|
|
173
175
|
const result = this.findRouteWithMatch(method, path);
|
|
174
176
|
if (!result) {
|
|
177
|
+
logger.debug('[Router] Route not matched', { method, path });
|
|
175
178
|
return;
|
|
176
179
|
}
|
|
177
180
|
|
|
178
181
|
const { route, match } = result;
|
|
182
|
+
logger.debug('[Router] Route matched', {
|
|
183
|
+
method,
|
|
184
|
+
path,
|
|
185
|
+
routePath: route.path,
|
|
186
|
+
controller: route.controllerClass?.name,
|
|
187
|
+
methodName: route.methodName,
|
|
188
|
+
});
|
|
179
189
|
if (match.matched) {
|
|
180
190
|
context.params = match.params;
|
|
181
191
|
}
|
|
@@ -196,6 +206,7 @@ export class Router {
|
|
|
196
206
|
public async handle(context: Context): Promise<Response | undefined> {
|
|
197
207
|
const method = context.method as HttpMethod;
|
|
198
208
|
const path = this.normalizePath(context.path);
|
|
209
|
+
const logger = LoggerManager.getLogger();
|
|
199
210
|
|
|
200
211
|
// 使用 findRouteWithMatch 获取路由和匹配结果
|
|
201
212
|
const result = this.findRouteWithMatch(method, path);
|
|
@@ -204,7 +215,13 @@ export class Router {
|
|
|
204
215
|
}
|
|
205
216
|
|
|
206
217
|
const { route, match } = result;
|
|
207
|
-
|
|
218
|
+
|
|
219
|
+
logger.debug('[Router] Executing handler', {
|
|
220
|
+
path: route.path,
|
|
221
|
+
controller: route.controllerClass?.name,
|
|
222
|
+
methodName: route.methodName,
|
|
223
|
+
});
|
|
224
|
+
|
|
208
225
|
// 设置路径参数
|
|
209
226
|
if (match.matched) {
|
|
210
227
|
context.params = match.params;
|
|
@@ -483,6 +483,141 @@ describe('Controller Path Normalization', () => {
|
|
|
483
483
|
expect(data.success).toBe(true);
|
|
484
484
|
});
|
|
485
485
|
|
|
486
|
+
// 场景 a: @Controller() + @GET('test') / @POST('test') -> /test
|
|
487
|
+
test('scenario a: @Controller() with @GET("test") and @POST("test") should match /test', async () => {
|
|
488
|
+
@Controller()
|
|
489
|
+
class TestController {
|
|
490
|
+
@GET('test')
|
|
491
|
+
public getTest() {
|
|
492
|
+
return { method: 'GET', path: 'test' };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
@POST('test')
|
|
496
|
+
public postTest() {
|
|
497
|
+
return { method: 'POST', path: 'test' };
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
app.registerController(TestController);
|
|
502
|
+
await app.listen();
|
|
503
|
+
|
|
504
|
+
const getRes = await fetch(`http://localhost:${port}/test`);
|
|
505
|
+
expect(getRes.status).toBe(200);
|
|
506
|
+
expect((await getRes.json()).method).toBe('GET');
|
|
507
|
+
|
|
508
|
+
const postRes = await fetch(`http://localhost:${port}/test`, { method: 'POST' });
|
|
509
|
+
expect(postRes.status).toBe(200);
|
|
510
|
+
expect((await postRes.json()).method).toBe('POST');
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
// 场景 b: @Controller() + @GET('/test') / @POST('/test') -> /test
|
|
514
|
+
test('scenario b: @Controller() with @GET("/test") and @POST("/test") should match /test', async () => {
|
|
515
|
+
@Controller()
|
|
516
|
+
class TestController {
|
|
517
|
+
@GET('/test')
|
|
518
|
+
public getTest() {
|
|
519
|
+
return { method: 'GET', path: 'test' };
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
@POST('/test')
|
|
523
|
+
public postTest() {
|
|
524
|
+
return { method: 'POST', path: 'test' };
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
app.registerController(TestController);
|
|
529
|
+
await app.listen();
|
|
530
|
+
|
|
531
|
+
const getRes = await fetch(`http://localhost:${port}/test`);
|
|
532
|
+
expect(getRes.status).toBe(200);
|
|
533
|
+
expect((await getRes.json()).method).toBe('GET');
|
|
534
|
+
|
|
535
|
+
const postRes = await fetch(`http://localhost:${port}/test`, { method: 'POST' });
|
|
536
|
+
expect(postRes.status).toBe(200);
|
|
537
|
+
expect((await postRes.json()).method).toBe('POST');
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
// 场景 c: @Controller('') + @GET() / @POST() -> /
|
|
541
|
+
test('scenario c: @Controller("") with @GET() and @POST() should match /', async () => {
|
|
542
|
+
@Controller('')
|
|
543
|
+
class TestController {
|
|
544
|
+
@GET()
|
|
545
|
+
public getRoot() {
|
|
546
|
+
return { method: 'GET', path: '/' };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
@POST()
|
|
550
|
+
public postRoot() {
|
|
551
|
+
return { method: 'POST', path: '/' };
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
app.registerController(TestController);
|
|
556
|
+
await app.listen();
|
|
557
|
+
|
|
558
|
+
const getRes = await fetch(`http://localhost:${port}/`);
|
|
559
|
+
expect(getRes.status).toBe(200);
|
|
560
|
+
expect((await getRes.json()).method).toBe('GET');
|
|
561
|
+
|
|
562
|
+
const postRes = await fetch(`http://localhost:${port}/`, { method: 'POST' });
|
|
563
|
+
expect(postRes.status).toBe(200);
|
|
564
|
+
expect((await postRes.json()).method).toBe('POST');
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// 场景 d: @Controller() + @GET() / @POST() -> /
|
|
568
|
+
test('scenario d: @Controller() with @GET() and @POST() should match /', async () => {
|
|
569
|
+
@Controller()
|
|
570
|
+
class TestController {
|
|
571
|
+
@GET()
|
|
572
|
+
public getRoot() {
|
|
573
|
+
return { method: 'GET', path: '/' };
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
@POST()
|
|
577
|
+
public postRoot() {
|
|
578
|
+
return { method: 'POST', path: '/' };
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
app.registerController(TestController);
|
|
583
|
+
await app.listen();
|
|
584
|
+
|
|
585
|
+
const getRes = await fetch(`http://localhost:${port}/`);
|
|
586
|
+
expect(getRes.status).toBe(200);
|
|
587
|
+
expect((await getRes.json()).method).toBe('GET');
|
|
588
|
+
|
|
589
|
+
const postRes = await fetch(`http://localhost:${port}/`, { method: 'POST' });
|
|
590
|
+
expect(postRes.status).toBe(200);
|
|
591
|
+
expect((await postRes.json()).method).toBe('POST');
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
// 场景 e: @Controller() + @GET('/') / @POST('/') -> /
|
|
595
|
+
test('scenario e: @Controller() with @GET("/") and @POST("/") should match /', async () => {
|
|
596
|
+
@Controller()
|
|
597
|
+
class TestController {
|
|
598
|
+
@GET('/')
|
|
599
|
+
public getRoot() {
|
|
600
|
+
return { method: 'GET', path: '/' };
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
@POST('/')
|
|
604
|
+
public postRoot() {
|
|
605
|
+
return { method: 'POST', path: '/' };
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
app.registerController(TestController);
|
|
610
|
+
await app.listen();
|
|
611
|
+
|
|
612
|
+
const getRes = await fetch(`http://localhost:${port}/`);
|
|
613
|
+
expect(getRes.status).toBe(200);
|
|
614
|
+
expect((await getRes.json()).method).toBe('GET');
|
|
615
|
+
|
|
616
|
+
const postRes = await fetch(`http://localhost:${port}/`, { method: 'POST' });
|
|
617
|
+
expect(postRes.status).toBe(200);
|
|
618
|
+
expect((await postRes.json()).method).toBe('POST');
|
|
619
|
+
});
|
|
620
|
+
|
|
486
621
|
// 测试所有场景在同一应用中同时工作
|
|
487
622
|
test('should handle all path normalization scenarios simultaneously', async () => {
|
|
488
623
|
@Controller('/')
|