From 55359bf31acfdf0186f6916cd0dc1897d1f24cd2 Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Tue, 11 Jun 2024 00:14:46 -0700 Subject: [PATCH 1/5] fix: init commit --- src/adapter-express/express-utils/http-status-middleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapter-express/express-utils/http-status-middleware.ts b/src/adapter-express/express-utils/http-status-middleware.ts index ba4df30..feb1384 100644 --- a/src/adapter-express/express-utils/http-status-middleware.ts +++ b/src/adapter-express/express-utils/http-status-middleware.ts @@ -16,7 +16,7 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { if (path === "/" || path === "") { path = "/"; } - + // branch const statusCode = statusCodeMapping[path]; if (statusCode) { From cdf0fbd6c864a0e538372dc102664538112f0e30 Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 11 Jun 2024 00:48:25 -0700 Subject: [PATCH 2/5] fix: decorator method case confict fix --- .../express-utils/decorators.ts | 29 +++++++++++++++---- .../express-utils/http-status-middleware.ts | 26 ++++++++++++++--- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/adapter-express/express-utils/decorators.ts b/src/adapter-express/express-utils/decorators.ts index d29120a..fcc2c04 100644 --- a/src/adapter-express/express-utils/decorators.ts +++ b/src/adapter-express/express-utils/decorators.ts @@ -45,8 +45,13 @@ export function controller(path: string, ...middleware: Array) { 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]; + } } } @@ -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); @@ -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); diff --git a/src/adapter-express/express-utils/http-status-middleware.ts b/src/adapter-express/express-utils/http-status-middleware.ts index feb1384..ae82c46 100644 --- a/src/adapter-express/express-utils/http-status-middleware.ts +++ b/src/adapter-express/express-utils/http-status-middleware.ts @@ -12,21 +12,39 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { use(req: Request, res: Response, next: NextFunction): void | Promise { 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": From 469f65e9248aefe5db084608684ca211204c2f5b Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Tue, 11 Jun 2024 01:43:52 -0700 Subject: [PATCH 3/5] refactor: without and with decor in the same route conflict --- .../express-utils/decorators.ts | 26 +++++++------- .../express-utils/http-status-middleware.ts | 36 +++++++++++++++---- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/adapter-express/express-utils/decorators.ts b/src/adapter-express/express-utils/decorators.ts index fcc2c04..6b8fc87 100644 --- a/src/adapter-express/express-utils/decorators.ts +++ b/src/adapter-express/express-utils/decorators.ts @@ -34,21 +34,19 @@ export function controller(path: string, ...middleware: Array) { 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]) { - let realPath = pathMetadata[key]["path"] === "/" ? path : `${path}${pathMetadata[key]["path"]}`; + const realPath = + pathMetadata[key]["path"] === "/" ? path : `${path}${pathMetadata[key]["path"]}`; if (statusCodePathMapping[realPath]) { - statusCodePathMapping[`${realPath}/-${pathMetadata[key]["method"].toLowerCase()}`] = statusCodeMetadata[key]; + statusCodePathMapping[`${realPath}/-${pathMetadata[key]["method"].toLowerCase()}`] = + statusCodeMetadata[key]; } else { statusCodePathMapping[realPath] = statusCodeMetadata[key]; } @@ -188,13 +186,13 @@ function enhancedHttpMethod( if (pathMetadata) { pathMetadata[key] = { path, - method + method, }; } else { pathMetadata = {}; pathMetadata[key] = { path, - method + method, }; } @@ -251,13 +249,13 @@ export function httpMethod( if (pathMetadata) { pathMetadata[key] = { path, - method + method, }; } else { pathMetadata = {}; pathMetadata[key] = { path, - method + method, }; } diff --git a/src/adapter-express/express-utils/http-status-middleware.ts b/src/adapter-express/express-utils/http-status-middleware.ts index ae82c46..b6616c3 100644 --- a/src/adapter-express/express-utils/http-status-middleware.ts +++ b/src/adapter-express/express-utils/http-status-middleware.ts @@ -12,17 +12,22 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { use(req: Request, res: Response, next: NextFunction): void | Promise { 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); + console.log("status code mapping", statusCodeMapping); if (path === "/" || path === "") { path = "/"; } - const statusCode = statusCodeMapping[`${path}/-${req.method.toLowerCase()}`] | statusCodeMapping[path]; + const statusCode = + statusCodeMapping[`${path}/-${req.method.toLowerCase()}`] || statusCodeMapping[path]; if (statusCode) { res.status(statusCode); } else { - const patternMatchStatusCode = this.findMatchingParameterPath(path, statusCodeMapping, req.method.toLowerCase()); + const patternMatchStatusCode = this.findMatchingParameterPath( + path, + statusCodeMapping, + req.method.toLowerCase(), + ); if (patternMatchStatusCode) { res.status(patternMatchStatusCode); @@ -33,9 +38,20 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { next(); } - private findMatchingParameterPath(path, mapping, method) { - for (let pathCode in mapping) { - const patternCheck = new RegExp('^' + pathCode.replace(/:[^\s/]+/g, '([^/]+)') + '$'); + /** + * 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, + method: string, + ): number | null { + for (const pathCode in mapping) { + const patternCheck = new RegExp("^" + pathCode.replace(/:[^\s/]+/g, "([^/]+)") + "$"); if (patternCheck.test(path)) { return mapping[`${pathCode}/-${method}`] || mapping[pathCode]; @@ -45,6 +61,11 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { 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": @@ -56,6 +77,9 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { case "put": res.statusCode = 204; break; + case "patch": + res.statusCode = 204; + break; case "delete": res.statusCode = 204; break; From 975e45112181088bcb9d8f1487e8272deb49d3df Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 11 Jun 2024 11:43:03 -0700 Subject: [PATCH 4/5] fix: patch issue where not using http decorator shows incorrect code --- src/adapter-express/express-utils/decorators.ts | 8 ++------ .../express-utils/http-status-middleware.ts | 11 +++++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/adapter-express/express-utils/decorators.ts b/src/adapter-express/express-utils/decorators.ts index 6b8fc87..2ffcc38 100644 --- a/src/adapter-express/express-utils/decorators.ts +++ b/src/adapter-express/express-utils/decorators.ts @@ -44,12 +44,8 @@ export function controller(path: string, ...middleware: Array) { const 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]; - } + statusCodePathMapping[`${realPath}/-${pathMetadata[key]["method"].toLowerCase()}`] = + statusCodeMetadata[key]; } } diff --git a/src/adapter-express/express-utils/http-status-middleware.ts b/src/adapter-express/express-utils/http-status-middleware.ts index b6616c3..f7f1009 100644 --- a/src/adapter-express/express-utils/http-status-middleware.ts +++ b/src/adapter-express/express-utils/http-status-middleware.ts @@ -12,13 +12,16 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { use(req: Request, res: Response, next: NextFunction): void | Promise { 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 = "/"; } - const statusCode = - statusCodeMapping[`${path}/-${req.method.toLowerCase()}`] || statusCodeMapping[path]; + path = `${path}/-${formattedMethod}`; + + const statusCode = statusCodeMapping[path]; if (statusCode) { res.status(statusCode); @@ -26,7 +29,7 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { const patternMatchStatusCode = this.findMatchingParameterPath( path, statusCodeMapping, - req.method.toLowerCase(), + formattedMethod, ); if (patternMatchStatusCode) { @@ -54,7 +57,7 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { const patternCheck = new RegExp("^" + pathCode.replace(/:[^\s/]+/g, "([^/]+)") + "$"); if (patternCheck.test(path)) { - return mapping[`${pathCode}/-${method}`] || mapping[pathCode]; + return mapping[pathCode]; } } From fd4087276672a6b3c34563736892e4065a497bfa Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Tue, 11 Jun 2024 12:43:53 -0700 Subject: [PATCH 5/5] fix: remove unused method param in find param pattern --- .../express-utils/http-status-middleware.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/adapter-express/express-utils/http-status-middleware.ts b/src/adapter-express/express-utils/http-status-middleware.ts index f7f1009..f76edd5 100644 --- a/src/adapter-express/express-utils/http-status-middleware.ts +++ b/src/adapter-express/express-utils/http-status-middleware.ts @@ -26,11 +26,7 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { if (statusCode) { res.status(statusCode); } else { - const patternMatchStatusCode = this.findMatchingParameterPath( - path, - statusCodeMapping, - formattedMethod, - ); + const patternMatchStatusCode = this.findMatchingParameterPath(path, statusCodeMapping); if (patternMatchStatusCode) { res.status(patternMatchStatusCode); @@ -48,11 +44,7 @@ export class HttpStatusCodeMiddleware extends ExpressoMiddleware { * @param method - The method to check. * @returns The status code if found, otherwise null. **/ - private findMatchingParameterPath( - path: string, - mapping: Record, - method: string, - ): number | null { + private findMatchingParameterPath(path: string, mapping: Record): number | null { for (const pathCode in mapping) { const patternCheck = new RegExp("^" + pathCode.replace(/:[^\s/]+/g, "([^/]+)") + "$");