wnodex 0.1.0 → 0.2.1

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 (207) hide show
  1. package/.turbo/turbo-build.log +7 -0
  2. package/LICENSE +1 -1
  3. package/README.md +5 -91
  4. package/dist/{schemas/wnodex-config.d.ts → config.d.ts} +14 -9
  5. package/dist/config.d.ts.map +1 -0
  6. package/dist/config.js +22 -0
  7. package/dist/index.d.ts +68 -38
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +126 -41
  10. package/dist/setup-config.d.ts +3 -0
  11. package/dist/setup-config.d.ts.map +1 -0
  12. package/dist/setup-config.js +9 -0
  13. package/dist/setup-middlewares.d.ts +4 -0
  14. package/dist/setup-middlewares.d.ts.map +1 -0
  15. package/dist/setup-middlewares.js +32 -0
  16. package/package.json +42 -15
  17. package/rolldown.config.ts +20 -0
  18. package/src/config.ts +27 -0
  19. package/src/index.ts +149 -38
  20. package/src/setup-config.ts +17 -0
  21. package/src/setup-middlewares.ts +49 -0
  22. package/tests/body-parser.schema.test.ts +57 -0
  23. package/tests/compression.schema.test.ts +27 -0
  24. package/tests/cookie-parser.schema.test.ts +56 -0
  25. package/tests/cors.schema.test.ts +35 -0
  26. package/tests/helmet.schema.test.ts +33 -0
  27. package/tests/hpp.schema.test.ts +31 -0
  28. package/tests/index.test.ts +105 -0
  29. package/tests/passport.schema.test.ts +33 -0
  30. package/tests/rate-limit.schema.test.ts +56 -0
  31. package/tests/session.schema.test.ts +80 -0
  32. package/tsconfig.json +8 -8
  33. package/vitest.config.ts +13 -0
  34. package/dist/consts/cors.d.ts +0 -15
  35. package/dist/consts/cors.d.ts.map +0 -1
  36. package/dist/consts/cors.js +0 -29
  37. package/dist/consts/error-codes.d.ts +0 -10
  38. package/dist/consts/error-codes.d.ts.map +0 -1
  39. package/dist/consts/error-codes.js +0 -12
  40. package/dist/consts/index.d.ts +0 -8
  41. package/dist/consts/index.d.ts.map +0 -1
  42. package/dist/consts/index.js +0 -10
  43. package/dist/consts/log-levels.d.ts +0 -8
  44. package/dist/consts/log-levels.d.ts.map +0 -1
  45. package/dist/consts/log-levels.js +0 -10
  46. package/dist/consts/node-env.d.ts +0 -7
  47. package/dist/consts/node-env.d.ts.map +0 -1
  48. package/dist/consts/node-env.js +0 -9
  49. package/dist/consts/port.d.ts +0 -6
  50. package/dist/consts/port.d.ts.map +0 -1
  51. package/dist/consts/port.js +0 -26
  52. package/dist/consts/rate-limit.d.ts +0 -9
  53. package/dist/consts/rate-limit.d.ts.map +0 -1
  54. package/dist/consts/rate-limit.js +0 -11
  55. package/dist/consts/session.d.ts +0 -6
  56. package/dist/consts/session.d.ts.map +0 -1
  57. package/dist/consts/session.js +0 -8
  58. package/dist/errors/base-error.d.ts +0 -57
  59. package/dist/errors/base-error.d.ts.map +0 -1
  60. package/dist/errors/base-error.js +0 -93
  61. package/dist/errors/config-error.d.ts +0 -20
  62. package/dist/errors/config-error.d.ts.map +0 -1
  63. package/dist/errors/config-error.js +0 -30
  64. package/dist/errors/http-error.d.ts +0 -17
  65. package/dist/errors/http-error.d.ts.map +0 -1
  66. package/dist/errors/http-error.js +0 -24
  67. package/dist/errors/index.d.ts +0 -5
  68. package/dist/errors/index.d.ts.map +0 -1
  69. package/dist/errors/index.js +0 -7
  70. package/dist/errors/validation-error.d.ts +0 -19
  71. package/dist/errors/validation-error.d.ts.map +0 -1
  72. package/dist/errors/validation-error.js +0 -29
  73. package/dist/helpers/get-node-env.d.ts +0 -13
  74. package/dist/helpers/get-node-env.d.ts.map +0 -1
  75. package/dist/helpers/get-node-env.js +0 -27
  76. package/dist/helpers/index.d.ts +0 -5
  77. package/dist/helpers/index.d.ts.map +0 -1
  78. package/dist/helpers/index.js +0 -7
  79. package/dist/helpers/is-development.d.ts +0 -9
  80. package/dist/helpers/is-development.d.ts.map +0 -1
  81. package/dist/helpers/is-development.js +0 -16
  82. package/dist/helpers/setup-config.d.ts +0 -12
  83. package/dist/helpers/setup-config.d.ts.map +0 -1
  84. package/dist/helpers/setup-config.js +0 -21
  85. package/dist/helpers/setup-middlewares.d.ts +0 -12
  86. package/dist/helpers/setup-middlewares.d.ts.map +0 -1
  87. package/dist/helpers/setup-middlewares.js +0 -45
  88. package/dist/logger.d.ts +0 -3
  89. package/dist/logger.d.ts.map +0 -1
  90. package/dist/logger.js +0 -21
  91. package/dist/middlewares/compression.d.ts +0 -18
  92. package/dist/middlewares/compression.d.ts.map +0 -1
  93. package/dist/middlewares/compression.js +0 -26
  94. package/dist/middlewares/cookie-parser.ts.d.ts +0 -20
  95. package/dist/middlewares/cookie-parser.ts.d.ts.map +0 -1
  96. package/dist/middlewares/cookie-parser.ts.js +0 -33
  97. package/dist/middlewares/cors.d.ts +0 -22
  98. package/dist/middlewares/cors.d.ts.map +0 -1
  99. package/dist/middlewares/cors.js +0 -34
  100. package/dist/middlewares/error-handler.d.ts +0 -21
  101. package/dist/middlewares/error-handler.d.ts.map +0 -1
  102. package/dist/middlewares/error-handler.js +0 -43
  103. package/dist/middlewares/helmet.d.ts +0 -16
  104. package/dist/middlewares/helmet.d.ts.map +0 -1
  105. package/dist/middlewares/helmet.js +0 -28
  106. package/dist/middlewares/hpp.d.ts +0 -18
  107. package/dist/middlewares/hpp.d.ts.map +0 -1
  108. package/dist/middlewares/hpp.js +0 -30
  109. package/dist/middlewares/index.d.ts +0 -10
  110. package/dist/middlewares/index.d.ts.map +0 -1
  111. package/dist/middlewares/index.js +0 -12
  112. package/dist/middlewares/passport.d.ts +0 -16
  113. package/dist/middlewares/passport.d.ts.map +0 -1
  114. package/dist/middlewares/passport.js +0 -27
  115. package/dist/middlewares/rate-limit.d.ts +0 -23
  116. package/dist/middlewares/rate-limit.d.ts.map +0 -1
  117. package/dist/middlewares/rate-limit.js +0 -35
  118. package/dist/middlewares/session.d.ts +0 -16
  119. package/dist/middlewares/session.d.ts.map +0 -1
  120. package/dist/middlewares/session.js +0 -23
  121. package/dist/schemas/compression.d.ts +0 -5
  122. package/dist/schemas/compression.d.ts.map +0 -1
  123. package/dist/schemas/compression.js +0 -5
  124. package/dist/schemas/cookie-parser.d.ts +0 -9
  125. package/dist/schemas/cookie-parser.d.ts.map +0 -1
  126. package/dist/schemas/cookie-parser.js +0 -14
  127. package/dist/schemas/cors.d.ts +0 -6
  128. package/dist/schemas/cors.d.ts.map +0 -1
  129. package/dist/schemas/cors.js +0 -9
  130. package/dist/schemas/error-metadata.d.ts +0 -18
  131. package/dist/schemas/error-metadata.d.ts.map +0 -1
  132. package/dist/schemas/error-metadata.js +0 -12
  133. package/dist/schemas/helmet.d.ts +0 -6
  134. package/dist/schemas/helmet.d.ts.map +0 -1
  135. package/dist/schemas/helmet.js +0 -8
  136. package/dist/schemas/hpp.d.ts +0 -5
  137. package/dist/schemas/hpp.d.ts.map +0 -1
  138. package/dist/schemas/hpp.js +0 -8
  139. package/dist/schemas/index.d.ts +0 -13
  140. package/dist/schemas/index.d.ts.map +0 -1
  141. package/dist/schemas/index.js +0 -15
  142. package/dist/schemas/node-env.d.ts +0 -16
  143. package/dist/schemas/node-env.d.ts.map +0 -1
  144. package/dist/schemas/node-env.js +0 -8
  145. package/dist/schemas/passport.d.ts +0 -6
  146. package/dist/schemas/passport.d.ts.map +0 -1
  147. package/dist/schemas/passport.js +0 -8
  148. package/dist/schemas/port.d.ts +0 -4
  149. package/dist/schemas/port.d.ts.map +0 -1
  150. package/dist/schemas/port.js +0 -16
  151. package/dist/schemas/rate-limit.d.ts +0 -9
  152. package/dist/schemas/rate-limit.d.ts.map +0 -1
  153. package/dist/schemas/rate-limit.js +0 -26
  154. package/dist/schemas/session.d.ts +0 -18
  155. package/dist/schemas/session.d.ts.map +0 -1
  156. package/dist/schemas/session.js +0 -32
  157. package/dist/schemas/wnodex-config.d.ts.map +0 -1
  158. package/dist/schemas/wnodex-config.js +0 -24
  159. package/dist/tsconfig.lib.tsbuildinfo +0 -1
  160. package/dist/wnodex.d.ts +0 -69
  161. package/dist/wnodex.d.ts.map +0 -1
  162. package/dist/wnodex.js +0 -131
  163. package/src/consts/cors.ts +0 -27
  164. package/src/consts/error-codes.ts +0 -9
  165. package/src/consts/index.ts +0 -7
  166. package/src/consts/log-levels.ts +0 -7
  167. package/src/consts/node-env.ts +0 -6
  168. package/src/consts/port.ts +0 -23
  169. package/src/consts/rate-limit.ts +0 -9
  170. package/src/consts/session.ts +0 -6
  171. package/src/errors/base-error.ts +0 -111
  172. package/src/errors/config-error.ts +0 -31
  173. package/src/errors/http-error.ts +0 -21
  174. package/src/errors/index.ts +0 -4
  175. package/src/errors/validation-error.ts +0 -30
  176. package/src/helpers/get-node-env.ts +0 -27
  177. package/src/helpers/index.ts +0 -4
  178. package/src/helpers/is-development.ts +0 -15
  179. package/src/helpers/setup-config.ts +0 -25
  180. package/src/helpers/setup-middlewares.ts +0 -56
  181. package/src/logger.ts +0 -22
  182. package/src/middlewares/compression.ts +0 -32
  183. package/src/middlewares/cookie-parser.ts.ts +0 -37
  184. package/src/middlewares/cors.ts +0 -35
  185. package/src/middlewares/error-handler.ts +0 -54
  186. package/src/middlewares/helmet.ts +0 -29
  187. package/src/middlewares/hpp.ts +0 -31
  188. package/src/middlewares/index.ts +0 -9
  189. package/src/middlewares/passport.ts +0 -30
  190. package/src/middlewares/rate-limit.ts +0 -39
  191. package/src/middlewares/session.ts +0 -25
  192. package/src/schemas/compression.ts +0 -8
  193. package/src/schemas/cookie-parser.ts +0 -21
  194. package/src/schemas/cors.ts +0 -13
  195. package/src/schemas/error-metadata.ts +0 -15
  196. package/src/schemas/helmet.ts +0 -11
  197. package/src/schemas/hpp.ts +0 -9
  198. package/src/schemas/index.ts +0 -12
  199. package/src/schemas/node-env.ts +0 -11
  200. package/src/schemas/passport.ts +0 -11
  201. package/src/schemas/port.ts +0 -22
  202. package/src/schemas/rate-limit.ts +0 -33
  203. package/src/schemas/session.ts +0 -40
  204. package/src/schemas/wnodex-config.ts +0 -26
  205. package/src/wnodex.ts +0 -152
  206. package/tsconfig.lib.json +0 -13
  207. package/tsconfig.tsbuildinfo +0 -1
