@expressots/core 2.9.1 → 2.11.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 (95) hide show
  1. package/lib/CHANGELOG.md +145 -89
  2. package/lib/cjs/application/{app-container.js → application-container.js} +95 -94
  3. package/lib/cjs/application/application-factory.js +39 -74
  4. package/lib/cjs/application/index.js +7 -7
  5. package/lib/cjs/common/{color-service.provider.js → color-codes.js} +46 -47
  6. package/lib/cjs/common/index.js +2 -2
  7. package/lib/cjs/common/{package-resolver.provider.js → package-resolver.js} +35 -35
  8. package/lib/cjs/common/{project-config.provider.js → project-config.js} +2 -2
  9. package/lib/cjs/console/console.js +60 -61
  10. package/lib/cjs/console/index.js +5 -5
  11. package/lib/cjs/container-module/container-module.js +111 -111
  12. package/lib/cjs/container-module/index.js +6 -6
  13. package/lib/cjs/controller/base-controller.js +74 -75
  14. package/lib/cjs/controller/index.js +5 -5
  15. package/lib/cjs/decorator/index.js +17 -17
  16. package/lib/cjs/decorator/scope-binding.js +44 -44
  17. package/lib/cjs/error/app-error.js +26 -26
  18. package/lib/cjs/error/error-handler-middleware.js +28 -28
  19. package/lib/cjs/error/index.js +9 -9
  20. package/lib/cjs/error/report.js +53 -54
  21. package/lib/cjs/error/status-code.js +89 -89
  22. package/lib/cjs/index.js +26 -26
  23. package/lib/cjs/middleware/index.js +32 -32
  24. package/lib/cjs/middleware/interfaces/body-parser.interface.js +2 -2
  25. package/lib/cjs/middleware/interfaces/compression.interface.js +2 -2
  26. package/lib/cjs/middleware/interfaces/cookie-parser.interface.js +2 -2
  27. package/lib/cjs/middleware/interfaces/cookie-session/cookie-session.interface.js +2 -2
  28. package/lib/cjs/middleware/interfaces/cookie-session/keygrip.interface.js +2 -2
  29. package/lib/cjs/middleware/interfaces/cors.interface.js +2 -2
  30. package/lib/cjs/middleware/interfaces/express-rate-limit.interface.js +2 -2
  31. package/lib/cjs/middleware/interfaces/express-session.interface.js +2 -2
  32. package/lib/cjs/middleware/interfaces/helmet.interface.js +2 -2
  33. package/lib/cjs/middleware/interfaces/morgan.interface.js +2 -2
  34. package/lib/cjs/middleware/interfaces/multer.interface.js +2 -2
  35. package/lib/cjs/middleware/interfaces/serve-favicon.interface.js +2 -2
  36. package/lib/cjs/middleware/interfaces/serve-static.interface.js +2 -2
  37. package/lib/cjs/middleware/middleware-resolver.js +72 -72
  38. package/lib/cjs/middleware/middleware-service.js +480 -481
  39. package/lib/cjs/provider/db-in-memory/db-in-memory.provider.js +82 -82
  40. package/lib/cjs/provider/dto-validator/dto-validator.provider.js +53 -53
  41. package/lib/cjs/provider/environment/env-validator.provider.js +98 -98
  42. package/lib/cjs/provider/index.js +13 -13
  43. package/lib/cjs/provider/logger/logger.provider.js +117 -117
  44. package/lib/cjs/provider/provider-manager.js +49 -50
  45. package/lib/cjs/render/handlebars.interface.js +2 -2
  46. package/lib/cjs/render/index.js +2 -2
  47. package/lib/cjs/render/render.type.js +2 -2
  48. package/lib/cjs/types/application/{app-container.d.ts → application-container.d.ts} +64 -62
  49. package/lib/cjs/types/application/application-factory.d.ts +19 -34
  50. package/lib/cjs/types/application/index.d.ts +2 -2
  51. package/lib/cjs/types/common/{color-service.provider.d.ts → color-codes.d.ts} +29 -29
  52. package/lib/cjs/types/common/index.d.ts +1 -1
  53. package/lib/cjs/types/common/{package-resolver.provider.d.ts → package-resolver.d.ts} +8 -8
  54. package/lib/cjs/types/common/{project-config.provider.d.ts → project-config.d.ts} +56 -56
  55. package/lib/cjs/types/console/console.d.ts +27 -27
  56. package/lib/cjs/types/console/index.d.ts +1 -1
  57. package/lib/cjs/types/container-module/container-module.d.ts +28 -28
  58. package/lib/cjs/types/container-module/index.d.ts +1 -1
  59. package/lib/cjs/types/controller/base-controller.d.ts +48 -48
  60. package/lib/cjs/types/controller/index.d.ts +1 -1
  61. package/lib/cjs/types/decorator/index.d.ts +1 -1
  62. package/lib/cjs/types/decorator/scope-binding.d.ts +33 -33
  63. package/lib/cjs/types/error/app-error.d.ts +29 -29
  64. package/lib/cjs/types/error/error-handler-middleware.d.ts +11 -11
  65. package/lib/cjs/types/error/index.d.ts +3 -3
  66. package/lib/cjs/types/error/report.d.ts +25 -25
  67. package/lib/cjs/types/error/status-code.d.ts +136 -136
  68. package/lib/cjs/types/index.d.ts +10 -10
  69. package/lib/cjs/types/middleware/index.d.ts +13 -13
  70. package/lib/cjs/types/middleware/interfaces/body-parser.interface.d.ts +33 -33
  71. package/lib/cjs/types/middleware/interfaces/compression.interface.d.ts +98 -98
  72. package/lib/cjs/types/middleware/interfaces/cookie-parser.interface.d.ts +9 -9
  73. package/lib/cjs/types/middleware/interfaces/cookie-session/cookie-session.interface.d.ts +57 -57
  74. package/lib/cjs/types/middleware/interfaces/cookie-session/keygrip.interface.d.ts +27 -27
  75. package/lib/cjs/types/middleware/interfaces/cors.interface.d.ts +57 -57
  76. package/lib/cjs/types/middleware/interfaces/express-rate-limit.interface.d.ts +292 -292
  77. package/lib/cjs/types/middleware/interfaces/express-session.interface.d.ts +207 -207
  78. package/lib/cjs/types/middleware/interfaces/helmet.interface.d.ts +210 -210
  79. package/lib/cjs/types/middleware/interfaces/morgan.interface.d.ts +40 -40
  80. package/lib/cjs/types/middleware/interfaces/multer.interface.d.ts +255 -255
  81. package/lib/cjs/types/middleware/interfaces/serve-favicon.interface.d.ts +11 -11
  82. package/lib/cjs/types/middleware/interfaces/serve-static.interface.d.ts +70 -70
  83. package/lib/cjs/types/middleware/middleware-resolver.d.ts +11 -11
  84. package/lib/cjs/types/middleware/middleware-service.d.ts +368 -368
  85. package/lib/cjs/types/provider/db-in-memory/db-in-memory.provider.d.ts +43 -43
  86. package/lib/cjs/types/provider/dto-validator/dto-validator.provider.d.ts +11 -11
  87. package/lib/cjs/types/provider/environment/env-validator.provider.d.ts +35 -35
  88. package/lib/cjs/types/provider/index.d.ts +5 -5
  89. package/lib/cjs/types/provider/logger/logger.provider.d.ts +54 -54
  90. package/lib/cjs/types/provider/provider-manager.d.ts +16 -16
  91. package/lib/cjs/types/render/handlebars.interface.d.ts +46 -46
  92. package/lib/cjs/types/render/index.d.ts +2 -2
  93. package/lib/cjs/types/render/render.type.d.ts +12 -12
  94. package/lib/package.json +16 -17
  95. package/package.json +16 -17
