@mxweb/core 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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +61 -0
  3. package/dist/application.d.ts +402 -0
  4. package/dist/application.js +1 -0
  5. package/dist/application.mjs +1 -0
  6. package/dist/common.d.ts +323 -0
  7. package/dist/common.js +1 -0
  8. package/dist/common.mjs +1 -0
  9. package/dist/config.d.ts +258 -0
  10. package/dist/config.js +1 -0
  11. package/dist/config.mjs +1 -0
  12. package/dist/context.d.ts +48 -0
  13. package/dist/context.js +1 -0
  14. package/dist/context.mjs +1 -0
  15. package/dist/controller.d.ts +238 -0
  16. package/dist/controller.js +1 -0
  17. package/dist/controller.mjs +1 -0
  18. package/dist/decorator.d.ts +349 -0
  19. package/dist/decorator.js +1 -0
  20. package/dist/decorator.mjs +1 -0
  21. package/dist/error.d.ts +301 -0
  22. package/dist/error.js +1 -0
  23. package/dist/error.mjs +1 -0
  24. package/dist/execute.d.ts +469 -0
  25. package/dist/execute.js +1 -0
  26. package/dist/execute.mjs +1 -0
  27. package/dist/feature.d.ts +239 -0
  28. package/dist/feature.js +1 -0
  29. package/dist/feature.mjs +1 -0
  30. package/dist/hooks.d.ts +251 -0
  31. package/dist/hooks.js +1 -0
  32. package/dist/hooks.mjs +1 -0
  33. package/dist/index.d.ts +14 -0
  34. package/dist/index.js +1 -0
  35. package/dist/index.mjs +1 -0
  36. package/dist/logger.d.ts +360 -0
  37. package/dist/logger.js +1 -0
  38. package/dist/logger.mjs +1 -0
  39. package/dist/response.d.ts +665 -0
  40. package/dist/response.js +1 -0
  41. package/dist/response.mjs +1 -0
  42. package/dist/route.d.ts +298 -0
  43. package/dist/route.js +1 -0
  44. package/dist/route.mjs +1 -0
  45. package/dist/router.d.ts +205 -0
  46. package/dist/router.js +1 -0
  47. package/dist/router.mjs +1 -0
  48. package/dist/service.d.ts +261 -0
  49. package/dist/service.js +1 -0
  50. package/dist/service.mjs +1 -0
  51. package/package.json +168 -0