@@ -1,111 +0,0 @@
1
- import type {
2
- ErrorMetadataInput,
3
- ErrorMetadataOutput,
4
- } from '../schemas/error-metadata.js';
5
- import { ErrorMetadataSchema } from '../schemas/error-metadata.js';
6
-
7
- /**
8
- * Base class for all custom application errors.
9
- * Extends the native `Error` to preserve stack traces and adds structured, validated metadata.
10
- *
11
- * Provides consistent error information for debugging and structured logging.
12
- */
13
- export class BaseError extends Error {
14
- /**
15
- * The machine-readable error code, suitable for programmatic handling.
16
- * @readonly
17
- */
18
- public readonly code: string;
19
-
20
- /**
21
- * The optional cause that triggered this error instance.
22
- * May be any value, but usually another Error object for chained errors.
23
- * @override
24
- * @readonly
25
- */
26
- public override readonly cause?: unknown;
27
-
28
- /**
29
- * Additional error context for debugging (may contain any serializable data).
30
- * @readonly
31
- */
32
- public readonly context?: unknown;
33
-
34
- /**
35
- * Constructs a new instance of BaseError.
36
- * Validates the provided metadata to ensure it matches the ErrorMetadataSchema.
37
- * @param message The human-readable error message.
38
- * @param metadata Structured metadata for this error instance. It will be validated.
39
- * @throws {Error} Throws if the metadata is invalid according to ErrorMetadataSchema.
40
- * @example
41
- * const err = new BaseError('User not found', { code: 'USER_NOT_FOUND' });
42
- */
43
- constructor(message: string, metadata: ErrorMetadataInput) {
44
- super(message);
45
-
46
- // Sets the name property to the actual class name (useful with inheritance).
47
- this.name = new.target.name;
48
-
49
- const validatedMetadata = this.validateMetadata(metadata, message);
50
-
51
- this.code = validatedMetadata.code;
52
- this.cause = validatedMetadata.cause;
53
- this.context = validatedMetadata.context;
54
-
55
- // Ensures proper prototype chain for custom errors (required for ES5/TypeScript).
56
- Object.setPrototypeOf(this, new.target.prototype);
57
- }
58
-
59
- /**
60
- * Validates the error metadata using ErrorMetadataSchema.
61
- * Throws a clear error if validation fails, including the original Zod error as the cause.
62
- * @param input The raw metadata input.
63
- * @param parentErrorMessage The message of the error being constructed, for improved diagnostics.
64
- * @returns {ErrorMetadataOutput} Parsed and validated metadata.
65
- * @throws {Error} If the metadata does not pass validation.
66
- * @example
67
- * this.validateMetadata({ code: 'INVALID', context: { foo: 42 } }, 'Something failed');
68
- */
69
- private validateMetadata(
70
- input: ErrorMetadataInput,
71
- parentErrorMessage: string
72
- ): ErrorMetadataOutput {
73
- const result = ErrorMetadataSchema.safeParse(input);
74
-
75
- if (!result.success) {
76
- const errorMessage = `Invalid metadata provided for error "${parentErrorMessage}" (Code: ${input.code || 'N/A'}).`;
77
-
78
- throw new Error(errorMessage, { cause: result.error });
79
- }
80
-
81
- return result.data;
82
- }
83
-
84
- /**
85
- * Returns a plain object representation of the error,
86
- * useful for structured logging, serialization, or external error reporting.
87
- * Nested error causes include their name, message, and stack trace.
88
- * @returns {object} A plain, serializable object representing this error.
89
- * @example
90
- * JSON.stringify(err.toJSON());
91
- */
92
- toJSON(): object {
93
- const normalizedCause =
94
- this.cause instanceof Error
95
- ? {
96
- name: this.cause.name,
97
- message: this.cause.message,
98
- stack: this.cause.stack,
99
- }
100
- : this.cause;
101
-
102
- return {
103
- name: this.name,
104
- message: this.message,
105
- code: this.code,
106
- cause: normalizedCause,
107
- context: this.context,
108
- stack: this.stack,
109
- };
110
- }
111
- }
@@ -1,31 +0,0 @@
1
- import { ERROR_CODES } from '../consts/error-codes.js';
2
- import { BaseError } from './base-error.js';
3
-
4
- /**
5
- * Custom error class for configuration failures.
6
- * Represents issues occurring during loading, parsing, or applying application configuration.
7
- * Automatically sets the error code to `"CONFIG_ERROR"`. Useful for problems involving environment
8
- * variables, config files, or settings.
9
- */
10
- export class ConfigError extends BaseError {
11
- /**
12
- * Constructs a new ConfigError instance.
13
- * @param message The human-readable error message describing the configuration problem.
14
- * @param cause Optional original error or value that triggered the configuration failure, such as a file system or validation error.
15
- * @param context Optional additional context data, such as the config file path, setting name, or problematic value.
16
- *
17
- * @example
18
- * throw new ConfigError('Failed to load config file', readError, { file: './config.json' });
19
- */
20
- constructor(
21
- message: string,
22
- cause?: unknown,
23
- context?: Record<string | number | symbol, unknown>
24
- ) {
25
- super(message, {
26
- code: ERROR_CODES.CONFIG_ERROR,
27
- cause,
28
- context,
29
- });
30
- }
31
- }
@@ -1,21 +0,0 @@
1
- /**
2
- * Custom HTTP error class with status code.
3
- * Represents an HTTP error with both status code and message.
4
- */
5
- export class HttpError extends Error {
6
- public readonly statusCode: number;
7
-
8
- /**
9
- * Creates an instance of HttpError.
10
- * @param message Error message describing the problem.
11
- * @param statusCode HTTP status code, default is 500.
12
- * @example
13
- * // Throws an error with 404 status code
14
- * throw new HttpError('Not Found', 404);
15
- */
16
- constructor(message: string, statusCode = 500) {
17
- super(message);
18
- this.statusCode = statusCode;
19
- this.name = this.constructor.name;
20
- }
21
- }
@@ -1,4 +0,0 @@
1
- export * from './base-error.js';
2
- export * from './config-error.js';
3
- export * from './http-error.js';
4
- export * from './validation-error.js';
@@ -1,30 +0,0 @@
1
- import { ERROR_CODES } from '../consts/error-codes.js';
2
- import { BaseError } from './base-error.js';
3
-
4
- /**
5
- * Custom error class for validation failures.
6
- * Used for input errors, schema violations, or data integrity issues.
7
- * Automatically sets the error code to `"VALIDATION_ERROR"` to allow standard handling of validation errors.
8
- */
9
- export class ValidationError extends BaseError {
10
- /**
11
- * Constructs a new ValidationError instance.
12
- * @param message The human-readable error message describing what failed validation.
13
- * @param cause Optional original error or value that caused the validation failure, such as a ZodError or validation detail.
14
- * @param context Optional additional context data, for example the invalid field, config path, or offending input.
15
- *
16
- * @example
17
- * throw new ValidationError('Invalid email format', zodResult.error, { field: 'email' });
18
- */
19
- constructor(
20
- message: string,
21
- cause?: unknown,
22
- context?: Record<string | number | symbol, unknown>
23
- ) {
24
- super(message, {
25
- code: ERROR_CODES.VALIDATION_ERROR,
26
- cause,
27
- context,
28
- });
29
- }
30
- }
@@ -1,27 +0,0 @@
1
- import { NODE_ENV_VALUES } from '../consts/node-env.js';
2
- import { ValidationError } from '../errors/validation-error.js';
3
- import { type NodeEnv, NodeEnvSchema } from '../schemas/node-env.js';
4
-
5
- /**
6
- * Parses the `NODE_ENV` environment variable from `process.env`.
7
- * @returns {NodeEnv} The validated environment value.
8
- * @throws {ValidationError} Thrown if `NODE_ENV` is invalid or not defined according to schema.
9
- *
10
- * @example
11
- * // Example usage:
12
- * const env = getNodeEnv();
13
- * // env will be one of 'production', 'development', etc.
14
- */
15
- export function getNodeEnv(): NodeEnv {
16
- const rawNodeEnv = process.env.NODE_ENV;
17
- const result = NodeEnvSchema.safeParse(rawNodeEnv);
18
-
19
- if (!result.success) {
20
- throw new ValidationError('Invalid Node Env', result.error, {
21
- provided: rawNodeEnv,
22
- expected: Object.values(NODE_ENV_VALUES).join('|'),
23
- });
24
- }
25
-
26
- return result.data;
27
- }
@@ -1,4 +0,0 @@
1
- export * from './get-node-env.js';
2
- export * from './is-development.js';
3
- export * from './setup-config.js';
4
- export * from './setup-middlewares.js';
@@ -1,15 +0,0 @@
1
- import { NODE_ENV_VALUES } from '../consts/node-env.js';
2
- import { getNodeEnv } from './get-node-env.js';
3
-
4
- /**
5
- * Returns true if process.env.NODE_ENV is set to 'development'.
6
- * @returns {boolean} Returns `true` if the current Node environment is development, otherwise `false`.
7
- *
8
- * @example
9
- * const dev = isDevelopment();
10
- */
11
- export function isDevelopment(): boolean {
12
- const nodeEnv = getNodeEnv();
13
-
14
- return nodeEnv === NODE_ENV_VALUES.DEVELOPMENT;
15
- }
@@ -1,25 +0,0 @@
1
- import { ConfigError } from '../errors/config-error.js';
2
- import {
3
- WnodexConfigInput,
4
- WnodexConfigOutput,
5
- WnodexConfigSchema,
6
- } from '../schemas';
7
-
8
- /**
9
- * Validates the provided config using NodexConfigSchema.
10
- * Throws ValidationError if validation fails.
11
- * @param config Raw configuration input for Nodex.
12
- * @returns Validated configuration.
13
- * @throws {ConfigError} If configuration is invalid.
14
- * @example
15
- * const config = setupConfig(rawConfig);
16
- */
17
- export function setupConfig(config: WnodexConfigInput): WnodexConfigOutput {
18
- const result = WnodexConfigSchema.safeParse(config);
19
-
20
- if (!result.success) {
21
- throw new ConfigError('Invalid Nodex Configuration', result.error);
22
- }
23
-
24
- return result.data;
25
- }
@@ -1,56 +0,0 @@
1
- import express, { type Application } from 'express';
2
-
3
- import { configureCompression } from '../middlewares/compression.js';
4
- import { configureCookieParser } from '../middlewares/cookie-parser.ts.js';
5
- import { configureCors } from '../middlewares/cors.js';
6
- import { errorHandler } from '../middlewares/error-handler.js';
7
- import { configureHelmet } from '../middlewares/helmet.js';
8
- import { configureHpp } from '../middlewares/hpp.js';
9
- import { configurePassport } from '../middlewares/passport.js';
10
- import { configureRateLimit } from '../middlewares/rate-limit.js';
11
- import { configureSession } from '../middlewares/session.js';
12
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
13
-
14
- /**
15
- * Sets up Express middlewares for the application.
16
- * Currently applies the helmet security middleware.
17
- * @param app The express application instance.
18
- * @param config The Nodex parsed config object.
19
- * @example
20
- * this.config = setupMiddlewares();
21
- */
22
- export function setupMiddlewares(
23
- app: Application,
24
- config: WnodexConfigOutput
25
- ): void {
26
- // Helmet
27
- configureHelmet(app, config);
28
-
29
- // CORS
30
- configureCors(app, config);
31
-
32
- // Body Parsers
33
- app.use(express.json());
34
- app.use(express.urlencoded({ extended: true }));
35
-
36
- // cookie-parser
37
- configureCookieParser(app, config);
38
-
39
- // compression
40
- configureCompression(app, config);
41
-
42
- // express-rate-limit
43
- configureRateLimit(app, config);
44
-
45
- // hpp
46
- configureHpp(app, config);
47
-
48
- // express-session - Session management
49
- configureSession(app, config);
50
-
51
- // passport - Authentication middleware
52
- configurePassport(app, config);
53
-
54
- // Error Handler (keep it last)
55
- app.use(errorHandler);
56
- }
package/src/logger.ts DELETED
@@ -1,22 +0,0 @@
1
- import pretty from 'pino-pretty';
2
-
3
- import pino from 'pino';
4
-
5
- import { LOG_LEVELS } from './consts/log-levels.js';
6
- import { isDevelopment } from './helpers/is-development.js';
7
-
8
- const prettyStream = pretty({
9
- colorize: true,
10
- levelFirst: true,
11
- translateTime: true,
12
- ignore: 'pid,hostname',
13
- singleLine: true,
14
- });
15
-
16
- const baseOptions = {
17
- level: LOG_LEVELS.INFO,
18
- };
19
-
20
- export const logger = isDevelopment()
21
- ? pino(baseOptions, prettyStream)
22
- : pino(baseOptions);
@@ -1,32 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import compression from 'compression';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures compression middleware to compress HTTP responses,
9
- * improving network performance by reducing response size.
10
- * Reads a flag from the app configuration to enable or disable compression.
11
- * @param app Express Application instance to apply compression middleware on.
12
- * @param config Typed Nodex configuration object containing compression option.
13
- *
14
- * @returns The result of app.use(compression()) if enabled, otherwise undefined.
15
- *
16
- * @example
17
- * private setupMiddlewares() {
18
- * configureCors(this.app, this.config);
19
- * }
20
- */
21
- export function configureCompression(
22
- app: Application,
23
- config: WnodexConfigOutput
24
- ) {
25
- const { compression: isCompressionEnabled } = config;
26
-
27
- if (!isCompressionEnabled) {
28
- return;
29
- }
30
-
31
- return app.use(compression());
32
- }
@@ -1,37 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import cookieParser from 'cookie-parser';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures the cookie-parser middleware for the Express app based on the provided configuration.
9
- * @param app The Express application instance to configure.
10
- * @param config The application configuration object, which includes cookieParser options.
11
- * @returns Void.
12
- *
13
- * @example
14
- * ```
15
- * import express from 'express';
16
- * import { configureCookieParser } from './middlewares/cookie-parser';
17
- * import config from './config';
18
- *
19
- * const app = express();
20
- * configureCookieParser(app, config);
21
- * ```
22
- */
23
- export function configureCookieParser(
24
- app: Application,
25
- config: WnodexConfigOutput
26
- ) {
27
- const { cookieParser: options } = config;
28
-
29
- if (typeof options === 'boolean') {
30
- if (!options) return;
31
- return app.use(cookieParser());
32
- } else {
33
- const { secret, options: opts } = options;
34
-
35
- return app.use(cookieParser(secret, opts));
36
- }
37
- }
@@ -1,35 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import cors from 'cors';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures the CORS middleware for an Express application.
9
- * Reads the `cors` option from the provided config.
10
- * If the option is a boolean:
11
- * - `false` disables CORS middleware.
12
- * - `true` enables CORS middleware with default settings.
13
- * If the option is an object, it is spread into the CORS middleware options.
14
- * @param app The Express Application instance to attach middleware to.
15
- * @param config Typed configuration input that includes the CORS option.
16
- *
17
- * @returns The Express Application instance with CORS middleware applied,
18
- * or undefined if CORS is disabled.
19
- *
20
- * @example
21
- * private setupMiddlewares() {
22
- * configureCors(this.app, this.config);
23
- * }
24
- */
25
- export function configureCors(app: Application, config: WnodexConfigOutput) {
26
- const { cors: options } = config;
27
-
28
- if (typeof options === 'boolean') {
29
- if (options === false || !options) return;
30
-
31
- return app.use(cors());
32
- } else {
33
- return app.use(cors({ ...options }));
34
- }
35
- }
@@ -1,54 +0,0 @@
1
- import type { NextFunction, Request, Response } from 'express';
2
-
3
- import type { HttpError } from '../errors/http-error.js';
4
- import { isDevelopment } from '../helpers/is-development.js';
5
-
6
- /**
7
- * Middleware for centralized error handling.
8
- * Catches all errors thrown in the application and responds with a structured JSON.
9
- * Logs the error for debugging purposes.
10
- * @param err The thrown error object or value.
11
- * @param _req The current Express request object.
12
- * @param res The current Express response object.
13
- * @param _next Next middleware function in chain.
14
- *
15
- * @example
16
- * private setupMiddlewares() {
17
- * // other middlewares
18
- * // ...
19
- * // Configure errorHandler as the last one middleware.
20
- * app.use(errorHandler);
21
- * }
22
- */
23
- export function errorHandler(
24
- err: HttpError | Error,
25
- _req: Request,
26
- res: Response,
27
- _next: NextFunction
28
- ): void {
29
- // Log the error for server debugging
30
- console.error(err);
31
-
32
- // Determine HTTP status code; default to 500 if unavailable
33
- const statusCode =
34
- typeof err === 'object' && err !== null && 'statusCode' in err
35
- ? err.statusCode
36
- : 500;
37
-
38
- // Get message or default to 'Internal Server Error'
39
- const message =
40
- typeof err === 'object' && err !== null && 'message' in err
41
- ? err.message
42
- : 'Internal Server Error';
43
-
44
- // Send JSON response
45
- res.status(statusCode).json({
46
- error: {
47
- message,
48
- // Show stack trace only in development environment
49
- ...(isDevelopment() && {
50
- stack: err.stack,
51
- }),
52
- },
53
- });
54
- }
@@ -1,29 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import helmet from 'helmet';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures the Helmet middleware in an Express app.
9
- * @param app The Express Application instance on which to apply Helmet.
10
- * @param config Typed project configuration from which the Helmet option is read.
11
- *
12
- * @returns The Express Application instance (`app`) if Helmet is enable.
13
- *
14
- * @example
15
- * private setupMiddlewares() {
16
- * configureHelmet(this.app, this.config);
17
- * }
18
- */
19
- export function configureHelmet(app: Application, config: WnodexConfigOutput) {
20
- const { helmet: options } = config;
21
-
22
- if (typeof options === 'boolean') {
23
- if (options === false || !options) return;
24
-
25
- return app.use(helmet());
26
- } else {
27
- return app.use(helmet({ ...options }));
28
- }
29
- }
@@ -1,31 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import hpp from 'hpp';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures the hpp middleware protection against HTTP Parameter Pollution.
9
- *
10
- * Applies the hpp middleware to the Express application instance based on the
11
- * provided configuration. If configuration is a boolean and true, applies with default.
12
- * If configuration is an array, uses it as a whitelist of allowed duplicate parameters.
13
- * @param app The Express application instance.
14
- * @param config The normalized middleware configuration, specifying hpp options.
15
- * @returns Void.
16
- *
17
- * @example
18
- * configureHpp(app, { hpp: true });
19
- * configureHpp(app, { hpp: ['param1', 'param2'] });
20
- */
21
- export function configureHpp(app: Application, config: WnodexConfigOutput) {
22
- const { hpp: options } = config;
23
-
24
- if (typeof options === 'boolean') {
25
- if (options === false || !options) return;
26
-
27
- return app.use(hpp());
28
- } else {
29
- return app.use(hpp({ whitelist: [...options] }));
30
- }
31
- }
@@ -1,9 +0,0 @@
1
- export * from './compression.js';
2
- export * from './cookie-parser.ts.js';
3
- export * from './cors.js';
4
- export * from './error-handler.js';
5
- export * from './helmet.js';
6
- export * from './hpp.js';
7
- export * from './passport.js';
8
- export * from './rate-limit.js';
9
- export * from './session.js';
@@ -1,30 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import type { WnodexConfigInput } from '../schemas/wnodex-config.js';
4
-
5
- /**
6
- * Configures the passport instance.
7
- * @param app The express application instance.
8
- * @param config The Wnodex raw config.
9
- * @returns The passport instance.
10
- * @example
11
- * construction(config: WnodexConfigInput) {
12
- * this.app = express();
13
- * this.config = configureConfig(config);
14
- * this.passport = configurePassport(this.app, this.config)
15
- * }
16
- */
17
- export function configurePassport(app: Application, config: WnodexConfigInput) {
18
- const { passport } = config;
19
-
20
- /**
21
- * If config.passport is either undefined, true (invalid config), or false, passport won't be instantiated.
22
- */
23
- if (!passport || typeof passport === 'boolean') return;
24
-
25
- app.set('passport', passport);
26
- app.use(passport.initialize());
27
- app.use(passport.session());
28
-
29
- return passport;
30
- }
@@ -1,39 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import rateLimit from 'express-rate-limit';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures the rate limiting middleware to protect the application
9
- * from excessive requests, mitigating abuse and denial of service attacks.
10
- *
11
- * It reads the `rateLimit` option from the provided config.
12
- * If it's a boolean:
13
- * - `false` disables rate limiting.
14
- * - `true` enables rate limiting with default settings.
15
- * If it's an object, it spreads the properties into the rateLimit options.
16
- * @param app Express Application instance to apply the rate limiter to.
17
- * @param config Typed application configuration object containing rateLimit option.
18
- *
19
- * @returns The result of `app.use(rateLimit())` if rate limiting is enabled, otherwise `undefined`.
20
- *
21
- * @example
22
- * private setupMiddlewares() {
23
- * configureHelmet(this.app, this.config);
24
- * }
25
- */
26
- export function configureRateLimit(
27
- app: Application,
28
- config: WnodexConfigOutput
29
- ) {
30
- const { rateLimit: options } = config;
31
-
32
- if (typeof options === 'boolean') {
33
- if (options === false || !options) return;
34
-
35
- return app.use(rateLimit());
36
- } else {
37
- return app.use(rateLimit({ ...options }));
38
- }
39
- }