@catbee/utils 0.0.8-rc.2 → 0.0.8-rc.4

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 (66) hide show
  1. package/build/index.cjs +6591 -0
  2. package/build/index.d.ts +5131 -0
  3. package/build/index.mjs +6321 -0
  4. package/package.json +12 -36
  5. package/build/esm/config.d.ts +0 -121
  6. package/build/esm/config.js +0 -132
  7. package/build/esm/index.d.ts +0 -26
  8. package/build/esm/index.js +0 -49
  9. package/build/esm/servers/server.builder.d.ts +0 -507
  10. package/build/esm/servers/server.builder.js +0 -654
  11. package/build/esm/servers/server.d.ts +0 -255
  12. package/build/esm/servers/server.js +0 -963
  13. package/build/esm/types/api-response.d.ts +0 -151
  14. package/build/esm/types/api-response.js +0 -33
  15. package/build/esm/types/index.d.ts +0 -124
  16. package/build/esm/types/index.js +0 -24
  17. package/build/esm/types/server.d.ts +0 -267
  18. package/build/esm/types/server.js +0 -24
  19. package/build/esm/utils/array.utils.d.ts +0 -167
  20. package/build/esm/utils/array.utils.js +0 -344
  21. package/build/esm/utils/async.utils.d.ts +0 -264
  22. package/build/esm/utils/async.utils.js +0 -619
  23. package/build/esm/utils/cache.utils.d.ts +0 -152
  24. package/build/esm/utils/cache.utils.js +0 -294
  25. package/build/esm/utils/context-store.utils.d.ts +0 -188
  26. package/build/esm/utils/context-store.utils.js +0 -290
  27. package/build/esm/utils/crypto.utils.d.ts +0 -159
  28. package/build/esm/utils/crypto.utils.js +0 -278
  29. package/build/esm/utils/date.utils.d.ts +0 -158
  30. package/build/esm/utils/date.utils.js +0 -383
  31. package/build/esm/utils/decorators.utils.d.ts +0 -511
  32. package/build/esm/utils/decorators.utils.js +0 -1013
  33. package/build/esm/utils/dir.utils.d.ts +0 -195
  34. package/build/esm/utils/dir.utils.js +0 -476
  35. package/build/esm/utils/env.utils.d.ts +0 -376
  36. package/build/esm/utils/env.utils.js +0 -782
  37. package/build/esm/utils/exception.utils.d.ts +0 -229
  38. package/build/esm/utils/exception.utils.js +0 -382
  39. package/build/esm/utils/fs.utils.d.ts +0 -163
  40. package/build/esm/utils/fs.utils.js +0 -348
  41. package/build/esm/utils/http-status-codes.d.ts +0 -265
  42. package/build/esm/utils/http-status-codes.js +0 -294
  43. package/build/esm/utils/id.utils.d.ts +0 -35
  44. package/build/esm/utils/id.utils.js +0 -83
  45. package/build/esm/utils/logger.utils.d.ts +0 -159
  46. package/build/esm/utils/logger.utils.js +0 -304
  47. package/build/esm/utils/middleware.utils.d.ts +0 -99
  48. package/build/esm/utils/middleware.utils.js +0 -235
  49. package/build/esm/utils/obj.utils.d.ts +0 -123
  50. package/build/esm/utils/obj.utils.js +0 -412
  51. package/build/esm/utils/performance.utils.d.ts +0 -135
  52. package/build/esm/utils/performance.utils.js +0 -273
  53. package/build/esm/utils/request.utils.d.ts +0 -85
  54. package/build/esm/utils/request.utils.js +0 -190
  55. package/build/esm/utils/response.utils.d.ts +0 -162
  56. package/build/esm/utils/response.utils.js +0 -261
  57. package/build/esm/utils/stream.utils.d.ts +0 -87
  58. package/build/esm/utils/stream.utils.js +0 -209
  59. package/build/esm/utils/string.utils.d.ts +0 -92
  60. package/build/esm/utils/string.utils.js +0 -164
  61. package/build/esm/utils/type.utils.d.ts +0 -89
  62. package/build/esm/utils/type.utils.js +0 -186
  63. package/build/esm/utils/url.utils.d.ts +0 -140
  64. package/build/esm/utils/url.utils.js +0 -303
  65. package/build/esm/utils/validate.utils.d.ts +0 -176
  66. package/build/esm/utils/validate.utils.js +0 -319
