Skip to content

Commit

Permalink
Merge pull request #39 from expressots/fix/param-id-http-decorator-fa…
Browse files Browse the repository at this point in the history
…iling

Fix/param id http decorator failing
  • Loading branch information
rsaz authored Jun 11, 2024
2 parents 2b1b00d + fd40872 commit 4cf0563
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
39 changes: 25 additions & 14 deletions src/adapter-express/express-utils/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@ export function controller(path: string, ...middleware: Array<Middleware>) {
target,
};

const pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect);
const statusCodeMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.statusCode, Reflect);

let statusCodePathMapping = Reflect.getOwnMetadata(HTTP_CODE_METADATA.httpCode, Reflect);

if (!statusCodePathMapping) {
statusCodePathMapping = {};
}
const pathMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.path, Reflect) || {};
const statusCodeMetadata = Reflect.getOwnMetadata(HTTP_CODE_METADATA.statusCode, Reflect) || {};
const statusCodePathMapping =
Reflect.getOwnMetadata(HTTP_CODE_METADATA.httpCode, Reflect) || {};

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

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

Expand Down Expand Up @@ -181,10 +180,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 +243,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
39 changes: 38 additions & 1 deletion src/adapter-express/express-utils/http-status-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,55 @@ 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;
const formattedMethod = req.method.toLowerCase();

console.log("status code mapping", statusCodeMapping);
if (path === "/" || path === "") {
path = "/";
}

path = `${path}/-${formattedMethod}`;

const statusCode = statusCodeMapping[path];

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

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

/**
* Find the matching parameter path.
* @param path - The path to match.
* @param mapping - The mapping to check.
* @param method - The method to check.
* @returns The status code if found, otherwise null.
**/
private findMatchingParameterPath(path: string, mapping: Record<string, number>): number | null {
for (const pathCode in mapping) {
const patternCheck = new RegExp("^" + pathCode.replace(/:[^\s/]+/g, "([^/]+)") + "$");

if (patternCheck.test(path)) {
return mapping[pathCode];
}
}

return null;
}

/**
* Set the default status code based on the request method.
* @param req - The request object.
* @param res - The response object.
**/
private setDefaultStatusCode(req: Request, res: Response): void {
switch (req.method.toLowerCase()) {
case "get":
Expand All @@ -38,6 +72,9 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware {
case "put":
res.statusCode = 204;
break;
case "patch":
res.statusCode = 204;
break;
case "delete":
res.statusCode = 204;
break;
Expand Down

0 comments on commit 4cf0563

Please sign in to comment.