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,220 +1,220 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared types
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export type ErrorContext = Record<string, unknown>;
|
|
6
|
-
|
|
7
|
-
export interface AppErrorOptions {
|
|
8
|
-
isOperational?: boolean;
|
|
9
|
-
code?: string | null;
|
|
10
|
-
context?: ErrorContext | null;
|
|
11
|
-
cause?: Error | null;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Operational Error Class
|
|
16
|
-
*
|
|
17
|
-
* Use for expected errors that can be handled gracefully
|
|
18
|
-
*/
|
|
19
|
-
export class AppError extends Error {
|
|
20
|
-
public readonly statusCode: number;
|
|
21
|
-
public readonly isOperational: boolean;
|
|
22
|
-
public readonly code: string | null;
|
|
23
|
-
public readonly context: ErrorContext;
|
|
24
|
-
public readonly cause: Error | null;
|
|
25
|
-
public readonly timestamp: string;
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
message: string,
|
|
29
|
-
statusCode = 500,
|
|
30
|
-
{
|
|
31
|
-
isOperational = true,
|
|
32
|
-
code = null,
|
|
33
|
-
context = null,
|
|
34
|
-
cause = null,
|
|
35
|
-
}: AppErrorOptions = {},
|
|
36
|
-
) {
|
|
37
|
-
super(message);
|
|
38
|
-
|
|
39
|
-
this.name = "AppError";
|
|
40
|
-
this.statusCode = statusCode;
|
|
41
|
-
this.isOperational = isOperational;
|
|
42
|
-
this.code = code;
|
|
43
|
-
this.context = context ?? {};
|
|
44
|
-
this.cause = cause;
|
|
45
|
-
this.timestamp = new Date().toISOString();
|
|
46
|
-
|
|
47
|
-
// Preserve stack trace
|
|
48
|
-
Error.captureStackTrace(this, this.constructor);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Convert to JSON response format
|
|
53
|
-
*/
|
|
54
|
-
toJSON(): {
|
|
55
|
-
success: false;
|
|
56
|
-
message: string;
|
|
57
|
-
code: string | null;
|
|
58
|
-
statusCode: number;
|
|
59
|
-
context?: ErrorContext;
|
|
60
|
-
timestamp: string;
|
|
61
|
-
} {
|
|
62
|
-
return {
|
|
63
|
-
success: false,
|
|
64
|
-
message: this.message,
|
|
65
|
-
code: this.code,
|
|
66
|
-
statusCode: this.statusCode,
|
|
67
|
-
...(Object.keys(this.context).length > 0 && { context: this.context }),
|
|
68
|
-
timestamp: this.timestamp,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Get full error details for logging
|
|
74
|
-
*/
|
|
75
|
-
getFullDetails(): {
|
|
76
|
-
message: string;
|
|
77
|
-
statusCode: number;
|
|
78
|
-
code: string | null;
|
|
79
|
-
isOperational: boolean;
|
|
80
|
-
context: ErrorContext;
|
|
81
|
-
cause: string | null;
|
|
82
|
-
stack?: string;
|
|
83
|
-
timestamp: string;
|
|
84
|
-
} {
|
|
85
|
-
return {
|
|
86
|
-
message: this.message,
|
|
87
|
-
statusCode: this.statusCode,
|
|
88
|
-
code: this.code,
|
|
89
|
-
isOperational: this.isOperational,
|
|
90
|
-
context: this.context,
|
|
91
|
-
cause: this.cause?.message ?? null,
|
|
92
|
-
stack: this.stack,
|
|
93
|
-
timestamp: this.timestamp,
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Validation Error
|
|
100
|
-
*/
|
|
101
|
-
export class ValidationError extends AppError {
|
|
102
|
-
public readonly errors: unknown[];
|
|
103
|
-
|
|
104
|
-
constructor(
|
|
105
|
-
message: string,
|
|
106
|
-
errors: unknown[] = [],
|
|
107
|
-
context: ErrorContext | null = null,
|
|
108
|
-
) {
|
|
109
|
-
super(message, 422, {
|
|
110
|
-
isOperational: true,
|
|
111
|
-
code: "VALIDATION_ERROR",
|
|
112
|
-
context,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
this.name = "ValidationError";
|
|
116
|
-
this.errors = errors;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
override toJSON() {
|
|
120
|
-
return {
|
|
121
|
-
...super.toJSON(),
|
|
122
|
-
errors: this.errors,
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Authentication Error
|
|
129
|
-
*/
|
|
130
|
-
export class AuthenticationError extends AppError {
|
|
131
|
-
constructor(message = "Unauthorized", context: ErrorContext | null = null) {
|
|
132
|
-
super(message, 401, {
|
|
133
|
-
isOperational: true,
|
|
134
|
-
code: "AUTHENTICATION_ERROR",
|
|
135
|
-
context,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
this.name = "AuthenticationError";
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Authorization Error
|
|
144
|
-
*/
|
|
145
|
-
export class AuthorizationError extends AppError {
|
|
146
|
-
constructor(message = "Forbidden", context: ErrorContext | null = null) {
|
|
147
|
-
super(message, 403, {
|
|
148
|
-
isOperational: true,
|
|
149
|
-
code: "AUTHORIZATION_ERROR",
|
|
150
|
-
context,
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
this.name = "AuthorizationError";
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Not Found Error
|
|
159
|
-
*/
|
|
160
|
-
export class NotFoundError extends AppError {
|
|
161
|
-
constructor(resource = "Resource", context: ErrorContext | null = null) {
|
|
162
|
-
super(`${resource} not found`, 404, {
|
|
163
|
-
isOperational: true,
|
|
164
|
-
code: "NOT_FOUND",
|
|
165
|
-
context,
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
this.name = "NotFoundError";
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Conflict Error
|
|
174
|
-
*/
|
|
175
|
-
export class ConflictError extends AppError {
|
|
176
|
-
constructor(message: string, context: ErrorContext | null = null) {
|
|
177
|
-
super(message, 409, {
|
|
178
|
-
isOperational: true,
|
|
179
|
-
code: "CONFLICT",
|
|
180
|
-
context,
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
this.name = "ConflictError";
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Bad Request Error
|
|
189
|
-
*/
|
|
190
|
-
export class BadRequestError extends AppError {
|
|
191
|
-
constructor(message: string, context: ErrorContext | null = null) {
|
|
192
|
-
super(message, 400, {
|
|
193
|
-
isOperational: true,
|
|
194
|
-
code: "BAD_REQUEST",
|
|
195
|
-
context,
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
this.name = "BadRequestError";
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Internal Server Error
|
|
204
|
-
*/
|
|
205
|
-
export class InternalServerError extends AppError {
|
|
206
|
-
constructor(
|
|
207
|
-
message = "Internal server error",
|
|
208
|
-
cause: Error | null = null,
|
|
209
|
-
context: ErrorContext | null = null,
|
|
210
|
-
) {
|
|
211
|
-
super(message, 500, {
|
|
212
|
-
isOperational: false,
|
|
213
|
-
code: "INTERNAL_SERVER_ERROR",
|
|
214
|
-
cause,
|
|
215
|
-
context,
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
this.name = "InternalServerError";
|
|
219
|
-
}
|
|
220
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Shared types
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type ErrorContext = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
export interface AppErrorOptions {
|
|
8
|
+
isOperational?: boolean;
|
|
9
|
+
code?: string | null;
|
|
10
|
+
context?: ErrorContext | null;
|
|
11
|
+
cause?: Error | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Operational Error Class
|
|
16
|
+
*
|
|
17
|
+
* Use for expected errors that can be handled gracefully
|
|
18
|
+
*/
|
|
19
|
+
export class AppError extends Error {
|
|
20
|
+
public readonly statusCode: number;
|
|
21
|
+
public readonly isOperational: boolean;
|
|
22
|
+
public readonly code: string | null;
|
|
23
|
+
public readonly context: ErrorContext;
|
|
24
|
+
public readonly cause: Error | null;
|
|
25
|
+
public readonly timestamp: string;
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
message: string,
|
|
29
|
+
statusCode = 500,
|
|
30
|
+
{
|
|
31
|
+
isOperational = true,
|
|
32
|
+
code = null,
|
|
33
|
+
context = null,
|
|
34
|
+
cause = null,
|
|
35
|
+
}: AppErrorOptions = {},
|
|
36
|
+
) {
|
|
37
|
+
super(message);
|
|
38
|
+
|
|
39
|
+
this.name = "AppError";
|
|
40
|
+
this.statusCode = statusCode;
|
|
41
|
+
this.isOperational = isOperational;
|
|
42
|
+
this.code = code;
|
|
43
|
+
this.context = context ?? {};
|
|
44
|
+
this.cause = cause;
|
|
45
|
+
this.timestamp = new Date().toISOString();
|
|
46
|
+
|
|
47
|
+
// Preserve stack trace
|
|
48
|
+
Error.captureStackTrace(this, this.constructor);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Convert to JSON response format
|
|
53
|
+
*/
|
|
54
|
+
toJSON(): {
|
|
55
|
+
success: false;
|
|
56
|
+
message: string;
|
|
57
|
+
code: string | null;
|
|
58
|
+
statusCode: number;
|
|
59
|
+
context?: ErrorContext;
|
|
60
|
+
timestamp: string;
|
|
61
|
+
} {
|
|
62
|
+
return {
|
|
63
|
+
success: false,
|
|
64
|
+
message: this.message,
|
|
65
|
+
code: this.code,
|
|
66
|
+
statusCode: this.statusCode,
|
|
67
|
+
...(Object.keys(this.context).length > 0 && { context: this.context }),
|
|
68
|
+
timestamp: this.timestamp,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get full error details for logging
|
|
74
|
+
*/
|
|
75
|
+
getFullDetails(): {
|
|
76
|
+
message: string;
|
|
77
|
+
statusCode: number;
|
|
78
|
+
code: string | null;
|
|
79
|
+
isOperational: boolean;
|
|
80
|
+
context: ErrorContext;
|
|
81
|
+
cause: string | null;
|
|
82
|
+
stack?: string;
|
|
83
|
+
timestamp: string;
|
|
84
|
+
} {
|
|
85
|
+
return {
|
|
86
|
+
message: this.message,
|
|
87
|
+
statusCode: this.statusCode,
|
|
88
|
+
code: this.code,
|
|
89
|
+
isOperational: this.isOperational,
|
|
90
|
+
context: this.context,
|
|
91
|
+
cause: this.cause?.message ?? null,
|
|
92
|
+
stack: this.stack,
|
|
93
|
+
timestamp: this.timestamp,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Validation Error
|
|
100
|
+
*/
|
|
101
|
+
export class ValidationError extends AppError {
|
|
102
|
+
public readonly errors: unknown[];
|
|
103
|
+
|
|
104
|
+
constructor(
|
|
105
|
+
message: string,
|
|
106
|
+
errors: unknown[] = [],
|
|
107
|
+
context: ErrorContext | null = null,
|
|
108
|
+
) {
|
|
109
|
+
super(message, 422, {
|
|
110
|
+
isOperational: true,
|
|
111
|
+
code: "VALIDATION_ERROR",
|
|
112
|
+
context,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
this.name = "ValidationError";
|
|
116
|
+
this.errors = errors;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
override toJSON() {
|
|
120
|
+
return {
|
|
121
|
+
...super.toJSON(),
|
|
122
|
+
errors: this.errors,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Authentication Error
|
|
129
|
+
*/
|
|
130
|
+
export class AuthenticationError extends AppError {
|
|
131
|
+
constructor(message = "Unauthorized", context: ErrorContext | null = null) {
|
|
132
|
+
super(message, 401, {
|
|
133
|
+
isOperational: true,
|
|
134
|
+
code: "AUTHENTICATION_ERROR",
|
|
135
|
+
context,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
this.name = "AuthenticationError";
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Authorization Error
|
|
144
|
+
*/
|
|
145
|
+
export class AuthorizationError extends AppError {
|
|
146
|
+
constructor(message = "Forbidden", context: ErrorContext | null = null) {
|
|
147
|
+
super(message, 403, {
|
|
148
|
+
isOperational: true,
|
|
149
|
+
code: "AUTHORIZATION_ERROR",
|
|
150
|
+
context,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
this.name = "AuthorizationError";
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Not Found Error
|
|
159
|
+
*/
|
|
160
|
+
export class NotFoundError extends AppError {
|
|
161
|
+
constructor(resource = "Resource", context: ErrorContext | null = null) {
|
|
162
|
+
super(`${resource} not found`, 404, {
|
|
163
|
+
isOperational: true,
|
|
164
|
+
code: "NOT_FOUND",
|
|
165
|
+
context,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
this.name = "NotFoundError";
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Conflict Error
|
|
174
|
+
*/
|
|
175
|
+
export class ConflictError extends AppError {
|
|
176
|
+
constructor(message: string, context: ErrorContext | null = null) {
|
|
177
|
+
super(message, 409, {
|
|
178
|
+
isOperational: true,
|
|
179
|
+
code: "CONFLICT",
|
|
180
|
+
context,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
this.name = "ConflictError";
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Bad Request Error
|
|
189
|
+
*/
|
|
190
|
+
export class BadRequestError extends AppError {
|
|
191
|
+
constructor(message: string, context: ErrorContext | null = null) {
|
|
192
|
+
super(message, 400, {
|
|
193
|
+
isOperational: true,
|
|
194
|
+
code: "BAD_REQUEST",
|
|
195
|
+
context,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
this.name = "BadRequestError";
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Internal Server Error
|
|
204
|
+
*/
|
|
205
|
+
export class InternalServerError extends AppError {
|
|
206
|
+
constructor(
|
|
207
|
+
message = "Internal server error",
|
|
208
|
+
cause: Error | null = null,
|
|
209
|
+
context: ErrorContext | null = null,
|
|
210
|
+
) {
|
|
211
|
+
super(message, 500, {
|
|
212
|
+
isOperational: false,
|
|
213
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
214
|
+
cause,
|
|
215
|
+
context,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
this.name = "InternalServerError";
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { env } from "../config/env.ts";
|
|
2
|
-
|
|
3
|
-
type LogLevel = "debug" | "info" | "warn" | "error" | "fatal";
|
|
4
|
-
|
|
5
|
-
const COLORS = {
|
|
6
|
-
reset: "\x1b[0m",
|
|
7
|
-
red: "\x1b[31m",
|
|
8
|
-
yellow: "\x1b[33m",
|
|
9
|
-
green: "\x1b[32m",
|
|
10
|
-
blue: "\x1b[36m",
|
|
11
|
-
gray: "\x1b[90m",
|
|
12
|
-
magenta: "\x1b[35m",
|
|
13
|
-
} as const;
|
|
14
|
-
|
|
15
|
-
const LOG_LEVELS: Record<LogLevel, number> = {
|
|
16
|
-
debug: 0,
|
|
17
|
-
info: 1,
|
|
18
|
-
warn: 2,
|
|
19
|
-
error: 3,
|
|
20
|
-
fatal: 4,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const getCurrentLogLevel = (): number =>
|
|
24
|
-
LOG_LEVELS[env.LOG_LEVEL as LogLevel] ?? LOG_LEVELS.info;
|
|
25
|
-
|
|
26
|
-
const formatLog = (
|
|
27
|
-
level: LogLevel,
|
|
28
|
-
message: string,
|
|
29
|
-
data?: unknown,
|
|
30
|
-
): string => {
|
|
31
|
-
const timestamp = new Date().toISOString();
|
|
32
|
-
const dataStr = data ? ` ${JSON.stringify(data)}` : "";
|
|
33
|
-
return `[${timestamp}] ${level.toUpperCase()}: ${message}${dataStr}`;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const formatStack = (stack?: string): string => (stack ? `\n${stack}` : "");
|
|
37
|
-
|
|
38
|
-
const log =
|
|
39
|
-
(level: LogLevel, color: string, consoleFn: (...args: unknown[]) => void) =>
|
|
40
|
-
(message: string, data?: unknown, stack?: string): void => {
|
|
41
|
-
if (getCurrentLogLevel() <= LOG_LEVELS[level]) {
|
|
42
|
-
const stackTrace = formatStack(stack);
|
|
43
|
-
consoleFn(
|
|
44
|
-
`${color}${formatLog(level, message, data)}${stackTrace}${COLORS.reset}`,
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export const logger = {
|
|
50
|
-
debug: log("debug", COLORS.gray, console.log),
|
|
51
|
-
info: log("info", COLORS.blue, console.log),
|
|
52
|
-
warn: log("warn", COLORS.yellow, console.warn),
|
|
53
|
-
error: log("error", COLORS.red, console.error),
|
|
54
|
-
fatal: log("fatal", `${COLORS.red}${COLORS.magenta}`, console.error),
|
|
55
|
-
};
|
|
1
|
+
import { env } from "../config/env.ts";
|
|
2
|
+
|
|
3
|
+
type LogLevel = "debug" | "info" | "warn" | "error" | "fatal";
|
|
4
|
+
|
|
5
|
+
const COLORS = {
|
|
6
|
+
reset: "\x1b[0m",
|
|
7
|
+
red: "\x1b[31m",
|
|
8
|
+
yellow: "\x1b[33m",
|
|
9
|
+
green: "\x1b[32m",
|
|
10
|
+
blue: "\x1b[36m",
|
|
11
|
+
gray: "\x1b[90m",
|
|
12
|
+
magenta: "\x1b[35m",
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
const LOG_LEVELS: Record<LogLevel, number> = {
|
|
16
|
+
debug: 0,
|
|
17
|
+
info: 1,
|
|
18
|
+
warn: 2,
|
|
19
|
+
error: 3,
|
|
20
|
+
fatal: 4,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getCurrentLogLevel = (): number =>
|
|
24
|
+
LOG_LEVELS[env.LOG_LEVEL as LogLevel] ?? LOG_LEVELS.info;
|
|
25
|
+
|
|
26
|
+
const formatLog = (
|
|
27
|
+
level: LogLevel,
|
|
28
|
+
message: string,
|
|
29
|
+
data?: unknown,
|
|
30
|
+
): string => {
|
|
31
|
+
const timestamp = new Date().toISOString();
|
|
32
|
+
const dataStr = data ? ` ${JSON.stringify(data)}` : "";
|
|
33
|
+
return `[${timestamp}] ${level.toUpperCase()}: ${message}${dataStr}`;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const formatStack = (stack?: string): string => (stack ? `\n${stack}` : "");
|
|
37
|
+
|
|
38
|
+
const log =
|
|
39
|
+
(level: LogLevel, color: string, consoleFn: (...args: unknown[]) => void) =>
|
|
40
|
+
(message: string, data?: unknown, stack?: string): void => {
|
|
41
|
+
if (getCurrentLogLevel() <= LOG_LEVELS[level]) {
|
|
42
|
+
const stackTrace = formatStack(stack);
|
|
43
|
+
consoleFn(
|
|
44
|
+
`${color}${formatLog(level, message, data)}${stackTrace}${COLORS.reset}`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const logger = {
|
|
50
|
+
debug: log("debug", COLORS.gray, console.log),
|
|
51
|
+
info: log("info", COLORS.blue, console.log),
|
|
52
|
+
warn: log("warn", COLORS.yellow, console.warn),
|
|
53
|
+
error: log("error", COLORS.red, console.error),
|
|
54
|
+
fatal: log("fatal", `${COLORS.red}${COLORS.magenta}`, console.error),
|
|
55
|
+
};
|