@@ -1,255 +0,0 @@
1
- import express, { Express, Router } from 'express';
2
- import http from 'http';
3
- import https from 'https';
4
- import { ServerConfig, ServerHooks } from '../types/server';
5
- /**
6
- * Production-ready Express server with enterprise features.
7
- *
8
- * Core Features:
9
- * - Security: Helmet, CORS, rate limiting, timeouts
10
- * - Monitoring: Request logs, metrics, health checks
11
- * - Performance: Compression, caching, static files
12
- * - Reliability: Graceful shutdown, error handling
13
- * - Developer UX: OpenAPI docs, debugging tools
14
- * - Extensibility: Hooks, middleware, custom routes
15
- *
16
- * Designed for microservices and production workloads.
17
- * Includes K8s readiness probes and zero-downtime support.
18
- */
19
- export declare class ExpressServer {
20
- /** Prometheus client registry for metrics collection */
21
- private register;
22
- /** HTTP server instance (null when not running) */
23
- protected server: http.Server | https.Server | null;
24
- /** Merged configuration with defaults applied */
25
- protected config: ServerConfig;
26
- /** User-defined lifecycle hooks */
27
- protected hooks: ServerHooks;
28
- /** Global API prefix (from config) */
29
- protected globalPrefix: string;
30
- /** Internal fallback router */
31
- private rootRouter;
32
- /** User-supplied router */
33
- private externalRouter?;
34
- /** Internal Express app instance */
35
- private app;
36
- /** Set of active WebSocket connections */
37
- private connections;
38
- /** Flag indicating if the server is shutting down */
39
- private isShuttingDown;
40
- /**
41
- * Collection of registered health check functions.
42
- * These are executed when the health check endpoint is accessed.
43
- */
44
- private healthChecks;
45
- /** Prometheus metrics for monitoring */
46
- private requestCounter?;
47
- private routeTimings?;
48
- private requestSizes?;
49
- private clientIPs?;
50
- /** Promise that resolves when initialization (middleware + routes) is complete */
51
- private initPromise;
52
- /**
53
- * Initializes server with intelligent defaults and security best practices.
54
- * All settings can be customized via config and hooks.
55
- *
56
- * Default Security:
57
- * - Secure headers (Helmet)
58
- * - Rate limiting
59
- * - Request timeouts
60
- * - Body size limits
61
- * - CORS protection
62
- *
63
- * Default Monitoring:
64
- * - Request/Response logging
65
- * - Prometheus metrics
66
- * - Health checks
67
- * - Request tracing
68
- */
69
- constructor(config: Partial<ServerConfig>, hooks?: ServerHooks);
70
- /**
71
- * Execute a lifecycle hook safely with comprehensive error handling.
72
- * Prevents hook failures from crashing the server while logging issues.
73
- *
74
- * @param hook Name of the lifecycle hook to execute
75
- * @param args Arguments to pass to the hook function
76
- */
77
- private runHook;
78
- /**
79
- * Initialize the Express server with middleware and routes.
80
- */
81
- private initialize;
82
- /**
83
- * Configure and register all middlewares in the optimal order.
84
- *
85
- * Middleware Order (CRITICAL - don't change without understanding implications):
86
- * 1. Basic server configuration (trust proxy, x-powered-by)
87
- * 2. Request ID generation (for tracing)
88
- * 3. Request context setup (for logging correlation)
89
- * 4. Timeout protection (prevents hanging requests)
90
- * 5. Response time tracking (for performance monitoring)
91
- * 6. Request logging (after ID/context setup)
92
- * 7. Custom request hooks
93
- * 8. Security middleware (rate limiting, CORS, Helmet)
94
- * 9. Response compression
95
- * 10. Static file serving
96
- * 11. Request parsing (body parsing, cookies)
97
- * 12. API documentation (OpenAPI)
98
- * 13. Global headers
99
- * 14. Custom response hooks
100
- */
101
- protected setupMiddleware(): Promise<void>;
102
- /**
103
- * Configure server routes and error handling.
104
- * Sets up in following order:
105
- *
106
- * 1. Built-in routes (health, metrics)
107
- * 2. Application routes
108
- * 3. 404 handler
109
- * 4. Error handler
110
- */
111
- protected setupRoutes(): Promise<void>;
112
- /**
113
- * Register a new health check function for monitoring service dependencies.
114
- *
115
- * Health checks are executed when the health endpoint is accessed and
116
- * help determine if the service is ready to handle requests.
117
- *
118
- * Examples:
119
- * - Database connectivity
120
- * - External service availability
121
- * - File system access
122
- * - Memory/CPU usage checks
123
- *
124
- * @param name Unique identifier for the check (used in detailed responses)
125
- * @param check Function returning boolean or Promise<boolean> indicating health
126
- * @returns This instance for method chaining
127
- */
128
- registerHealthCheck(name: string, check: () => Promise<boolean> | boolean): this;
129
- /**
130
- * Get the underlying Express application instance.
131
- * Use this for advanced Express features not exposed by this wrapper.
132
- *
133
- * @returns The raw Express app instance
134
- */
135
- getApp(): Express;
136
- /**
137
- * Get the active HTTP/HTTPS server instance.
138
- * Returns null if the server is not currently running.
139
- *
140
- * @returns The HTTP/HTTPS server instance or null
141
- */
142
- getServer(): http.Server | https.Server | null;
143
- /**
144
- * Start the HTTP server and begin listening for requests.
145
- *
146
- * This method:
147
- * - Executes beforeStart hooks
148
- * - Binds to the configured host/port
149
- * - Sets up error handling for startup failures
150
- * - Executes afterStart hooks on success
151
- * - Logs startup information
152
- *
153
- * @returns Promise resolving to the running HTTP server instance
154
- * @throws Error if server fails to start or port is already in use
155
- */
156
- start(): Promise<http.Server | https.Server>;
157
- /**
158
- * Stop the HTTP server gracefully.
159
- *
160
- * This method:
161
- * - Executes beforeStop hooks
162
- * - Stops accepting new connections
163
- * - Waits for existing connections to finish
164
- * - Closes the server
165
- * - Executes afterStop hooks
166
- * - Logs shutdown information
167
- *
168
- * Graceful shutdown ensures:
169
- * - No requests are dropped
170
- * - Resources are properly cleaned up
171
- * - Monitoring systems are notified
172
- */
173
- stop(force?: boolean): Promise<void>;
174
- /**
175
- * Enable graceful shutdown on OS signals for production deployment.
176
- *
177
- * This is essential for:
178
- * - Container orchestration (Docker, Kubernetes)
179
- * - Process managers (PM2, systemd)
180
- * - Load balancer health checks
181
- * - Zero-downtime deployments
182
- *
183
- * @param signals Array of process signals to listen for (default: SIGINT, SIGTERM)
184
- */
185
- enableGracefulShutdown(signals?: NodeJS.Signals[]): this;
186
- /**
187
- * Set an externally created base router.
188
- * This will override the internal rootRouter.
189
- */
190
- setBaseRouter(router: Router): this;
191
- /**
192
- * Create and register a new router (only used if not injecting one externally - use `setBaseRouter` instead).
193
- */
194
- createRouter(prefix?: string): Router;
195
- /**
196
- * Register a new route handler with support for multiple HTTP methods.
197
- * The route is automatically registered under the globalPrefix if set.
198
- *
199
- * @param methods Array of HTTP methods (get, post, put, delete, etc.)
200
- * @param path Route path with Express path patterns support
201
- * @param handlers One or more Express request handlers (middleware + final handler)
202
- * @returns This instance for method chaining
203
- */
204
- registerRoute(methods: Array<keyof Pick<Express, 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head'>>, path: string, ...handlers: Array<express.RequestHandler>): this;
205
- /**
206
- * Register custom middleware with optional path restriction.
207
- *
208
- * Use this for:
209
- * - Adding authentication to specific routes
210
- * - Custom logging or validation
211
- * - Request transformation
212
- * - Third-party middleware integration
213
- *
214
- * @param path Optional path prefix or middleware function if no path
215
- * @param middleware Middleware handler (required if path is provided)
216
- * @returns This instance for method chaining
217
- */
218
- registerMiddleware(path: string | express.RequestHandler, middleware?: express.RequestHandler): this;
219
- /**
220
- * Register one or more middleware functions to be applied globally.
221
- * This is a simpler alternative to registerMiddleware when you just want
222
- * to add middleware without path restrictions.
223
- *
224
- * @param middlewares One or more Express middleware functions
225
- * @returns This instance for method chaining
226
- */
227
- useMiddleware(...middlewares: express.RequestHandler[]): this;
228
- /**
229
- * Get Prometheus registry (to add custom counters/histograms)
230
- *
231
- * @return {*} {client.Registry}
232
- */
233
- getMetricsRegistry(): typeof import("prom-client").Registry;
234
- /**
235
- * Get server configuration
236
- *
237
- * @return {*} {ServerConfig}
238
- */
239
- getConfig(): ServerConfig;
240
- /**
241
- * Wait until server initialization (middleware + routes) has completed.
242
- * Useful for integration tests that inspect app before starting.
243
- */
244
- waitUntilReady(): Promise<void>;
245
- private normalizePath;
246
- private normalizeRouteForMetrics;
247
- /**
248
- * Destroy all active connections (gracefully if possible).
249
- * If a connection does not close cleanly, it will be force-destroyed.
250
- */
251
- private destroyConnections;
252
- private validateHttpsFiles;
253
- private static isBuiltServerConfig;
254
- private static optionalRequire;
255
- }