Skip to content

⬆️ Update dependency express to v5 #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ HOST="localhost" # Hostname for the server
CORS_ORIGIN="http://localhost:*" # Allowed CORS origin, adjust as necessary

# Rate Limiting
COMMON_RATE_LIMIT_WINDOW_MS="1000" # Window size for rate limiting (ms)
COMMON_RATE_LIMIT_WINDOW_MS="1000" # Window size for rate limiting (ms)
COMMON_RATE_LIMIT_MAX_REQUESTS="20" # Max number of requests per window per IP
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"main": "index.ts",
"private": true,
"scripts": {
"build": "tsup",
"build": "tsc && tsup",
"start:dev": "tsx watch --clear-screen=false src/index.ts | pino-pretty",
"start:prod": "node dist/index.js",
"lint": "biome lint",
Expand All @@ -23,7 +23,7 @@
"cors": "2.8.5",
"dotenv": "16.5.0",
"envalid": "8.0.0",
"express": "4.21.2",
"express": "5.1.0",
"express-rate-limit": "7.5.0",
"helmet": "8.1.0",
"http-status-codes": "2.3.0",
Expand Down
284 changes: 128 additions & 156 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/api/healthCheck/healthCheckRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { z } from "zod";

import { createApiResponse } from "@/api-docs/openAPIResponseBuilders";
import { ServiceResponse } from "@/common/models/serviceResponse";
import { handleServiceResponse } from "@/common/utils/httpHandlers";

export const healthCheckRegistry = new OpenAPIRegistry();
export const healthCheckRouter: Router = express.Router();
Expand All @@ -18,5 +17,5 @@ healthCheckRegistry.registerPath({

healthCheckRouter.get("/", (_req: Request, res: Response) => {
const serviceResponse = ServiceResponse.success("Service is healthy", null);
return handleServiceResponse(serviceResponse, res);
res.status(serviceResponse.statusCode).send(serviceResponse);
});
5 changes: 2 additions & 3 deletions src/api/user/userController.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import type { Request, RequestHandler, Response } from "express";

import { userService } from "@/api/user/userService";
import { handleServiceResponse } from "@/common/utils/httpHandlers";

class UserController {
public getUsers: RequestHandler = async (_req: Request, res: Response) => {
const serviceResponse = await userService.findAll();
return handleServiceResponse(serviceResponse, res);
res.status(serviceResponse.statusCode).send(serviceResponse);
};

public getUser: RequestHandler = async (req: Request, res: Response) => {
const id = Number.parseInt(req.params.id as string, 10);
const serviceResponse = await userService.findById(id);
return handleServiceResponse(serviceResponse, res);
res.status(serviceResponse.statusCode).send(serviceResponse);
};
}

Expand Down
25 changes: 9 additions & 16 deletions src/common/__tests__/errorHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,20 @@ describe("Error Handler Middleware", () => {
});

app.use(errorHandler());
app.use("*", (req, res) => res.status(StatusCodes.NOT_FOUND).send("Not Found"));
});

describe("Handling unknown routes", () => {
it("returns 404 for unknown routes", async () => {
const response = await request(app).get("/this-route-does-not-exist");
expect(response.status).toBe(StatusCodes.NOT_FOUND);
});
it("returns 404 for unknown routes", async () => {
const response = await request(app).get("/this-route-does-not-exist");
expect(response.status).toBe(StatusCodes.NOT_FOUND);
});

describe("Handling thrown errors", () => {
it("handles thrown errors with a 500 status code", async () => {
const response = await request(app).get("/error");
expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR);
});
it("handles thrown errors with a 500 status code", async () => {
const response = await request(app).get("/error");
expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR);
});

describe("Handling errors passed to next()", () => {
it("handles errors passed to next() with a 500 status code", async () => {
const response = await request(app).get("/next-error");
expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR);
});
it("handles errors passed to next() with a 500 status code", async () => {
const response = await request(app).get("/next-error");
expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR);
});
});
4 changes: 3 additions & 1 deletion src/common/__tests__/requestLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ describe("Request Logger Middleware", () => {

beforeAll(() => {
app.use(requestLogger);
app.get("/success", (req, res) => res.status(StatusCodes.OK).send("Success"));
app.get("/success", (_req, res) => {
res.status(StatusCodes.OK).send("Success");
});
app.get("/redirect", (req, res) => res.redirect("/success"));
app.get("/error", () => {
throw new Error("Test error");
Expand Down
2 changes: 1 addition & 1 deletion src/common/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ErrorRequestHandler, RequestHandler } from "express";
import { StatusCodes } from "http-status-codes";

const unexpectedRequest: RequestHandler = (_req, res) => {
res.sendStatus(StatusCodes.NOT_FOUND);
res.status(StatusCodes.NOT_FOUND).send("Not Found");
};

const addErrorToRequestLog: ErrorRequestHandler = (err, _req, res, next) => {
Expand Down
6 changes: 1 addition & 5 deletions src/common/utils/httpHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import type { ZodError, ZodSchema } from "zod";

import { ServiceResponse } from "@/common/models/serviceResponse";

export const handleServiceResponse = (serviceResponse: ServiceResponse<unknown>, response: Response) => {
return response.status(serviceResponse.statusCode).send(serviceResponse);
};

export const validateRequest = (schema: ZodSchema) => (req: Request, res: Response, next: NextFunction) => {
try {
schema.parse({ body: req.body, query: req.query, params: req.params });
Expand All @@ -16,6 +12,6 @@ export const validateRequest = (schema: ZodSchema) => (req: Request, res: Respon
const errorMessage = `Invalid input: ${(err as ZodError).errors.map((e) => e.message).join(", ")}`;
const statusCode = StatusCodes.BAD_REQUEST;
const serviceResponse = ServiceResponse.failure(errorMessage, null, statusCode);
return handleServiceResponse(serviceResponse, res);
res.status(serviceResponse.statusCode).send(serviceResponse);
}
};