nodejs-structure-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/bin/index.js +143 -0
- package/lib/generator.js +145 -0
- package/lib/modules/app-setup.js +479 -0
- package/lib/modules/caching-setup.js +76 -0
- package/lib/modules/config-files.js +151 -0
- package/lib/modules/database-setup.js +116 -0
- package/lib/modules/kafka-setup.js +249 -0
- package/lib/modules/project-setup.js +32 -0
- package/lib/prompts.js +128 -0
- package/package.json +66 -0
- package/templates/clean-architecture/js/src/domain/models/User.js.ejs +11 -0
- package/templates/clean-architecture/js/src/errors/ApiError.js +14 -0
- package/templates/clean-architecture/js/src/errors/BadRequestError.js +11 -0
- package/templates/clean-architecture/js/src/errors/BadRequestError.spec.js.ejs +22 -0
- package/templates/clean-architecture/js/src/errors/NotFoundError.js +11 -0
- package/templates/clean-architecture/js/src/errors/NotFoundError.spec.js.ejs +22 -0
- package/templates/clean-architecture/js/src/index.js.ejs +56 -0
- package/templates/clean-architecture/js/src/infrastructure/config/env.js.ejs +47 -0
- package/templates/clean-architecture/js/src/infrastructure/log/logger.js +36 -0
- package/templates/clean-architecture/js/src/infrastructure/log/logger.spec.js.ejs +63 -0
- package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.js.ejs +88 -0
- package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.spec.js.ejs +142 -0
- package/templates/clean-architecture/js/src/infrastructure/webserver/middleware/errorMiddleware.js +30 -0
- package/templates/clean-architecture/js/src/infrastructure/webserver/server.js.ejs +93 -0
- package/templates/clean-architecture/js/src/infrastructure/webserver/swagger.js.ejs +6 -0
- package/templates/clean-architecture/js/src/interfaces/controllers/userController.js.ejs +190 -0
- package/templates/clean-architecture/js/src/interfaces/controllers/userController.spec.js.ejs +234 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/context.js.ejs +13 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/context.spec.js.ejs +31 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/index.js.ejs +5 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/index.js.ejs +6 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.js.ejs +27 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.spec.js.ejs +66 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/index.js.ejs +6 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/user.types.js.ejs +19 -0
- package/templates/clean-architecture/js/src/interfaces/routes/api.js.ejs +17 -0
- package/templates/clean-architecture/js/src/interfaces/routes/api.spec.js.ejs +38 -0
- package/templates/clean-architecture/js/src/usecases/CreateUser.js.ejs +14 -0
- package/templates/clean-architecture/js/src/usecases/CreateUser.spec.js.ejs +51 -0
- package/templates/clean-architecture/js/src/usecases/DeleteUser.js +11 -0
- package/templates/clean-architecture/js/src/usecases/DeleteUser.spec.js.ejs +47 -0
- package/templates/clean-architecture/js/src/usecases/GetAllUsers.js +12 -0
- package/templates/clean-architecture/js/src/usecases/GetAllUsers.spec.js.ejs +61 -0
- package/templates/clean-architecture/js/src/usecases/UpdateUser.js.ejs +11 -0
- package/templates/clean-architecture/js/src/usecases/UpdateUser.spec.js.ejs +48 -0
- package/templates/clean-architecture/js/src/utils/errorMessages.js +14 -0
- package/templates/clean-architecture/js/src/utils/httpCodes.js +9 -0
- package/templates/clean-architecture/ts/src/config/env.ts.ejs +46 -0
- package/templates/clean-architecture/ts/src/config/swagger.ts.ejs +6 -0
- package/templates/clean-architecture/ts/src/domain/user.ts.ejs +9 -0
- package/templates/clean-architecture/ts/src/errors/ApiError.ts +15 -0
- package/templates/clean-architecture/ts/src/errors/BadRequestError.spec.ts.ejs +22 -0
- package/templates/clean-architecture/ts/src/errors/BadRequestError.ts +9 -0
- package/templates/clean-architecture/ts/src/errors/NotFoundError.spec.ts.ejs +22 -0
- package/templates/clean-architecture/ts/src/errors/NotFoundError.ts +9 -0
- package/templates/clean-architecture/ts/src/index.ts.ejs +144 -0
- package/templates/clean-architecture/ts/src/infrastructure/log/logger.spec.ts.ejs +63 -0
- package/templates/clean-architecture/ts/src/infrastructure/log/logger.ts +36 -0
- package/templates/clean-architecture/ts/src/infrastructure/repositories/UserRepository.spec.ts.ejs +175 -0
- package/templates/clean-architecture/ts/src/infrastructure/repositories/userRepository.ts.ejs +125 -0
- package/templates/clean-architecture/ts/src/interfaces/controllers/userController.spec.ts.ejs +331 -0
- package/templates/clean-architecture/ts/src/interfaces/controllers/userController.ts.ejs +208 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/context.spec.ts.ejs +32 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/context.ts.ejs +17 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/index.ts.ejs +3 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/index.ts.ejs +4 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.ts.ejs +29 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/index.ts.ejs +4 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/user.types.ts.ejs +17 -0
- package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.spec.ts.ejs +40 -0
- package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.ts.ejs +18 -0
- package/templates/clean-architecture/ts/src/usecases/createUser.spec.ts.ejs +51 -0
- package/templates/clean-architecture/ts/src/usecases/createUser.ts.ejs +11 -0
- package/templates/clean-architecture/ts/src/usecases/deleteUser.spec.ts.ejs +47 -0
- package/templates/clean-architecture/ts/src/usecases/deleteUser.ts +9 -0
- package/templates/clean-architecture/ts/src/usecases/getAllUsers.spec.ts.ejs +63 -0
- package/templates/clean-architecture/ts/src/usecases/getAllUsers.ts +10 -0
- package/templates/clean-architecture/ts/src/usecases/updateUser.spec.ts.ejs +48 -0
- package/templates/clean-architecture/ts/src/usecases/updateUser.ts.ejs +10 -0
- package/templates/clean-architecture/ts/src/utils/errorMessages.ts +12 -0
- package/templates/clean-architecture/ts/src/utils/errorMiddleware.ts.ejs +27 -0
- package/templates/clean-architecture/ts/src/utils/httpCodes.ts +7 -0
- package/templates/common/.cursorrules.ejs +60 -0
- package/templates/common/.dockerignore +12 -0
- package/templates/common/.env.example.ejs +60 -0
- package/templates/common/.gitattributes +46 -0
- package/templates/common/.gitlab-ci.yml.ejs +86 -0
- package/templates/common/.lintstagedrc +6 -0
- package/templates/common/.prettierrc +7 -0
- package/templates/common/.snyk.ejs +45 -0
- package/templates/common/Dockerfile +73 -0
- package/templates/common/Jenkinsfile.ejs +87 -0
- package/templates/common/README.md.ejs +148 -0
- package/templates/common/_github/workflows/ci.yml.ejs +46 -0
- package/templates/common/_github/workflows/security.yml.ejs +36 -0
- package/templates/common/_gitignore +5 -0
- package/templates/common/_husky/pre-commit +4 -0
- package/templates/common/caching/clean/js/CreateUser.js.ejs +29 -0
- package/templates/common/caching/clean/js/DeleteUser.js.ejs +27 -0
- package/templates/common/caching/clean/js/GetAllUsers.js.ejs +37 -0
- package/templates/common/caching/clean/js/UpdateUser.js.ejs +27 -0
- package/templates/common/caching/clean/ts/createUser.ts.ejs +27 -0
- package/templates/common/caching/clean/ts/deleteUser.ts.ejs +24 -0
- package/templates/common/caching/clean/ts/getAllUsers.ts.ejs +34 -0
- package/templates/common/caching/clean/ts/updateUser.ts.ejs +25 -0
- package/templates/common/caching/js/memoryCache.js.ejs +60 -0
- package/templates/common/caching/js/memoryCache.spec.js.ejs +101 -0
- package/templates/common/caching/js/redisClient.js.ejs +75 -0
- package/templates/common/caching/js/redisClient.spec.js.ejs +147 -0
- package/templates/common/caching/ts/memoryCache.spec.ts.ejs +102 -0
- package/templates/common/caching/ts/memoryCache.ts.ejs +73 -0
- package/templates/common/caching/ts/redisClient.spec.ts.ejs +157 -0
- package/templates/common/caching/ts/redisClient.ts.ejs +89 -0
- package/templates/common/database/js/database.js.ejs +19 -0
- package/templates/common/database/js/database.spec.js.ejs +56 -0
- package/templates/common/database/js/models/User.js.ejs +91 -0
- package/templates/common/database/js/models/User.js.mongoose.ejs +35 -0
- package/templates/common/database/js/models/User.spec.js.ejs +94 -0
- package/templates/common/database/js/mongoose.js.ejs +33 -0
- package/templates/common/database/js/mongoose.spec.js.ejs +43 -0
- package/templates/common/database/ts/database.spec.ts.ejs +56 -0
- package/templates/common/database/ts/database.ts.ejs +21 -0
- package/templates/common/database/ts/models/User.spec.ts.ejs +100 -0
- package/templates/common/database/ts/models/User.ts.ejs +102 -0
- package/templates/common/database/ts/models/User.ts.mongoose.ejs +34 -0
- package/templates/common/database/ts/mongoose.spec.ts.ejs +42 -0
- package/templates/common/database/ts/mongoose.ts.ejs +28 -0
- package/templates/common/docker-compose.yml.ejs +159 -0
- package/templates/common/ecosystem.config.js.ejs +40 -0
- package/templates/common/eslint.config.mjs.ejs +77 -0
- package/templates/common/health/js/healthRoute.js.ejs +50 -0
- package/templates/common/health/js/healthRoute.spec.js.ejs +70 -0
- package/templates/common/health/ts/healthRoute.spec.ts.ejs +76 -0
- package/templates/common/health/ts/healthRoute.ts.ejs +49 -0
- package/templates/common/jest.config.js.ejs +32 -0
- package/templates/common/jest.e2e.config.js.ejs +8 -0
- package/templates/common/kafka/js/config/kafka.js +9 -0
- package/templates/common/kafka/js/config/kafka.spec.js.ejs +27 -0
- package/templates/common/kafka/js/messaging/baseConsumer.js.ejs +30 -0
- package/templates/common/kafka/js/messaging/baseConsumer.spec.js.ejs +58 -0
- package/templates/common/kafka/js/messaging/userEventSchema.js.ejs +12 -0
- package/templates/common/kafka/js/messaging/userEventSchema.spec.js.ejs +27 -0
- package/templates/common/kafka/js/messaging/welcomeEmailConsumer.js.ejs +44 -0
- package/templates/common/kafka/js/messaging/welcomeEmailConsumer.spec.js.ejs +86 -0
- package/templates/common/kafka/js/services/kafkaService.js.ejs +93 -0
- package/templates/common/kafka/js/services/kafkaService.spec.js.ejs +106 -0
- package/templates/common/kafka/js/utils/kafkaEvents.js.ejs +7 -0
- package/templates/common/kafka/ts/config/kafka.spec.ts.ejs +27 -0
- package/templates/common/kafka/ts/config/kafka.ts +7 -0
- package/templates/common/kafka/ts/messaging/baseConsumer.spec.ts.ejs +50 -0
- package/templates/common/kafka/ts/messaging/baseConsumer.ts.ejs +27 -0
- package/templates/common/kafka/ts/messaging/userEventSchema.spec.ts.ejs +51 -0
- package/templates/common/kafka/ts/messaging/userEventSchema.ts.ejs +12 -0
- package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.spec.ts.ejs +86 -0
- package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.ts.ejs +38 -0
- package/templates/common/kafka/ts/services/kafkaService.spec.ts.ejs +81 -0
- package/templates/common/kafka/ts/services/kafkaService.ts.ejs +95 -0
- package/templates/common/kafka/ts/utils/kafkaEvents.ts.ejs +5 -0
- package/templates/common/migrate-mongo-config.js.ejs +31 -0
- package/templates/common/migrations/init.js.ejs +23 -0
- package/templates/common/package.json.ejs +137 -0
- package/templates/common/prompts/add-feature.md.ejs +26 -0
- package/templates/common/prompts/project-context.md.ejs +43 -0
- package/templates/common/prompts/troubleshoot.md.ejs +28 -0
- package/templates/common/public/css/style.css +147 -0
- package/templates/common/scripts/run-e2e.js.ejs +63 -0
- package/templates/common/shutdown/js/gracefulShutdown.js.ejs +65 -0
- package/templates/common/shutdown/js/gracefulShutdown.spec.js.ejs +149 -0
- package/templates/common/shutdown/ts/gracefulShutdown.spec.ts.ejs +179 -0
- package/templates/common/shutdown/ts/gracefulShutdown.ts.ejs +59 -0
- package/templates/common/sonar-project.properties.ejs +27 -0
- package/templates/common/src/config/auth.js.ejs +19 -0
- package/templates/common/src/config/auth.ts.ejs +19 -0
- package/templates/common/src/controllers/authController.js.ejs +101 -0
- package/templates/common/src/controllers/authController.ts.ejs +101 -0
- package/templates/common/src/middleware/auth.js.ejs +20 -0
- package/templates/common/src/middleware/auth.ts.ejs +25 -0
- package/templates/common/src/middleware/upload.js.ejs +31 -0
- package/templates/common/src/middleware/upload.ts.ejs +32 -0
- package/templates/common/src/routes/authRoutes.js.ejs +20 -0
- package/templates/common/src/routes/authRoutes.ts.ejs +20 -0
- package/templates/common/src/tests/e2e/e2e.users.test.js.ejs +120 -0
- package/templates/common/src/tests/e2e/e2e.users.test.ts.ejs +120 -0
- package/templates/common/src/utils/errorMiddleware.spec.js.ejs +79 -0
- package/templates/common/src/utils/errorMiddleware.spec.ts.ejs +94 -0
- package/templates/common/swagger.yml.ejs +118 -0
- package/templates/common/tsconfig.json +23 -0
- package/templates/common/views/ejs/index.ejs +55 -0
- package/templates/common/views/pug/index.pug +40 -0
- package/templates/db/mysql/V1__Initial_Setup.sql.ejs +10 -0
- package/templates/db/postgres/V1__Initial_Setup.sql.ejs +10 -0
- package/templates/mvc/js/src/config/env.js.ejs +46 -0
- package/templates/mvc/js/src/config/swagger.js.ejs +6 -0
- package/templates/mvc/js/src/controllers/userController.js.ejs +288 -0
- package/templates/mvc/js/src/controllers/userController.spec.js.ejs +481 -0
- package/templates/mvc/js/src/errors/ApiError.js +14 -0
- package/templates/mvc/js/src/errors/BadRequestError.js +11 -0
- package/templates/mvc/js/src/errors/BadRequestError.spec.js.ejs +22 -0
- package/templates/mvc/js/src/errors/NotFoundError.js +11 -0
- package/templates/mvc/js/src/errors/NotFoundError.spec.js.ejs +22 -0
- package/templates/mvc/js/src/graphql/context.js.ejs +7 -0
- package/templates/mvc/js/src/graphql/context.spec.js.ejs +29 -0
- package/templates/mvc/js/src/graphql/index.js.ejs +5 -0
- package/templates/mvc/js/src/graphql/resolvers/index.js.ejs +6 -0
- package/templates/mvc/js/src/graphql/resolvers/user.resolvers.js.ejs +25 -0
- package/templates/mvc/js/src/graphql/resolvers/user.resolvers.spec.js.ejs +64 -0
- package/templates/mvc/js/src/graphql/typeDefs/index.js.ejs +6 -0
- package/templates/mvc/js/src/graphql/typeDefs/user.types.js.ejs +19 -0
- package/templates/mvc/js/src/index.js.ejs +141 -0
- package/templates/mvc/js/src/routes/api.js.ejs +15 -0
- package/templates/mvc/js/src/routes/api.spec.js.ejs +41 -0
- package/templates/mvc/js/src/utils/errorMessages.js +14 -0
- package/templates/mvc/js/src/utils/errorMiddleware.js +29 -0
- package/templates/mvc/js/src/utils/httpCodes.js +9 -0
- package/templates/mvc/js/src/utils/logger.js +40 -0
- package/templates/mvc/js/src/utils/logger.spec.js.ejs +63 -0
- package/templates/mvc/ts/src/config/env.ts.ejs +45 -0
- package/templates/mvc/ts/src/config/swagger.ts.ejs +6 -0
- package/templates/mvc/ts/src/controllers/userController.spec.ts.ejs +481 -0
- package/templates/mvc/ts/src/controllers/userController.ts.ejs +292 -0
- package/templates/mvc/ts/src/errors/ApiError.ts +15 -0
- package/templates/mvc/ts/src/errors/BadRequestError.spec.ts.ejs +22 -0
- package/templates/mvc/ts/src/errors/BadRequestError.ts +9 -0
- package/templates/mvc/ts/src/errors/NotFoundError.spec.ts.ejs +27 -0
- package/templates/mvc/ts/src/errors/NotFoundError.ts +9 -0
- package/templates/mvc/ts/src/graphql/context.spec.ts.ejs +30 -0
- package/templates/mvc/ts/src/graphql/context.ts.ejs +12 -0
- package/templates/mvc/ts/src/graphql/index.ts.ejs +3 -0
- package/templates/mvc/ts/src/graphql/resolvers/index.ts.ejs +4 -0
- package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -0
- package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.ts.ejs +29 -0
- package/templates/mvc/ts/src/graphql/typeDefs/index.ts.ejs +4 -0
- package/templates/mvc/ts/src/graphql/typeDefs/user.types.ts.ejs +17 -0
- package/templates/mvc/ts/src/index.ts.ejs +157 -0
- package/templates/mvc/ts/src/routes/api.spec.ts.ejs +59 -0
- package/templates/mvc/ts/src/routes/api.ts.ejs +17 -0
- package/templates/mvc/ts/src/utils/errorMessages.ts +12 -0
- package/templates/mvc/ts/src/utils/errorMiddleware.ts.ejs +27 -0
- package/templates/mvc/ts/src/utils/httpCodes.ts +7 -0
- package/templates/mvc/ts/src/utils/logger.spec.ts.ejs +63 -0
- package/templates/mvc/ts/src/utils/logger.ts +36 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const { errorMiddleware } = require('<% if (architecture === "MVC") { %>@/utils/errorMiddleware<% } else { %>@/infrastructure/webserver/middleware/errorMiddleware<% } %>');
|
|
2
|
+
const { ApiError } = require('@/errors/ApiError');
|
|
3
|
+
const HTTP_STATUS = require('@/utils/httpCodes');
|
|
4
|
+
const logger = require('<% if (architecture === "MVC") { %>@/utils/logger<% } else { %>@/infrastructure/log/logger<% } %>');
|
|
5
|
+
|
|
6
|
+
jest.mock('<% if (architecture === "MVC") { %>@/utils/logger<% } else { %>@/infrastructure/log/logger<% } %>');
|
|
7
|
+
|
|
8
|
+
describe('Error Middleware', () => {
|
|
9
|
+
let mockRequest;
|
|
10
|
+
let mockResponse;
|
|
11
|
+
let nextFunction;
|
|
12
|
+
const originalEnv = process.env.NODE_ENV;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
mockRequest = {
|
|
16
|
+
originalUrl: '/test',
|
|
17
|
+
method: 'GET',
|
|
18
|
+
ip: '127.0.0.1'
|
|
19
|
+
};
|
|
20
|
+
mockResponse = {
|
|
21
|
+
status: jest.fn().mockReturnThis(),
|
|
22
|
+
json: jest.fn()
|
|
23
|
+
};
|
|
24
|
+
nextFunction = jest.fn();
|
|
25
|
+
jest.clearAllMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
process.env.NODE_ENV = originalEnv;
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should handle standard Error by wrapping it in a 500 ApiError', () => {
|
|
33
|
+
const error = new Error('Standard Error');
|
|
34
|
+
errorMiddleware(error, mockRequest, mockResponse, nextFunction);
|
|
35
|
+
|
|
36
|
+
expect(logger.error).toHaveBeenCalled();
|
|
37
|
+
expect(mockResponse.status).toHaveBeenCalledWith(HTTP_STATUS.INTERNAL_SERVER_ERROR);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should handle custom ApiError directly', () => {
|
|
41
|
+
const customError = new ApiError(HTTP_STATUS.BAD_REQUEST, 'Bad Request Data', true);
|
|
42
|
+
errorMiddleware(customError, mockRequest, mockResponse, nextFunction);
|
|
43
|
+
|
|
44
|
+
expect(mockResponse.status).toHaveBeenCalledWith(HTTP_STATUS.BAD_REQUEST);
|
|
45
|
+
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({
|
|
46
|
+
statusCode: HTTP_STATUS.BAD_REQUEST,
|
|
47
|
+
message: 'Bad Request Data'
|
|
48
|
+
}));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should include stack trace in development environment', () => {
|
|
52
|
+
process.env.NODE_ENV = 'development';
|
|
53
|
+
const error = new Error('Test Error');
|
|
54
|
+
errorMiddleware(error, mockRequest, mockResponse, nextFunction);
|
|
55
|
+
|
|
56
|
+
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({
|
|
57
|
+
stack: expect.any(String)
|
|
58
|
+
}));
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should omit stack trace in production environment', () => {
|
|
62
|
+
process.env.NODE_ENV = 'production';
|
|
63
|
+
const error = new Error('Test Error');
|
|
64
|
+
errorMiddleware(error, mockRequest, mockResponse, nextFunction);
|
|
65
|
+
|
|
66
|
+
const jsonArg = mockResponse.json.mock.calls[0][0];
|
|
67
|
+
expect(jsonArg.stack).toBeUndefined();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should handle error without stack trace', () => {
|
|
71
|
+
const { ApiError } = require('@/errors/ApiError');
|
|
72
|
+
const customError = new ApiError(500, 'No Stack', false);
|
|
73
|
+
delete customError.stack;
|
|
74
|
+
errorMiddleware(customError, mockRequest, mockResponse, nextFunction);
|
|
75
|
+
|
|
76
|
+
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('No Stack'));
|
|
77
|
+
expect(logger.error).toHaveBeenCalledWith('No stack trace');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { errorMiddleware } from '<% if (architecture === "MVC" || language === "TypeScript") { %>@/utils/errorMiddleware<% } else { %>@/infrastructure/webserver/middleware/errorMiddleware<% } %>';
|
|
2
|
+
import { Request, Response } from 'express';
|
|
3
|
+
import { ApiError } from '@/errors/ApiError';
|
|
4
|
+
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
5
|
+
import logger from '<% if (architecture === "MVC") { %>@/utils/logger<% } else { %>@/infrastructure/log/logger<% } %>';
|
|
6
|
+
|
|
7
|
+
jest.mock('<% if (architecture === "MVC") { %>@/utils/logger<% } else { %>@/infrastructure/log/logger<% } %>');
|
|
8
|
+
|
|
9
|
+
describe('Error Middleware', () => {
|
|
10
|
+
let mockRequest: Partial<Request>;
|
|
11
|
+
let mockResponse: Partial<Response>;
|
|
12
|
+
let nextFunction: jest.Mock;
|
|
13
|
+
const originalEnv = process.env.NODE_ENV;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
mockRequest = {
|
|
17
|
+
originalUrl: '/test',
|
|
18
|
+
method: 'GET',
|
|
19
|
+
ip: '127.0.0.1'
|
|
20
|
+
};
|
|
21
|
+
mockResponse = {
|
|
22
|
+
status: jest.fn().mockReturnThis(),
|
|
23
|
+
json: jest.fn()
|
|
24
|
+
};
|
|
25
|
+
nextFunction = jest.fn();
|
|
26
|
+
jest.clearAllMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
process.env.NODE_ENV = originalEnv;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should handle standard Error by wrapping it in a 500 ApiError', () => {
|
|
34
|
+
const error = new Error('Standard Error');
|
|
35
|
+
errorMiddleware(error, mockRequest as Request, mockResponse as Response, nextFunction);
|
|
36
|
+
|
|
37
|
+
expect(logger.error).toHaveBeenCalled();
|
|
38
|
+
expect(mockResponse.status).toHaveBeenCalledWith(HTTP_STATUS.INTERNAL_SERVER_ERROR);
|
|
39
|
+
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({
|
|
40
|
+
statusCode: HTTP_STATUS.INTERNAL_SERVER_ERROR,
|
|
41
|
+
message: 'Standard Error'
|
|
42
|
+
}));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should default to Internal Server Error message if none provided on Error', () => {
|
|
46
|
+
const error = new Error();
|
|
47
|
+
errorMiddleware(error, mockRequest as Request, mockResponse as Response, nextFunction);
|
|
48
|
+
|
|
49
|
+
expect(mockResponse.status).toHaveBeenCalledWith(HTTP_STATUS.INTERNAL_SERVER_ERROR);
|
|
50
|
+
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({
|
|
51
|
+
message: 'Internal Server Error'
|
|
52
|
+
}));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should handle custom ApiError directly', () => {
|
|
56
|
+
const customError = new ApiError(HTTP_STATUS.BAD_REQUEST, 'Bad Request Data', true);
|
|
57
|
+
errorMiddleware(customError, mockRequest as Request, mockResponse as Response, nextFunction);
|
|
58
|
+
|
|
59
|
+
expect(logger.error).not.toHaveBeenCalled();
|
|
60
|
+
expect(mockResponse.status).toHaveBeenCalledWith(HTTP_STATUS.BAD_REQUEST);
|
|
61
|
+
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({
|
|
62
|
+
statusCode: HTTP_STATUS.BAD_REQUEST,
|
|
63
|
+
message: 'Bad Request Data'
|
|
64
|
+
}));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should include stack trace in development environment', () => {
|
|
68
|
+
process.env.NODE_ENV = 'development';
|
|
69
|
+
const error = new Error('Test Error');
|
|
70
|
+
errorMiddleware(error, mockRequest as Request, mockResponse as Response, nextFunction);
|
|
71
|
+
|
|
72
|
+
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({
|
|
73
|
+
stack: expect.any(String)
|
|
74
|
+
}));
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should omit stack trace in production environment', () => {
|
|
78
|
+
process.env.NODE_ENV = 'production';
|
|
79
|
+
const error = new Error('Test Error');
|
|
80
|
+
errorMiddleware(error, mockRequest as Request, mockResponse as Response, nextFunction);
|
|
81
|
+
|
|
82
|
+
const jsonArg = (mockResponse.json as jest.Mock).mock.calls[0][0];
|
|
83
|
+
expect(jsonArg.stack).toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should handle error without stack trace', () => {
|
|
87
|
+
const customError = new ApiError(HTTP_STATUS.INTERNAL_SERVER_ERROR, 'No Stack', false);
|
|
88
|
+
delete customError.stack;
|
|
89
|
+
errorMiddleware(customError, mockRequest as Request, mockResponse as Response, nextFunction);
|
|
90
|
+
|
|
91
|
+
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('No Stack'));
|
|
92
|
+
expect(logger.error).toHaveBeenCalledWith('No stack trace');
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
openapi: 3.0.0
|
|
2
|
+
info:
|
|
3
|
+
title: <%= projectName %> API
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
description: API documentation for <%= projectName %>
|
|
6
|
+
servers:
|
|
7
|
+
- url: http://localhost:3000
|
|
8
|
+
description: Local Server
|
|
9
|
+
components:
|
|
10
|
+
schemas:
|
|
11
|
+
User:
|
|
12
|
+
type: object
|
|
13
|
+
required:
|
|
14
|
+
- name
|
|
15
|
+
- email
|
|
16
|
+
properties:
|
|
17
|
+
id:
|
|
18
|
+
type: integer
|
|
19
|
+
description: The auto-generated id of the user
|
|
20
|
+
name:
|
|
21
|
+
type: string
|
|
22
|
+
description: The name of the user
|
|
23
|
+
email:
|
|
24
|
+
type: string
|
|
25
|
+
description: The email of the user
|
|
26
|
+
example:
|
|
27
|
+
id: 1
|
|
28
|
+
name: John Doe
|
|
29
|
+
email: john@example.com
|
|
30
|
+
tags:
|
|
31
|
+
- name: Users
|
|
32
|
+
description: The users managing API
|
|
33
|
+
paths:
|
|
34
|
+
/api/users:
|
|
35
|
+
get:
|
|
36
|
+
summary: Returns the list of all the users
|
|
37
|
+
tags:
|
|
38
|
+
- Users
|
|
39
|
+
responses:
|
|
40
|
+
'200':
|
|
41
|
+
description: The list of the users
|
|
42
|
+
content:
|
|
43
|
+
application/json:
|
|
44
|
+
schema:
|
|
45
|
+
type: array
|
|
46
|
+
items:
|
|
47
|
+
$ref: '#/components/schemas/User'
|
|
48
|
+
post:
|
|
49
|
+
summary: Create a new user
|
|
50
|
+
tags:
|
|
51
|
+
- Users
|
|
52
|
+
requestBody:
|
|
53
|
+
required: true
|
|
54
|
+
content:
|
|
55
|
+
application/json:
|
|
56
|
+
schema:
|
|
57
|
+
$ref: '#/components/schemas/User'
|
|
58
|
+
responses:
|
|
59
|
+
'201':
|
|
60
|
+
description: The created user.
|
|
61
|
+
content:
|
|
62
|
+
application/json:
|
|
63
|
+
schema:
|
|
64
|
+
$ref: '#/components/schemas/User'
|
|
65
|
+
'500':
|
|
66
|
+
description: Some server error
|
|
67
|
+
/api/users/{id}:
|
|
68
|
+
patch:
|
|
69
|
+
summary: Update an existing user
|
|
70
|
+
tags:
|
|
71
|
+
- Users
|
|
72
|
+
parameters:
|
|
73
|
+
- in: path
|
|
74
|
+
name: id
|
|
75
|
+
schema:
|
|
76
|
+
type: string
|
|
77
|
+
required: true
|
|
78
|
+
description: The user id
|
|
79
|
+
requestBody:
|
|
80
|
+
required: true
|
|
81
|
+
content:
|
|
82
|
+
application/json:
|
|
83
|
+
schema:
|
|
84
|
+
type: object
|
|
85
|
+
properties:
|
|
86
|
+
name:
|
|
87
|
+
type: string
|
|
88
|
+
email:
|
|
89
|
+
type: string
|
|
90
|
+
responses:
|
|
91
|
+
'200':
|
|
92
|
+
description: The updated user
|
|
93
|
+
content:
|
|
94
|
+
application/json:
|
|
95
|
+
schema:
|
|
96
|
+
$ref: '#/components/schemas/User'
|
|
97
|
+
'404':
|
|
98
|
+
description: User not found
|
|
99
|
+
'500':
|
|
100
|
+
description: Some server error
|
|
101
|
+
delete:
|
|
102
|
+
summary: Delete an existing user
|
|
103
|
+
tags:
|
|
104
|
+
- Users
|
|
105
|
+
parameters:
|
|
106
|
+
- in: path
|
|
107
|
+
name: id
|
|
108
|
+
schema:
|
|
109
|
+
type: string
|
|
110
|
+
required: true
|
|
111
|
+
description: The user id
|
|
112
|
+
responses:
|
|
113
|
+
'200':
|
|
114
|
+
description: User deleted successfully
|
|
115
|
+
'404':
|
|
116
|
+
description: User not found
|
|
117
|
+
'500':
|
|
118
|
+
description: Some server error
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"baseUrl": ".",
|
|
12
|
+
"paths": {
|
|
13
|
+
"@/*": ["src/*"]
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"include": [
|
|
17
|
+
"src/**/*"
|
|
18
|
+
],
|
|
19
|
+
"exclude": [
|
|
20
|
+
"node_modules",
|
|
21
|
+
"dist"
|
|
22
|
+
]
|
|
23
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title><%= projectName %></title>
|
|
7
|
+
<link rel="stylesheet" href="/css/style.css">
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div class="container">
|
|
11
|
+
<header class="header">
|
|
12
|
+
<div class="logo">🚀</div>
|
|
13
|
+
<h1>Welcome to <%= projectName %></h1>
|
|
14
|
+
<p class="subtitle">A production-ready Node.js microservice starter.</p>
|
|
15
|
+
</header>
|
|
16
|
+
|
|
17
|
+
<div class="card-grid">
|
|
18
|
+
<div class="card">
|
|
19
|
+
<h3>Architecture</h3>
|
|
20
|
+
<p><%= architecture %></p>
|
|
21
|
+
</div>
|
|
22
|
+
<div class="card">
|
|
23
|
+
<h3>Database</h3>
|
|
24
|
+
<p><%= database %></p>
|
|
25
|
+
</div>
|
|
26
|
+
<div class="card">
|
|
27
|
+
<h3>Communication</h3>
|
|
28
|
+
<p><%= communication %></p>
|
|
29
|
+
</div>
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
<% if (communication === 'Kafka') { %>
|
|
33
|
+
<div class="status-card">
|
|
34
|
+
<div class="status-icon">🔄</div>
|
|
35
|
+
<div class="status-content">
|
|
36
|
+
<h3>Kafka Connected</h3>
|
|
37
|
+
<p>Connection to broker established successfully.</p>
|
|
38
|
+
</div>
|
|
39
|
+
</div>
|
|
40
|
+
<% } else { %>
|
|
41
|
+
<div class="status-card">
|
|
42
|
+
<div class="status-icon">✅</div>
|
|
43
|
+
<div class="status-content">
|
|
44
|
+
<h3>API Active</h3>
|
|
45
|
+
<p>REST API is running and ready to accept requests.</p>
|
|
46
|
+
</div>
|
|
47
|
+
</div>
|
|
48
|
+
<% } %>
|
|
49
|
+
|
|
50
|
+
<footer>
|
|
51
|
+
<p>Generated with ❤️ by Node.js Quickstart Generator</p>
|
|
52
|
+
</footer>
|
|
53
|
+
</div>
|
|
54
|
+
</body>
|
|
55
|
+
</html>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
doctype html
|
|
2
|
+
html(lang="en")
|
|
3
|
+
head
|
|
4
|
+
meta(charset="UTF-8")
|
|
5
|
+
meta(name="viewport", content="width=device-width, initial-scale=1.0")
|
|
6
|
+
title= projectName
|
|
7
|
+
link(rel="stylesheet", href="/css/style.css")
|
|
8
|
+
body
|
|
9
|
+
.container
|
|
10
|
+
header.header
|
|
11
|
+
.logo 🚀
|
|
12
|
+
h1 Welcome to #{projectName}
|
|
13
|
+
p.subtitle A production-ready Node.js microservice starter.
|
|
14
|
+
|
|
15
|
+
.card-grid
|
|
16
|
+
.card
|
|
17
|
+
h3 Architecture
|
|
18
|
+
p #{architecture}
|
|
19
|
+
.card
|
|
20
|
+
h3 Database
|
|
21
|
+
p #{database}
|
|
22
|
+
.card
|
|
23
|
+
h3 Communication
|
|
24
|
+
p #{communication}
|
|
25
|
+
|
|
26
|
+
if communication === 'Kafka'
|
|
27
|
+
.status-card
|
|
28
|
+
.status-icon 🔄
|
|
29
|
+
.status-content
|
|
30
|
+
h3 Kafka Connected
|
|
31
|
+
p Connection to broker established successfully.
|
|
32
|
+
else
|
|
33
|
+
.status-card
|
|
34
|
+
.status-icon ✅
|
|
35
|
+
.status-content
|
|
36
|
+
h3 API Active
|
|
37
|
+
p REST API is running and ready to accept requests.
|
|
38
|
+
|
|
39
|
+
footer
|
|
40
|
+
p Generated with ❤️ by Node.js Quickstart Generator
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
CREATE TABLE users (
|
|
2
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
3
|
+
name VARCHAR(255) NOT NULL,
|
|
4
|
+
email VARCHAR(255) NOT NULL UNIQUE,
|
|
5
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
6
|
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
7
|
+
deleted_at TIMESTAMP NULL
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
INSERT INTO users (name, email) VALUES ('Admin User', 'admin@example.com');
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
CREATE TABLE users (
|
|
2
|
+
id SERIAL PRIMARY KEY,
|
|
3
|
+
name VARCHAR(255) NOT NULL,
|
|
4
|
+
email VARCHAR(255) NOT NULL UNIQUE,
|
|
5
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
6
|
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
7
|
+
deleted_at TIMESTAMP NULL
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
INSERT INTO users (name, email) VALUES ('Admin User', 'admin@example.com');
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const { z } = require('zod');
|
|
2
|
+
const logger = require('../utils/logger');
|
|
3
|
+
|
|
4
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
5
|
+
require('dotenv').config();
|
|
6
|
+
}
|
|
7
|
+
const envSchema = z.object({
|
|
8
|
+
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
|
|
9
|
+
PORT: z.string().transform(Number).default('3000'),
|
|
10
|
+
<%_ if (database !== 'None') { -%>
|
|
11
|
+
DB_HOST: z.string(),
|
|
12
|
+
<%_ if (database === 'MySQL') { -%>
|
|
13
|
+
DB_USER: z.string(),
|
|
14
|
+
DB_PASSWORD: z.string(),
|
|
15
|
+
DB_NAME: z.string(),
|
|
16
|
+
DB_PORT: z.string().transform(Number),
|
|
17
|
+
<%_ } else if (database === 'PostgreSQL') { -%>
|
|
18
|
+
DB_USER: z.string(),
|
|
19
|
+
DB_PASSWORD: z.string(),
|
|
20
|
+
DB_NAME: z.string(),
|
|
21
|
+
DB_PORT: z.string().transform(Number),
|
|
22
|
+
<%_ } else if (database === 'MongoDB') { -%>
|
|
23
|
+
DB_NAME: z.string(),
|
|
24
|
+
DB_PORT: z.string().transform(Number),
|
|
25
|
+
<%_ } -%>
|
|
26
|
+
<%_ } -%>
|
|
27
|
+
<%_ if (caching === 'Redis') { -%>
|
|
28
|
+
REDIS_HOST: z.string(),
|
|
29
|
+
REDIS_PORT: z.string().transform(Number),
|
|
30
|
+
REDIS_PASSWORD: z.string().optional(),
|
|
31
|
+
<%_ } -%>
|
|
32
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
33
|
+
KAFKA_BROKER: z.string(),
|
|
34
|
+
<%_ } -%>
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const _env = envSchema.safeParse(process.env);
|
|
38
|
+
|
|
39
|
+
if (!_env.success) {
|
|
40
|
+
logger.error('❌ Invalid environment variables:', _env.error.format());
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const env = _env.data;
|
|
45
|
+
|
|
46
|
+
module.exports = { env };
|