Skip to content

Commit

Permalink
feat: enhance HttpStatusCodeMiddleware to accept globalPrefix for pat…
Browse files Browse the repository at this point in the history
…h handling
  • Loading branch information
rsaz committed Nov 16, 2024
1 parent a9f61f5 commit a568598
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/adapter-express/application-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class AppExpress extends ApplicationBase implements Server.IWebServer {
this.middlewares.push(...(pipeline as Array<ExpressHandler>));

/* Apply the status code to the response */
this.middlewares.unshift(new HttpStatusCodeMiddleware() as ExpressoMiddleware);
this.middlewares.unshift(new HttpStatusCodeMiddleware(this.globalPrefix) as ExpressoMiddleware);

Check warning on line 168 in src/adapter-express/application-express.ts

View check run for this annotation

Codecov / codecov/patch

src/adapter-express/application-express.ts#L168

Added line #L168 was not covered by tests

const expressServer = new InversifyExpressServer(this.appContainer.Container, null, {

Check warning on line 170 in src/adapter-express/application-express.ts

View check run for this annotation

Codecov / codecov/patch

src/adapter-express/application-express.ts#L170

Added line #L170 was not covered by tests
rootPath: this.globalPrefix as string,
Expand Down
19 changes: 14 additions & 5 deletions src/adapter-express/express-utils/http-status-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ import { HTTP_CODE_METADATA } from "./constants";
* @returns express.RequestHandler
*/
export class HttpStatusCodeMiddleware extends ExpressoMiddleware {
private globalPrefix: string;

constructor(globalPrefix: string = "/") {
super();
this.globalPrefix = globalPrefix;
}

Check warning on line 18 in src/adapter-express/express-utils/http-status-middleware.ts

View check run for this annotation

Codecov / codecov/patch

src/adapter-express/express-utils/http-status-middleware.ts#L12-L18

Added lines #L12 - L18 were not covered by tests
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();

if (path === "/" || path === "") {
path = "/";
let path = req.path;
if (this.globalPrefix !== "/" && path.startsWith(this.globalPrefix)) {
path = path.slice(this.globalPrefix.length);

Check warning on line 24 in src/adapter-express/express-utils/http-status-middleware.ts

View check run for this annotation

Codecov / codecov/patch

src/adapter-express/express-utils/http-status-middleware.ts#L22-L24

Added lines #L22 - L24 were not covered by tests
}

path = `${path}/-${formattedMethod}`;
path = path.endsWith("/") ? path.slice(0, -1) : path;
const formattedMethod = req.method.toLowerCase();

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

Check warning on line 30 in src/adapter-express/express-utils/http-status-middleware.ts

View check run for this annotation

Codecov / codecov/patch

src/adapter-express/express-utils/http-status-middleware.ts#L27-L30

Added lines #L27 - L30 were not covered by tests

const statusCode = statusCodeMapping[path];

Expand Down

0 comments on commit a568598

Please sign in to comment.