@@ -12,21 +12,55 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware {
12
12
use ( req : Request , res : Response , next : NextFunction ) : void | Promise < void > {
13
13
const statusCodeMapping = Reflect . getMetadata ( HTTP_CODE_METADATA . httpCode , Reflect ) ;
14
14
let path = req . path . endsWith ( "/" ) ? req . path . slice ( 0 , - 1 ) : req . path ;
15
+ const formattedMethod = req . method . toLowerCase ( ) ;
15
16
17
+ console . log ( "status code mapping" , statusCodeMapping ) ;
16
18
if ( path === "/" || path === "" ) {
17
19
path = "/" ;
18
20
}
19
21
22
+ path = `${ path } /-${ formattedMethod } ` ;
23
+
20
24
const statusCode = statusCodeMapping [ path ] ;
21
25
22
26
if ( statusCode ) {
23
27
res . status ( statusCode ) ;
24
28
} else {
25
- this . setDefaultStatusCode ( req , res ) ;
29
+ const patternMatchStatusCode = this . findMatchingParameterPath ( path , statusCodeMapping ) ;
30
+
31
+ if ( patternMatchStatusCode ) {
32
+ res . status ( patternMatchStatusCode ) ;
33
+ } else {
34
+ this . setDefaultStatusCode ( req , res ) ;
35
+ }
26
36
}
27
37
next ( ) ;
28
38
}
29
39
40
+ /**
41
+ * Find the matching parameter path.
42
+ * @param path - The path to match.
43
+ * @param mapping - The mapping to check.
44
+ * @param method - The method to check.
45
+ * @returns The status code if found, otherwise null.
46
+ **/
47
+ private findMatchingParameterPath ( path : string , mapping : Record < string , number > ) : number | null {
48
+ for ( const pathCode in mapping ) {
49
+ const patternCheck = new RegExp ( "^" + pathCode . replace ( / : [ ^ \s / ] + / g, "([^/]+)" ) + "$" ) ;
50
+
51
+ if ( patternCheck . test ( path ) ) {
52
+ return mapping [ pathCode ] ;
53
+ }
54
+ }
55
+
56
+ return null ;
57
+ }
58
+
59
+ /**
60
+ * Set the default status code based on the request method.
61
+ * @param req - The request object.
62
+ * @param res - The response object.
63
+ **/
30
64
private setDefaultStatusCode ( req : Request , res : Response ) : void {
31
65
switch ( req . method . toLowerCase ( ) ) {
32
66
case "get" :
@@ -38,6 +72,9 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware {
38
72
case "put" :
39
73
res . statusCode = 204 ;
40
74
break ;
75
+ case "patch" :
76
+ res . statusCode = 204 ;
77
+ break ;
41
78
case "delete" :
42
79
res . statusCode = 204 ;
43
80
break ;
0 commit comments