@@ -0,0 +1,360 @@
1
+ /**
2
+ * @fileoverview Colorful logging utility for development and debugging.
3
+ *
4
+ * This module provides a Logger class with ANSI color support for server-side
5
+ * console output. Logging is automatically disabled in production environment
6
+ * or when DEBUG=false.
7
+ *
8
+ * Features:
9
+ * - Color-coded log levels (warn, log, error, info, debug)
10
+ * - HTTP request logging with status code coloring
11
+ * - Timestamp logging
12
+ * - JSON pretty-printing
13
+ * - Console grouping and table output
14
+ * - Time measurement utilities
15
+ *
16
+ * @module logger
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { logger, Logger } from "@mxweb/core";
21
+ *
22
+ * // Use the default singleton instance
23
+ * logger.info("Application started");
24
+ * logger.error("Something went wrong", error);
25
+ *
26
+ * // Create a custom logger with prefix
27
+ * const myLogger = Logger.create("MyModule");
28
+ * myLogger.debug("Debug message");
29
+ * ```
30
+ */
31
+ /**
32
+ * Available log levels for the Logger.
33
+ * Each level corresponds to a console method and has an associated color.
34
+ */
35
+ type LogLevel = "warn" | "log" | "error" | "info" | "debug";
36
+ /**
37
+ * A colorful, feature-rich logging utility for server-side development.
38
+ *
39
+ * The Logger class provides structured logging with ANSI color support,
40
+ * automatic environment-based filtering, and various output formats.
41
+ *
42
+ * Logging is enabled when:
43
+ * - NODE_ENV !== "production" AND
44
+ * - DEBUG !== "false"
45
+ *
46
+ * @remarks
47
+ * Use the singleton `logger` export for most cases.
48
+ * Create custom instances with `Logger.create()` for module-specific prefixes.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * // Singleton usage
53
+ * import { logger } from "@mxweb/core";
54
+ *
55
+ * logger.info("Server starting...");
56
+ * logger.warn("Deprecated API used");
57
+ * logger.error("Failed to connect", error);
58
+ * logger.debug("Variable value:", someVar);
59
+ *
60
+ * // HTTP logging
61
+ * logger.http("GET", "/api/users", 200, 1024, 50);
62
+ *
63
+ * // Timing
64
+ * logger.time("database-query");
65
+ * await queryDatabase();
66
+ * logger.timeEnd("database-query");
67
+ *
68
+ * // JSON output
69
+ * logger.json("info", "User data", { id: 1, name: "John" });
70
+ * ```
71
+ */
72
+ export declare class Logger {
73
+ /** Singleton instance for the default logger */
74
+ private static _instance;
75
+ /** The prefix shown in log messages (e.g., "[MyModule]") */
76
+ private readonly prefix;
77
+ /** Whether logging is enabled based on environment */
78
+ private readonly allowLog;
79
+ /**
80
+ * Private constructor to enforce singleton pattern for default instance.
81
+ * Use `Logger.instance` or `Logger.create()` to get logger instances.
82
+ *
83
+ * @param prefix - The prefix to show in log messages
84
+ */
85
+ private constructor();
86
+ /**
87
+ * Gets the singleton Logger instance with the default "@mxweb" prefix.
88
+ *
89
+ * @returns The singleton Logger instance
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * const log = Logger.instance;
94
+ * log.info("Using singleton logger");
95
+ * ```
96
+ */
97
+ static get instance(): Logger;
98
+ /**
99
+ * Creates a new Logger instance with a custom prefix.
100
+ *
101
+ * @param prefix - The prefix to show in log messages
102
+ * @returns A new Logger instance
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * const dbLogger = Logger.create("Database");
107
+ * dbLogger.info("Connected"); // Output: [Database] Connected
108
+ *
109
+ * const authLogger = Logger.create("Auth");
110
+ * authLogger.warn("Token expired"); // Output: [Auth] Token expired
111
+ * ```
112
+ */
113
+ static create(prefix: string): Logger;
114
+ /**
115
+ * Checks if logging is allowed in the current environment.
116
+ *
117
+ * @returns true if logging is enabled
118
+ * @internal
119
+ */
120
+ private isAllowLog;
121
+ /**
122
+ * Formats the prefix with the appropriate ANSI color for the log level.
123
+ *
124
+ * @param level - The log level to get color for
125
+ * @returns The formatted prefix string with color codes
126
+ * @internal
127
+ */
128
+ private formatPrefix;
129
+ /**
130
+ * Internal method that performs the actual console output.
131
+ *
132
+ * @param level - The log level for coloring
133
+ * @param method - The console method to use
134
+ * @param message - The primary message to log
135
+ * @param optionalParams - Additional parameters to log
136
+ * @internal
137
+ */
138
+ private output;
139
+ /**
140
+ * Logs a warning message in yellow.
141
+ *
142
+ * @param message - The warning message
143
+ * @param optionalParams - Additional parameters to log
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * logger.warn("Deprecated API used");
148
+ * logger.warn("Rate limit approaching:", { current: 95, limit: 100 });
149
+ * ```
150
+ */
151
+ warn(message?: any, ...optionalParams: any[]): void;
152
+ /**
153
+ * Logs a general message in cyan.
154
+ *
155
+ * @param message - The message to log
156
+ * @param optionalParams - Additional parameters to log
157
+ *
158
+ * @example
159
+ * ```ts
160
+ * logger.log("Processing request...");
161
+ * logger.log("Items:", items.length);
162
+ * ```
163
+ */
164
+ log(message?: any, ...optionalParams: any[]): void;
165
+ /**
166
+ * Logs an error message in red.
167
+ *
168
+ * @param message - The error message
169
+ * @param optionalParams - Additional parameters (often an Error object)
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * logger.error("Failed to connect to database");
174
+ * logger.error("Unhandled exception:", error);
175
+ * ```
176
+ */
177
+ error(message?: any, ...optionalParams: any[]): void;
178
+ /**
179
+ * Logs an informational message in green.
180
+ *
181
+ * @param message - The info message
182
+ * @param optionalParams - Additional parameters to log
183
+ *
184
+ * @example
185
+ * ```ts
186
+ * logger.info("Server started on port 3000");
187
+ * logger.info("User logged in:", userId);
188
+ * ```
189
+ */
190
+ info(message?: any, ...optionalParams: any[]): void;
191
+ /**
192
+ * Logs a debug message in blue.
193
+ * Useful for development-only detailed logging.
194
+ *
195
+ * @param message - The debug message
196
+ * @param optionalParams - Additional parameters to log
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * logger.debug("Variable state:", { x: 1, y: 2 });
201
+ * logger.debug("Entering function processData");
202
+ * ```
203
+ */
204
+ debug(message?: any, ...optionalParams: any[]): void;
205
+ /**
206
+ * Logs an HTTP request with color-coded status.
207
+ *
208
+ * Status code colors:
209
+ * - 5xx: Red (server error)
210
+ * - 4xx: Yellow (client error)
211
+ * - 3xx: Cyan (redirect)
212
+ * - 2xx: Green (success)
213
+ *
214
+ * @param method - The HTTP method (GET, POST, etc.)
215
+ * @param path - The request path
216
+ * @param statusCode - The HTTP response status code
217
+ * @param responseSize - The response body size in bytes
218
+ * @param duration - The request duration in milliseconds
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * logger.http("GET", "/api/users", 200, 1024, 50);
223
+ * // Output: [mxweb] GET /api/users 200 - 1024 bytes in 50ms
224
+ *
225
+ * logger.http("POST", "/api/login", 401, 128, 25);
226
+ * // Output: [mxweb] POST /api/login 401 - 128 bytes in 25ms (yellow status)
227
+ * ```
228
+ */
229
+ http(method: string, path: string, statusCode: number, responseSize: number, duration: number): void;
230
+ /**
231
+ * Logs a message with an ISO timestamp prefix.
232
+ *
233
+ * @param level - The log level to use
234
+ * @param message - The message to log
235
+ * @param optionalParams - Additional parameters to log
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * logger.timestamp("info", "Server started");
240
+ * // Output: [mxweb] [2024-01-15T10:30:00.000Z] Server started
241
+ * ```
242
+ */
243
+ timestamp(level: LogLevel, message?: any, ...optionalParams: any[]): void;
244
+ /**
245
+ * Logs an object as pretty-printed JSON.
246
+ *
247
+ * @param level - The log level to use
248
+ * @param label - A label to identify the data
249
+ * @param data - The data to stringify and log
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * logger.json("debug", "User object", { id: 1, name: "John", roles: ["admin"] });
254
+ * // Output:
255
+ * // [mxweb] User object:
256
+ * // {
257
+ * // "id": 1,
258
+ * // "name": "John",
259
+ * // "roles": ["admin"]
260
+ * // }
261
+ * ```
262
+ */
263
+ json(level: LogLevel, label: string, data: unknown): void;
264
+ /**
265
+ * Logs a visual separator line.
266
+ * Useful for separating log sections.
267
+ *
268
+ * @param char - The character to repeat (default: "-")
269
+ * @param length - The number of repetitions (default: 50)
270
+ *
271
+ * @example
272
+ * ```ts
273
+ * logger.separator();
274
+ * // Output: --------------------------------------------------
275
+ *
276
+ * logger.separator("=", 30);
277
+ * // Output: ==============================
278
+ * ```
279
+ */
280
+ separator(char?: string, length?: number): void;
281
+ /**
282
+ * Logs messages in a collapsible group.
283
+ *
284
+ * @param label - The group label
285
+ * @param fn - Function containing log statements to group
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * logger.group("Request Details", () => {
290
+ * logger.info("Method: GET");
291
+ * logger.info("Path: /api/users");
292
+ * logger.info("Headers:", headers);
293
+ * });
294
+ * ```
295
+ */
296
+ group(label: string, fn: () => void): void;
297
+ /**
298
+ * Logs data in a tabular format.
299
+ *
300
+ * @param data - Array of objects to display as a table
301
+ * @param columns - Optional array of column names to include
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * logger.table([
306
+ * { id: 1, name: "Alice", role: "admin" },
307
+ * { id: 2, name: "Bob", role: "user" },
308
+ * ]);
309
+ *
310
+ * logger.table(users, ["name", "email"]);
311
+ * ```
312
+ */
313
+ table(data: any[], columns?: string[]): void;
314
+ /**
315
+ * Starts a timer with the given label.
316
+ * Use with `timeEnd()` to measure elapsed time.
317
+ *
318
+ * @param label - The timer label (must match in timeEnd)
319
+ *
320
+ * @example
321
+ * ```ts
322
+ * logger.time("database-query");
323
+ * const result = await db.query("SELECT * FROM users");
324
+ * logger.timeEnd("database-query");
325
+ * // Output: [mxweb] database-query: 45.123ms
326
+ * ```
327
+ */
328
+ time(label: string): void;
329
+ /**
330
+ * Stops a timer and logs the elapsed time.
331
+ *
332
+ * @param label - The timer label (must match the one used in time())
333
+ *
334
+ * @example
335
+ * ```ts
336
+ * logger.time("operation");
337
+ * await someOperation();
338
+ * logger.timeEnd("operation");
339
+ * // Output: [mxweb] operation: 123.456ms
340
+ * ```
341
+ */
342
+ timeEnd(label: string): void;
343
+ }
344
+ /**
345
+ * The default singleton Logger instance.
346
+ *
347
+ * This is the recommended way to use the logger for most cases.
348
+ * The singleton uses "@mxweb" as the default prefix.
349
+ *
350
+ * @example
351
+ * ```ts
352
+ * import { logger } from "@mxweb/core";
353
+ *
354
+ * logger.info("Application initialized");
355
+ * logger.warn("Configuration missing, using defaults");
356
+ * logger.error("Failed to start:", error);
357
+ * ```
358
+ */
359
+ export declare const logger: Logger;
360
+ export {};
package/dist/logger.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var t=require("@mxweb/utils");const o={warn:"",log:"",error:"",info:"",debug:"",reset:""};class r{constructor(o="@mxweb"){this.prefix=o,this.allowLog="production"!==t.getEnv("NODE_ENV")&&"false"!==t.getEnv("DEBUG")}static get instance(){return r._instance||(r._instance=new r),r._instance}static create(t){return new r(t)}isAllowLog(){return this.allowLog}formatPrefix(t){return`${o[t]}[${this.prefix}]${o.reset}`}output(t,o,r,...i){if(!this.isAllowLog())return;this.formatPrefix(t)}warn(t,...o){this.output("warn","warn",t,...o)}log(t,...o){this.output("log","log",t,...o)}error(t,...o){this.output("error","error",t,...o)}info(t,...o){this.output("info","info",t,...o)}debug(t,...o){this.output("debug","debug",t,...o)}http(t,r,i,e,s){const n=`${t} ${r} ${i>=500?o.error:i>=400?o.warn:i>=300?o.log:o.info}${i}${o.reset} - ${e} bytes in ${s}ms`;this.output("info","info",n)}timestamp(t,o,...r){if(!this.isAllowLog())return;(new Date).toISOString(),this.formatPrefix(t)}json(t,o,r){if(!this.isAllowLog())return;this.formatPrefix(t)}separator(t="-",o=50){this.isAllowLog()}group(t,o){if(!this.isAllowLog())return;this.formatPrefix("info");o()}table(t,o){this.isAllowLog()}time(t){this.isAllowLog()}timeEnd(t){this.isAllowLog()}}const i=r.instance;exports.Logger=r,exports.logger=i;
@@ -0,0 +1 @@
1
+ import{getEnv as t}from"@mxweb/utils";const o={warn:"",log:"",error:"",info:"",debug:"",reset:""};class i{constructor(o="@mxweb"){this.prefix=o,this.allowLog="production"!==t("NODE_ENV")&&"false"!==t("DEBUG")}static get instance(){return i._instance||(i._instance=new i),i._instance}static create(t){return new i(t)}isAllowLog(){return this.allowLog}formatPrefix(t){return`${o[t]}[${this.prefix}]${o.reset}`}output(t,o,i,...r){if(!this.isAllowLog())return;this.formatPrefix(t)}warn(t,...o){this.output("warn","warn",t,...o)}log(t,...o){this.output("log","log",t,...o)}error(t,...o){this.output("error","error",t,...o)}info(t,...o){this.output("info","info",t,...o)}debug(t,...o){this.output("debug","debug",t,...o)}http(t,i,r,s,e){const n=`${t} ${i} ${r>=500?o.error:r>=400?o.warn:r>=300?o.log:o.info}${r}${o.reset} - ${s} bytes in ${e}ms`;this.output("info","info",n)}timestamp(t,o,...i){if(!this.isAllowLog())return;(new Date).toISOString(),this.formatPrefix(t)}json(t,o,i){if(!this.isAllowLog())return;this.formatPrefix(t)}separator(t="-",o=50){this.isAllowLog()}group(t,o){if(!this.isAllowLog())return;this.formatPrefix("info");o()}table(t,o){this.isAllowLog()}time(t){this.isAllowLog()}timeEnd(t){this.isAllowLog()}}const r=i.instance;export{i as Logger,r as logger};