create-charcole 2.2.0 → 2.2.2
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/.github/workflows/release.yml +26 -26
- package/CHANGELOG.md +301 -301
- package/LICENSE +21 -21
- package/README.md +357 -354
- package/bin/index.js +494 -444
- package/bin/lib/pkgManager.js +49 -49
- package/bin/lib/templateHandler.js +33 -33
- package/package.json +42 -27
- package/packages/swagger/BACKWARD_COMPATIBILITY.md +1 -1
- package/packages/swagger/CHANGELOG.md +1 -1
- package/packages/swagger/README.md +3 -3
- package/packages/swagger/package.json +57 -44
- package/packages/swagger/src/index.d.ts +126 -126
- package/packages/swagger/src/index.js +12 -12
- package/packages/swagger/src/setup.js +100 -100
- package/template/js/.env.example +16 -15
- package/template/js/README.md +982 -978
- package/template/js/basePackage.json +26 -26
- package/template/js/src/app.js +81 -81
- package/template/js/src/config/constants.js +20 -20
- package/template/js/src/config/env.js +26 -26
- package/template/js/src/config/swagger.config.js +15 -15
- package/template/js/src/lib/swagger/SWAGGER_GUIDE.md +3 -3
- package/template/js/src/middlewares/errorHandler.js +180 -180
- package/template/js/src/middlewares/requestLogger.js +33 -33
- package/template/js/src/middlewares/validateRequest.js +42 -42
- package/template/js/src/modules/auth/auth.constants.js +3 -3
- package/template/js/src/modules/auth/auth.controller.js +29 -29
- package/template/js/src/modules/auth/auth.middlewares.js +19 -19
- package/template/js/src/modules/auth/auth.routes.js +131 -131
- package/template/js/src/modules/auth/auth.schemas.js +60 -60
- package/template/js/src/modules/auth/auth.service.js +67 -67
- package/template/js/src/modules/auth/package.json +6 -6
- package/template/js/src/modules/health/controller.js +151 -151
- package/template/js/src/modules/swagger/package.json +5 -5
- package/template/js/src/repositories/user.repo.js +19 -19
- package/template/js/src/routes/index.js +25 -25
- package/template/js/src/routes/protected.js +57 -57
- package/template/js/src/server.js +38 -38
- package/template/js/src/utils/AppError.js +182 -182
- package/template/js/src/utils/logger.js +73 -73
- package/template/js/src/utils/response.js +51 -51
- package/template/ts/.env.example +16 -15
- package/template/ts/README.md +982 -978
- package/template/ts/basePackage.json +36 -36
- package/template/ts/build.js +46 -46
- package/template/ts/src/app.ts +71 -71
- package/template/ts/src/config/constants.ts +27 -27
- package/template/ts/src/config/env.ts +40 -40
- package/template/ts/src/config/swagger.config.ts +30 -30
- package/template/ts/src/lib/swagger/SWAGGER_GUIDE.md +2 -2
- package/template/ts/src/middlewares/errorHandler.ts +201 -201
- package/template/ts/src/middlewares/requestLogger.ts +38 -38
- package/template/ts/src/middlewares/validateRequest.ts +46 -46
- package/template/ts/src/modules/auth/auth.constants.ts +6 -6
- package/template/ts/src/modules/auth/auth.controller.ts +32 -32
- package/template/ts/src/modules/auth/auth.middlewares.ts +46 -46
- package/template/ts/src/modules/auth/auth.routes.ts +52 -52
- package/template/ts/src/modules/auth/auth.schemas.ts +73 -73
- package/template/ts/src/modules/auth/auth.service.ts +106 -106
- package/template/ts/src/modules/auth/package.json +10 -10
- package/template/ts/src/modules/health/controller.ts +80 -80
- package/template/ts/src/modules/swagger/package.json +5 -5
- package/template/ts/src/repositories/user.repo.ts +33 -33
- package/template/ts/src/routes/index.ts +24 -24
- package/template/ts/src/routes/protected.ts +46 -46
- package/template/ts/src/server.ts +41 -41
- package/template/ts/src/types/express.d.ts +9 -9
- package/template/ts/src/utils/AppError.ts +220 -220
- package/template/ts/src/utils/logger.ts +55 -55
- package/template/ts/src/utils/response.ts +100 -100
- package/template/ts/tsconfig.json +26 -26
- package/packages/swagger/package-lock.json +0 -1715
- package/tmpclaude-1049-cwd +0 -1
- package/tmpclaude-3e37-cwd +0 -1
- package/tmpclaude-4d73-cwd +0 -1
- package/tmpclaude-8a8e-cwd +0 -1
|
@@ -1,182 +1,182 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Operational Error Class
|
|
3
|
-
*
|
|
4
|
-
* Use for expected errors that can be handled gracefully
|
|
5
|
-
* Examples: validation errors, resource not found, auth failures
|
|
6
|
-
*
|
|
7
|
-
* These errors are logged and sent back to client as JSON
|
|
8
|
-
*/
|
|
9
|
-
export class AppError extends Error {
|
|
10
|
-
constructor(
|
|
11
|
-
message,
|
|
12
|
-
statusCode = 500,
|
|
13
|
-
{ isOperational = true, code = null, context = null, cause = null } = {},
|
|
14
|
-
) {
|
|
15
|
-
super(message);
|
|
16
|
-
|
|
17
|
-
// Operational or Programmer error
|
|
18
|
-
this.isOperational = isOperational;
|
|
19
|
-
|
|
20
|
-
// HTTP status code
|
|
21
|
-
this.statusCode = statusCode;
|
|
22
|
-
|
|
23
|
-
// Error code for client handling (e.g., 'INVALID_EMAIL', 'USER_NOT_FOUND')
|
|
24
|
-
this.code = code;
|
|
25
|
-
|
|
26
|
-
// Additional context data
|
|
27
|
-
this.context = context || {};
|
|
28
|
-
|
|
29
|
-
// Original error that caused this
|
|
30
|
-
this.cause = cause;
|
|
31
|
-
|
|
32
|
-
// Timestamp
|
|
33
|
-
this.timestamp = new Date().toISOString();
|
|
34
|
-
|
|
35
|
-
// Preserve stack trace
|
|
36
|
-
Error.captureStackTrace(this, this.constructor);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Convert to JSON response format
|
|
41
|
-
*/
|
|
42
|
-
toJSON() {
|
|
43
|
-
return {
|
|
44
|
-
success: false,
|
|
45
|
-
message: this.message,
|
|
46
|
-
code: this.code,
|
|
47
|
-
statusCode: this.statusCode,
|
|
48
|
-
...(Object.keys(this.context).length > 0 && { context: this.context }),
|
|
49
|
-
timestamp: this.timestamp,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Get full error details for logging
|
|
55
|
-
*/
|
|
56
|
-
getFullDetails() {
|
|
57
|
-
return {
|
|
58
|
-
message: this.message,
|
|
59
|
-
statusCode: this.statusCode,
|
|
60
|
-
code: this.code,
|
|
61
|
-
isOperational: this.isOperational,
|
|
62
|
-
context: this.context,
|
|
63
|
-
cause: this.cause?.message || null,
|
|
64
|
-
stack: this.stack,
|
|
65
|
-
timestamp: this.timestamp,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Validation Error - extends AppError
|
|
72
|
-
* Use for input validation failures
|
|
73
|
-
*/
|
|
74
|
-
export class ValidationError extends AppError {
|
|
75
|
-
constructor(message, errors = [], context = null) {
|
|
76
|
-
super(message, 422, {
|
|
77
|
-
isOperational: true,
|
|
78
|
-
code: "VALIDATION_ERROR",
|
|
79
|
-
context,
|
|
80
|
-
});
|
|
81
|
-
this.errors = errors;
|
|
82
|
-
this.name = "ValidationError";
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
toJSON() {
|
|
86
|
-
return {
|
|
87
|
-
...super.toJSON(),
|
|
88
|
-
errors: this.errors,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Authentication Error
|
|
95
|
-
* Use for auth/permission failures
|
|
96
|
-
*/
|
|
97
|
-
export class AuthenticationError extends AppError {
|
|
98
|
-
constructor(message = "Unauthorized", context = null) {
|
|
99
|
-
super(message, 401, {
|
|
100
|
-
isOperational: true,
|
|
101
|
-
code: "AUTHENTICATION_ERROR",
|
|
102
|
-
context,
|
|
103
|
-
});
|
|
104
|
-
this.name = "AuthenticationError";
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Authorization Error
|
|
110
|
-
* Use for permission denied scenarios
|
|
111
|
-
*/
|
|
112
|
-
export class AuthorizationError extends AppError {
|
|
113
|
-
constructor(message = "Forbidden", context = null) {
|
|
114
|
-
super(message, 403, {
|
|
115
|
-
isOperational: true,
|
|
116
|
-
code: "AUTHORIZATION_ERROR",
|
|
117
|
-
context,
|
|
118
|
-
});
|
|
119
|
-
this.name = "AuthorizationError";
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Not Found Error
|
|
125
|
-
* Use when resource doesn't exist
|
|
126
|
-
*/
|
|
127
|
-
export class NotFoundError extends AppError {
|
|
128
|
-
constructor(resource = "Resource", context = null) {
|
|
129
|
-
super(`${resource} not found`, 404, {
|
|
130
|
-
isOperational: true,
|
|
131
|
-
code: "NOT_FOUND",
|
|
132
|
-
context,
|
|
133
|
-
});
|
|
134
|
-
this.name = "NotFoundError";
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Conflict Error
|
|
140
|
-
* Use for duplicate resources, business logic conflicts
|
|
141
|
-
*/
|
|
142
|
-
export class ConflictError extends AppError {
|
|
143
|
-
constructor(message, context = null) {
|
|
144
|
-
super(message, 409, {
|
|
145
|
-
isOperational: true,
|
|
146
|
-
code: "CONFLICT",
|
|
147
|
-
context,
|
|
148
|
-
});
|
|
149
|
-
this.name = "ConflictError";
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Bad Request Error
|
|
155
|
-
* Use for malformed requests
|
|
156
|
-
*/
|
|
157
|
-
export class BadRequestError extends AppError {
|
|
158
|
-
constructor(message, context = null) {
|
|
159
|
-
super(message, 400, {
|
|
160
|
-
isOperational: true,
|
|
161
|
-
code: "BAD_REQUEST",
|
|
162
|
-
context,
|
|
163
|
-
});
|
|
164
|
-
this.name = "BadRequestError";
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Internal Server Error
|
|
170
|
-
* Use for unexpected server-side errors (programmer errors)
|
|
171
|
-
*/
|
|
172
|
-
export class InternalServerError extends AppError {
|
|
173
|
-
constructor(message = "Internal server error", cause = null, context = null) {
|
|
174
|
-
super(message, 500, {
|
|
175
|
-
isOperational: false,
|
|
176
|
-
code: "INTERNAL_SERVER_ERROR",
|
|
177
|
-
cause,
|
|
178
|
-
context,
|
|
179
|
-
});
|
|
180
|
-
this.name = "InternalServerError";
|
|
181
|
-
}
|
|
182
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Operational Error Class
|
|
3
|
+
*
|
|
4
|
+
* Use for expected errors that can be handled gracefully
|
|
5
|
+
* Examples: validation errors, resource not found, auth failures
|
|
6
|
+
*
|
|
7
|
+
* These errors are logged and sent back to client as JSON
|
|
8
|
+
*/
|
|
9
|
+
export class AppError extends Error {
|
|
10
|
+
constructor(
|
|
11
|
+
message,
|
|
12
|
+
statusCode = 500,
|
|
13
|
+
{ isOperational = true, code = null, context = null, cause = null } = {},
|
|
14
|
+
) {
|
|
15
|
+
super(message);
|
|
16
|
+
|
|
17
|
+
// Operational or Programmer error
|
|
18
|
+
this.isOperational = isOperational;
|
|
19
|
+
|
|
20
|
+
// HTTP status code
|
|
21
|
+
this.statusCode = statusCode;
|
|
22
|
+
|
|
23
|
+
// Error code for client handling (e.g., 'INVALID_EMAIL', 'USER_NOT_FOUND')
|
|
24
|
+
this.code = code;
|
|
25
|
+
|
|
26
|
+
// Additional context data
|
|
27
|
+
this.context = context || {};
|
|
28
|
+
|
|
29
|
+
// Original error that caused this
|
|
30
|
+
this.cause = cause;
|
|
31
|
+
|
|
32
|
+
// Timestamp
|
|
33
|
+
this.timestamp = new Date().toISOString();
|
|
34
|
+
|
|
35
|
+
// Preserve stack trace
|
|
36
|
+
Error.captureStackTrace(this, this.constructor);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Convert to JSON response format
|
|
41
|
+
*/
|
|
42
|
+
toJSON() {
|
|
43
|
+
return {
|
|
44
|
+
success: false,
|
|
45
|
+
message: this.message,
|
|
46
|
+
code: this.code,
|
|
47
|
+
statusCode: this.statusCode,
|
|
48
|
+
...(Object.keys(this.context).length > 0 && { context: this.context }),
|
|
49
|
+
timestamp: this.timestamp,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get full error details for logging
|
|
55
|
+
*/
|
|
56
|
+
getFullDetails() {
|
|
57
|
+
return {
|
|
58
|
+
message: this.message,
|
|
59
|
+
statusCode: this.statusCode,
|
|
60
|
+
code: this.code,
|
|
61
|
+
isOperational: this.isOperational,
|
|
62
|
+
context: this.context,
|
|
63
|
+
cause: this.cause?.message || null,
|
|
64
|
+
stack: this.stack,
|
|
65
|
+
timestamp: this.timestamp,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Validation Error - extends AppError
|
|
72
|
+
* Use for input validation failures
|
|
73
|
+
*/
|
|
74
|
+
export class ValidationError extends AppError {
|
|
75
|
+
constructor(message, errors = [], context = null) {
|
|
76
|
+
super(message, 422, {
|
|
77
|
+
isOperational: true,
|
|
78
|
+
code: "VALIDATION_ERROR",
|
|
79
|
+
context,
|
|
80
|
+
});
|
|
81
|
+
this.errors = errors;
|
|
82
|
+
this.name = "ValidationError";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
toJSON() {
|
|
86
|
+
return {
|
|
87
|
+
...super.toJSON(),
|
|
88
|
+
errors: this.errors,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Authentication Error
|
|
95
|
+
* Use for auth/permission failures
|
|
96
|
+
*/
|
|
97
|
+
export class AuthenticationError extends AppError {
|
|
98
|
+
constructor(message = "Unauthorized", context = null) {
|
|
99
|
+
super(message, 401, {
|
|
100
|
+
isOperational: true,
|
|
101
|
+
code: "AUTHENTICATION_ERROR",
|
|
102
|
+
context,
|
|
103
|
+
});
|
|
104
|
+
this.name = "AuthenticationError";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Authorization Error
|
|
110
|
+
* Use for permission denied scenarios
|
|
111
|
+
*/
|
|
112
|
+
export class AuthorizationError extends AppError {
|
|
113
|
+
constructor(message = "Forbidden", context = null) {
|
|
114
|
+
super(message, 403, {
|
|
115
|
+
isOperational: true,
|
|
116
|
+
code: "AUTHORIZATION_ERROR",
|
|
117
|
+
context,
|
|
118
|
+
});
|
|
119
|
+
this.name = "AuthorizationError";
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Not Found Error
|
|
125
|
+
* Use when resource doesn't exist
|
|
126
|
+
*/
|
|
127
|
+
export class NotFoundError extends AppError {
|
|
128
|
+
constructor(resource = "Resource", context = null) {
|
|
129
|
+
super(`${resource} not found`, 404, {
|
|
130
|
+
isOperational: true,
|
|
131
|
+
code: "NOT_FOUND",
|
|
132
|
+
context,
|
|
133
|
+
});
|
|
134
|
+
this.name = "NotFoundError";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Conflict Error
|
|
140
|
+
* Use for duplicate resources, business logic conflicts
|
|
141
|
+
*/
|
|
142
|
+
export class ConflictError extends AppError {
|
|
143
|
+
constructor(message, context = null) {
|
|
144
|
+
super(message, 409, {
|
|
145
|
+
isOperational: true,
|
|
146
|
+
code: "CONFLICT",
|
|
147
|
+
context,
|
|
148
|
+
});
|
|
149
|
+
this.name = "ConflictError";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Bad Request Error
|
|
155
|
+
* Use for malformed requests
|
|
156
|
+
*/
|
|
157
|
+
export class BadRequestError extends AppError {
|
|
158
|
+
constructor(message, context = null) {
|
|
159
|
+
super(message, 400, {
|
|
160
|
+
isOperational: true,
|
|
161
|
+
code: "BAD_REQUEST",
|
|
162
|
+
context,
|
|
163
|
+
});
|
|
164
|
+
this.name = "BadRequestError";
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Internal Server Error
|
|
170
|
+
* Use for unexpected server-side errors (programmer errors)
|
|
171
|
+
*/
|
|
172
|
+
export class InternalServerError extends AppError {
|
|
173
|
+
constructor(message = "Internal server error", cause = null, context = null) {
|
|
174
|
+
super(message, 500, {
|
|
175
|
+
isOperational: false,
|
|
176
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
177
|
+
cause,
|
|
178
|
+
context,
|
|
179
|
+
});
|
|
180
|
+
this.name = "InternalServerError";
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import { env } from "../config/env.js";
|
|
2
|
-
|
|
3
|
-
const COLORS = {
|
|
4
|
-
reset: "\x1b[0m",
|
|
5
|
-
red: "\x1b[31m",
|
|
6
|
-
yellow: "\x1b[33m",
|
|
7
|
-
green: "\x1b[32m",
|
|
8
|
-
blue: "\x1b[36m",
|
|
9
|
-
gray: "\x1b[90m",
|
|
10
|
-
magenta: "\x1b[35m",
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const LOG_LEVELS = {
|
|
14
|
-
debug: 0,
|
|
15
|
-
info: 1,
|
|
16
|
-
warn: 2,
|
|
17
|
-
error: 3,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const getCurrentLogLevel = () => LOG_LEVELS[env.LOG_LEVEL] || LOG_LEVELS.info;
|
|
21
|
-
|
|
22
|
-
const formatLog = (level, message, data) => {
|
|
23
|
-
const timestamp = new Date().toISOString();
|
|
24
|
-
const dataStr = data ? ` ${JSON.stringify(data)}` : "";
|
|
25
|
-
return `[${timestamp}] ${level}:${dataStr ? " " + message + dataStr : " " + message}`;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const formatStack = (stack) => {
|
|
29
|
-
if (!stack) return "";
|
|
30
|
-
return `\n${stack}`;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const logger = {
|
|
34
|
-
debug: (message, data) => {
|
|
35
|
-
if (getCurrentLogLevel() <= LOG_LEVELS.debug) {
|
|
36
|
-
console.log(
|
|
37
|
-
`${COLORS.gray}${formatLog("DEBUG", message, data)}${COLORS.reset}`,
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
|
|
42
|
-
info: (message, data) => {
|
|
43
|
-
if (getCurrentLogLevel() <= LOG_LEVELS.info) {
|
|
44
|
-
console.log(
|
|
45
|
-
`${COLORS.blue}${formatLog("INFO", message, data)}${COLORS.reset}`,
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
warn: (message, data) => {
|
|
51
|
-
if (getCurrentLogLevel() <= LOG_LEVELS.warn) {
|
|
52
|
-
console.warn(
|
|
53
|
-
`${COLORS.yellow}${formatLog("WARN", message, data)}${COLORS.reset}`,
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
error: (message, data, stack) => {
|
|
59
|
-
if (getCurrentLogLevel() <= LOG_LEVELS.error) {
|
|
60
|
-
const stackTrace = formatStack(stack);
|
|
61
|
-
console.error(
|
|
62
|
-
`${COLORS.red}${formatLog("ERROR", message, data)}${stackTrace}${COLORS.reset}`,
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
fatal: (message, data, stack) => {
|
|
68
|
-
const stackTrace = formatStack(stack);
|
|
69
|
-
console.error(
|
|
70
|
-
`${COLORS.red}${COLORS.magenta}${formatLog("FATAL", message, data)}${stackTrace}${COLORS.reset}`,
|
|
71
|
-
);
|
|
72
|
-
},
|
|
73
|
-
};
|
|
1
|
+
import { env } from "../config/env.js";
|
|
2
|
+
|
|
3
|
+
const COLORS = {
|
|
4
|
+
reset: "\x1b[0m",
|
|
5
|
+
red: "\x1b[31m",
|
|
6
|
+
yellow: "\x1b[33m",
|
|
7
|
+
green: "\x1b[32m",
|
|
8
|
+
blue: "\x1b[36m",
|
|
9
|
+
gray: "\x1b[90m",
|
|
10
|
+
magenta: "\x1b[35m",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const LOG_LEVELS = {
|
|
14
|
+
debug: 0,
|
|
15
|
+
info: 1,
|
|
16
|
+
warn: 2,
|
|
17
|
+
error: 3,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const getCurrentLogLevel = () => LOG_LEVELS[env.LOG_LEVEL] || LOG_LEVELS.info;
|
|
21
|
+
|
|
22
|
+
const formatLog = (level, message, data) => {
|
|
23
|
+
const timestamp = new Date().toISOString();
|
|
24
|
+
const dataStr = data ? ` ${JSON.stringify(data)}` : "";
|
|
25
|
+
return `[${timestamp}] ${level}:${dataStr ? " " + message + dataStr : " " + message}`;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const formatStack = (stack) => {
|
|
29
|
+
if (!stack) return "";
|
|
30
|
+
return `\n${stack}`;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const logger = {
|
|
34
|
+
debug: (message, data) => {
|
|
35
|
+
if (getCurrentLogLevel() <= LOG_LEVELS.debug) {
|
|
36
|
+
console.log(
|
|
37
|
+
`${COLORS.gray}${formatLog("DEBUG", message, data)}${COLORS.reset}`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
info: (message, data) => {
|
|
43
|
+
if (getCurrentLogLevel() <= LOG_LEVELS.info) {
|
|
44
|
+
console.log(
|
|
45
|
+
`${COLORS.blue}${formatLog("INFO", message, data)}${COLORS.reset}`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
warn: (message, data) => {
|
|
51
|
+
if (getCurrentLogLevel() <= LOG_LEVELS.warn) {
|
|
52
|
+
console.warn(
|
|
53
|
+
`${COLORS.yellow}${formatLog("WARN", message, data)}${COLORS.reset}`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
error: (message, data, stack) => {
|
|
59
|
+
if (getCurrentLogLevel() <= LOG_LEVELS.error) {
|
|
60
|
+
const stackTrace = formatStack(stack);
|
|
61
|
+
console.error(
|
|
62
|
+
`${COLORS.red}${formatLog("ERROR", message, data)}${stackTrace}${COLORS.reset}`,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
fatal: (message, data, stack) => {
|
|
68
|
+
const stackTrace = formatStack(stack);
|
|
69
|
+
console.error(
|
|
70
|
+
`${COLORS.red}${COLORS.magenta}${formatLog("FATAL", message, data)}${stackTrace}${COLORS.reset}`,
|
|
71
|
+
);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Send success response
|
|
3
|
-
*
|
|
4
|
-
* @param {Response} res - Express response object
|
|
5
|
-
* @param {*} data - Response data
|
|
6
|
-
* @param {number} statusCode - HTTP status code (default: 200)
|
|
7
|
-
* @param {string} message - Success message (default: 'Success')
|
|
8
|
-
*/
|
|
9
|
-
export const sendSuccess = (
|
|
10
|
-
res,
|
|
11
|
-
data,
|
|
12
|
-
statusCode = 200,
|
|
13
|
-
message = "Success",
|
|
14
|
-
) => {
|
|
15
|
-
return res.status(statusCode).json({
|
|
16
|
-
success: true,
|
|
17
|
-
message,
|
|
18
|
-
data,
|
|
19
|
-
timestamp: new Date().toISOString(),
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Send error response (DEPRECATED - use AppError instead)
|
|
25
|
-
* This is kept for backward compatibility
|
|
26
|
-
*/
|
|
27
|
-
export const sendError = (res, message, statusCode = 500, errors = null) => {
|
|
28
|
-
return res.status(statusCode).json({
|
|
29
|
-
success: false,
|
|
30
|
-
message,
|
|
31
|
-
...(errors && { errors }),
|
|
32
|
-
timestamp: new Date().toISOString(),
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Send validation error response (DEPRECATED - use ValidationError instead)
|
|
38
|
-
* This is kept for backward compatibility
|
|
39
|
-
*/
|
|
40
|
-
export const sendValidationError = (res, errors, statusCode = 422) => {
|
|
41
|
-
return res.status(statusCode).json({
|
|
42
|
-
success: false,
|
|
43
|
-
message: "Validation failed",
|
|
44
|
-
errors: errors.map((err) => ({
|
|
45
|
-
field: err.path.join("."),
|
|
46
|
-
message: err.message,
|
|
47
|
-
code: err.code,
|
|
48
|
-
})),
|
|
49
|
-
timestamp: new Date().toISOString(),
|
|
50
|
-
});
|
|
51
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Send success response
|
|
3
|
+
*
|
|
4
|
+
* @param {Response} res - Express response object
|
|
5
|
+
* @param {*} data - Response data
|
|
6
|
+
* @param {number} statusCode - HTTP status code (default: 200)
|
|
7
|
+
* @param {string} message - Success message (default: 'Success')
|
|
8
|
+
*/
|
|
9
|
+
export const sendSuccess = (
|
|
10
|
+
res,
|
|
11
|
+
data,
|
|
12
|
+
statusCode = 200,
|
|
13
|
+
message = "Success",
|
|
14
|
+
) => {
|
|
15
|
+
return res.status(statusCode).json({
|
|
16
|
+
success: true,
|
|
17
|
+
message,
|
|
18
|
+
data,
|
|
19
|
+
timestamp: new Date().toISOString(),
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Send error response (DEPRECATED - use AppError instead)
|
|
25
|
+
* This is kept for backward compatibility
|
|
26
|
+
*/
|
|
27
|
+
export const sendError = (res, message, statusCode = 500, errors = null) => {
|
|
28
|
+
return res.status(statusCode).json({
|
|
29
|
+
success: false,
|
|
30
|
+
message,
|
|
31
|
+
...(errors && { errors }),
|
|
32
|
+
timestamp: new Date().toISOString(),
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Send validation error response (DEPRECATED - use ValidationError instead)
|
|
38
|
+
* This is kept for backward compatibility
|
|
39
|
+
*/
|
|
40
|
+
export const sendValidationError = (res, errors, statusCode = 422) => {
|
|
41
|
+
return res.status(statusCode).json({
|
|
42
|
+
success: false,
|
|
43
|
+
message: "Validation failed",
|
|
44
|
+
errors: errors.map((err) => ({
|
|
45
|
+
field: err.path.join("."),
|
|
46
|
+
message: err.message,
|
|
47
|
+
code: err.code,
|
|
48
|
+
})),
|
|
49
|
+
timestamp: new Date().toISOString(),
|
|
50
|
+
});
|
|
51
|
+
};
|
package/template/ts/.env.example
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
# Server Configuration
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
# Server Configuration
|
|
2
|
+
APP_NAME=CHARCOLE API
|
|
3
|
+
NODE_ENV=development
|
|
4
|
+
PORT=3000
|
|
5
|
+
|
|
6
|
+
# Logging
|
|
7
|
+
LOG_LEVEL=info
|
|
8
|
+
|
|
9
|
+
# CORS
|
|
10
|
+
CORS_ORIGIN=*
|
|
11
|
+
|
|
12
|
+
# Request
|
|
13
|
+
REQUEST_TIMEOUT=30000
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Authentication
|
|
16
17
|
JWT_SECRET=your-secret-key-here
|