create-charcole 1.1.0 → 2.0.1
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 -0
- package/CHANGELOG.md +25 -0
- package/README.md +11 -1
- package/bin/index.js +90 -71
- package/bin/lib/pkgManager.js +66 -0
- package/bin/lib/templateHandler.js +70 -0
- package/package.json +4 -1
- package/template/js/basePackage.json +28 -0
- package/template/{package-lock.json → js/package-lock.json} +1253 -1253
- package/template/{package.json → js/package.json} +28 -28
- package/template/ts/.env.example +8 -0
- package/template/ts/ARCHITECTURE_DIAGRAMS.md +283 -0
- package/template/ts/CHECKLIST.md +279 -0
- package/template/ts/COMPLETE.md +405 -0
- package/template/ts/ERROR_HANDLING.md +393 -0
- package/template/ts/IMPLEMENTATION.md +368 -0
- package/template/ts/IMPLEMENTATION_COMPLETE.md +363 -0
- package/template/ts/INDEX.md +290 -0
- package/template/ts/QUICK_REFERENCE.md +270 -0
- package/template/ts/README.md +855 -0
- package/template/ts/basePackage.json +36 -0
- package/template/ts/package-lock.json +2428 -0
- package/template/ts/package.json +32 -0
- package/template/ts/src/app.js +75 -0
- package/template/ts/src/app.ts +66 -0
- package/template/ts/src/config/constants.js +20 -0
- package/template/ts/src/config/constants.ts +27 -0
- package/template/ts/src/config/env.js +26 -0
- package/template/ts/src/config/env.ts +40 -0
- package/template/ts/src/middlewares/errorHandler.js +180 -0
- package/template/ts/src/middlewares/errorHandler.ts +209 -0
- package/template/ts/src/middlewares/requestLogger.js +33 -0
- package/template/ts/src/middlewares/requestLogger.ts +38 -0
- package/template/ts/src/middlewares/validateRequest.js +42 -0
- package/template/ts/src/middlewares/validateRequest.ts +46 -0
- package/template/ts/src/modules/health/controller.js +50 -0
- package/template/ts/src/modules/health/controller.ts +64 -0
- package/template/ts/src/routes.js +17 -0
- package/template/ts/src/routes.ts +16 -0
- package/template/ts/src/server.js +38 -0
- package/template/ts/src/server.ts +42 -0
- package/template/ts/src/types/express.d.ts +9 -0
- package/template/ts/src/utils/AppError.js +182 -0
- package/template/ts/src/utils/AppError.ts +220 -0
- package/template/ts/src/utils/logger.js +73 -0
- package/template/ts/src/utils/logger.ts +55 -0
- package/template/ts/src/utils/response.js +51 -0
- package/template/ts/src/utils/response.ts +100 -0
- package/template/ts/test-api.js +100 -0
- package/template/ts/tsconfig.json +19 -0
- /package/template/{.env.example → js/.env.example} +0 -0
- /package/template/{ARCHITECTURE_DIAGRAMS.md → js/ARCHITECTURE_DIAGRAMS.md} +0 -0
- /package/template/{CHECKLIST.md → js/CHECKLIST.md} +0 -0
- /package/template/{COMPLETE.md → js/COMPLETE.md} +0 -0
- /package/template/{ERROR_HANDLING.md → js/ERROR_HANDLING.md} +0 -0
- /package/template/{IMPLEMENTATION.md → js/IMPLEMENTATION.md} +0 -0
- /package/template/{IMPLEMENTATION_COMPLETE.md → js/IMPLEMENTATION_COMPLETE.md} +0 -0
- /package/template/{INDEX.md → js/INDEX.md} +0 -0
- /package/template/{QUICK_REFERENCE.md → js/QUICK_REFERENCE.md} +0 -0
- /package/template/{README.md → js/README.md} +0 -0
- /package/template/{src → js/src}/app.js +0 -0
- /package/template/{src → js/src}/config/constants.js +0 -0
- /package/template/{src → js/src}/config/env.js +0 -0
- /package/template/{src → js/src}/middlewares/errorHandler.js +0 -0
- /package/template/{src → js/src}/middlewares/requestLogger.js +0 -0
- /package/template/{src → js/src}/middlewares/validateRequest.js +0 -0
- /package/template/{src → js/src}/modules/health/controller.js +0 -0
- /package/template/{src → js/src}/routes.js +0 -0
- /package/template/{src → js/src}/server.js +0 -0
- /package/template/{src → js/src}/utils/AppError.js +0 -0
- /package/template/{src → js/src}/utils/logger.js +0 -0
- /package/template/{src → js/src}/utils/response.js +0 -0
- /package/template/{test-api.js → js/test-api.js} +0 -0
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { env } from "../config/env.js";
|
|
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
|
+
};
|
|
@@ -0,0 +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
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { Response } from "express";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Standard success response shape
|
|
5
|
+
*/
|
|
6
|
+
export type SuccessResponse<T> = {
|
|
7
|
+
success: true;
|
|
8
|
+
message: string;
|
|
9
|
+
data: T;
|
|
10
|
+
timestamp: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Send success response
|
|
15
|
+
*/
|
|
16
|
+
export const sendSuccess = <T>(
|
|
17
|
+
res: Response,
|
|
18
|
+
data: T,
|
|
19
|
+
statusCode = 200,
|
|
20
|
+
message = "Success",
|
|
21
|
+
): Response<SuccessResponse<T>> => {
|
|
22
|
+
return res.status(statusCode).json({
|
|
23
|
+
success: true,
|
|
24
|
+
message,
|
|
25
|
+
data,
|
|
26
|
+
timestamp: new Date().toISOString(),
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Error response shape (legacy)
|
|
32
|
+
*/
|
|
33
|
+
type ErrorResponse = {
|
|
34
|
+
success: false;
|
|
35
|
+
message: string;
|
|
36
|
+
errors?: unknown;
|
|
37
|
+
timestamp: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Send error response (DEPRECATED — use AppError instead)
|
|
42
|
+
* Kept for backward compatibility
|
|
43
|
+
*/
|
|
44
|
+
export const sendError = (
|
|
45
|
+
res: Response,
|
|
46
|
+
message: string,
|
|
47
|
+
statusCode = 500,
|
|
48
|
+
errors: unknown = null,
|
|
49
|
+
): Response<ErrorResponse> => {
|
|
50
|
+
return res.status(statusCode).json({
|
|
51
|
+
success: false,
|
|
52
|
+
message,
|
|
53
|
+
...(errors !== null && { errors }),
|
|
54
|
+
timestamp: new Date().toISOString(),
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Validation error item (Zod / Joi–style compatible)
|
|
60
|
+
*/
|
|
61
|
+
type ValidationIssue = {
|
|
62
|
+
path: (string | number)[];
|
|
63
|
+
message: string;
|
|
64
|
+
code?: string;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Validation error response shape
|
|
69
|
+
*/
|
|
70
|
+
type ValidationErrorResponse = {
|
|
71
|
+
success: false;
|
|
72
|
+
message: string;
|
|
73
|
+
errors: {
|
|
74
|
+
field: string;
|
|
75
|
+
message: string;
|
|
76
|
+
code?: string;
|
|
77
|
+
}[];
|
|
78
|
+
timestamp: string;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Send validation error response (DEPRECATED — use ValidationError instead)
|
|
83
|
+
* Kept for backward compatibility
|
|
84
|
+
*/
|
|
85
|
+
export const sendValidationError = (
|
|
86
|
+
res: Response,
|
|
87
|
+
errors: ValidationIssue[],
|
|
88
|
+
statusCode = 422,
|
|
89
|
+
): Response<ValidationErrorResponse> => {
|
|
90
|
+
return res.status(statusCode).json({
|
|
91
|
+
success: false,
|
|
92
|
+
message: "Validation failed",
|
|
93
|
+
errors: errors.map((err) => ({
|
|
94
|
+
field: err.path.join("."),
|
|
95
|
+
message: err.message,
|
|
96
|
+
code: err.code,
|
|
97
|
+
})),
|
|
98
|
+
timestamp: new Date().toISOString(),
|
|
99
|
+
});
|
|
100
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import http from "http";
|
|
4
|
+
|
|
5
|
+
const tests = [
|
|
6
|
+
{
|
|
7
|
+
name: "Test 1: GET / (root)",
|
|
8
|
+
method: "GET",
|
|
9
|
+
path: "/",
|
|
10
|
+
body: null,
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: "Test 2: GET /api/health",
|
|
14
|
+
method: "GET",
|
|
15
|
+
path: "/api/health",
|
|
16
|
+
body: null,
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: "Test 3: POST /api/items (valid)",
|
|
20
|
+
method: "POST",
|
|
21
|
+
path: "/api/items",
|
|
22
|
+
body: JSON.stringify({ name: "Test Item", description: "A test item" }),
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "Test 4: POST /api/items (invalid - missing name)",
|
|
26
|
+
method: "POST",
|
|
27
|
+
path: "/api/items",
|
|
28
|
+
body: JSON.stringify({ description: "No name" }),
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: "Test 5: GET /api/nonexistent (404)",
|
|
32
|
+
method: "GET",
|
|
33
|
+
path: "/api/nonexistent",
|
|
34
|
+
body: null,
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const runTest = (test) => {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
const options = {
|
|
41
|
+
hostname: "localhost",
|
|
42
|
+
port: 3000,
|
|
43
|
+
path: test.path,
|
|
44
|
+
method: test.method,
|
|
45
|
+
headers: {
|
|
46
|
+
"Content-Type": "application/json",
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const req = http.request(options, (res) => {
|
|
51
|
+
let data = "";
|
|
52
|
+
|
|
53
|
+
res.on("data", (chunk) => {
|
|
54
|
+
data += chunk;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
res.on("end", () => {
|
|
58
|
+
try {
|
|
59
|
+
const json = JSON.parse(data);
|
|
60
|
+
console.log(`\n✅ ${test.name}`);
|
|
61
|
+
console.log(` Status: ${res.statusCode}`);
|
|
62
|
+
console.log(` Response:`, JSON.stringify(json, null, 2));
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.log(`\n✅ ${test.name}`);
|
|
65
|
+
console.log(` Status: ${res.statusCode}`);
|
|
66
|
+
console.log(` Response:`, data);
|
|
67
|
+
}
|
|
68
|
+
resolve();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
req.on("error", (error) => {
|
|
73
|
+
console.error(`\n❌ ${test.name}`);
|
|
74
|
+
console.error(` Error: ${error.message}`);
|
|
75
|
+
resolve();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (test.body) {
|
|
79
|
+
req.write(test.body);
|
|
80
|
+
}
|
|
81
|
+
req.end();
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const runTests = async () => {
|
|
86
|
+
console.log("🚀 Testing Charcole API Error Handling\n");
|
|
87
|
+
console.log("=".repeat(60));
|
|
88
|
+
|
|
89
|
+
for (const test of tests) {
|
|
90
|
+
await runTest(test);
|
|
91
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
console.log("\n" + "=".repeat(60));
|
|
95
|
+
console.log("\n✅ All tests completed!");
|
|
96
|
+
process.exit(0);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Wait for server to be ready
|
|
100
|
+
setTimeout(runTests, 1000);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"allowJs": false,
|
|
14
|
+
"noEmitOnError": true,
|
|
15
|
+
"sourceMap": true
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*.ts"],
|
|
18
|
+
"exclude": ["node_modules", "dist"]
|
|
19
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|