express-project-builder 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/dist/bin/index.d.ts +3 -0
- package/dist/bin/index.d.ts.map +1 -0
- package/dist/bin/index.js +177 -0
- package/dist/bin/index.js.map +1 -0
- package/dist/lib/config/createEnvExample.d.ts +3 -0
- package/dist/lib/config/createEnvExample.d.ts.map +1 -0
- package/dist/lib/config/createEnvExample.js +39 -0
- package/dist/lib/config/createEnvExample.js.map +1 -0
- package/dist/lib/config/createEslintConfig.d.ts +2 -0
- package/dist/lib/config/createEslintConfig.d.ts.map +1 -0
- package/dist/lib/config/createEslintConfig.js +48 -0
- package/dist/lib/config/createEslintConfig.js.map +1 -0
- package/dist/lib/config/createGitignore.d.ts +3 -0
- package/dist/lib/config/createGitignore.d.ts.map +1 -0
- package/dist/lib/config/createGitignore.js +51 -0
- package/dist/lib/config/createGitignore.js.map +1 -0
- package/dist/lib/config/createPackageJson.d.ts +3 -0
- package/dist/lib/config/createPackageJson.d.ts.map +1 -0
- package/dist/lib/config/createPackageJson.js +74 -0
- package/dist/lib/config/createPackageJson.js.map +1 -0
- package/dist/lib/config/createPrettierConfig.d.ts +2 -0
- package/dist/lib/config/createPrettierConfig.d.ts.map +1 -0
- package/dist/lib/config/createPrettierConfig.js +30 -0
- package/dist/lib/config/createPrettierConfig.js.map +1 -0
- package/dist/lib/config/createTsConfig.d.ts +2 -0
- package/dist/lib/config/createTsConfig.d.ts.map +1 -0
- package/dist/lib/config/createTsConfig.js +133 -0
- package/dist/lib/config/createTsConfig.js.map +1 -0
- package/dist/lib/prisma/create_Schema.Prisma.d.ts +3 -0
- package/dist/lib/prisma/create_Schema.Prisma.d.ts.map +1 -0
- package/dist/lib/prisma/create_Schema.Prisma.js +36 -0
- package/dist/lib/prisma/create_Schema.Prisma.js.map +1 -0
- package/dist/lib/src/builders/create_QueryBuilder_Helpers.d.ts +3 -0
- package/dist/lib/src/builders/create_QueryBuilder_Helpers.d.ts.map +1 -0
- package/dist/lib/src/builders/create_QueryBuilder_Helpers.js +292 -0
- package/dist/lib/src/builders/create_QueryBuilder_Helpers.js.map +1 -0
- package/dist/lib/src/config/create_Config_Index.d.ts +9 -0
- package/dist/lib/src/config/create_Config_Index.d.ts.map +1 -0
- package/dist/lib/src/config/create_Config_Index.js +92 -0
- package/dist/lib/src/config/create_Config_Index.js.map +1 -0
- package/dist/lib/src/constants/create_UserRole_Constant.d.ts +7 -0
- package/dist/lib/src/constants/create_UserRole_Constant.d.ts.map +1 -0
- package/dist/lib/src/constants/create_UserRole_Constant.js +39 -0
- package/dist/lib/src/constants/create_UserRole_Constant.js.map +1 -0
- package/dist/lib/src/createAppFile.d.ts +2 -0
- package/dist/lib/src/createAppFile.d.ts.map +1 -0
- package/dist/lib/src/createAppFile.js +122 -0
- package/dist/lib/src/createAppFile.js.map +1 -0
- package/dist/lib/src/createServerFile.d.ts +3 -0
- package/dist/lib/src/createServerFile.d.ts.map +1 -0
- package/dist/lib/src/createServerFile.js +75 -0
- package/dist/lib/src/createServerFile.js.map +1 -0
- package/dist/lib/src/createSrcStructure.d.ts +2 -0
- package/dist/lib/src/createSrcStructure.d.ts.map +1 -0
- package/dist/lib/src/createSrcStructure.js +50 -0
- package/dist/lib/src/createSrcStructure.js.map +1 -0
- package/dist/lib/src/errors/create_AppError_Class.d.ts +7 -0
- package/dist/lib/src/errors/create_AppError_Class.d.ts.map +1 -0
- package/dist/lib/src/errors/create_AppError_Class.js +65 -0
- package/dist/lib/src/errors/create_AppError_Class.js.map +1 -0
- package/dist/lib/src/errors/create_HandleCastError.d.ts +9 -0
- package/dist/lib/src/errors/create_HandleCastError.d.ts.map +1 -0
- package/dist/lib/src/errors/create_HandleCastError.js +101 -0
- package/dist/lib/src/errors/create_HandleCastError.js.map +1 -0
- package/dist/lib/src/errors/create_HandleDB_ValidationError.d.ts +9 -0
- package/dist/lib/src/errors/create_HandleDB_ValidationError.d.ts.map +1 -0
- package/dist/lib/src/errors/create_HandleDB_ValidationError.js +119 -0
- package/dist/lib/src/errors/create_HandleDB_ValidationError.js.map +1 -0
- package/dist/lib/src/errors/create_HandleDuplicateError.d.ts +9 -0
- package/dist/lib/src/errors/create_HandleDuplicateError.d.ts.map +1 -0
- package/dist/lib/src/errors/create_HandleDuplicateError.js +99 -0
- package/dist/lib/src/errors/create_HandleDuplicateError.js.map +1 -0
- package/dist/lib/src/errors/create_HandleZodValidationError.d.ts +7 -0
- package/dist/lib/src/errors/create_HandleZodValidationError.d.ts.map +1 -0
- package/dist/lib/src/errors/create_HandleZodValidationError.js +50 -0
- package/dist/lib/src/errors/create_HandleZodValidationError.js.map +1 -0
- package/dist/lib/src/interfaces/create_EmailFomat_type.d.ts +7 -0
- package/dist/lib/src/interfaces/create_EmailFomat_type.d.ts.map +1 -0
- package/dist/lib/src/interfaces/create_EmailFomat_type.js +34 -0
- package/dist/lib/src/interfaces/create_EmailFomat_type.js.map +1 -0
- package/dist/lib/src/interfaces/create_Errors_type.d.ts +7 -0
- package/dist/lib/src/interfaces/create_Errors_type.d.ts.map +1 -0
- package/dist/lib/src/interfaces/create_Errors_type.js +43 -0
- package/dist/lib/src/interfaces/create_Errors_type.js.map +1 -0
- package/dist/lib/src/interfaces/create_Index_d_type.d.ts +9 -0
- package/dist/lib/src/interfaces/create_Index_d_type.d.ts.map +1 -0
- package/dist/lib/src/interfaces/create_Index_d_type.js +45 -0
- package/dist/lib/src/interfaces/create_Index_d_type.js.map +1 -0
- package/dist/lib/src/interfaces/create_JWT_Token_type.d.ts +9 -0
- package/dist/lib/src/interfaces/create_JWT_Token_type.d.ts.map +1 -0
- package/dist/lib/src/interfaces/create_JWT_Token_type.js +33 -0
- package/dist/lib/src/interfaces/create_JWT_Token_type.js.map +1 -0
- package/dist/lib/src/interfaces/create_UserRole_type.d.ts +7 -0
- package/dist/lib/src/interfaces/create_UserRole_type.d.ts.map +1 -0
- package/dist/lib/src/interfaces/create_UserRole_type.js +33 -0
- package/dist/lib/src/interfaces/create_UserRole_type.js.map +1 -0
- package/dist/lib/src/middlewares/create_Auth_Gurad.d.ts +9 -0
- package/dist/lib/src/middlewares/create_Auth_Gurad.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_Auth_Gurad.js +99 -0
- package/dist/lib/src/middlewares/create_Auth_Gurad.js.map +1 -0
- package/dist/lib/src/middlewares/create_BigIntSerializer_Guard.d.ts +7 -0
- package/dist/lib/src/middlewares/create_BigIntSerializer_Guard.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_BigIntSerializer_Guard.js +64 -0
- package/dist/lib/src/middlewares/create_BigIntSerializer_Guard.js.map +1 -0
- package/dist/lib/src/middlewares/create_FormDataToSetJSONformatData_Guard.d.ts +7 -0
- package/dist/lib/src/middlewares/create_FormDataToSetJSONformatData_Guard.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_FormDataToSetJSONformatData_Guard.js +63 -0
- package/dist/lib/src/middlewares/create_FormDataToSetJSONformatData_Guard.js.map +1 -0
- package/dist/lib/src/middlewares/create_Global_ErrorHandler_Guard.d.ts +12 -0
- package/dist/lib/src/middlewares/create_Global_ErrorHandler_Guard.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_Global_ErrorHandler_Guard.js +375 -0
- package/dist/lib/src/middlewares/create_Global_ErrorHandler_Guard.js.map +1 -0
- package/dist/lib/src/middlewares/create_Handle_File_Upload_Middleware.d.ts +7 -0
- package/dist/lib/src/middlewares/create_Handle_File_Upload_Middleware.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_Handle_File_Upload_Middleware.js +260 -0
- package/dist/lib/src/middlewares/create_Handle_File_Upload_Middleware.js.map +1 -0
- package/dist/lib/src/middlewares/create_NotFound_Guard.d.ts +7 -0
- package/dist/lib/src/middlewares/create_NotFound_Guard.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_NotFound_Guard.js +47 -0
- package/dist/lib/src/middlewares/create_NotFound_Guard.js.map +1 -0
- package/dist/lib/src/middlewares/create_RateLimiting_Handler_Guard.d.ts +7 -0
- package/dist/lib/src/middlewares/create_RateLimiting_Handler_Guard.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_RateLimiting_Handler_Guard.js +270 -0
- package/dist/lib/src/middlewares/create_RateLimiting_Handler_Guard.js.map +1 -0
- package/dist/lib/src/middlewares/create_ValidateRequest_Guard.d.ts +7 -0
- package/dist/lib/src/middlewares/create_ValidateRequest_Guard.d.ts.map +1 -0
- package/dist/lib/src/middlewares/create_ValidateRequest_Guard.js +104 -0
- package/dist/lib/src/middlewares/create_ValidateRequest_Guard.js.map +1 -0
- package/dist/lib/src/modules/create_DemoModules_SrcStructure.d.ts +7 -0
- package/dist/lib/src/modules/create_DemoModules_SrcStructure.d.ts.map +1 -0
- package/dist/lib/src/modules/create_DemoModules_SrcStructure.js +367 -0
- package/dist/lib/src/modules/create_DemoModules_SrcStructure.js.map +1 -0
- package/dist/lib/src/routers/create_Router_Index_Path.d.ts +7 -0
- package/dist/lib/src/routers/create_Router_Index_Path.d.ts.map +1 -0
- package/dist/lib/src/routers/create_Router_Index_Path.js +54 -0
- package/dist/lib/src/routers/create_Router_Index_Path.js.map +1 -0
- package/dist/lib/src/shared/create_Prisma_Shated.d.ts +9 -0
- package/dist/lib/src/shared/create_Prisma_Shated.d.ts.map +1 -0
- package/dist/lib/src/shared/create_Prisma_Shated.js +34 -0
- package/dist/lib/src/shared/create_Prisma_Shated.js.map +1 -0
- package/dist/lib/src/utils/createCatchAsync_Utils.d.ts +7 -0
- package/dist/lib/src/utils/createCatchAsync_Utils.d.ts.map +1 -0
- package/dist/lib/src/utils/createCatchAsync_Utils.js +59 -0
- package/dist/lib/src/utils/createCatchAsync_Utils.js.map +1 -0
- package/dist/lib/src/utils/createLoggerFile.d.ts +7 -0
- package/dist/lib/src/utils/createLoggerFile.d.ts.map +1 -0
- package/dist/lib/src/utils/createLoggerFile.js +35 -0
- package/dist/lib/src/utils/createLoggerFile.js.map +1 -0
- package/dist/lib/src/utils/create_Common_Utils.d.ts +9 -0
- package/dist/lib/src/utils/create_Common_Utils.d.ts.map +1 -0
- package/dist/lib/src/utils/create_Common_Utils.js +262 -0
- package/dist/lib/src/utils/create_Common_Utils.js.map +1 -0
- package/dist/lib/src/utils/create_Date_Time_Validation_Utils.d.ts +7 -0
- package/dist/lib/src/utils/create_Date_Time_Validation_Utils.d.ts.map +1 -0
- package/dist/lib/src/utils/create_Date_Time_Validation_Utils.js +181 -0
- package/dist/lib/src/utils/create_Date_Time_Validation_Utils.js.map +1 -0
- package/dist/lib/src/utils/create_Node_Cache_Utils.d.ts +7 -0
- package/dist/lib/src/utils/create_Node_Cache_Utils.d.ts.map +1 -0
- package/dist/lib/src/utils/create_Node_Cache_Utils.js +149 -0
- package/dist/lib/src/utils/create_Node_Cache_Utils.js.map +1 -0
- package/dist/lib/src/utils/create_Remove_Uploaded_Files_Utils.d.ts +7 -0
- package/dist/lib/src/utils/create_Remove_Uploaded_Files_Utils.d.ts.map +1 -0
- package/dist/lib/src/utils/create_Remove_Uploaded_Files_Utils.js +162 -0
- package/dist/lib/src/utils/create_Remove_Uploaded_Files_Utils.js.map +1 -0
- package/dist/lib/src/utils/create_SendEmail_Utils.d.ts +7 -0
- package/dist/lib/src/utils/create_SendEmail_Utils.d.ts.map +1 -0
- package/dist/lib/src/utils/create_SendEmail_Utils.js +140 -0
- package/dist/lib/src/utils/create_SendEmail_Utils.js.map +1 -0
- package/dist/lib/src/utils/create_sendResponse_Utils.d.ts +7 -0
- package/dist/lib/src/utils/create_sendResponse_Utils.d.ts.map +1 -0
- package/dist/lib/src/utils/create_sendResponse_Utils.js +95 -0
- package/dist/lib/src/utils/create_sendResponse_Utils.js.map +1 -0
- package/dist/lib/utils/helpers.d.ts +5 -0
- package/dist/lib/utils/helpers.d.ts.map +1 -0
- package/dist/lib/utils/helpers.js +31 -0
- package/dist/lib/utils/helpers.js.map +1 -0
- package/dist/lib/utils/types.d.ts +5 -0
- package/dist/lib/utils/types.d.ts.map +1 -0
- package/dist/lib/utils/types.js +2 -0
- package/dist/lib/utils/types.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { createFile } from "../../utils/helpers.js";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
/**
|
|
5
|
+
* Creates an authentication middleware file when authentication is enabled
|
|
6
|
+
* @param projectPath - Absolute path to the project root
|
|
7
|
+
* @param answers - User answers from CLI prompts
|
|
8
|
+
* @returns Promise that resolves when the auth guard file is created (if auth is enabled)
|
|
9
|
+
*/
|
|
10
|
+
export const create_Auth_Guard = async (projectPath, answers) => {
|
|
11
|
+
try {
|
|
12
|
+
if (answers.includeAuth) {
|
|
13
|
+
const authMiddlewareTemplate = `import { JwtPayload } from 'jsonwebtoken'
|
|
14
|
+
import config from '../config'
|
|
15
|
+
import AppError from '../errors/AppError'
|
|
16
|
+
import { TUserRole } from '../interfaces/userRole_type'
|
|
17
|
+
import catchAsync from '../utils/catchAsync'
|
|
18
|
+
import { verifyToken } from '../utils/commonUtils'
|
|
19
|
+
|
|
20
|
+
// initiate authentication route auth function
|
|
21
|
+
const auth = (...rolesAndFlags: Array<TUserRole | boolean>) => {
|
|
22
|
+
// Check if the last argument is a boolean flag
|
|
23
|
+
let isIgnoreAuthentication = false;
|
|
24
|
+
if (typeof rolesAndFlags[rolesAndFlags.length - 1] === 'boolean') {
|
|
25
|
+
isIgnoreAuthentication = rolesAndFlags.pop() as boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The remaining arguments are the required roles
|
|
29
|
+
const requiredRoles = rolesAndFlags as TUserRole[];
|
|
30
|
+
return catchAsync(async (req, res, next) => {
|
|
31
|
+
// Skip authentication if flag is set
|
|
32
|
+
if (isIgnoreAuthentication) {
|
|
33
|
+
return next();
|
|
34
|
+
}
|
|
35
|
+
// Extract token from Authorization header
|
|
36
|
+
const authHeader = req.headers.authorization;
|
|
37
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
38
|
+
throw new AppError(401, 'UNAUTHORIZED', 'You are not authorized. No token provided.');
|
|
39
|
+
}
|
|
40
|
+
const token = authHeader.split(' ')[1];
|
|
41
|
+
|
|
42
|
+
// Verify token
|
|
43
|
+
const decoded = verifyToken(token, config.jwt_access_token_secret as string);
|
|
44
|
+
const { user_id, role, iat } = decoded;
|
|
45
|
+
|
|
46
|
+
// Check if user exists (implementation depends on your user model)
|
|
47
|
+
// const user = await User.isUserStatusCheckFindBy_id(user_id);
|
|
48
|
+
// if (!user) {
|
|
49
|
+
// throw new AppError(404, 'NOT_FOUND', 'User not found!');
|
|
50
|
+
// }
|
|
51
|
+
// Check if user is deleted
|
|
52
|
+
// if (user.isDeleted) {
|
|
53
|
+
// throw new AppError(401, 'UNAUTHORIZED', 'This user has been deleted!');
|
|
54
|
+
// }
|
|
55
|
+
// Check if user is blocked
|
|
56
|
+
// if (user.status === 'blocked') {
|
|
57
|
+
// throw new AppError(403, 'FORBIDDEN', 'This user is blocked!');
|
|
58
|
+
// }
|
|
59
|
+
// Check if user role matches
|
|
60
|
+
// if (user.role !== role) {
|
|
61
|
+
// throw new AppError(403, 'FORBIDDEN', 'Invalid user role!');
|
|
62
|
+
// }
|
|
63
|
+
// Check if password was changed after token was issued
|
|
64
|
+
// if (user.passwordChangedAt) {
|
|
65
|
+
// const isPasswordChanged = User.isJWTIssuedAtBeforePasswordChanged(
|
|
66
|
+
// user.passwordChangedAt,
|
|
67
|
+
// iat as number
|
|
68
|
+
// );
|
|
69
|
+
// if (isPasswordChanged) {
|
|
70
|
+
// // Clear cache if needed
|
|
71
|
+
// // delete_cache_from_RAM(user._id?.toString() as string);
|
|
72
|
+
// throw new AppError(401, 'UNAUTHORIZED', 'Password has been changed. Please login again.');
|
|
73
|
+
// }
|
|
74
|
+
// }
|
|
75
|
+
|
|
76
|
+
// Check if user has required role
|
|
77
|
+
if (requiredRoles.length > 0 && !requiredRoles.includes(role)) {
|
|
78
|
+
throw new AppError(403, 'FORBIDDEN', 'You do not have permission to perform this action.');
|
|
79
|
+
}
|
|
80
|
+
// Attach user to request object
|
|
81
|
+
req.user = decoded as JwtPayload;
|
|
82
|
+
next();
|
|
83
|
+
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
export default auth
|
|
87
|
+
`;
|
|
88
|
+
await createFile(path.join(projectPath, "src/app/middlewares", "auth.ts"), authMiddlewareTemplate);
|
|
89
|
+
// Success message with green checkmark and text
|
|
90
|
+
console.log(chalk.green("✅ Successfully created Auth_Guard file"));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
// Error message with red cross and text
|
|
95
|
+
console.error(chalk.red("❌ Error creating Auth_Guard file:"), err);
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=create_Auth_Gurad.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_Auth_Gurad.js","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_Auth_Gurad.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EACpC,WAAmB,EACnB,OAAiB,EACF,EAAE;IACjB,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;YACxB,MAAM,sBAAsB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0EpC,CAAC;YAEI,MAAM,UAAU,CACd,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,qBAAqB,EAAE,SAAS,CAAC,EACxD,sBAAsB,CACvB,CAAC;YAEF,gDAAgD;YAChD,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,wCAAwC,CAAC,CAAC,CAAC;QACrE,CAAC;IACH,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,wCAAwC;QACxC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,mCAAmC,CAAC,EAAE,GAAG,CAAC,CAAC;QACnE,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a BigInt serializer middleware file for handling BigInt values in JSON responses
|
|
3
|
+
* @param projectPath - Absolute path to the project root
|
|
4
|
+
* @returns Promise that resolves when the BigInt serializer guard file is created
|
|
5
|
+
*/
|
|
6
|
+
export declare const create_BigIntSerializer_Guard: (projectPath: string) => Promise<void>;
|
|
7
|
+
//# sourceMappingURL=create_BigIntSerializer_Guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_BigIntSerializer_Guard.d.ts","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_BigIntSerializer_Guard.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,GACxC,aAAa,MAAM,KAClB,OAAO,CAAC,IAAI,CA+Dd,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { createFile } from "../../utils/helpers.js";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a BigInt serializer middleware file for handling BigInt values in JSON responses
|
|
6
|
+
* @param projectPath - Absolute path to the project root
|
|
7
|
+
* @returns Promise that resolves when the BigInt serializer guard file is created
|
|
8
|
+
*/
|
|
9
|
+
export const create_BigIntSerializer_Guard = async (projectPath) => {
|
|
10
|
+
try {
|
|
11
|
+
const bigIntSerializerMiddlewareTemplate = `/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
12
|
+
import { Request, Response, NextFunction } from 'express';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* BigInt serializer middleware for JSON responses
|
|
16
|
+
* Handles BigInt values by converting them to strings during JSON serialization
|
|
17
|
+
* This prevents errors when BigInt values are included in response bodies
|
|
18
|
+
*
|
|
19
|
+
* @param req - Express Request object
|
|
20
|
+
* @param res - Express Response object
|
|
21
|
+
* @param next - Express NextFunction
|
|
22
|
+
*/
|
|
23
|
+
export function bigIntSerializer(
|
|
24
|
+
req: Request,
|
|
25
|
+
res: Response,
|
|
26
|
+
next: NextFunction
|
|
27
|
+
): void {
|
|
28
|
+
// Store the original json method
|
|
29
|
+
const originalJson = res.json;
|
|
30
|
+
|
|
31
|
+
// Override the json method
|
|
32
|
+
res.json = function (body: any): Response {
|
|
33
|
+
// Define a replacer function to handle BigInt values
|
|
34
|
+
const replacer = (key: string, value: any): any => {
|
|
35
|
+
// Convert BigInt to string
|
|
36
|
+
if (typeof value === 'bigint') {
|
|
37
|
+
return value.toString();
|
|
38
|
+
}
|
|
39
|
+
// Return other values unchanged
|
|
40
|
+
return value;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Serialize the body with the replacer
|
|
44
|
+
const serializedBody = JSON.stringify(body, replacer);
|
|
45
|
+
|
|
46
|
+
// Parse it back to an object and send it using the original method
|
|
47
|
+
return originalJson.call(this, JSON.parse(serializedBody));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Continue to the next middleware
|
|
51
|
+
next();
|
|
52
|
+
}
|
|
53
|
+
`;
|
|
54
|
+
await createFile(path.join(projectPath, "src/app/middlewares", "bigIntSerializer.ts"), bigIntSerializerMiddlewareTemplate);
|
|
55
|
+
// Success message with green checkmark and text
|
|
56
|
+
console.log(chalk.green("✅ BigIntSerializer Guard file created successfully"));
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
// Error message with red cross and text
|
|
60
|
+
console.error(chalk.red("❌ Error creating BigIntSerializer Guard file:"), err);
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=create_BigIntSerializer_Guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_BigIntSerializer_Guard.js","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_BigIntSerializer_Guard.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;GAIG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,KAAK,EAChD,WAAmB,EACJ,EAAE;IACjB,IAAI,CAAC;QACH,MAAM,kCAAkC,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0C9C,CAAC;QAEE,MAAM,UAAU,CACd,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,qBAAqB,EAAE,qBAAqB,CAAC,EACpE,kCAAkC,CACnC,CAAC;QAEF,gDAAgD;QAChD,OAAO,CAAC,GAAG,CACT,KAAK,CAAC,KAAK,CAAC,oDAAoD,CAAC,CAClE,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,wCAAwC;QACxC,OAAO,CAAC,KAAK,CACX,KAAK,CAAC,GAAG,CAAC,+CAA+C,CAAC,EAC1D,GAAG,CACJ,CAAC;QACF,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a middleware to parse JSON data from form data
|
|
3
|
+
* @param projectPath - Absolute path to the project root
|
|
4
|
+
* @returns Promise that resolves when the form data to JSON format guard file is created
|
|
5
|
+
*/
|
|
6
|
+
export declare const create_formDataToSetJSONformatData_Guard: (projectPath: string) => Promise<void>;
|
|
7
|
+
//# sourceMappingURL=create_FormDataToSetJSONformatData_Guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_FormDataToSetJSONformatData_Guard.d.ts","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_FormDataToSetJSONformatData_Guard.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,eAAO,MAAM,wCAAwC,GACnD,aAAa,MAAM,KAClB,OAAO,CAAC,IAAI,CAoEd,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { createFile } from "../../utils/helpers.js";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a middleware to parse JSON data from form data
|
|
6
|
+
* @param projectPath - Absolute path to the project root
|
|
7
|
+
* @returns Promise that resolves when the form data to JSON format guard file is created
|
|
8
|
+
*/
|
|
9
|
+
export const create_formDataToSetJSONformatData_Guard = async (projectPath) => {
|
|
10
|
+
try {
|
|
11
|
+
const formDataToSetJSONformatDataMiddlewareTemplate = `import { NextFunction, Request, Response } from 'express';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Middleware to parse JSON data from form data
|
|
15
|
+
* This middleware expects the request body to contain a 'data' field with a JSON string.
|
|
16
|
+
* It parses the JSON string and replaces req.body with the parsed object.
|
|
17
|
+
*
|
|
18
|
+
* @param req - Express Request object
|
|
19
|
+
* @param res - Express Response object
|
|
20
|
+
* @param next - Express NextFunction
|
|
21
|
+
*/
|
|
22
|
+
const formData_ToSet_JSONformat_Data = (
|
|
23
|
+
req: Request,
|
|
24
|
+
res: Response,
|
|
25
|
+
next: NextFunction
|
|
26
|
+
): void => {
|
|
27
|
+
try {
|
|
28
|
+
// Check if the data field exists in the request body
|
|
29
|
+
if (!req.body || !req.body.data) {
|
|
30
|
+
throw new Error('Missing required field: data');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Parse the JSON string from the data field
|
|
34
|
+
const parsedData = JSON.parse(req.body.data);
|
|
35
|
+
|
|
36
|
+
// Replace the entire request body with the parsed data
|
|
37
|
+
req.body = parsedData;
|
|
38
|
+
|
|
39
|
+
// Continue to the next middleware
|
|
40
|
+
next();
|
|
41
|
+
} catch (error) {
|
|
42
|
+
// Handle JSON parsing errors or missing data field
|
|
43
|
+
res.status(400).json({
|
|
44
|
+
success: false,
|
|
45
|
+
message: 'Invalid JSON data provided',
|
|
46
|
+
error: error instanceof Error ? error.message : 'Unknown error'
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export default formData_ToSet_JSONformat_Data;
|
|
52
|
+
`;
|
|
53
|
+
await createFile(path.join(projectPath, "src/app/middlewares", "formDataToSetJSONformatData.ts"), formDataToSetJSONformatDataMiddlewareTemplate);
|
|
54
|
+
// Success message with green checkmark and text
|
|
55
|
+
console.log(chalk.green("✅ Successfully created formDataToSetJSONformatData_Guard file"));
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
// Error message with red cross and text
|
|
59
|
+
console.error(chalk.red("❌ Error creating formDataToSetJSONformatData_Guard file:"), err);
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=create_FormDataToSetJSONformatData_Guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_FormDataToSetJSONformatData_Guard.js","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_FormDataToSetJSONformatData_Guard.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;GAIG;AACH,MAAM,CAAC,MAAM,wCAAwC,GAAG,KAAK,EAC3D,WAAmB,EACJ,EAAE;IACjB,IAAI,CAAC;QACH,MAAM,6CAA6C,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyCzD,CAAC;QAEE,MAAM,UAAU,CACd,IAAI,CAAC,IAAI,CACP,WAAW,EACX,qBAAqB,EACrB,gCAAgC,CACjC,EACD,6CAA6C,CAC9C,CAAC;QAEF,gDAAgD;QAChD,OAAO,CAAC,GAAG,CACT,KAAK,CAAC,KAAK,CACT,+DAA+D,CAChE,CACF,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,wCAAwC;QACxC,OAAO,CAAC,KAAK,CACX,KAAK,CAAC,GAAG,CAAC,0DAA0D,CAAC,EACrE,GAAG,CACJ,CAAC;QACF,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface Answers {
|
|
2
|
+
database: "MongoDB with Mongoose" | "PostgreSQL with Prisma" | "None" | string;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Creates a global error handler middleware file based on the selected database
|
|
6
|
+
* @param projectPath - Absolute path to the project root
|
|
7
|
+
* @param answers - User answers from CLI prompts
|
|
8
|
+
* @returns Promise that resolves when the global error handler file is created
|
|
9
|
+
*/
|
|
10
|
+
export declare const create_Global_Error_Handler_Guard: (projectPath: string, answers: Answers) => Promise<void>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=create_Global_ErrorHandler_Guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_Global_ErrorHandler_Guard.d.ts","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_Global_ErrorHandler_Guard.ts"],"names":[],"mappings":"AAKA,UAAU,OAAO;IACf,QAAQ,EACJ,uBAAuB,GACvB,wBAAwB,GACxB,MAAM,GACN,MAAM,CAAC;CAEZ;AAED;;;;;GAKG;AACH,eAAO,MAAM,iCAAiC,GAC5C,aAAa,MAAM,EACnB,SAAS,OAAO,KACf,OAAO,CAAC,IAAI,CAiYd,CAAC"}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { createFile } from "../../utils/helpers.js";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a global error handler middleware file based on the selected database
|
|
6
|
+
* @param projectPath - Absolute path to the project root
|
|
7
|
+
* @param answers - User answers from CLI prompts
|
|
8
|
+
* @returns Promise that resolves when the global error handler file is created
|
|
9
|
+
*/
|
|
10
|
+
export const create_Global_Error_Handler_Guard = async (projectPath, answers) => {
|
|
11
|
+
try {
|
|
12
|
+
if (answers.database === "MongoDB with Mongoose") {
|
|
13
|
+
const globalErrorHandlerMiddlewareTemplate = `/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
14
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
15
|
+
import { NextFunction, Request, Response } from 'express'
|
|
16
|
+
import { TErrorSource, TGenericErrorResponse } from '../interfaces/errors'
|
|
17
|
+
import { ZodError } from 'zod'
|
|
18
|
+
import handleZodValidationError from '../errors/handleZodValidationError'
|
|
19
|
+
import handleMongooseValidationError from '../errors/handleMongooseValidationError'
|
|
20
|
+
import handleMongooseCastError from '../errors/handleMongooseCastError'
|
|
21
|
+
import handleMongooseDuplicateError from '../errors/handleMongooseDuplicateError'
|
|
22
|
+
import AppError from '../errors/AppError'
|
|
23
|
+
import {
|
|
24
|
+
removeSingleUploadedFile,
|
|
25
|
+
removeUploadedFiles
|
|
26
|
+
} from '../utils/removeUploadedFiles'
|
|
27
|
+
import config from '../config'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Global error handler middleware for MongoDB with Mongoose
|
|
31
|
+
* Handles all types of errors and returns consistent error responses
|
|
32
|
+
*/
|
|
33
|
+
const globalErrorHandler = async (
|
|
34
|
+
err: any,
|
|
35
|
+
req: Request,
|
|
36
|
+
res: Response,
|
|
37
|
+
next: NextFunction
|
|
38
|
+
): Promise<void> => {
|
|
39
|
+
// Initialize default error details
|
|
40
|
+
let statusCode: number = 500;
|
|
41
|
+
let message: string = 'Something went wrong';
|
|
42
|
+
let errorSources: TErrorSource[] = [
|
|
43
|
+
{
|
|
44
|
+
path: '',
|
|
45
|
+
message: 'Something went wrong'
|
|
46
|
+
}
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
// Zod validation error handling
|
|
50
|
+
if (err instanceof ZodError) {
|
|
51
|
+
const simplifiedError: TGenericErrorResponse = handleZodValidationError(err);
|
|
52
|
+
statusCode = simplifiedError.statusCode;
|
|
53
|
+
message = simplifiedError.message;
|
|
54
|
+
errorSources = simplifiedError.errorSources;
|
|
55
|
+
}
|
|
56
|
+
// Mongoose validation error handling
|
|
57
|
+
else if (err.name === 'ValidationError') {
|
|
58
|
+
const simplifiedError: TGenericErrorResponse = handleMongooseValidationError(err);
|
|
59
|
+
statusCode = simplifiedError.statusCode;
|
|
60
|
+
message = simplifiedError.message;
|
|
61
|
+
errorSources = simplifiedError.errorSources;
|
|
62
|
+
}
|
|
63
|
+
// Mongoose cast error handling
|
|
64
|
+
else if (err.name === 'CastError') {
|
|
65
|
+
const simplifiedError: TGenericErrorResponse = handleMongooseCastError(err);
|
|
66
|
+
statusCode = simplifiedError.statusCode;
|
|
67
|
+
message = simplifiedError.message;
|
|
68
|
+
errorSources = simplifiedError.errorSources;
|
|
69
|
+
}
|
|
70
|
+
// Mongoose Duplicate error handling
|
|
71
|
+
else if (err.code === 11000) {
|
|
72
|
+
const simplifiedError: TGenericErrorResponse = handleMongooseDuplicateError(err);
|
|
73
|
+
statusCode = simplifiedError.statusCode;
|
|
74
|
+
message = simplifiedError.message;
|
|
75
|
+
errorSources = simplifiedError.errorSources;
|
|
76
|
+
}
|
|
77
|
+
// Custom AppError handling
|
|
78
|
+
else if (err instanceof AppError) {
|
|
79
|
+
statusCode = err.statusCode;
|
|
80
|
+
message = err.message;
|
|
81
|
+
errorSources = [
|
|
82
|
+
{
|
|
83
|
+
path: err.path || '',
|
|
84
|
+
message: err.message
|
|
85
|
+
}
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
// Built-in error handling
|
|
89
|
+
else if (err instanceof Error) {
|
|
90
|
+
message = err.message;
|
|
91
|
+
errorSources = [
|
|
92
|
+
{
|
|
93
|
+
path: '',
|
|
94
|
+
message: err.message
|
|
95
|
+
}
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Clean up uploaded files if they exist
|
|
100
|
+
try {
|
|
101
|
+
// Handle multiple files (req.files)
|
|
102
|
+
if (req.files) {
|
|
103
|
+
// Type assertion for files object with field names as keys
|
|
104
|
+
const files = req.files as { [fieldname: string]: Express.Multer.File[] };
|
|
105
|
+
await removeUploadedFiles(files);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Handle single file (req.file)
|
|
109
|
+
if (req.file) {
|
|
110
|
+
await removeSingleUploadedFile(req.file.path);
|
|
111
|
+
}
|
|
112
|
+
} catch (cleanupError) {
|
|
113
|
+
console.error('Error during file cleanup:', cleanupError);
|
|
114
|
+
// Don't throw the cleanup error, just log it
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Send error response
|
|
118
|
+
res.status(statusCode).json({
|
|
119
|
+
status: statusCode,
|
|
120
|
+
success: false,
|
|
121
|
+
message,
|
|
122
|
+
error: errorSources,
|
|
123
|
+
stack: config.NODE_ENV === 'development' ? err.stack : null
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export default globalErrorHandler;
|
|
128
|
+
`;
|
|
129
|
+
await createFile(path.join(projectPath, "src/app/middlewares", "globalErrorHandler.ts"), globalErrorHandlerMiddlewareTemplate);
|
|
130
|
+
}
|
|
131
|
+
if (answers.database === "PostgreSQL with Prisma") {
|
|
132
|
+
const globalErrorHandlerMiddlewareTemplate = `/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
133
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
134
|
+
import { NextFunction, Request, Response } from 'express'
|
|
135
|
+
import {
|
|
136
|
+
PrismaClientKnownRequestError,
|
|
137
|
+
PrismaClientValidationError
|
|
138
|
+
} from '@prisma/client/runtime/library'
|
|
139
|
+
import { TErrorSource, TGenericErrorResponse } from '../interfaces/errors'
|
|
140
|
+
import { ZodError } from 'zod'
|
|
141
|
+
import handleZodValidationError from '../errors/handleZodValidationError'
|
|
142
|
+
import handlePrismaValidationError from '../errors/handlePrismaValidationError'
|
|
143
|
+
import handlePrismaCastError from '../errors/handlePrismaCastError'
|
|
144
|
+
import handlePrismaDuplicateError from '../errors/handlePrismaDuplicateError'
|
|
145
|
+
import AppError from '../errors/AppError'
|
|
146
|
+
import {
|
|
147
|
+
removeSingleUploadedFile,
|
|
148
|
+
removeUploadedFiles
|
|
149
|
+
} from '../utils/removeUploadedFiles'
|
|
150
|
+
import config from '../config'
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Global error handler middleware for PostgreSQL with Prisma
|
|
154
|
+
* Handles all types of errors and returns consistent error responses
|
|
155
|
+
*/
|
|
156
|
+
const globalErrorHandler = async (
|
|
157
|
+
err: any,
|
|
158
|
+
req: Request,
|
|
159
|
+
res: Response,
|
|
160
|
+
next: NextFunction
|
|
161
|
+
): Promise<void> => {
|
|
162
|
+
// Initialize default error details
|
|
163
|
+
let statusCode: number = 500
|
|
164
|
+
let message: string = 'Something went wrong'
|
|
165
|
+
let errorSources: TErrorSource[] = [
|
|
166
|
+
{
|
|
167
|
+
path: '',
|
|
168
|
+
message: 'Something went wrong'
|
|
169
|
+
}
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
// Zod validation error handling
|
|
173
|
+
if (err instanceof ZodError) {
|
|
174
|
+
const simplifiedError: TGenericErrorResponse = handleZodValidationError(err)
|
|
175
|
+
statusCode = simplifiedError.statusCode
|
|
176
|
+
message = simplifiedError.message
|
|
177
|
+
errorSources = simplifiedError.errorSources
|
|
178
|
+
}
|
|
179
|
+
// Prisma validation error handling
|
|
180
|
+
else if (
|
|
181
|
+
err instanceof PrismaClientValidationError ||
|
|
182
|
+
(err instanceof PrismaClientKnownRequestError && err.code === 'P2018')
|
|
183
|
+
) {
|
|
184
|
+
const simplifiedError: TGenericErrorResponse | null =
|
|
185
|
+
handlePrismaValidationError(err)
|
|
186
|
+
if (simplifiedError) {
|
|
187
|
+
statusCode = simplifiedError.statusCode
|
|
188
|
+
message = simplifiedError.message
|
|
189
|
+
errorSources = simplifiedError.errorSources
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// Prisma cast error handling
|
|
193
|
+
else if (
|
|
194
|
+
err instanceof PrismaClientKnownRequestError &&
|
|
195
|
+
(err.code === 'P2023' || err.code === 'P2025')
|
|
196
|
+
) {
|
|
197
|
+
const simplifiedError: TGenericErrorResponse | null =
|
|
198
|
+
handlePrismaCastError(err)
|
|
199
|
+
if (simplifiedError) {
|
|
200
|
+
statusCode = simplifiedError.statusCode
|
|
201
|
+
message = simplifiedError.message
|
|
202
|
+
errorSources = simplifiedError.errorSources
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Prisma Duplicate error handling
|
|
206
|
+
else if (
|
|
207
|
+
err instanceof PrismaClientKnownRequestError &&
|
|
208
|
+
err.code === 'P2002'
|
|
209
|
+
) {
|
|
210
|
+
const simplifiedError: TGenericErrorResponse | null =
|
|
211
|
+
handlePrismaDuplicateError(err)
|
|
212
|
+
if (simplifiedError) {
|
|
213
|
+
statusCode = simplifiedError.statusCode
|
|
214
|
+
message = simplifiedError.message
|
|
215
|
+
errorSources = simplifiedError.errorSources
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// Custom AppError handling
|
|
219
|
+
else if (err instanceof AppError) {
|
|
220
|
+
statusCode = err.statusCode
|
|
221
|
+
message = err.message
|
|
222
|
+
errorSources = [
|
|
223
|
+
{
|
|
224
|
+
path: err.path || '',
|
|
225
|
+
message: err.message
|
|
226
|
+
}
|
|
227
|
+
]
|
|
228
|
+
}
|
|
229
|
+
// Built-in error handling
|
|
230
|
+
else if (err instanceof Error) {
|
|
231
|
+
message = err.message
|
|
232
|
+
errorSources = [
|
|
233
|
+
{
|
|
234
|
+
path: '',
|
|
235
|
+
message: err.message
|
|
236
|
+
}
|
|
237
|
+
]
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Clean up uploaded files if they exist
|
|
241
|
+
try {
|
|
242
|
+
// Handle multiple files (req.files)
|
|
243
|
+
if (req.files) {
|
|
244
|
+
// Type assertion for files object with field names as keys
|
|
245
|
+
const files = req.files as { [fieldname: string]: Express.Multer.File[] }
|
|
246
|
+
await removeUploadedFiles(files)
|
|
247
|
+
}
|
|
248
|
+
// Handle single file (req.file)
|
|
249
|
+
if (req.file) {
|
|
250
|
+
await removeSingleUploadedFile(req.file.path)
|
|
251
|
+
}
|
|
252
|
+
} catch (cleanupError) {
|
|
253
|
+
console.error('Error during file cleanup:', cleanupError)
|
|
254
|
+
// Don't throw the cleanup error, just log it
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Send error response
|
|
258
|
+
res.status(statusCode).json({
|
|
259
|
+
status: statusCode,
|
|
260
|
+
success: false,
|
|
261
|
+
message,
|
|
262
|
+
error: errorSources,
|
|
263
|
+
stack: config.NODE_ENV === 'development' ? err.stack : null
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export default globalErrorHandler
|
|
268
|
+
`;
|
|
269
|
+
await createFile(path.join(projectPath, "src/app/middlewares", "globalErrorHandler.ts"), globalErrorHandlerMiddlewareTemplate);
|
|
270
|
+
}
|
|
271
|
+
if (answers.database === "None") {
|
|
272
|
+
const globalErrorHandlerMiddlewareTemplate = `/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
273
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
274
|
+
import { NextFunction, Request, Response } from 'express'
|
|
275
|
+
import { TErrorSource, TGenericErrorResponse } from '../interfaces/errors'
|
|
276
|
+
import { ZodError } from 'zod'
|
|
277
|
+
import handleZodValidationError from '../errors/handleZodValidationError'
|
|
278
|
+
import AppError from '../errors/AppError'
|
|
279
|
+
import {
|
|
280
|
+
removeSingleUploadedFile,
|
|
281
|
+
removeUploadedFiles
|
|
282
|
+
} from '../utils/removeUploadedFiles'
|
|
283
|
+
import config from '../config'
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Global error handler middleware for applications without a database
|
|
287
|
+
* Handles all types of errors and returns consistent error responses
|
|
288
|
+
*/
|
|
289
|
+
const globalErrorHandler = async (
|
|
290
|
+
err: any,
|
|
291
|
+
req: Request,
|
|
292
|
+
res: Response,
|
|
293
|
+
next: NextFunction
|
|
294
|
+
): Promise<void> => {
|
|
295
|
+
// Initialize default error details
|
|
296
|
+
let statusCode: number = 500;
|
|
297
|
+
let message: string = 'Something went wrong';
|
|
298
|
+
let errorSources: TErrorSource[] = [
|
|
299
|
+
{
|
|
300
|
+
path: '',
|
|
301
|
+
message: 'Something went wrong'
|
|
302
|
+
}
|
|
303
|
+
];
|
|
304
|
+
|
|
305
|
+
// Zod validation error handling
|
|
306
|
+
if (err instanceof ZodError) {
|
|
307
|
+
const simplifiedError: TGenericErrorResponse = handleZodValidationError(err);
|
|
308
|
+
statusCode = simplifiedError.statusCode;
|
|
309
|
+
message = simplifiedError.message;
|
|
310
|
+
errorSources = simplifiedError.errorSources;
|
|
311
|
+
}
|
|
312
|
+
// Custom AppError handling
|
|
313
|
+
else if (err instanceof AppError) {
|
|
314
|
+
statusCode = err.statusCode;
|
|
315
|
+
message = err.message;
|
|
316
|
+
errorSources = [
|
|
317
|
+
{
|
|
318
|
+
path: err.path || '',
|
|
319
|
+
message: err.message
|
|
320
|
+
}
|
|
321
|
+
];
|
|
322
|
+
}
|
|
323
|
+
// Built-in error handling
|
|
324
|
+
else if (err instanceof Error) {
|
|
325
|
+
message = err.message;
|
|
326
|
+
errorSources = [
|
|
327
|
+
{
|
|
328
|
+
path: '',
|
|
329
|
+
message: err.message
|
|
330
|
+
}
|
|
331
|
+
];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Clean up uploaded files if they exist
|
|
335
|
+
try {
|
|
336
|
+
// Handle multiple files (req.files)
|
|
337
|
+
if (req.files) {
|
|
338
|
+
// Type assertion for files object with field names as keys
|
|
339
|
+
const files = req.files as { [fieldname: string]: Express.Multer.File[] };
|
|
340
|
+
await removeUploadedFiles(files);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Handle single file (req.file)
|
|
344
|
+
if (req.file) {
|
|
345
|
+
await removeSingleUploadedFile(req.file.path);
|
|
346
|
+
}
|
|
347
|
+
} catch (cleanupError) {
|
|
348
|
+
console.error('Error during file cleanup:', cleanupError);
|
|
349
|
+
// Don't throw the cleanup error, just log it
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Send error response
|
|
353
|
+
res.status(statusCode).json({
|
|
354
|
+
status: statusCode,
|
|
355
|
+
success: false,
|
|
356
|
+
message,
|
|
357
|
+
error: errorSources,
|
|
358
|
+
stack: config.NODE_ENV === 'development' ? err.stack : null
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
export default globalErrorHandler;
|
|
363
|
+
`;
|
|
364
|
+
await createFile(path.join(projectPath, "src/app/middlewares", "globalErrorHandler.ts"), globalErrorHandlerMiddlewareTemplate);
|
|
365
|
+
}
|
|
366
|
+
// Success message with green checkmark and text
|
|
367
|
+
console.log(chalk.green("✅ Successfully created Global Error Handler Middleware file"));
|
|
368
|
+
}
|
|
369
|
+
catch (err) {
|
|
370
|
+
// Error message with red cross and text
|
|
371
|
+
console.error(chalk.red("❌ Error creating Global Error Handler Guard file:"), err);
|
|
372
|
+
throw err;
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
//# sourceMappingURL=create_Global_ErrorHandler_Guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_Global_ErrorHandler_Guard.js","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_Global_ErrorHandler_Guard.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,MAAM,OAAO,CAAC;AAY1B;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,KAAK,EACpD,WAAmB,EACnB,OAAgB,EACD,EAAE;IACjB,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,QAAQ,KAAK,uBAAuB,EAAE,CAAC;YACjD,MAAM,oCAAoC,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmHlD,CAAC;YAEI,MAAM,UAAU,CACd,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,qBAAqB,EAAE,uBAAuB,CAAC,EACtE,oCAAoC,CACrC,CAAC;QACJ,CAAC;QAED,IAAI,OAAO,CAAC,QAAQ,KAAK,wBAAwB,EAAE,CAAC;YAClD,MAAM,oCAAoC,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwIlD,CAAC;YAEI,MAAM,UAAU,CACd,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,qBAAqB,EAAE,uBAAuB,CAAC,EACtE,oCAAoC,CACrC,CAAC;QACJ,CAAC;QAED,IAAI,OAAO,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;YAChC,MAAM,oCAAoC,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2FlD,CAAC;YAEI,MAAM,UAAU,CACd,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,qBAAqB,EAAE,uBAAuB,CAAC,EACtE,oCAAoC,CACrC,CAAC;QACJ,CAAC;QAED,gDAAgD;QAChD,OAAO,CAAC,GAAG,CACT,KAAK,CAAC,KAAK,CACT,6DAA6D,CAC9D,CACF,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,wCAAwC;QACxC,OAAO,CAAC,KAAK,CACX,KAAK,CAAC,GAAG,CAAC,mDAAmD,CAAC,EAC9D,GAAG,CACJ,CAAC;QACF,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a file upload middleware with dynamic field configuration
|
|
3
|
+
* @param projectPath - Absolute path to the project root
|
|
4
|
+
* @returns Promise that resolves when the file upload middleware file is created
|
|
5
|
+
*/
|
|
6
|
+
export declare const create_Handle_File_Upload_Middleware: (projectPath: string) => Promise<void>;
|
|
7
|
+
//# sourceMappingURL=create_Handle_File_Upload_Middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create_Handle_File_Upload_Middleware.d.ts","sourceRoot":"","sources":["../../../../src/lib/src/middlewares/create_Handle_File_Upload_Middleware.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,eAAO,MAAM,oCAAoC,GAC/C,aAAa,MAAM,KAClB,OAAO,CAAC,IAAI,CAmQd,CAAC"}
|