@yamato-daiwa/express-extensions 1.1.2 → 1.2.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.
@@ -21,7 +21,7 @@ class QueryParametersProcessor {
21
21
  serializedQueryParameters = request.url.split("?")[1];
22
22
  }
23
23
  catch (error) {
24
- es_extensions_1.Logger.throwErrorAndLog({
24
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
25
25
  errorInstance: new QueryParametersProcessor.QueryParametersDeserializingError(),
26
26
  title: QueryParametersProcessor.QueryParametersDeserializingError.localization.defaultTitle,
27
27
  occurrenceLocation: "QueryParametersProcessor.process(propertiesSpecification, deserializer)",
@@ -31,10 +31,10 @@ class QueryParametersProcessor {
31
31
  const deserializedQueryParameters = deserializer(serializedQueryParameters ?? "");
32
32
  const processingResult = es_extensions_1.RawObjectDataProcessor.process(deserializedQueryParameters, {
33
33
  nameForLogging: "DeserializedQueryParameters",
34
- subtype: es_extensions_1.RawObjectDataProcessor.ObjectSubtypes.fixedKeyAndValuePairsObject,
34
+ subtype: es_extensions_1.RawObjectDataProcessor.ObjectSubtypes.fixedSchema,
35
35
  properties: propertiesSpecification
36
36
  });
37
- if (processingResult.rawDataIsInvalid) {
37
+ if (processingResult.isRawDataInvalid) {
38
38
  throw new routing_controllers_1.BadRequestError(processingResult.validationErrorsMessages.join("\n"));
39
39
  }
40
40
  return processingResult.processedData;
@@ -22,7 +22,7 @@ function parseAndValidateJSON_RequestBody({ requestBodySizeLimit__bytesPackageFo
22
22
  mustOutputIf: mustLogDataAfterParsing
23
23
  });
24
24
  const requestBodyValidationAndProcessingResult = es_extensions_1.RawObjectDataProcessor.process(request.body, validationAndProcessing);
25
- if (requestBodyValidationAndProcessingResult.rawDataIsInvalid) {
25
+ if (requestBodyValidationAndProcessingResult.isRawDataInvalid) {
26
26
  response.
27
27
  status(es_extensions_1.HTTP_StatusCodes.badRequest).
28
28
  json(requestBodyValidationAndProcessingResult.validationErrorsMessages);
@@ -14,7 +14,7 @@ function validateAndProcessJSON_RequestBody(validationAndProcessing, { mustLogDa
14
14
  });
15
15
  const requestBodyProcessingResult = es_extensions_1.RawObjectDataProcessor.
16
16
  process(request.body, validationAndProcessing);
17
- if (requestBodyProcessingResult.rawDataIsInvalid) {
17
+ if (requestBodyProcessingResult.isRawDataInvalid) {
18
18
  response.
19
19
  status(es_extensions_1.HTTP_StatusCodes.badRequest).
20
20
  json(requestBodyProcessingResult.validationErrorsMessages);
@@ -36,7 +36,7 @@ function Route(HTTP_Method, pathTemplate) {
36
36
  HTTP_MethodInRoutingControllersFormat = "trace";
37
37
  break;
38
38
  default:
39
- es_extensions_1.Logger.throwErrorAndLog({
39
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
40
40
  errorInstance: new es_extensions_1.InvalidParameterValueError({
41
41
  parameterNumber: 1,
42
42
  parameterName: "HTTP_Method",
@@ -1,5 +1,6 @@
1
1
  export { ExpressMiddleware } from "./ExpressMiddleware";
2
2
  export { QueryParametersProcessor } from "./QueryParametersProcessor";
3
+ export { initializeRoutingControllersExpressHTTPS_Application } from "./initializeRoutingControllersExpressHTTPS_Application";
3
4
  export { Route } from "./Route";
4
5
  export { parseAndValidateJSON_RequestBody } from "./RequestBody/parseAndValidateJSON_RequestBody";
5
6
  export { validateAndProcessJSON_RequestBody } from "./RequestBody/validateAndProcessJSON_RequestBody";
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.disposeExpressSession = exports.saveExpressSession = exports.validateAndProcessJSON_RequestBody = exports.parseAndValidateJSON_RequestBody = exports.Route = exports.QueryParametersProcessor = exports.ExpressMiddleware = void 0;
3
+ exports.disposeExpressSession = exports.saveExpressSession = exports.validateAndProcessJSON_RequestBody = exports.parseAndValidateJSON_RequestBody = exports.Route = exports.initializeRoutingControllersExpressHTTPS_Application = exports.QueryParametersProcessor = exports.ExpressMiddleware = void 0;
4
4
  var ExpressMiddleware_1 = require("./ExpressMiddleware");
5
5
  Object.defineProperty(exports, "ExpressMiddleware", { enumerable: true, get: function () { return ExpressMiddleware_1.ExpressMiddleware; } });
6
6
  var QueryParametersProcessor_1 = require("./QueryParametersProcessor");
7
7
  Object.defineProperty(exports, "QueryParametersProcessor", { enumerable: true, get: function () { return QueryParametersProcessor_1.QueryParametersProcessor; } });
8
+ var initializeRoutingControllersExpressHTTPS_Application_1 = require("./initializeRoutingControllersExpressHTTPS_Application");
9
+ Object.defineProperty(exports, "initializeRoutingControllersExpressHTTPS_Application", { enumerable: true, get: function () { return initializeRoutingControllersExpressHTTPS_Application_1.initializeRoutingControllersExpressHTTPS_Application; } });
8
10
  var Route_1 = require("./Route");
9
11
  Object.defineProperty(exports, "Route", { enumerable: true, get: function () { return Route_1.Route; } });
10
12
  var parseAndValidateJSON_RequestBody_1 = require("./RequestBody/parseAndValidateJSON_RequestBody");
@@ -0,0 +1,25 @@
1
+ import { type Express as ExpressApplication } from "express";
2
+ import NodeHTTPS from "https";
3
+ import { type RoutingControllersOptions } from "routing-controllers";
4
+ export declare function initializeRoutingControllersExpressHTTPS_Application({ configuration: { HTTPS, routingControllers }, eventsHandlers: { onExpressApplicationCreated, onHTTPS_ServerCreated, onRoutingControllersSetupComplete, onApplicationStarted } }: Readonly<{
5
+ configuration: Readonly<{
6
+ HTTPS: Readonly<{
7
+ port: number;
8
+ } & (({
9
+ SSL_Key: string;
10
+ } | {
11
+ SSL_KeyFilePath__absoluteOrRelative: string;
12
+ }) & ({
13
+ SSL_Certificate: string;
14
+ } | {
15
+ SSL_CertificateFilePath__absoluteOrRelative: string;
16
+ }))>;
17
+ routingControllers: RoutingControllersOptions;
18
+ }>;
19
+ eventsHandlers: Readonly<{
20
+ onExpressApplicationCreated: (expressApplication: ExpressApplication) => Promise<void>;
21
+ onHTTPS_ServerCreated?: (HTTPS_Server: NodeHTTPS.Server, expressApplication: ExpressApplication) => Promise<void>;
22
+ onRoutingControllersSetupComplete?: (expressApplication: ExpressApplication) => Promise<void>;
23
+ onApplicationStarted?: () => Promise<void>;
24
+ }>;
25
+ }>): Promise<void>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.initializeRoutingControllersExpressHTTPS_Application = initializeRoutingControllersExpressHTTPS_Application;
7
+ const express_1 = __importDefault(require("express"));
8
+ const https_1 = __importDefault(require("https"));
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const es_extensions_1 = require("@yamato-daiwa/es-extensions");
11
+ const routing_controllers_1 = require("routing-controllers");
12
+ async function initializeRoutingControllersExpressHTTPS_Application({ configuration: { HTTPS, routingControllers }, eventsHandlers: { onExpressApplicationCreated, onHTTPS_ServerCreated, onRoutingControllersSetupComplete, onApplicationStarted } }) {
13
+ const expressApplication = (0, express_1.default)();
14
+ await onExpressApplicationCreated(expressApplication);
15
+ const HTTPS_Server = https_1.default.createServer({
16
+ key: "SSL_Key" in HTTPS ?
17
+ HTTPS.SSL_Key : fs_1.default.readFileSync(HTTPS.SSL_KeyFilePath__absoluteOrRelative, "utf8"),
18
+ cert: "SSL_Certificate" in HTTPS ?
19
+ HTTPS.SSL_Certificate : fs_1.default.readFileSync(HTTPS.SSL_CertificateFilePath__absoluteOrRelative, "utf8")
20
+ }, expressApplication);
21
+ await onHTTPS_ServerCreated?.(HTTPS_Server, expressApplication);
22
+ (0, routing_controllers_1.useExpressServer)(expressApplication, routingControllers);
23
+ await onRoutingControllersSetupComplete?.(expressApplication);
24
+ HTTPS_Server.listen(HTTPS.port, () => {
25
+ onApplicationStarted?.().catch(es_extensions_1.Logger.logPromiseError);
26
+ });
27
+ }
@@ -0,0 +1,2 @@
1
+ import type Express from "express";
2
+ export default function redirectOnNotFound(targetRoute: string): (_request: Express.Request, response: Express.Response) => void;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = redirectOnNotFound;
4
+ function redirectOnNotFound(targetRoute) {
5
+ return (_request, response) => {
6
+ if (!response.headersSent) {
7
+ response.redirect(targetRoute);
8
+ }
9
+ };
10
+ }
package/README.md CHANGED
@@ -21,6 +21,8 @@ Also, install the following peer dependencies if not installed yet.
21
21
  ## Functionality
22
22
 
23
23
  + [`ExpressMiddleware`](#expressmiddleware)
24
+ + [`initializeRoutingControllersExpressHTTPS_Application`](#initializeroutingcontrollersexpresshttps_application)
25
+ + [`redirectOnNotFound`](#redirectonnotfound)
24
26
  + [`Route`](#route)
25
27
  + [`QueryParametersProcessor`](#queryparametersprocessor)
26
28
 
@@ -93,6 +95,239 @@ export default class DebuggerMiddleware extends ExpressMiddleware {
93
95
  }
94
96
  ```
95
97
 
98
+ ### `initializeRoutingControllersExpressHTTPS_Application`
99
+
100
+ Initializer of the application using **routing-controllers**, **Express** and **HTTPS protocol**.
101
+
102
+ ```ts
103
+ (
104
+ {
105
+ configuration: {
106
+ HTTPS,
107
+ routingControllers
108
+ },
109
+ eventsHandlers: {
110
+ onExpressApplicationCreated,
111
+ onHTTPS_ServerCreated,
112
+ onRoutingControllersSetupComplete,
113
+ onApplicationStarted
114
+ }
115
+ }: Readonly<{
116
+
117
+ configuration: Readonly<{
118
+
119
+ HTTPS: Readonly<
120
+ { port: number; } &
121
+ (
122
+ (
123
+ { SSL_Key: string; } |
124
+ { SSL_KeyFilePath__absoluteOrRelative: string; }
125
+ ) &
126
+ (
127
+ { SSL_Certificate: string; } |
128
+ { SSL_CertificateFilePath__absoluteOrRelative: string; }
129
+ )
130
+ )
131
+ >;
132
+ routingControllers: RoutingControllersOptions;
133
+ }>;
134
+
135
+ eventsHandlers: Readonly<{
136
+ onExpressApplicationCreated: (expressApplication: ExpressApplication) => Promise<void>;
137
+ onHTTPS_ServerCreated?: (HTTPS_Server: NodeHTTPS.Server, expressApplication: ExpressApplication) => Promise<void>;
138
+ onRoutingControllersSetupComplete?: () => Promise<void>;
139
+ onApplicationStarted?: () => Promise<void>;
140
+ }>;
141
+
142
+ }>
143
+ ): Promise<void>
144
+ ```
145
+
146
+
147
+ #### Background
148
+
149
+ The initialization of **routing-controllers** + **Express** + **HTTPS** application
150
+ [has not been documented well](https://github.com/typestack/routing-controllers/discussions/1243)
151
+ and a little bit complicated:
152
+
153
+ ```ts
154
+ import Express, { type Express as ExpressApplication } from "express";
155
+ import createExpressApplication from "express";
156
+ import { useExpressServer as supportClassSyntax } from "routing-controllers";
157
+
158
+ import HTTPS from "https";
159
+ import FileSystem from "fs";
160
+
161
+
162
+ const expressApplication: ExpressApplication = createExpressApplication();
163
+
164
+ expressApplication.get(
165
+ "/",
166
+ (_request: Express.Request, response: Express.Response): void => {
167
+ response.send("<h1>Hello, world!</h1>");
168
+ }
169
+ );
170
+
171
+ const HTTPS_Server: HTTPS.Server = HTTPS.createServer(
172
+ {
173
+ key: FileSystem.readFileSync("./SSL/key.pem"),
174
+ cert: FileSystem.readFileSync("./SSL/cert.pem")
175
+ },
176
+ expressApplication
177
+ );
178
+
179
+ supportClassSyntax(expressApplication);
180
+
181
+ HTTPS_Server.listen(443, "127.0.0.1");
182
+ ```
183
+
184
+ **initializeRoutingControllersExpressHTTPS_Application** encapsulates this complexity.
185
+
186
+
187
+ #### Configuration
188
+
189
+ ##### `HTTPS` - The HTTPS Requirements
190
+
191
+ <dl>
192
+
193
+ <dt><code>HTTPS</code></dt>
194
+ <dd>
195
+ <dl>
196
+ <dt>Type</dt>
197
+ <dd>String</dd>
198
+ <dt>Is Required</dt>
199
+ <dd>Yes</dd>
200
+ <dt>Description</dt>
201
+ <dd>
202
+ HTTPS port number will be listened.
203
+ Valid port number expected.
204
+ </dd>
205
+ </dl>
206
+ </dd>
207
+
208
+ <dt><code>SSL_Key</code></dt>
209
+ <dd>
210
+ <dl>
211
+ <dt>Type</dt>
212
+ <dd>String</dd>
213
+ <dt>Required If</dt>
214
+ <dd><code>SSL_KeyFilePath__absoluteOrRelative</code> not specified</dd>
215
+ <dt>Description</dt>
216
+ <dt>Description</dt>
217
+ <dd>The string which must represent the valid SSL key.</dd>
218
+ </dl>
219
+ </dd>
220
+
221
+ <dt><code>SSL_KeyFilePath__absoluteOrRelative</code></dt>
222
+ <dd>
223
+ <dl>
224
+ <dt>Type</dt>
225
+ <dd>String</dd>
226
+ <dt>Required If</dt>
227
+ <dd><code>SSL_Key</code> not specified</dd>
228
+ <dt>Description</dt>
229
+ <dd>Expected the valid path (absolute or relative) of the existing file contains the valid SSL key.</dd>
230
+ </dl>
231
+ </dd>
232
+
233
+ <dt><code>SSL_Certificate</code></dt>
234
+ <dd>
235
+ <dl>
236
+ <dt>Type</dt>
237
+ <dd>String</dd>
238
+ <dt>Required If</dt>
239
+ <dd><code>SSL_CertificateFilePath__absoluteOrRelative</code> not specified</dd>
240
+ <dt>Description</dt>
241
+ <dd>The string which must represent the valid SSL certificate.</dd>
242
+ </dl>
243
+ </dd>
244
+
245
+ <dt><code>SSL_CertificateFilePath__absoluteOrRelative</code></dt>
246
+ <dd>
247
+ <dl>
248
+ <dt>Type</dt>
249
+ <dd>String</dd>
250
+ <dt>Required If</dt>
251
+ <dd><code>SSL_Certificate</code> not specified</dd>
252
+ <dt>Description</dt>
253
+ <dd>Expected the valid path (absolute or relative) of the existing file contains the valid SSL certificate.</dd>
254
+ </dl>
255
+ </dd>
256
+
257
+ </dl>
258
+
259
+
260
+ ##### `routingControllers` - The "routing-controllers" configuration
261
+
262
+ Options of [routing-controllers](https://www.npmjs.com/package/routing-controllers/v/0.11.3).
263
+ See the [TypeScript type definitions of `RoutingControllersOptions` type](https://app.unpkg.com/routing-controllers@0.11.3/files/types/RoutingControllersOptions.d.ts)
264
+ for reference.
265
+
266
+
267
+ #### Events Handers
268
+ ##### `onExpressApplicationCreated`
269
+
270
+ ```
271
+ (expressApplication: ExpressApplication) => Promise<void>
272
+ ```
273
+
274
+ As it obviously from the function name, called when basic Express application created.
275
+ Basically being used to use the Express plugins and middlewares.
276
+
277
+ + **routing-controllers** library has not been involved yet.
278
+ + The express application instance will be passed via parameter.
279
+
280
+
281
+ ##### `onHTTPS_ServerCreated`
282
+
283
+ ```
284
+ (HTTPS_Server: NodeHTTPS.Server, expressApplication: ExpressApplication) => Promise<void>
285
+ ```
286
+
287
+ As it obviously from the function name, called when the HTTPS server created.
288
+
289
+ + **routing-controllers** library has not been involved yet.
290
+ + The HTTPS server instance, and also the express application instance will be passed via parameters.
291
+
292
+
293
+
294
+ ##### `onRoutingControllersSetupComplete`
295
+
296
+ ```
297
+ (expressApplication: ExpressApplication) => Promise<void>;
298
+ ```
299
+
300
+ As it obviously from the function name, called when the **routing-controllers** functionality has been initialized.
301
+
302
+
303
+ ##### `onRoutingControllersSetupComplete`
304
+
305
+ ```
306
+ () => Promise<void>
307
+ ```
308
+
309
+ As it obviously from the function name, called when the application actually started and ready to accept the HTTP
310
+ requests.
311
+ Usually being called to log the application starting.
312
+
313
+
314
+ ### `redirectOnNotFound`
315
+
316
+ ```
317
+ redirectOnNotFound(targetRoute: string): (_request: Express.Request, response: Express.Response) => void
318
+ ```
319
+
320
+ Redirects to specified route when `response.headersSent` value is false.
321
+ Intended to be used via `expressApplicationUse()` _after_ the initialization of **routing-controllers** application
322
+ complete to redirect to "Not found" page.
323
+
324
+ Although with the plain Express the "Not found" page is being provided by other way, with **routing-controllers** the
325
+ [usual ways may not work](https://github.com/typestack/routing-controllers/discussions/1476) because of incorrectly
326
+ arranged routes.
327
+ There is no official recommendation how to redirect to "Not found" page when no route matchings found so
328
+ `redirectOnNotFound` function may be used for such purposes.
329
+
330
+
96
331
  ### `Route`
97
332
 
98
333
  The adapter for `Method` decorator from **routing-controllers** to `HTTP_Methods` enumeration from
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yamato-daiwa/express-extensions",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Additional functionality for Express.js and also \"routing-controllers\" aimed to reduce the routine code.",
5
5
  "keywords": [
6
6
  "nodejs",
@@ -35,19 +35,20 @@
35
35
  "routing-controllers": "~0.11.0"
36
36
  },
37
37
  "dependencies": {
38
- "@yamato-daiwa/es-extensions": "1.8.0-alpha.13",
38
+ "@yamato-daiwa/es-extensions": "1.8.0",
39
39
  "fundamental-constants": "0.8.0",
40
40
  "qs": "6.14.0"
41
41
  },
42
42
  "devDependencies": {
43
- "@types/express-session": "1.18.1",
44
- "@yamato-daiwa/style_guides": "0.6.8",
43
+ "@types/express": "5.0.3",
44
+ "@types/express-session": "1.18.2",
45
+ "@yamato-daiwa/style_guides": "0.9.0",
45
46
  "body-parser": "2.2.0",
46
47
  "express": "5.1.0",
47
- "express-session": "1.18.1",
48
+ "express-session": "1.18.2",
48
49
  "rimraf": "6.0.1",
49
- "routing-controllers": "0.11.2",
50
- "typescript": "5.8.3"
50
+ "routing-controllers": "0.11.3",
51
+ "typescript": "5.9.2"
51
52
  },
52
53
  "scripts": {
53
54
  "Rebuild Distributable Without Preliminary Cleaning": "tsc",