@@ -1,368 +1,368 @@
1
- /// <reference types="node" />
2
- import express, { Request, Response, NextFunction } from "express";
3
- import { OptionsJson } from "./interfaces/body-parser.interface";
4
- import { CompressionOptions } from "./interfaces/compression.interface";
5
- import { CorsOptions } from "./interfaces/cors.interface";
6
- import { CookieParserOptions } from "./interfaces/cookie-parser.interface";
7
- import { ServeStaticOptions } from "./interfaces/serve-static.interface";
8
- import { CookieSessionOptions } from "./interfaces/cookie-session/cookie-session.interface";
9
- import { ServeFaviconOptions } from "./interfaces/serve-favicon.interface";
10
- import { FormatFn, OptionsMorgan } from "./interfaces/morgan.interface";
11
- import { RateLimitOptions } from "./interfaces/express-rate-limit.interface";
12
- import { OptionsHelmet } from "./interfaces/helmet.interface";
13
- import { multer } from "./interfaces/multer.interface";
14
- import { SessionOptions } from "./interfaces/express-session.interface";
15
- /**
16
- * ExpressHandler Type
17
- *
18
- * The ExpressHandler type is a union type that represents various types of Express middleware functions.
19
- * It can be one of the following types:
20
- * - express.ErrorRequestHandler: Handles errors in the middleware pipeline.
21
- * - express.RequestParamHandler: Handles parameters in the middleware pipeline.
22
- * - express.RequestHandler: General request handler.
23
- * - undefined: Represents the absence of a handler.
24
- */
25
- export type ExpressHandler = express.ErrorRequestHandler | express.RequestParamHandler | express.RequestHandler | undefined;
26
- /**
27
- * Expresso middleware interface.
28
- */
29
- interface IExpressoMiddleware {
30
- use(req: Request, res: Response, next: NextFunction): Promise<void> | void;
31
- }
32
- /**
33
- * Abstract class for creating custom Expresso middleware.
34
- * Custom middleware classes should extend this class and implement the use method.
35
- *
36
- */
37
- export declare abstract class ExpressoMiddleware implements IExpressoMiddleware {
38
- get name(): string;
39
- abstract use(req: Request, res: Response, next: NextFunction): Promise<void> | void;
40
- }
41
- /**
42
- * MiddlewareOptions Type
43
- *
44
- * The MiddlewareOptions type represents arguments that can be passed to a middleware function.
45
- * It can either be a expressjs request handler function, a middleware configuration object that is composed by a route and an expressjs handler or
46
- */
47
- export type MiddlewareOptions = ExpressHandler | MiddlewareConfig | IExpressoMiddleware;
48
- /**
49
- * MiddlewareConfig Interface
50
- *
51
- * The MiddlewareConfig interface specifies the structure for middleware configuration objects.
52
- * - path: Optional. The route path for which the middleware is configured.
53
- * - middlewares: An array of ExpressHandler types that make up the middleware pipeline for the route specified by 'path'.
54
- */
55
- type MiddlewareConfig = {
56
- path?: string;
57
- middlewares: Array<ExpressHandler | IExpressoMiddleware>;
58
- };
59
- /**
60
- * MiddlewarePipeline Interface
61
- *
62
- * The MiddlewarePipeline interface represents the metadata and actual middleware to be executed in a middleware pipeline.
63
- * - timestamp: The date and time at which the middleware was added to the pipeline.
64
- * - middleware: Can be either an ExpressHandler function or a MiddlewareConfig object defining a more complex middleware setup.
65
- */
66
- interface MiddlewarePipeline {
67
- timestamp: Date;
68
- middleware: ExpressHandler | MiddlewareConfig | IExpressoMiddleware;
69
- }
70
- /**
71
- * Interface for configuring and managing middlewares in the application.
72
- * Provides methods to be added automatically in the application without the need to import packages.
73
- */
74
- interface IMiddleware {
75
- /**
76
- * Adds a Rate Limit middleware to the middleware collection.
77
- * The rate limiter is responsible for adding dynamic rate limit and request throttling to the application.
78
- *
79
- * @param options - Optional configuration options for the rate limiter.
80
- */
81
- addRateLimiter(options?: RateLimitOptions): void;
82
- /**
83
- * Adds a Body Parser middleware to the middleware collection.
84
- * The body parser is responsible for parsing the incoming request bodies in a middleware.
85
- *
86
- * @param options - Optional configuration options for the JSON body parser.
87
- */
88
- addBodyParser(options?: OptionsJson): void;
89
- /**
90
- * Adds Cross-Origin Resource Sharing (CORS) middleware to enable or control cross-origin requests.
91
- *
92
- * @param options - Optional configuration options for CORS. Defines the behavior of CORS requests like allowed origins, methods, headers, etc.
93
- */
94
- addCors(options?: CorsOptions): void;
95
- /**
96
- * Adds Compression middleware to reduce the size of the response body and improve the speed of the client-server communication.
97
- *
98
- * @param options - Optional configuration options for Compression. Allows fine-tuning the compression behavior, such as setting the compression level, threshold, and filter functions to determine which requests should be compressed.
99
- */
100
- addCompression(options?: CompressionOptions): void;
101
- /**
102
- * Adds Cookie Parser middleware to parse the cookie header and populate req.cookies with an object keyed by the cookie names.
103
- *
104
- * @param secret - A string or array used for signing cookies. This is optional and if not specified, the cookie-parser will not parse signed cookies.
105
- * @param options - Optional configuration options for Cookie Parser.
106
- */
107
- addCookieParser(secret?: string | Array<string> | undefined, options?: CookieParserOptions): void;
108
- /**
109
- * Adds Cookie Session middleware to enable cookie-based sessions.
110
- *
111
- * @param options - Optional configuration options for Cookie Session. Defines the behavior of cookie sessions like the name of the cookie, keys to sign the cookie, etc.
112
- */
113
- addCookieSession(options: CookieSessionOptions): void;
114
- /**
115
- * Adds Morgan middleware to log HTTP requests.
116
- *
117
- * @param format - The log format. Can be a string or a function.
118
- * @param options - Optional configuration options for Morgan. Defines the behavior of the logger like the output stream, buffer duration, etc.
119
- */
120
- addMorgan(format: string | FormatFn, options?: OptionsMorgan): void;
121
- /**
122
- * Adds a middleware to serve the favicon to the middleware collection.
123
- * The favicon is the icon that is displayed in the browser tab for the application.
124
- *
125
- * @param path - The path to the favicon file.
126
- * @param options - Optional configuration options for serving the favicon. Defines the behavior of the favicon middleware like cache control, custom headers, etc.
127
- */
128
- addServeFavicon(path: string | Buffer, options?: ServeFaviconOptions): void;
129
- /**
130
- * Add a middleware to enable express-session.
131
- *
132
- * @param options - Optional configuration options for Session.
133
- *
134
- */
135
- addSession(options: SessionOptions): void;
136
- /**
137
- * Configures the error handling middleware for the application.
138
- *
139
- * @param errorHandling - The Express error handler function that takes care of processing errors and formulating the response.
140
- */
141
- setErrorHandler(errorHandling?: ExpressHandler): void;
142
- /**
143
- * Adds a middleware to serve static files from the specified root directory.
144
- * Allows the application to serve files like images, CSS, JavaScript, etc.
145
- *
146
- * @param root - The root directory from which the static assets are to be served.
147
- * @param options - Optional configuration options for serving static files. Defines behavior like cache control, custom headers, etc.
148
- */
149
- serveStatic(root: string, options?: ServeStaticOptions): void;
150
- /**
151
- * Adds a middleware to the middleware collection.
152
- *
153
- * @param options - The Express request handler function to be added to the middleware collection, or a middleware configuration object
154
- * that is composed by a route and an expressjs handler, or a custom Expresso middleware.
155
- *
156
- * @example Express Handler
157
- * const middleware = (req, res, next) => {
158
- * // Your middleware logic here
159
- * next();
160
- * }
161
- *
162
- * @example Middleware Configuration Object
163
- * const middleware = {
164
- * path: "/",
165
- * middlewares: [] // Array of Express Handlers
166
- * }
167
- *
168
- * @example Expresso Middleware
169
- * class CustomMiddleware implements IExpressoMiddleware {
170
- * use(req: Request, res: Response, next: NextFunction): Promise<void> | void {
171
- * // Your middleware logic here
172
- * next();
173
- * }
174
- * }
175
- */
176
- addMiddleware(options: MiddlewareOptions): void;
177
- /**
178
- * View middleware pipeline formatted.
179
- * @returns void
180
- */
181
- viewMiddlewarePipeline(): void;
182
- /**
183
- * Gets the configured error handler middleware.
184
- *
185
- * @returns The error handler middleware.
186
- */
187
- getErrorHandler(): ExpressHandler;
188
- /**
189
- * Adds Helmet middleware to enhance security by setting various HTTP headers.
190
- *
191
- * @param options - Optional configuration options for Helmet.
192
- * @returns The configuration options for Helmet middleware.
193
- */
194
- addHelmet(options?: OptionsHelmet): void;
195
- /**
196
- * Adds Multer middleware for handling multipart/form-data, typically used for file uploads.
197
- *
198
- * @param options - Optional configuration options for Multer.
199
- */
200
- setupMulter(options?: multer.Options): multer.Multer;
201
- }
202
- /**
203
- * Singleton class that implements the IConfigure interface.
204
- * Manages the middleware configuration for the application,
205
- * including adding Body Parser and retrieving all configured middlewares.
206
- *
207
- * @see IConfigure
208
- */
209
- declare class Middleware implements IMiddleware {
210
- private middlewarePipeline;
211
- private errorHandler;
212
- private logger;
213
- /**
214
- * Retrieves the type of the middleware.
215
- *
216
- * @param middleware - The middleware to be checked.
217
- *
218
- * @returns The type of the middleware.
219
- */
220
- private getMiddlewareType;
221
- /**
222
- * Checks if a middleware with the given name exists in the middleware collection.
223
- *
224
- * @param middlewareName - The name of the middleware to be checked.
225
- *
226
- * @returns A boolean value indicating whether the middleware exists or not.
227
- */
228
- private middlewareExists;
229
- addRateLimiter(options?: RateLimitOptions): void;
230
- /**
231
- * Adds a Body Parser middleware to the middleware collection using the given options.
232
- *
233
- * @param options - Optional configuration options for the JSON body parser.
234
- */
235
- addBodyParser(options?: OptionsJson): void;
236
- /**
237
- * Adds Cross-Origin Resource Sharing (CORS) middleware to enable or control cross-origin requests.
238
- *
239
- * @param options - Optional configuration options for CORS. Defines the behavior of CORS requests like allowed origins, methods, headers, etc.
240
- */
241
- addCors(options?: CorsOptions): void;
242
- /**
243
- * Adds Compression middleware to reduce the size of the response body and improve the speed of the client-server communication.
244
- *
245
- * @param options - Optional configuration options for Compression. Allows fine-tuning the compression behavior, such as setting the compression level, threshold, and filter functions to determine which requests should be compressed.
246
- */
247
- addCompression(options?: CompressionOptions): void;
248
- /**
249
- * Adds Morgan middleware to log HTTP requests.
250
- *
251
- * @param format - The log format. Can be a string or a function.
252
- * @param options - Optional configuration options for Morgan. Defines the behavior of the logger like the output stream, buffer duration, etc.
253
- */
254
- addMorgan(format: string | FormatFn, options?: OptionsMorgan | undefined): void;
255
- /**
256
- * Adds Cookie Parser middleware to parse the cookie header and populate req.cookies with an object keyed by the cookie names.
257
- *
258
- * @param secret - A string or array used for signing cookies. This is optional and if not specified, the cookie-parser will not parse signed cookies.
259
- * @param options - Optional configuration options for Cookie Parser.
260
- */
261
- addCookieParser(secret?: string | Array<string> | undefined, options?: CookieParserOptions | undefined): void;
262
- /**
263
- * Adds Cookie Session middleware to enable cookie-based sessions.
264
- *
265
- * @param options - Optional configuration options for Cookie Session. Defines the behavior of cookie sessions like the name of the cookie, keys to sign the cookie, etc.
266
- */
267
- addCookieSession(options: CookieSessionOptions): void;
268
- /**
269
- * Adds a middleware to serve the favicon to the middleware collection.
270
- * The favicon is the icon that is displayed in the browser tab for the application.
271
- *
272
- * @param path - The path to the favicon file.
273
- * @param options - Optional configuration options for serving the favicon. Defines the behavior of the favicon middleware like cache control, custom headers, etc.
274
- */
275
- addServeFavicon(path: string | Buffer, options?: ServeFaviconOptions): void;
276
- setupMulter(options?: multer.Options): multer.Multer;
277
- /**
278
- * Adds a middleware to enhance security by setting various HTTP headers.
279
- *
280
- * @param options - Optional configuration options for Helmet.
281
- *
282
- */
283
- addHelmet(options?: OptionsHelmet): void;
284
- /**
285
- * Add a middleware to enable express-session.
286
- *
287
- * @param options - Optional configuration options for Session.
288
- *
289
- */
290
- addSession(options: SessionOptions): void;
291
- /**
292
- * Configures the error handling middleware for the application.
293
- *
294
- * @param errorHandling - The Express error handler function that takes care of processing errors and formulating the response.
295
- */
296
- setErrorHandler(errorHandling?: ExpressHandler): void;
297
- /**
298
- * Adds a middleware to serve static files from the specified root directory.
299
- * Allows the application to serve files like images, CSS, JavaScript, etc.
300
- *
301
- * @param root - The root directory from which the static assets are to be served.
302
- * @param options - Optional configuration options for serving static files. Defines behavior like cache control, custom headers, etc.
303
- */
304
- serveStatic(root: string, options?: ServeStaticOptions): void;
305
- /**
306
- * Helper method to add middleware configuration objects to the middleware collection.
307
- * @param middleware - The middleware configuration object to be added to the middleware collection.
308
- * @returns void
309
- */
310
- private addConfigMiddleware;
311
- /**
312
- * Helper method to add express request handler functions to the middleware collection.
313
- * @param middleware - The express request handler function to be added to the middleware collection.
314
- * @returns void
315
- */
316
- private addExpressHandlerMiddleware;
317
- /**
318
- * Helper method to add custom Expresso middleware to the middleware collection.
319
- * @param middleware - The custom Expresso middleware to be added to the middleware collection.
320
- * @returns void
321
- */
322
- private addIExpressoMiddleware;
323
- /**
324
- * Adds a middleware to the middleware collection.
325
- *
326
- * @param options - The Express request handler function to be added to the middleware collection, or a middleware configuration object
327
- * that is composed by a route and an expressjs handler, or a custom Expresso middleware.
328
- *
329
- * @example Express Handler
330
- * const middleware = (req, res, next) => {
331
- * // Your middleware logic here
332
- * next();
333
- * }
334
- *
335
- * @example Middleware Configuration Object
336
- * const middleware = {
337
- * path: "/",
338
- * middlewares: [] // Array of Express Handlers
339
- * }
340
- *
341
- * @example Expresso Middleware
342
- * class CustomMiddleware implements IExpressoMiddleware {
343
- * use(req: Request, res: Response, next: NextFunction): Promise<void> | void {
344
- * // Your middleware logic here
345
- * next();
346
- * }
347
- * }
348
- */
349
- addMiddleware(options: MiddlewareOptions): void;
350
- /**
351
- * Retrieves middleware pipeline in the order they were added.
352
- *
353
- * @returns An array of Express request handlers representing the middlewares.
354
- */
355
- getMiddlewarePipeline(): Array<MiddlewarePipeline>;
356
- /**
357
- * View middleware pipeline formatted.
358
- * @returns void
359
- */
360
- viewMiddlewarePipeline(): void;
361
- /**
362
- * Gets the configured error handler middleware.
363
- *
364
- * @returns The error handler middleware.
365
- */
366
- getErrorHandler(): ExpressHandler;
367
- }
368
- export { Middleware, IMiddleware };
1
+ /// <reference types="node" />
2
+ import express, { Request, Response, NextFunction } from "express";
3
+ import { OptionsJson } from "./interfaces/body-parser.interface";
4
+ import { CompressionOptions } from "./interfaces/compression.interface";
5
+ import { CorsOptions } from "./interfaces/cors.interface";
6
+ import { CookieParserOptions } from "./interfaces/cookie-parser.interface";
7
+ import { ServeStaticOptions } from "./interfaces/serve-static.interface";
8
+ import { CookieSessionOptions } from "./interfaces/cookie-session/cookie-session.interface";
9
+ import { ServeFaviconOptions } from "./interfaces/serve-favicon.interface";
10
+ import { FormatFn, OptionsMorgan } from "./interfaces/morgan.interface";
11
+ import { RateLimitOptions } from "./interfaces/express-rate-limit.interface";
12
+ import { OptionsHelmet } from "./interfaces/helmet.interface";
13
+ import { multer } from "./interfaces/multer.interface";
14
+ import { SessionOptions } from "./interfaces/express-session.interface";
15
+ /**
16
+ * ExpressHandler Type
17
+ *
18
+ * The ExpressHandler type is a union type that represents various types of Express middleware functions.
19
+ * It can be one of the following types:
20
+ * - express.ErrorRequestHandler: Handles errors in the middleware pipeline.
21
+ * - express.RequestParamHandler: Handles parameters in the middleware pipeline.
22
+ * - express.RequestHandler: General request handler.
23
+ * - undefined: Represents the absence of a handler.
24
+ */
25
+ export type ExpressHandler = express.ErrorRequestHandler | express.RequestParamHandler | express.RequestHandler | undefined;
26
+ /**
27
+ * Expresso middleware interface.
28
+ */
29
+ interface IExpressoMiddleware {
30
+ use(req: Request, res: Response, next: NextFunction): Promise<void> | void;
31
+ }
32
+ /**
33
+ * Abstract class for creating custom Expresso middleware.
34
+ * Custom middleware classes should extend this class and implement the use method.
35
+ *
36
+ */
37
+ export declare abstract class ExpressoMiddleware implements IExpressoMiddleware {
38
+ get name(): string;
39
+ abstract use(req: Request, res: Response, next: NextFunction): Promise<void> | void;
40
+ }
41
+ /**
42
+ * MiddlewareOptions Type
43
+ *
44
+ * The MiddlewareOptions type represents arguments that can be passed to a middleware function.
45
+ * It can either be a expressjs request handler function, a middleware configuration object that is composed by a route and an expressjs handler or
46
+ */
47
+ export type MiddlewareOptions = ExpressHandler | MiddlewareConfig | IExpressoMiddleware;
48
+ /**
49
+ * MiddlewareConfig Interface
50
+ *
51
+ * The MiddlewareConfig interface specifies the structure for middleware configuration objects.
52
+ * - path: Optional. The route path for which the middleware is configured.
53
+ * - middlewares: An array of ExpressHandler types that make up the middleware pipeline for the route specified by 'path'.
54
+ */
55
+ type MiddlewareConfig = {
56
+ path?: string;
57
+ middlewares: Array<ExpressHandler | IExpressoMiddleware>;
58
+ };
59
+ /**
60
+ * MiddlewarePipeline Interface
61
+ *
62
+ * The MiddlewarePipeline interface represents the metadata and actual middleware to be executed in a middleware pipeline.
63
+ * - timestamp: The date and time at which the middleware was added to the pipeline.
64
+ * - middleware: Can be either an ExpressHandler function or a MiddlewareConfig object defining a more complex middleware setup.
65
+ */
66
+ interface MiddlewarePipeline {
67
+ timestamp: Date;
68
+ middleware: ExpressHandler | MiddlewareConfig | IExpressoMiddleware;
69
+ }
70
+ /**
71
+ * Interface for configuring and managing middlewares in the application.
72
+ * Provides methods to be added automatically in the application without the need to import packages.
73
+ */
74
+ interface IMiddleware {
75
+ /**
76
+ * Adds a Rate Limit middleware to the middleware collection.
77
+ * The rate limiter is responsible for adding dynamic rate limit and request throttling to the application.
78
+ *
79
+ * @param options - Optional configuration options for the rate limiter.
80
+ */
81
+ addRateLimiter(options?: RateLimitOptions): void;
82
+ /**
83
+ * Adds a Body Parser middleware to the middleware collection.
84
+ * The body parser is responsible for parsing the incoming request bodies in a middleware.
85
+ *
86
+ * @param options - Optional configuration options for the JSON body parser.
87
+ */
88
+ addBodyParser(options?: OptionsJson): void;
89
+ /**
90
+ * Adds Cross-Origin Resource Sharing (CORS) middleware to enable or control cross-origin requests.
91
+ *
92
+ * @param options - Optional configuration options for CORS. Defines the behavior of CORS requests like allowed origins, methods, headers, etc.
93
+ */
94
+ addCors(options?: CorsOptions): void;
95
+ /**
96
+ * Adds Compression middleware to reduce the size of the response body and improve the speed of the client-server communication.
97
+ *
98
+ * @param options - Optional configuration options for Compression. Allows fine-tuning the compression behavior, such as setting the compression level, threshold, and filter functions to determine which requests should be compressed.
99
+ */
100
+ addCompression(options?: CompressionOptions): void;
101
+ /**
102
+ * Adds Cookie Parser middleware to parse the cookie header and populate req.cookies with an object keyed by the cookie names.
103
+ *
104
+ * @param secret - A string or array used for signing cookies. This is optional and if not specified, the cookie-parser will not parse signed cookies.
105
+ * @param options - Optional configuration options for Cookie Parser.
106
+ */
107
+ addCookieParser(secret?: string | Array<string> | undefined, options?: CookieParserOptions): void;
108
+ /**
109
+ * Adds Cookie Session middleware to enable cookie-based sessions.
110
+ *
111
+ * @param options - Optional configuration options for Cookie Session. Defines the behavior of cookie sessions like the name of the cookie, keys to sign the cookie, etc.
112
+ */
113
+ addCookieSession(options: CookieSessionOptions): void;
114
+ /**
115
+ * Adds Morgan middleware to log HTTP requests.
116
+ *
117
+ * @param format - The log format. Can be a string or a function.
118
+ * @param options - Optional configuration options for Morgan. Defines the behavior of the logger like the output stream, buffer duration, etc.
119
+ */
120
+ addMorgan(format: string | FormatFn, options?: OptionsMorgan): void;
121
+ /**
122
+ * Adds a middleware to serve the favicon to the middleware collection.
123
+ * The favicon is the icon that is displayed in the browser tab for the application.
124
+ *
125
+ * @param path - The path to the favicon file.
126
+ * @param options - Optional configuration options for serving the favicon. Defines the behavior of the favicon middleware like cache control, custom headers, etc.
127
+ */
128
+ addServeFavicon(path: string | Buffer, options?: ServeFaviconOptions): void;
129
+ /**
130
+ * Add a middleware to enable express-session.
131
+ *
132
+ * @param options - Optional configuration options for Session.
133
+ *
134
+ */
135
+ addSession(options: SessionOptions): void;
136
+ /**
137
+ * Configures the error handling middleware for the application.
138
+ *
139
+ * @param errorHandling - The Express error handler function that takes care of processing errors and formulating the response.
140
+ */
141
+ setErrorHandler(errorHandling?: ExpressHandler): void;
142
+ /**
143
+ * Adds a middleware to serve static files from the specified root directory.
144
+ * Allows the application to serve files like images, CSS, JavaScript, etc.
145
+ *
146
+ * @param root - The root directory from which the static assets are to be served.
147
+ * @param options - Optional configuration options for serving static files. Defines behavior like cache control, custom headers, etc.
148
+ */
149
+ serveStatic(root: string, options?: ServeStaticOptions): void;
150
+ /**
151
+ * Adds a middleware to the middleware collection.
152
+ *
153
+ * @param options - The Express request handler function to be added to the middleware collection, or a middleware configuration object
154
+ * that is composed by a route and an expressjs handler, or a custom Expresso middleware.
155
+ *
156
+ * @example Express Handler
157
+ * const middleware = (req, res, next) => {
158
+ * // Your middleware logic here
159
+ * next();
160
+ * }
161
+ *
162
+ * @example Middleware Configuration Object
163
+ * const middleware = {
164
+ * path: "/",
165
+ * middlewares: [] // Array of Express Handlers
166
+ * }
167
+ *
168
+ * @example Expresso Middleware
169
+ * class CustomMiddleware implements IExpressoMiddleware {
170
+ * use(req: Request, res: Response, next: NextFunction): Promise<void> | void {
171
+ * // Your middleware logic here
172
+ * next();
173
+ * }
174
+ * }
175
+ */
176
+ addMiddleware(options: MiddlewareOptions): void;
177
+ /**
178
+ * View middleware pipeline formatted.
179
+ * @returns void
180
+ */
181
+ viewMiddlewarePipeline(): void;
182
+ /**
183
+ * Gets the configured error handler middleware.
184
+ *
185
+ * @returns The error handler middleware.
186
+ */
187
+ getErrorHandler(): ExpressHandler;
188
+ /**
189
+ * Adds Helmet middleware to enhance security by setting various HTTP headers.
190
+ *
191
+ * @param options - Optional configuration options for Helmet.
192
+ * @returns The configuration options for Helmet middleware.
193
+ */
194
+ addHelmet(options?: OptionsHelmet): void;
195
+ /**
196
+ * Adds Multer middleware for handling multipart/form-data, typically used for file uploads.
197
+ *
198
+ * @param options - Optional configuration options for Multer.
199
+ */
200
+ setupMulter(options?: multer.Options): multer.Multer;
201
+ }
202
+ /**
203
+ * Singleton class that implements the IConfigure interface.
204
+ * Manages the middleware configuration for the application,
205
+ * including adding Body Parser and retrieving all configured middlewares.
206
+ *
207
+ * @see IConfigure
208
+ */
209
+ declare class Middleware implements IMiddleware {
210
+ private middlewarePipeline;
211
+ private errorHandler;
212
+ private logger;
213
+ /**
214
+ * Retrieves the type of the middleware.
215
+ *
216
+ * @param middleware - The middleware to be checked.
217
+ *
218
+ * @returns The type of the middleware.
219
+ */
220
+ private getMiddlewareType;
221
+ /**
222
+ * Checks if a middleware with the given name exists in the middleware collection.
223
+ *
224
+ * @param middlewareName - The name of the middleware to be checked.
225
+ *
226
+ * @returns A boolean value indicating whether the middleware exists or not.
227
+ */
228
+ private middlewareExists;
229
+ addRateLimiter(options?: RateLimitOptions): void;
230
+ /**
231
+ * Adds a Body Parser middleware to the middleware collection using the given options.
232
+ *
233
+ * @param options - Optional configuration options for the JSON body parser.
234
+ */
235
+ addBodyParser(options?: OptionsJson): void;
236
+ /**
237
+ * Adds Cross-Origin Resource Sharing (CORS) middleware to enable or control cross-origin requests.
238
+ *
239
+ * @param options - Optional configuration options for CORS. Defines the behavior of CORS requests like allowed origins, methods, headers, etc.
240
+ */
241
+ addCors(options?: CorsOptions): void;
242
+ /**
243
+ * Adds Compression middleware to reduce the size of the response body and improve the speed of the client-server communication.
244
+ *
245
+ * @param options - Optional configuration options for Compression. Allows fine-tuning the compression behavior, such as setting the compression level, threshold, and filter functions to determine which requests should be compressed.
246
+ */
247
+ addCompression(options?: CompressionOptions): void;
248
+ /**
249
+ * Adds Morgan middleware to log HTTP requests.
250
+ *
251
+ * @param format - The log format. Can be a string or a function.
252
+ * @param options - Optional configuration options for Morgan. Defines the behavior of the logger like the output stream, buffer duration, etc.
253
+ */
254
+ addMorgan(format: string | FormatFn, options?: OptionsMorgan | undefined): void;
255
+ /**
256
+ * Adds Cookie Parser middleware to parse the cookie header and populate req.cookies with an object keyed by the cookie names.
257
+ *
258
+ * @param secret - A string or array used for signing cookies. This is optional and if not specified, the cookie-parser will not parse signed cookies.
259
+ * @param options - Optional configuration options for Cookie Parser.
260
+ */
261
+ addCookieParser(secret?: string | Array<string> | undefined, options?: CookieParserOptions | undefined): void;
262
+ /**
263
+ * Adds Cookie Session middleware to enable cookie-based sessions.
264
+ *
265
+ * @param options - Optional configuration options for Cookie Session. Defines the behavior of cookie sessions like the name of the cookie, keys to sign the cookie, etc.
266
+ */
267
+ addCookieSession(options: CookieSessionOptions): void;
268
+ /**
269
+ * Adds a middleware to serve the favicon to the middleware collection.
270
+ * The favicon is the icon that is displayed in the browser tab for the application.
271
+ *
272
+ * @param path - The path to the favicon file.
273
+ * @param options - Optional configuration options for serving the favicon. Defines the behavior of the favicon middleware like cache control, custom headers, etc.
274
+ */
275
+ addServeFavicon(path: string | Buffer, options?: ServeFaviconOptions): void;
276
+ setupMulter(options?: multer.Options): multer.Multer;
277
+ /**
278
+ * Adds a middleware to enhance security by setting various HTTP headers.
279
+ *
280
+ * @param options - Optional configuration options for Helmet.
281
+ *
282
+ */
283
+ addHelmet(options?: OptionsHelmet): void;
284
+ /**
285
+ * Add a middleware to enable express-session.
286
+ *
287
+ * @param options - Optional configuration options for Session.
288
+ *
289
+ */
290
+ addSession(options: SessionOptions): void;
291
+ /**
292
+ * Configures the error handling middleware for the application.
293
+ *
294
+ * @param errorHandling - The Express error handler function that takes care of processing errors and formulating the response.
295
+ */
296
+ setErrorHandler(errorHandling?: ExpressHandler): void;
297
+ /**
298
+ * Adds a middleware to serve static files from the specified root directory.
299
+ * Allows the application to serve files like images, CSS, JavaScript, etc.
300
+ *
301
+ * @param root - The root directory from which the static assets are to be served.
302
+ * @param options - Optional configuration options for serving static files. Defines behavior like cache control, custom headers, etc.
303
+ */
304
+ serveStatic(root: string, options?: ServeStaticOptions): void;
305
+ /**
306
+ * Helper method to add middleware configuration objects to the middleware collection.
307
+ * @param middleware - The middleware configuration object to be added to the middleware collection.
308
+ * @returns void
309
+ */
310
+ private addConfigMiddleware;
311
+ /**
312
+ * Helper method to add express request handler functions to the middleware collection.
313
+ * @param middleware - The express request handler function to be added to the middleware collection.
314
+ * @returns void
315
+ */
316
+ private addExpressHandlerMiddleware;
317
+ /**
318
+ * Helper method to add custom Expresso middleware to the middleware collection.
319
+ * @param middleware - The custom Expresso middleware to be added to the middleware collection.
320
+ * @returns void
321
+ */
322
+ private addIExpressoMiddleware;
323
+ /**
324
+ * Adds a middleware to the middleware collection.
325
+ *
326
+ * @param options - The Express request handler function to be added to the middleware collection, or a middleware configuration object
327
+ * that is composed by a route and an expressjs handler, or a custom Expresso middleware.
328
+ *
329
+ * @example Express Handler
330
+ * const middleware = (req, res, next) => {
331
+ * // Your middleware logic here
332
+ * next();
333
+ * }
334
+ *
335
+ * @example Middleware Configuration Object
336
+ * const middleware = {
337
+ * path: "/",
338
+ * middlewares: [] // Array of Express Handlers
339
+ * }
340
+ *
341
+ * @example Expresso Middleware
342
+ * class CustomMiddleware implements IExpressoMiddleware {
343
+ * use(req: Request, res: Response, next: NextFunction): Promise<void> | void {
344
+ * // Your middleware logic here
345
+ * next();
346
+ * }
347
+ * }
348
+ */
349
+ addMiddleware(options: MiddlewareOptions): void;
350
+ /**
351
+ * Retrieves middleware pipeline in the order they were added.
352
+ *
353
+ * @returns An array of Express request handlers representing the middlewares.
354
+ */
355
+ getMiddlewarePipeline(): Array<MiddlewarePipeline>;
356
+ /**
357
+ * View middleware pipeline formatted.
358
+ * @returns void
359
+ */
360
+ viewMiddlewarePipeline(): void;
361
+ /**
362
+ * Gets the configured error handler middleware.
363
+ *
364
+ * @returns The error handler middleware.
365
+ */
366
+ getErrorHandler(): ExpressHandler;
367
+ }
368
+ export { Middleware, IMiddleware };