[fixed] - eslint

This commit is contained in:
Swapnil Bendal
2024-12-17 19:43:28 +05:30
parent ac7b46c661
commit eb8b1d3c54
7 changed files with 112 additions and 82 deletions

View File

@@ -1 +1 @@
npm test
yarn lint-staged

6
.lintstagedrc.json Normal file
View File

@@ -0,0 +1,6 @@
{
"*.js": [
"eslint",
"prettier --write **/*.js"
]
}

16
ecosystem.config.json Normal file
View File

@@ -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
}
}
]
}

View File

@@ -45,7 +45,7 @@ class App {
public listen(port: number): ReturnType<typeof this.app.listen> {
return this.app.listen(port, () => {
logger.info(`Server listening on port ${config.port}`);
logger.info(`Enviorment :- ${config.env}`);
logger.info(`Environment :- ${config.env}`);
})
}
}

View File

@@ -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<Router>; // 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<Router>): Promise<void> {
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

View File

@@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from 'express';
export function AsyncHandler() {
return function (
target: any,
target: object,
propertyKey: string,
descriptor: PropertyDescriptor
): void {

View File

@@ -1,32 +1,33 @@
class ApiError<T = any> extends Error {
statusCode: number;
data: T | null;
message: string;
success: boolean;
errors: Array<any>;
isOperational: boolean;
stack?: string;
class ApiError<T = unknown> extends Error {
statusCode: number;
data: T | null;
message: string;
success: boolean;
errors: Array<unknown>;
isOperational: boolean;
stack?: string;
constructor(
statusCode: number,
message: string = 'Something went wrong',
errors: Array<any> = [],
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<unknown> = [],
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;
export default ApiError;