Skip to content

Commit

Permalink
refactor : added types to app.ts, and fixed many small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdaudin committed Mar 24, 2023
1 parent 7b62d1d commit b80fd08
Show file tree
Hide file tree
Showing 17 changed files with 477 additions and 254 deletions.
108 changes: 102 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"start-dev:build": "tsc && npm run copy-static-files && tsc -w",
"start-dev:run": "nodemon --inspect --ext ts,js,pug,json,css build/server.js",
"start-dev": "concurrently npm:start-dev:*",
"test": "jest --verbose --watchAll",
"test-prod": "jest",
"test": "jest --verbose --watchAll --runInBand",
"test-prod": "jest --runInBand",
"cover": "jest --coverage",
"copy-static-files": "cp -v -R src/datasets build/ && cp -v src/views/*.pug build/views/",
"postinstall": "tsc && npm run copy-static-files"
Expand Down Expand Up @@ -39,22 +39,28 @@
"save-dev": "^0.0.1-security",
"utf8": "^3.0.0",
"validator": "^13.7.0",
"xss-clean": "^0.1.1"
"xss-clean": "^0.1.1",
"xss-filters": "^1.2.7"
},
"devDependencies": {
"@faker-js/faker": "^7.2.0",
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.17",
"@types/express-serve-static-core": "^4.17.33",
"@types/hpp": "^0.2.2",
"@types/jest": "^29.4.0",
"@types/jsonwebtoken": "^9.0.1",
"@types/lodash": "^4.14.191",
"@types/lodash.clonedeep": "^4.5.7",
"@types/lodash.groupby": "^4.6.7",
"@types/luxon": "^3.2.0",
"@types/morgan": "^1.9.4",
"@types/node": "^18.13.0",
"@types/nodemailer": "^6.4.7",
"@types/supertest": "^2.0.12",
"@types/utf8": "^3.0.1",
"@types/validator": "^13.7.12",
"@types/xss-filters": "^0.0.27",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"concurrently": "^7.6.0",
Expand Down
14 changes: 8 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import morgan from 'morgan';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
import mongoSanitize from 'express-mongo-sanitize';
import xss from 'xss-clean';
import xss from './utils/xss';
import hpp from 'hpp';

import AppError from './utils/appError';
Expand All @@ -13,6 +13,7 @@ import { router as destinationsRouter } from './destinations/destinationsRoutes'
import { router as userRouter } from './user/userRoutes';
import { router as airportRouter } from './airports/airportRoutes';
import { router as viewRouter } from './views/viewRoutes';
import { NextFunction, Request, Response } from 'express-serve-static-core';

const app = express();
// better to use early in the middleware.
Expand Down Expand Up @@ -74,11 +75,12 @@ app.all('*', (req, res, next) => {
});

// global error handler
// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.use((err, _req, res, _next) => {
err.statusCode = err.statusCode || 500;
err.status = err.status || 'error';

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
app.use((err: any, _req: Request, res: Response, _next: NextFunction) => {
if (!(err instanceof AppError)) {
err.statusCode = 500;
err.status = 'error';
}
if (err.name === 'JsonWebTokenError') err = handleJWTError();
if (err.name === 'TokenExpiredError') err = handleTokenExpiredError();

Expand Down
Loading

0 comments on commit b80fd08

Please sign in to comment.