@catbee/utils 0.0.5 → 0.0.6

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