Skip to content

Commit

Permalink
fix: decorator method case confict fix
Browse files Browse the repository at this point in the history
  • Loading branch information
projectjimcs committed Jun 11, 2024
1 parent 55359bf commit cdf0fbd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
29 changes: 23 additions & 6 deletions src/adapter-express/express-utils/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ export function controller(path: string, ...middleware: Array<Middleware>) {

for (const key in pathMetadata) {
if (statusCodeMetadata && statusCodeMetadata[key]) {
const realPath = pathMetadata[key] === "/" ? path : `${path}${pathMetadata[key]}`;
statusCodePathMapping[realPath] = statusCodeMetadata[key];
let realPath = pathMetadata[key]["path"] === "/" ? path : `${path}${pathMetadata[key]["path"]}`;

if (statusCodePathMapping[realPath]) {
statusCodePathMapping[`${realPath}/-${pathMetadata[key]["method"].toLowerCase()}`] = statusCodeMetadata[key];
} else {
statusCodePathMapping[realPath] = statusCodeMetadata[key];
}
}
}

Expand Down Expand Up @@ -181,10 +186,16 @@ function enhancedHttpMethod(
let pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect);

if (pathMetadata) {
pathMetadata[key] = path;
pathMetadata[key] = {
path,
method
};
} else {
pathMetadata = {};
pathMetadata[key] = path;
pathMetadata[key] = {
path,
method
};
}

Reflect.defineMetadata(HTTP_CODE_METADATA.path, pathMetadata, Reflect);
Expand Down Expand Up @@ -238,10 +249,16 @@ export function httpMethod(
let pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect);

if (pathMetadata) {
pathMetadata[key] = path;
pathMetadata[key] = {
path,
method
};
} else {
pathMetadata = {};
pathMetadata[key] = path;
pathMetadata[key] = {
path,
method
};
}

Reflect.defineMetadata(HTTP_CODE_METADATA.path, pathMetadata, Reflect);
Expand Down
26 changes: 22 additions & 4 deletions src/adapter-express/express-utils/http-status-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,39 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware {
use(req: Request, res: Response, next: NextFunction): void | Promise<void> {
const statusCodeMapping = Reflect.getMetadata(HTTP_CODE_METADATA.httpCode, Reflect);
let path = req.path.endsWith("/") ? req.path.slice(0, -1) : req.path;

console.log("status code mapping: ", statusCodeMapping);
if (path === "/" || path === "") {
path = "/";
}
// branch
const statusCode = statusCodeMapping[path];

const statusCode = statusCodeMapping[`${path}/-${req.method.toLowerCase()}`] | statusCodeMapping[path];

if (statusCode) {
res.status(statusCode);
} else {
this.setDefaultStatusCode(req, res);
const patternMatchStatusCode = this.findMatchingParameterPath(path, statusCodeMapping, req.method.toLowerCase());

if (patternMatchStatusCode) {
res.status(patternMatchStatusCode);
} else {
this.setDefaultStatusCode(req, res);
}
}
next();
}

private findMatchingParameterPath(path, mapping, method) {
for (let pathCode in mapping) {
const patternCheck = new RegExp('^' + pathCode.replace(/:[^\s/]+/g, '([^/]+)') + '$');

if (patternCheck.test(path)) {
return mapping[`${pathCode}/-${method}`] || mapping[pathCode];
}
}

return null;
}

private setDefaultStatusCode(req: Request, res: Response): void {
switch (req.method.toLowerCase()) {
case "get":
Expand Down

0 comments on commit cdf0fbd

Please sign in to comment.