Skip to content

Commit 4cf0563

Browse files
authored
Merge pull request #39 from expressots/fix/param-id-http-decorator-failing
Fix/param id http decorator failing
2 parents 2b1b00d + fd40872 commit 4cf0563

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

src/adapter-express/express-utils/decorators.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,18 @@ export function controller(path: string, ...middleware: Array<Middleware>) {
3434
target,
3535
};
3636

37-
const pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect);
38-
const statusCodeMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.statusCode, Reflect);
39-
40-
let statusCodePathMapping = Reflect.getOwnMetadata(HTTP_CODE_METADATA.httpCode, Reflect);
41-
42-
if (!statusCodePathMapping) {
43-
statusCodePathMapping = {};
44-
}
37+
const pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect) || {};
38+
const statusCodeMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.statusCode, Reflect) || {};
39+
const statusCodePathMapping =
40+
Reflect.getOwnMetadata(HTTP_CODE_METADATA.httpCode, Reflect) || {};
4541

4642
for (const key in pathMetadata) {
4743
if (statusCodeMetadata && statusCodeMetadata[key]) {
48-
const realPath = pathMetadata[key] === "/" ? path : `${path}${pathMetadata[key]}`;
49-
statusCodePathMapping[realPath] = statusCodeMetadata[key];
44+
const realPath =
45+
pathMetadata[key]["path"] === "/" ? path : `${path}${pathMetadata[key]["path"]}`;
46+
47+
statusCodePathMapping[`${realPath}/-${pathMetadata[key]["method"].toLowerCase()}`] =
48+
statusCodeMetadata[key];
5049
}
5150
}
5251

@@ -181,10 +180,16 @@ function enhancedHttpMethod(
181180
let pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect);
182181

183182
if (pathMetadata) {
184-
pathMetadata[key] = path;
183+
pathMetadata[key] = {
184+
path,
185+
method,
186+
};
185187
} else {
186188
pathMetadata = {};
187-
pathMetadata[key] = path;
189+
pathMetadata[key] = {
190+
path,
191+
method,
192+
};
188193
}
189194

190195
Reflect.defineMetadata(HTTP_CODE_METADATA.path, pathMetadata, Reflect);
@@ -238,10 +243,16 @@ export function httpMethod(
238243
let pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect);
239244

240245
if (pathMetadata) {
241-
pathMetadata[key] = path;
246+
pathMetadata[key] = {
247+
path,
248+
method,
249+
};
242250
} else {
243251
pathMetadata = {};
244-
pathMetadata[key] = path;
252+
pathMetadata[key] = {
253+
path,
254+
method,
255+
};
245256
}
246257

247258
Reflect.defineMetadata(HTTP_CODE_METADATA.path, pathMetadata, Reflect);

src/adapter-express/express-utils/http-status-middleware.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,55 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware {
1212
use(req: Request, res: Response, next: NextFunction): void | Promise<void> {
1313
const statusCodeMapping = Reflect.getMetadata(HTTP_CODE_METADATA.httpCode, Reflect);
1414
let path = req.path.endsWith("/") ? req.path.slice(0, -1) : req.path;
15+
const formattedMethod = req.method.toLowerCase();
1516

17+
console.log("status code mapping", statusCodeMapping);
1618
if (path === "/" || path === "") {
1719
path = "/";
1820
}
1921

22+
path = `${path}/-${formattedMethod}`;
23+
2024
const statusCode = statusCodeMapping[path];
2125

2226
if (statusCode) {
2327
res.status(statusCode);
2428
} 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+
}
2636
}
2737
next();
2838
}
2939

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+
**/
3064
private setDefaultStatusCode(req: Request, res: Response): void {
3165
switch (req.method.toLowerCase()) {
3266
case "get":
@@ -38,6 +72,9 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware {
3872
case "put":
3973
res.statusCode = 204;
4074
break;
75+
case "patch":
76+
res.statusCode = 204;
77+
break;
4178
case "delete":
4279
res.statusCode = 204;
4380
break;

0 commit comments

Comments
 (0)