@catbee/utils 0.0.5 → 0.0.7
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/LICENSE +21 -201
- package/README.md +51 -888
- package/build/esm/config.d.ts +84 -7
- package/build/esm/config.js +89 -5
- package/build/esm/config.js.map +1 -1
- package/build/esm/index.d.ts +4 -0
- package/build/esm/index.js +27 -0
- package/build/esm/index.js.map +1 -1
- package/build/esm/servers/server.builder.d.ts +508 -0
- package/build/esm/servers/server.builder.js +702 -0
- package/build/esm/servers/server.builder.js.map +1 -0
- package/build/esm/servers/server.d.ts +256 -0
- package/build/esm/servers/server.js +1201 -0
- package/build/esm/servers/server.js.map +1 -0
- package/build/esm/types/api-response.d.ts +33 -7
- package/build/esm/types/api-response.js +33 -1
- package/build/esm/types/api-response.js.map +1 -1
- package/build/esm/types/index.d.ts +125 -0
- package/build/esm/types/index.js +25 -0
- package/build/esm/types/index.js.map +1 -0
- package/build/esm/types/server.d.ts +268 -0
- package/build/esm/types/server.js +25 -0
- package/build/esm/types/server.js.map +1 -0
- package/build/esm/utils/array.utils.js +23 -0
- package/build/esm/utils/array.utils.js.map +1 -1
- package/build/esm/utils/async.utils.d.ts +91 -0
- package/build/esm/utils/async.utils.js +267 -0
- package/build/esm/utils/async.utils.js.map +1 -1
- package/build/esm/utils/cache.utils.js +23 -0
- package/build/esm/utils/cache.utils.js.map +1 -1
- package/build/esm/utils/context-store.utils.d.ts +3 -0
- package/build/esm/utils/context-store.utils.js +32 -6
- package/build/esm/utils/context-store.utils.js.map +1 -1
- package/build/esm/utils/crypto.utils.js +23 -0
- package/build/esm/utils/crypto.utils.js.map +1 -1
- package/build/esm/utils/date.utils.d.ts +159 -0
- package/build/esm/utils/date.utils.js +389 -0
- package/build/esm/utils/date.utils.js.map +1 -0
- package/build/esm/utils/decorators.utils.js +23 -1
- package/build/esm/utils/decorators.utils.js.map +1 -1
- package/build/esm/utils/dir.utils.js +23 -0
- package/build/esm/utils/dir.utils.js.map +1 -1
- package/build/esm/utils/env.utils.js +23 -0
- package/build/esm/utils/env.utils.js.map +1 -1
- package/build/esm/utils/exception.utils.js +31 -8
- package/build/esm/utils/exception.utils.js.map +1 -1
- package/build/esm/utils/fs.utils.js +23 -0
- package/build/esm/utils/fs.utils.js.map +1 -1
- package/build/esm/utils/http-status-codes.js +23 -0
- package/build/esm/utils/http-status-codes.js.map +1 -1
- package/build/esm/utils/id.utils.js +23 -0
- package/build/esm/utils/id.utils.js.map +1 -1
- package/build/esm/utils/logger.utils.d.ts +5 -0
- package/build/esm/utils/logger.utils.js +40 -3
- package/build/esm/utils/logger.utils.js.map +1 -1
- package/build/esm/utils/middleware.utils.d.ts +2 -0
- package/build/esm/utils/middleware.utils.js +61 -50
- package/build/esm/utils/middleware.utils.js.map +1 -1
- package/build/esm/utils/obj.utils.d.ts +10 -3
- package/build/esm/utils/obj.utils.js +225 -15
- package/build/esm/utils/obj.utils.js.map +1 -1
- package/build/esm/utils/performance.utils.d.ts +136 -0
- package/build/esm/utils/performance.utils.js +376 -0
- package/build/esm/utils/performance.utils.js.map +1 -0
- package/build/esm/utils/request.utils.d.ts +9 -0
- package/build/esm/utils/request.utils.js +55 -5
- package/build/esm/utils/request.utils.js.map +1 -1
- package/build/esm/utils/response.utils.d.ts +15 -1
- package/build/esm/utils/response.utils.js +51 -0
- package/build/esm/utils/response.utils.js.map +1 -1
- package/build/esm/utils/stream.utils.d.ts +88 -0
- package/build/esm/utils/stream.utils.js +284 -0
- package/build/esm/utils/stream.utils.js.map +1 -0
- package/build/esm/utils/string.utils.js +23 -0
- package/build/esm/utils/string.utils.js.map +1 -1
- package/build/esm/utils/type.utils.d.ts +90 -0
- package/build/esm/utils/type.utils.js +190 -0
- package/build/esm/utils/type.utils.js.map +1 -0
- package/build/esm/utils/url.utils.js +23 -0
- package/build/esm/utils/url.utils.js.map +1 -1
- package/build/esm/utils/validate.utils.d.ts +7 -0
- package/build/esm/utils/validate.utils.js +35 -2
- package/build/esm/utils/validate.utils.js.map +1 -1
- package/build/esnext/config.d.ts +84 -7
- package/build/esnext/config.js +89 -5
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/index.d.ts +4 -0
- package/build/esnext/index.js +27 -0
- package/build/esnext/index.js.map +1 -1
- package/build/esnext/servers/server.builder.d.ts +508 -0
- package/build/esnext/servers/server.builder.js +657 -0
- package/build/esnext/servers/server.builder.js.map +1 -0
- package/build/esnext/servers/server.d.ts +256 -0
- package/build/esnext/servers/server.js +908 -0
- package/build/esnext/servers/server.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +33 -7
- package/build/esnext/types/api-response.js +33 -1
- package/build/esnext/types/api-response.js.map +1 -1
- package/build/esnext/types/index.d.ts +125 -0
- package/build/esnext/types/index.js +25 -0
- package/build/esnext/types/index.js.map +1 -0
- package/build/esnext/types/server.d.ts +268 -0
- package/build/esnext/types/server.js +25 -0
- package/build/esnext/types/server.js.map +1 -0
- package/build/esnext/utils/array.utils.js +23 -0
- package/build/esnext/utils/array.utils.js.map +1 -1
- package/build/esnext/utils/async.utils.d.ts +91 -0
- package/build/esnext/utils/async.utils.js +213 -0
- package/build/esnext/utils/async.utils.js.map +1 -1
- package/build/esnext/utils/cache.utils.js +23 -0
- package/build/esnext/utils/cache.utils.js.map +1 -1
- package/build/esnext/utils/context-store.utils.d.ts +3 -0
- package/build/esnext/utils/context-store.utils.js +32 -6
- package/build/esnext/utils/context-store.utils.js.map +1 -1
- package/build/esnext/utils/crypto.utils.js +23 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -1
- package/build/esnext/utils/date.utils.d.ts +159 -0
- package/build/esnext/utils/date.utils.js +384 -0
- package/build/esnext/utils/date.utils.js.map +1 -0
- package/build/esnext/utils/decorators.utils.js +23 -1
- package/build/esnext/utils/decorators.utils.js.map +1 -1
- package/build/esnext/utils/dir.utils.js +23 -0
- package/build/esnext/utils/dir.utils.js.map +1 -1
- package/build/esnext/utils/env.utils.js +23 -0
- package/build/esnext/utils/env.utils.js.map +1 -1
- package/build/esnext/utils/exception.utils.js +30 -7
- package/build/esnext/utils/exception.utils.js.map +1 -1
- package/build/esnext/utils/fs.utils.js +23 -0
- package/build/esnext/utils/fs.utils.js.map +1 -1
- package/build/esnext/utils/http-status-codes.js +23 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -1
- package/build/esnext/utils/id.utils.js +23 -0
- package/build/esnext/utils/id.utils.js.map +1 -1
- package/build/esnext/utils/logger.utils.d.ts +5 -0
- package/build/esnext/utils/logger.utils.js +40 -3
- package/build/esnext/utils/logger.utils.js.map +1 -1
- package/build/esnext/utils/middleware.utils.d.ts +2 -0
- package/build/esnext/utils/middleware.utils.js +57 -50
- package/build/esnext/utils/middleware.utils.js.map +1 -1
- package/build/esnext/utils/obj.utils.d.ts +10 -3
- package/build/esnext/utils/obj.utils.js +176 -12
- package/build/esnext/utils/obj.utils.js.map +1 -1
- package/build/esnext/utils/performance.utils.d.ts +136 -0
- package/build/esnext/utils/performance.utils.js +271 -0
- package/build/esnext/utils/performance.utils.js.map +1 -0
- package/build/esnext/utils/request.utils.d.ts +9 -0
- package/build/esnext/utils/request.utils.js +46 -5
- package/build/esnext/utils/request.utils.js.map +1 -1
- package/build/esnext/utils/response.utils.d.ts +15 -1
- package/build/esnext/utils/response.utils.js +51 -0
- package/build/esnext/utils/response.utils.js.map +1 -1
- package/build/esnext/utils/stream.utils.d.ts +88 -0
- package/build/esnext/utils/stream.utils.js +210 -0
- package/build/esnext/utils/stream.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.js +23 -0
- package/build/esnext/utils/string.utils.js.map +1 -1
- package/build/esnext/utils/type.utils.d.ts +90 -0
- package/build/esnext/utils/type.utils.js +187 -0
- package/build/esnext/utils/type.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.js +23 -0
- package/build/esnext/utils/url.utils.js.map +1 -1
- package/build/esnext/utils/validate.utils.d.ts +7 -0
- package/build/esnext/utils/validate.utils.js +33 -0
- package/build/esnext/utils/validate.utils.js.map +1 -1
- package/build/src/config.d.ts +84 -7
- package/build/src/config.js +90 -6
- package/build/src/config.js.map +1 -1
- package/build/src/index.d.ts +4 -0
- package/build/src/index.js +27 -0
- package/build/src/index.js.map +1 -1
- package/build/src/servers/server.builder.d.ts +508 -0
- package/build/src/servers/server.builder.js +661 -0
- package/build/src/servers/server.builder.js.map +1 -0
- package/build/src/servers/server.d.ts +256 -0
- package/build/src/servers/server.js +948 -0
- package/build/src/servers/server.js.map +1 -0
- package/build/src/types/api-response.d.ts +33 -7
- package/build/src/types/api-response.js +34 -0
- package/build/src/types/api-response.js.map +1 -1
- package/build/src/types/index.d.ts +125 -0
- package/build/src/types/index.js +26 -0
- package/build/src/types/index.js.map +1 -0
- package/build/src/types/server.d.ts +268 -0
- package/build/src/types/server.js +26 -0
- package/build/src/types/server.js.map +1 -0
- package/build/src/utils/array.utils.js +23 -0
- package/build/src/utils/array.utils.js.map +1 -1
- package/build/src/utils/async.utils.d.ts +91 -0
- package/build/src/utils/async.utils.js +217 -0
- package/build/src/utils/async.utils.js.map +1 -1
- package/build/src/utils/cache.utils.js +23 -0
- package/build/src/utils/cache.utils.js.map +1 -1
- package/build/src/utils/context-store.utils.d.ts +3 -0
- package/build/src/utils/context-store.utils.js +32 -6
- package/build/src/utils/context-store.utils.js.map +1 -1
- package/build/src/utils/crypto.utils.js +23 -0
- package/build/src/utils/crypto.utils.js.map +1 -1
- package/build/src/utils/date.utils.d.ts +159 -0
- package/build/src/utils/date.utils.js +396 -0
- package/build/src/utils/date.utils.js.map +1 -0
- package/build/src/utils/decorators.utils.js +23 -1
- package/build/src/utils/decorators.utils.js.map +1 -1
- package/build/src/utils/dir.utils.js +23 -0
- package/build/src/utils/dir.utils.js.map +1 -1
- package/build/src/utils/env.utils.js +23 -0
- package/build/src/utils/env.utils.js.map +1 -1
- package/build/src/utils/exception.utils.js +30 -7
- package/build/src/utils/exception.utils.js.map +1 -1
- package/build/src/utils/fs.utils.js +23 -0
- package/build/src/utils/fs.utils.js.map +1 -1
- package/build/src/utils/http-status-codes.js +23 -0
- package/build/src/utils/http-status-codes.js.map +1 -1
- package/build/src/utils/id.utils.js +23 -0
- package/build/src/utils/id.utils.js.map +1 -1
- package/build/src/utils/logger.utils.d.ts +5 -0
- package/build/src/utils/logger.utils.js +41 -4
- package/build/src/utils/logger.utils.js.map +1 -1
- package/build/src/utils/middleware.utils.d.ts +2 -0
- package/build/src/utils/middleware.utils.js +56 -49
- package/build/src/utils/middleware.utils.js.map +1 -1
- package/build/src/utils/obj.utils.d.ts +10 -3
- package/build/src/utils/obj.utils.js +177 -12
- package/build/src/utils/obj.utils.js.map +1 -1
- package/build/src/utils/performance.utils.d.ts +136 -0
- package/build/src/utils/performance.utils.js +278 -0
- package/build/src/utils/performance.utils.js.map +1 -0
- package/build/src/utils/request.utils.d.ts +9 -0
- package/build/src/utils/request.utils.js +48 -5
- package/build/src/utils/request.utils.js.map +1 -1
- package/build/src/utils/response.utils.d.ts +15 -1
- package/build/src/utils/response.utils.js +52 -0
- package/build/src/utils/response.utils.js.map +1 -1
- package/build/src/utils/stream.utils.d.ts +88 -0
- package/build/src/utils/stream.utils.js +218 -0
- package/build/src/utils/stream.utils.js.map +1 -0
- package/build/src/utils/string.utils.js +23 -0
- package/build/src/utils/string.utils.js.map +1 -1
- package/build/src/utils/type.utils.d.ts +90 -0
- package/build/src/utils/type.utils.js +196 -0
- package/build/src/utils/type.utils.js.map +1 -0
- package/build/src/utils/url.utils.js +23 -0
- package/build/src/utils/url.utils.js.map +1 -1
- package/build/src/utils/validate.utils.d.ts +7 -0
- package/build/src/utils/validate.utils.js +36 -2
- package/build/src/utils/validate.utils.js.map +1 -1
- package/package.json +39 -9
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
import { deepObjMerge } from '../utils/obj.utils';
|
|
25
|
+
import { defaultServerConfig } from '../config';
|
|
26
|
+
import { isPort } from '../utils/validate.utils';
|
|
27
|
+
/**
|
|
28
|
+
* Builder class for creating and configuring an Express server configuration.
|
|
29
|
+
*
|
|
30
|
+
* This class provides a fluent interface to configure all aspects of the Express server
|
|
31
|
+
* including security settings, middleware, routing, and more.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const serverConfig = new ServerConfigBuilder()
|
|
36
|
+
* .withPort(3000)
|
|
37
|
+
* .withHost('localhost')
|
|
38
|
+
* .enableCors()
|
|
39
|
+
* .enableHelmet()
|
|
40
|
+
* .build();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export const BUILD_MARKER = Symbol.for('catbee.express.server.build');
|
|
44
|
+
export class ServerConfigBuilder {
|
|
45
|
+
constructor() {
|
|
46
|
+
this.config = {};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validates that a port number is valid and usable.
|
|
50
|
+
*
|
|
51
|
+
* @private
|
|
52
|
+
* @param port - The port number to validate
|
|
53
|
+
* @throws {Error} If port is not an integer or is outside the valid range (1-65535)
|
|
54
|
+
*/
|
|
55
|
+
validatePort(port) {
|
|
56
|
+
if (!isPort(port)) {
|
|
57
|
+
throw new Error(`Port must be a valid number between 1 and 65535, got: ${port}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Sets the port the server will listen on.
|
|
62
|
+
*
|
|
63
|
+
* @param port - The port number (1-65535)
|
|
64
|
+
* @returns The builder instance for chaining
|
|
65
|
+
* @throws {Error} If port is invalid
|
|
66
|
+
* @default 3000 (can be overridden via PORT env variable)
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* builder.withPort(3000)
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
withPort(port) {
|
|
74
|
+
this.validatePort(port);
|
|
75
|
+
this.config.port = port;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Sets the hostname the server will bind to.
|
|
80
|
+
*
|
|
81
|
+
* @param host - The hostname (e.g., 'localhost', '0.0.0.0', '127.0.0.1')
|
|
82
|
+
* @returns The builder instance for chaining
|
|
83
|
+
* @default '0.0.0.0' (can be overridden via HOST env variable)
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* builder.withHost('0.0.0.0') // Listen on all interfaces
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
withHost(host) {
|
|
91
|
+
this.config.host = host;
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Configures Cross-Origin Resource Sharing (CORS) for the server.
|
|
96
|
+
*
|
|
97
|
+
* @param opts - CORS options object or boolean (true to enable with defaults, false to disable)
|
|
98
|
+
* @returns The builder instance for chaining
|
|
99
|
+
* @default false (CORS is disabled by default)
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // Enable CORS with default options
|
|
104
|
+
* builder.withCors(true)
|
|
105
|
+
*
|
|
106
|
+
* // Configure CORS with specific options
|
|
107
|
+
* builder.withCors({
|
|
108
|
+
* origin: ['https://example.com'],
|
|
109
|
+
* methods: ['GET', 'POST']
|
|
110
|
+
* })
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
withCors(opts) {
|
|
114
|
+
this.config.cors = opts;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Enables CORS with default settings
|
|
119
|
+
*
|
|
120
|
+
* @returns The builder instance for chaining
|
|
121
|
+
*/
|
|
122
|
+
enableCors() {
|
|
123
|
+
return this.withCors(true);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Disables CORS
|
|
127
|
+
*
|
|
128
|
+
* @returns The builder instance for chaining
|
|
129
|
+
*/
|
|
130
|
+
disableCors() {
|
|
131
|
+
return this.withCors(false);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Configures the Helmet middleware for setting HTTP security headers.
|
|
135
|
+
*
|
|
136
|
+
* @param opts - Helmet options object or boolean (true to enable with defaults, false to disable)
|
|
137
|
+
* @returns The builder instance for chaining
|
|
138
|
+
* @default false (Helmet is disabled by default)
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* // Enable Helmet with default settings
|
|
143
|
+
* builder.withHelmet(true)
|
|
144
|
+
*
|
|
145
|
+
* // Configure Helmet with specific options
|
|
146
|
+
* builder.withHelmet({
|
|
147
|
+
* contentSecurityPolicy: false,
|
|
148
|
+
* xssFilter: true
|
|
149
|
+
* })
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
withHelmet(opts) {
|
|
153
|
+
this.config.helmet = opts;
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Enables Helmet with default settings
|
|
158
|
+
*
|
|
159
|
+
* @returns The builder instance for chaining
|
|
160
|
+
*/
|
|
161
|
+
enableHelmet() {
|
|
162
|
+
return this.withHelmet(true);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Disables Helmet
|
|
166
|
+
*
|
|
167
|
+
* @returns The builder instance for chaining
|
|
168
|
+
*/
|
|
169
|
+
disableHelmet() {
|
|
170
|
+
return this.withHelmet(false);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Configures response compression middleware.
|
|
174
|
+
*
|
|
175
|
+
* @param opts - Compression options object or boolean (true to enable with defaults, false to disable)
|
|
176
|
+
* @returns The builder instance for chaining
|
|
177
|
+
* @default false (Compression is disabled by default)
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* // Enable compression with default settings
|
|
182
|
+
* builder.withCompression(true)
|
|
183
|
+
*
|
|
184
|
+
* // Configure compression with specific options
|
|
185
|
+
* builder.withCompression({
|
|
186
|
+
* level: 6,
|
|
187
|
+
* threshold: 1024
|
|
188
|
+
* })
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
withCompression(opts) {
|
|
192
|
+
this.config.compression = opts;
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Enables compression with default settings
|
|
197
|
+
*
|
|
198
|
+
* @returns The builder instance for chaining
|
|
199
|
+
*/
|
|
200
|
+
enableCompression() {
|
|
201
|
+
return this.withCompression(true);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Disables compression
|
|
205
|
+
*
|
|
206
|
+
* @returns The builder instance for chaining
|
|
207
|
+
*/
|
|
208
|
+
disableCompression() {
|
|
209
|
+
return this.withCompression(false);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Configures rate limiting to protect against brute-force attacks.
|
|
213
|
+
*
|
|
214
|
+
* @param opts - Rate limit configuration options
|
|
215
|
+
* @returns The builder instance for chaining
|
|
216
|
+
* @default { enable: false, windowMs: 15 * 60 * 1000, max: 100, message: 'Too many requests', standardHeaders: true, legacyHeaders: false }
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* builder.withRateLimit({
|
|
221
|
+
* enable: true,
|
|
222
|
+
* windowMs: 15 * 60 * 1000, // 15 minutes
|
|
223
|
+
* max: 100 // limit each IP to 100 requests per windowMs
|
|
224
|
+
* })
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
withRateLimit(opts) {
|
|
228
|
+
this.mergeConfig('rateLimit', opts);
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Enables rate limiting with default or custom settings
|
|
233
|
+
*
|
|
234
|
+
* @param opts - Optional rate limit configuration (max requests, window, etc.)
|
|
235
|
+
* @returns The builder instance for chaining
|
|
236
|
+
*/
|
|
237
|
+
enableRateLimit(opts = {}) {
|
|
238
|
+
return this.setEnabled('rateLimit', true, opts);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Disables rate limiting
|
|
242
|
+
*
|
|
243
|
+
* @returns The builder instance for chaining
|
|
244
|
+
*/
|
|
245
|
+
disableRateLimit() {
|
|
246
|
+
return this.setEnabled('rateLimit', false);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Configures HTTP request logging middleware.
|
|
250
|
+
*
|
|
251
|
+
* @param opts - Request logging configuration options
|
|
252
|
+
* @returns The builder instance for chaining
|
|
253
|
+
* @default { enable: true in dev/false in prod, ignorePaths: ['/healthz', '/favicon.ico', '/metrics', '/docs', '/.well-known'], skipNotFoundRoutes: false }
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* builder.withRequestLogging({
|
|
258
|
+
* enable: true,
|
|
259
|
+
* ignorePaths: ['/health', '/metrics'],
|
|
260
|
+
* skipNotFoundRoutes: true
|
|
261
|
+
* })
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
withRequestLogging(opts) {
|
|
265
|
+
this.mergeConfig('requestLogging', opts);
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Enables request logging with default or custom settings
|
|
270
|
+
*
|
|
271
|
+
* @param opts - Optional request logging configuration
|
|
272
|
+
* @returns The builder instance for chaining
|
|
273
|
+
*/
|
|
274
|
+
enableRequestLogging(opts = {}) {
|
|
275
|
+
return this.setEnabled('requestLogging', true, opts);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Disables request logging
|
|
279
|
+
*
|
|
280
|
+
* @returns The builder instance for chaining
|
|
281
|
+
*/
|
|
282
|
+
disableRequestLogging() {
|
|
283
|
+
return this.setEnabled('requestLogging', false);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Configures server metrics collection and endpoints.
|
|
287
|
+
*
|
|
288
|
+
* @param opts - Metrics configuration options
|
|
289
|
+
* @returns The builder instance for chaining
|
|
290
|
+
* @default { enable: false, path: '/metrics', withGlobalPrefix: false }
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```typescript
|
|
294
|
+
* builder.withMetrics({
|
|
295
|
+
* enable: true,
|
|
296
|
+
* path: '/metrics'
|
|
297
|
+
* })
|
|
298
|
+
* ```
|
|
299
|
+
*/
|
|
300
|
+
withMetrics(opts) {
|
|
301
|
+
this.mergeConfig('metrics', opts);
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Enables Prometheus metrics collection and endpoint
|
|
306
|
+
*
|
|
307
|
+
* @param opts - Optional metrics configuration
|
|
308
|
+
* @returns The builder instance for chaining
|
|
309
|
+
*/
|
|
310
|
+
enableMetrics(opts = {}) {
|
|
311
|
+
return this.setEnabled('metrics', true, opts);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Disables Prometheus metrics
|
|
315
|
+
*
|
|
316
|
+
* @returns The builder instance for chaining
|
|
317
|
+
*/
|
|
318
|
+
disableMetrics() {
|
|
319
|
+
return this.setEnabled('metrics', false);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Configures server health check endpoint.
|
|
323
|
+
*
|
|
324
|
+
* @param opts - Health check configuration options
|
|
325
|
+
* @returns The builder instance for chaining
|
|
326
|
+
* @default { path: '/healthz', detailed: true, withGlobalPrefix: false }
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```typescript
|
|
330
|
+
* builder.withHealthCheck({
|
|
331
|
+
* path: '/health',
|
|
332
|
+
* detailed: true
|
|
333
|
+
* })
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
withHealthCheck(opts) {
|
|
337
|
+
this.mergeConfig('healthCheck', opts);
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Configures OpenAPI/Swagger documentation for the API.
|
|
342
|
+
*
|
|
343
|
+
* @param opts - OpenAPI configuration options
|
|
344
|
+
* @returns The builder instance for chaining
|
|
345
|
+
* @default { enable: false, mountPath: '/docs', verbose: false, withGlobalPrefix: false }
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```typescript
|
|
349
|
+
* builder.withOpenApi({
|
|
350
|
+
* enable: true,
|
|
351
|
+
* path: '/api-docs',
|
|
352
|
+
* filePath: './openapi.yaml'
|
|
353
|
+
* })
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
withOpenApi(opts) {
|
|
357
|
+
this.mergeConfig('openApi', opts);
|
|
358
|
+
return this;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Enables OpenAPI documentation with required file path
|
|
362
|
+
*
|
|
363
|
+
* @param filePath - Path to OpenAPI specification file (required)
|
|
364
|
+
* @param opts - Optional OpenAPI configuration
|
|
365
|
+
* @returns The builder instance for chaining
|
|
366
|
+
*/
|
|
367
|
+
enableOpenApi(filePath, opts = {}) {
|
|
368
|
+
this.setEnabled('openApi', true, Object.assign({ filePath }, opts));
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Disables OpenAPI documentation
|
|
373
|
+
*
|
|
374
|
+
* @returns The builder instance for chaining
|
|
375
|
+
*/
|
|
376
|
+
disableOpenApi() {
|
|
377
|
+
return this.setEnabled('openApi', false);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Configures the server as a microservice with versioning.
|
|
381
|
+
*
|
|
382
|
+
* @param opts - Microservice configuration options including app name and service version
|
|
383
|
+
* @returns The builder instance for chaining
|
|
384
|
+
* @default { isMicroservice: false, appName: 'express_app' }
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```typescript
|
|
388
|
+
* builder.withMicroService({
|
|
389
|
+
* appName: 'user-service',
|
|
390
|
+
* serviceVersion: {
|
|
391
|
+
* enable: true,
|
|
392
|
+
* version: '1.2.3'
|
|
393
|
+
* }
|
|
394
|
+
* })
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
withMicroService(opts) {
|
|
398
|
+
this.config.isMicroservice = true;
|
|
399
|
+
this.config.appName = opts.appName;
|
|
400
|
+
this.mergeConfig('serviceVersion', opts.serviceVersion);
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Configures the trust proxy settings to determine if X-Forwarded-* headers should be trusted.
|
|
405
|
+
*
|
|
406
|
+
* @param opts - Trust proxy configuration options
|
|
407
|
+
* @returns The builder instance for chaining
|
|
408
|
+
* @default false
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* // Trust proxy headers (useful when behind a load balancer)
|
|
413
|
+
* builder.withTrustProxy(true)
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
withTrustProxy(opts) {
|
|
417
|
+
this.config.trustProxy = opts;
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Configures the request ID middleware for tracing requests across services.
|
|
422
|
+
*
|
|
423
|
+
* @param opts - Request ID configuration options
|
|
424
|
+
* @returns The builder instance for chaining
|
|
425
|
+
* @default { headerName: 'x-request-id', exposeHeader: true }
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* ```typescript
|
|
429
|
+
* builder.withRequestId({
|
|
430
|
+
* headerName: 'X-Request-Id',
|
|
431
|
+
* generator: () => crypto.randomUUID()
|
|
432
|
+
* })
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
withRequestId(opts) {
|
|
436
|
+
this.mergeConfig('requestId', opts);
|
|
437
|
+
return this;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Configures the response time middleware for measuring request processing times.
|
|
441
|
+
*
|
|
442
|
+
* @param opts - Response time configuration options
|
|
443
|
+
* @returns The builder instance for chaining
|
|
444
|
+
* @default { enable: false, addHeader: true, logOnComplete: false }
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```typescript
|
|
448
|
+
* builder.withResponseTime({
|
|
449
|
+
* enable: true,
|
|
450
|
+
* addHeader: true,
|
|
451
|
+
* logOnComplete: true
|
|
452
|
+
* })
|
|
453
|
+
* ```
|
|
454
|
+
*/
|
|
455
|
+
withResponseTime(opts) {
|
|
456
|
+
this.mergeConfig('responseTime', opts);
|
|
457
|
+
return this;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Enables response time tracking with default or custom settings
|
|
461
|
+
*
|
|
462
|
+
* @param opts - Optional response time configuration
|
|
463
|
+
* @returns The builder instance for chaining
|
|
464
|
+
*/
|
|
465
|
+
enableResponseTime(opts = {}) {
|
|
466
|
+
this.setEnabled('responseTime', true, opts);
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Disables response time tracking
|
|
471
|
+
*
|
|
472
|
+
* @returns The builder instance for chaining
|
|
473
|
+
*/
|
|
474
|
+
disableResponseTime() {
|
|
475
|
+
return this.setEnabled('responseTime', false);
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Configures the body parser middleware options for parsing request bodies.
|
|
479
|
+
*
|
|
480
|
+
* @param opts - Body parser configuration options
|
|
481
|
+
* @returns The builder instance for chaining
|
|
482
|
+
* @default { json: { limit: '1mb' }, urlencoded: { extended: true, limit: '1mb' } }
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* ```typescript
|
|
486
|
+
* builder.withBodyParser({
|
|
487
|
+
* json: {
|
|
488
|
+
* limit: '1mb'
|
|
489
|
+
* },
|
|
490
|
+
* urlencoded: {
|
|
491
|
+
* extended: true,
|
|
492
|
+
* limit: '1mb'
|
|
493
|
+
* }
|
|
494
|
+
* })
|
|
495
|
+
* ```
|
|
496
|
+
*/
|
|
497
|
+
withBodyParser(opts) {
|
|
498
|
+
var _a;
|
|
499
|
+
this.config.bodyParser = deepObjMerge({}, (_a = this.config.bodyParser) !== null && _a !== void 0 ? _a : {}, opts);
|
|
500
|
+
return this;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Configures cookie parsing middleware.
|
|
504
|
+
*
|
|
505
|
+
* @param opts - Cookie parser options or boolean (true to enable with defaults, false to disable)
|
|
506
|
+
* @returns The builder instance for chaining
|
|
507
|
+
* @default false
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* ```typescript
|
|
511
|
+
* // Enable cookie parsing with default options
|
|
512
|
+
* builder.withCookies(true)
|
|
513
|
+
*
|
|
514
|
+
* // Enable cookie parsing with specific options
|
|
515
|
+
* builder.withCookies({
|
|
516
|
+
* secret: 'your-secret-key',
|
|
517
|
+
* secure: true
|
|
518
|
+
* })
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
521
|
+
withCookies(opts) {
|
|
522
|
+
this.config.cookieParser = opts;
|
|
523
|
+
return this;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Adds a static folder to serve files from.
|
|
527
|
+
*
|
|
528
|
+
* @param folder - Static folder configuration
|
|
529
|
+
* @returns The builder instance for chaining
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* ```typescript
|
|
533
|
+
* builder.withStaticFolder({
|
|
534
|
+
* path: '/assets',
|
|
535
|
+
* directory: './public',
|
|
536
|
+
* options: { maxAge: '1d' }
|
|
537
|
+
* })
|
|
538
|
+
* ```
|
|
539
|
+
*/
|
|
540
|
+
withStaticFolder(folder) {
|
|
541
|
+
var _a;
|
|
542
|
+
if (!folder.path)
|
|
543
|
+
throw new Error('Static folder requires a path');
|
|
544
|
+
const folders = [...((_a = this.config.staticFolders) !== null && _a !== void 0 ? _a : []), folder];
|
|
545
|
+
this.config.staticFolders = Array.from(new Map(folders.map(f => [f.path, f])).values());
|
|
546
|
+
return this;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Sets global headers to be included in all responses.
|
|
550
|
+
*
|
|
551
|
+
* @param headers - Object containing header name/value pairs or functions that return values
|
|
552
|
+
* @returns The builder instance for chaining
|
|
553
|
+
* @default {}
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* ```typescript
|
|
557
|
+
* builder.withGlobalHeaders({
|
|
558
|
+
* 'X-Powered-By': 'Catbee',
|
|
559
|
+
* 'Server-Time': () => new Date().toISOString()
|
|
560
|
+
* })
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
withGlobalHeaders(headers) {
|
|
564
|
+
this.mergeConfig('globalHeaders', headers);
|
|
565
|
+
return this;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Sets a global prefix for all routes.
|
|
569
|
+
*
|
|
570
|
+
* @param prefix - The prefix to prepend to all routes (e.g., '/api/v1')
|
|
571
|
+
* @returns The builder instance for chaining
|
|
572
|
+
* @default '/'
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```typescript
|
|
576
|
+
* builder.withGlobalPrefix('/api/v1')
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
579
|
+
withGlobalPrefix(prefix) {
|
|
580
|
+
this.config.globalPrefix = prefix;
|
|
581
|
+
return this;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Applies custom configuration overrides directly.
|
|
585
|
+
*
|
|
586
|
+
* @param overrides - Custom configuration options to merge
|
|
587
|
+
* @returns The builder instance for chaining
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```typescript
|
|
591
|
+
* builder.withCustom({
|
|
592
|
+
* port: 8080,
|
|
593
|
+
* customMiddleware: myMiddlewareFunction
|
|
594
|
+
* })
|
|
595
|
+
* ```
|
|
596
|
+
*/
|
|
597
|
+
withCustom(overrides) {
|
|
598
|
+
this.config = deepObjMerge({}, this.config, overrides);
|
|
599
|
+
return this;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Configures HTTPS server options.
|
|
603
|
+
*
|
|
604
|
+
* @param opts - HTTPS configuration (key, cert, ca, passphrase, etc.)
|
|
605
|
+
* @returns The builder instance for chaining
|
|
606
|
+
*
|
|
607
|
+
* @example
|
|
608
|
+
* ```typescript
|
|
609
|
+
* builder.withHttps({
|
|
610
|
+
* key: './localhost-key.pem',
|
|
611
|
+
* cert: './localhost-cert.pem'
|
|
612
|
+
* })
|
|
613
|
+
* ```
|
|
614
|
+
*/
|
|
615
|
+
withHttps(opts) {
|
|
616
|
+
this.config.https = opts;
|
|
617
|
+
return this;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Builds and returns the final server configuration.
|
|
621
|
+
*
|
|
622
|
+
* This method merges the user-specified configuration with default values,
|
|
623
|
+
* ensures all sections with 'enable' flags are properly structured, and
|
|
624
|
+
* produces the final configuration to be used by the server.
|
|
625
|
+
*
|
|
626
|
+
* @returns The complete ServerConfig object
|
|
627
|
+
*
|
|
628
|
+
* @example
|
|
629
|
+
* ```typescript
|
|
630
|
+
* const config = new ServerConfigBuilder()
|
|
631
|
+
* .withPort(3000)
|
|
632
|
+
* .withHost('localhost')
|
|
633
|
+
* .withCors(true)
|
|
634
|
+
* .build();
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
build() {
|
|
638
|
+
var _a;
|
|
639
|
+
const config = deepObjMerge({}, defaultServerConfig, this.config);
|
|
640
|
+
// Common validation
|
|
641
|
+
if (((_a = config.openApi) === null || _a === void 0 ? void 0 : _a.enable) && !config.openApi.filePath) {
|
|
642
|
+
throw new Error('OpenAPI is enabled but no filePath is specified');
|
|
643
|
+
}
|
|
644
|
+
return Object.freeze(Object.assign(Object.assign({}, config), { [BUILD_MARKER]: true }));
|
|
645
|
+
}
|
|
646
|
+
mergeConfig(key, value) {
|
|
647
|
+
const current = typeof this.config[key] === 'object' && this.config[key] !== null
|
|
648
|
+
? this.config[key]
|
|
649
|
+
: {};
|
|
650
|
+
this.config[key] = deepObjMerge({}, current, value);
|
|
651
|
+
}
|
|
652
|
+
setEnabled(key, enable, overrides = {}) {
|
|
653
|
+
this.mergeConfig(key, Object.assign(Object.assign({}, overrides), { enable }));
|
|
654
|
+
return this;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
//# sourceMappingURL=server.builder.js.map
|