From eb8b1d3c54e311209cf466322d0b25ec917a0ce8 Mon Sep 17 00:00:00 2001 From: Swapnil Bendal <84583651+Swapnil155@users.noreply.github.com> Date: Tue, 17 Dec 2024 19:43:28 +0530 Subject: [PATCH] [fixed] - eslint --- .husky/pre-commit | 2 +- .lintstagedrc.json | 6 ++ ecosystem.config.json | 16 +++++ src/app.ts | 2 +- src/routes/index.ts | 107 +++++++++++++++-------------- src/utils/handler/async.handler.ts | 2 +- src/utils/helper/ApiError.ts | 59 ++++++++-------- 7 files changed, 112 insertions(+), 82 deletions(-) create mode 100644 .lintstagedrc.json create mode 100644 ecosystem.config.json diff --git a/.husky/pre-commit b/.husky/pre-commit index 72c4429..1a1f1f0 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1 @@ -npm test +yarn lint-staged \ No newline at end of file diff --git a/.lintstagedrc.json b/.lintstagedrc.json new file mode 100644 index 0000000..d44ff9d --- /dev/null +++ b/.lintstagedrc.json @@ -0,0 +1,6 @@ +{ + "*.js": [ + "eslint", + "prettier --write **/*.js" + ] +} \ No newline at end of file diff --git a/ecosystem.config.json b/ecosystem.config.json new file mode 100644 index 0000000..72f64b2 --- /dev/null +++ b/ecosystem.config.json @@ -0,0 +1,16 @@ +{ + "apps": [ + { + "name": "Typescript-Backend", + "script": "src/index.ts", + "instances": 1, + "autorestart": true, + "watch": false, + "time": true, + "env": { + "NODE_ENV": "production", + "PORT": 3000 + } + } + ] +} diff --git a/src/app.ts b/src/app.ts index 5aea849..35c5af1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -45,7 +45,7 @@ class App { public listen(port: number): ReturnType { return this.app.listen(port, () => { logger.info(`Server listening on port ${config.port}`); - logger.info(`Enviorment :- ${config.env}`); + logger.info(`Environment :- ${config.env}`); }) } } diff --git a/src/routes/index.ts b/src/routes/index.ts index 995ff16..8248e57 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -3,68 +3,75 @@ import config from '../config/config'; // Define a reusable type for route definitions interface RouteDefinition { - path: string; - route: () => Router; // Lazy-loaded route + path: string; + route: () => Promise; // Function returning a Promise of Router } class Routes { - private router: Router; - private defaultRoutes: RouteDefinition[]; - private devRoutes: RouteDefinition[]; + private router: Router; + private defaultRoutes: RouteDefinition[]; + private devRoutes: RouteDefinition[]; - constructor() { - this.router = express.Router(); - this.defaultRoutes = this.initializeDefaultRoutes(); - this.devRoutes = this.initializeDevRoutes(); + constructor() { + this.router = express.Router(); + this.defaultRoutes = this.initializeDefaultRoutes(); + this.devRoutes = this.initializeDevRoutes(); - this.setupRoutes(); - } + this.setupRoutes(); + } - private initializeDefaultRoutes(): RouteDefinition[] { - return [ - { - path: '/products', - route: () => require('./productRoutes').default - } - ] - } + // Initialize default routes with dynamic imports + private initializeDefaultRoutes(): RouteDefinition[] { + return [ + { + path: '/products', + route: () => import('./productRoutes').then((module) => module.default) + } + ]; + } - private initializeDevRoutes(): RouteDefinition[] { - return [ - { - path: '/docs', - route: () => require('./docs/docs.route').default - } - ]; - } + // Initialize development-specific routes with dynamic imports + private initializeDevRoutes(): RouteDefinition[] { + return [ + // { + // path: '/docs', + // route: () => import('./docs/docs.route').then((module) => module.default) + // } + ]; + } - private setupRoutes(): void { - this.defaultRoutes.forEach(({ path, route }) => { - this.registerRoute(path, route); - }); + // Register all routes + private setupRoutes(): void { + this.defaultRoutes.forEach(({ path, route }) => { + this.registerRoute(path, route); + }); - this.setupEnvironmentSpecificRoutes(); - } + this.setupEnvironmentSpecificRoutes(); + } - private setupEnvironmentSpecificRoutes(): void { - if (config.env === 'development') { - this.devRoutes.forEach(({ path, route }) => { - this.registerRoute(path, route); - }); - } - } + // Register environment-specific routes + private setupEnvironmentSpecificRoutes(): void { + if (config.env === 'development') { + this.devRoutes.forEach(({ path, route }) => { + this.registerRoute(path, route); + }); + } + } - private registerRoute(path: string, route: () => Router): void { - try { - this.router.use(path, route()); - } catch (error) { - console.error(`Failed to load route at path: ${path}`, error); - } - } + // Register a single route safely with async handling + private async registerRoute(path: string, route: () => Promise): Promise { + try { + const loadedRoute = await route(); + this.router.use(path, loadedRoute); + } catch (error) { + console.error(`Failed to load route at path: ${path}`, error); + } + } - public getRouter(): Router { - return this.router; - } + // Return the router instance + public getRouter(): Router { + return this.router; + } } // Export the initialized router diff --git a/src/utils/handler/async.handler.ts b/src/utils/handler/async.handler.ts index 38b2b9d..96b9fd0 100644 --- a/src/utils/handler/async.handler.ts +++ b/src/utils/handler/async.handler.ts @@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from 'express'; export function AsyncHandler() { return function ( - target: any, + target: object, propertyKey: string, descriptor: PropertyDescriptor ): void { diff --git a/src/utils/helper/ApiError.ts b/src/utils/helper/ApiError.ts index 8f2e170..25c1615 100644 --- a/src/utils/helper/ApiError.ts +++ b/src/utils/helper/ApiError.ts @@ -1,32 +1,33 @@ -class ApiError extends Error { - statusCode: number; - data: T | null; - message: string; - success: boolean; - errors: Array; - isOperational: boolean; - stack?: string; +class ApiError extends Error { + statusCode: number; + data: T | null; + message: string; + success: boolean; + errors: Array; + isOperational: boolean; + stack?: string; - constructor( - statusCode: number, - message: string = 'Something went wrong', - errors: Array = [], - isOperational: boolean = true, - stack: string = '' - ) { - super(message); - this.statusCode = statusCode; - this.data = null; - this.message = message; - this.success = false; - this.errors = errors; - this.isOperational = isOperational; + constructor( + statusCode: number, + message: string = 'Something went wrong', + errors: Array = [], + isOperational: boolean = true, + stack?: string + ) { + super(message); + this.statusCode = statusCode; + this.data = null; + this.message = message; + this.success = false; + this.errors = errors; + this.isOperational = isOperational; - if (stack) { - this.stack = stack; - } else { - Error.captureStackTrace(this, this.constructor); - } - } + if (stack) { + this.stack = stack; + } else { + Error.captureStackTrace(this, this.constructor); + } + } } -export default ApiError; \ No newline at end of file + +export default ApiError;