forked from swapnil.bendal/TypeScript-Backend-Template
[fixed] - eslint
This commit is contained in:
@@ -1 +1 @@
|
||||
npm test
|
||||
yarn lint-staged
|
||||
6
.lintstagedrc.json
Normal file
6
.lintstagedrc.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"*.js": [
|
||||
"eslint",
|
||||
"prettier --write **/*.js"
|
||||
]
|
||||
}
|
||||
16
ecosystem.config.json
Normal file
16
ecosystem.config.json
Normal 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
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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}`);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
export function AsyncHandler() {
|
||||
return function (
|
||||
target: any,
|
||||
target: object,
|
||||
propertyKey: string,
|
||||
descriptor: PropertyDescriptor
|
||||
): void {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user