create-charcole 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.
@@ -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,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
+ #!/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);