@yamato-daiwa/express-extensions 0.0.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.
@@ -0,0 +1,5 @@
1
+ import { RawObjectDataProcessor, type ReadonlyParsedJSON } from "@yamato-daiwa/es-extensions";
2
+ export declare function parseAndValidateJSON_RequestBody<RequestData extends ReadonlyParsedJSON>(settings: Readonly<{
3
+ requestBodySizeLimit__bytesPackageFormat: string | number;
4
+ validationAndProcessing: RawObjectDataProcessor.ObjectDataSpecification;
5
+ }>): (request: unknown, response: unknown, toNextMiddleware: (error?: unknown) => void) => void;
@@ -0,0 +1,29 @@
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.parseAndValidateJSON_RequestBody = parseAndValidateJSON_RequestBody;
7
+ const body_parser_1 = __importDefault(require("body-parser"));
8
+ const es_extensions_1 = require("@yamato-daiwa/es-extensions");
9
+ function parseAndValidateJSON_RequestBody(settings) {
10
+ return (_request, _response, toNextMiddleware) => {
11
+ const request = _request;
12
+ const response = _response;
13
+ body_parser_1.default.json({ limit: settings.requestBodySizeLimit__bytesPackageFormat })(request, response, (error) => {
14
+ if ((0, es_extensions_1.isNeitherUndefinedNorNull)(error)) {
15
+ toNextMiddleware(error);
16
+ return;
17
+ }
18
+ const requestBodyValidationAndProcessingResult = es_extensions_1.RawObjectDataProcessor.process(request.body, settings.validationAndProcessing);
19
+ if (requestBodyValidationAndProcessingResult.rawDataIsInvalid) {
20
+ response.
21
+ status(es_extensions_1.HTTP_StatusCodes.badRequest).
22
+ json(requestBodyValidationAndProcessingResult.validationErrorsMessages);
23
+ return;
24
+ }
25
+ request.body = requestBodyValidationAndProcessingResult.processedData;
26
+ toNextMiddleware();
27
+ });
28
+ };
29
+ }
@@ -0,0 +1,3 @@
1
+ import { RawObjectDataProcessor } from "@yamato-daiwa/es-extensions";
2
+ import type { ReadonlyParsedJSON } from "@yamato-daiwa/es-extensions";
3
+ export declare function validateAndProcessJSON_RequestBody<RequestData extends ReadonlyParsedJSON>(validRequestBodySpecification: RawObjectDataProcessor.ObjectDataSpecification): (request: unknown, response: unknown, toNextMiddleware: () => void) => void;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateAndProcessJSON_RequestBody = validateAndProcessJSON_RequestBody;
4
+ const es_extensions_1 = require("@yamato-daiwa/es-extensions");
5
+ function validateAndProcessJSON_RequestBody(validRequestBodySpecification) {
6
+ return (request, response, toNextMiddleware) => {
7
+ if (!(0, es_extensions_1.isArbitraryObject)(request)) {
8
+ response.status(es_extensions_1.HTTP_StatusCodes.internalServerError);
9
+ es_extensions_1.Logger.logError({
10
+ errorType: es_extensions_1.InvalidParameterValueError.NAME,
11
+ title: es_extensions_1.InvalidParameterValueError.localization.defaultTitle,
12
+ description: es_extensions_1.InvalidParameterValueError.localization.generateDescription({
13
+ parameterName: "request",
14
+ parameterNumber: 1,
15
+ messageSpecificPart: `The "request" is not object and has type "${typeof request}"`
16
+ }),
17
+ occurrenceLocation: "validateAndProcessRequestBody -> innerFunction(request, response, toNextMiddleware)"
18
+ });
19
+ return;
20
+ }
21
+ const requestBodyProcessingResult = es_extensions_1.RawObjectDataProcessor.
22
+ process(request.body, validRequestBodySpecification);
23
+ if (requestBodyProcessingResult.rawDataIsInvalid) {
24
+ response.
25
+ status(es_extensions_1.HTTP_StatusCodes.badRequest).
26
+ json(requestBodyProcessingResult.validationErrorsMessages);
27
+ return;
28
+ }
29
+ request.body = requestBodyProcessingResult.processedData;
30
+ toNextMiddleware();
31
+ };
32
+ }
@@ -0,0 +1,7 @@
1
+ import type { Session } from "express-session";
2
+ export declare function disposeExpressSession(session: Session, options: Readonly<{
3
+ mustWaitUntilCompletion: true;
4
+ }>): Promise<void>;
5
+ export declare function disposeExpressSession(session: Session, options: Readonly<{
6
+ mustWaitUntilCompletion: false;
7
+ }>): void;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.disposeExpressSession = disposeExpressSession;
4
+ const es_extensions_1 = require("@yamato-daiwa/es-extensions");
5
+ function disposeExpressSession(session, { mustWaitUntilCompletion }) {
6
+ if (!mustWaitUntilCompletion) {
7
+ session.destroy(() => { });
8
+ }
9
+ return new Promise((resolve, reject) => {
10
+ session.destroy((error) => {
11
+ if ((0, es_extensions_1.isNeitherUndefinedNorNull)(error)) {
12
+ reject(error);
13
+ return;
14
+ }
15
+ resolve();
16
+ });
17
+ });
18
+ }
@@ -0,0 +1,2 @@
1
+ import type { Session } from "express-session";
2
+ export declare function saveExpressSession(session: Session): Promise<void> | void;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.saveExpressSession = saveExpressSession;
4
+ const es_extensions_1 = require("@yamato-daiwa/es-extensions");
5
+ function saveExpressSession(session) {
6
+ return new Promise((resolve, reject) => {
7
+ session.save((error) => {
8
+ if ((0, es_extensions_1.isNeitherUndefinedNorNull)(error)) {
9
+ reject(error);
10
+ }
11
+ resolve();
12
+ });
13
+ });
14
+ }
@@ -0,0 +1,4 @@
1
+ export { parseAndValidateJSON_RequestBody } from "./RequestBody/parseAndValidateJSON_RequestBody";
2
+ export { validateAndProcessJSON_RequestBody } from "./RequestBody/validateAndProcessJSON_RequestBody";
3
+ export { saveExpressSession } from "./Session/saveExpressSession";
4
+ export { disposeExpressSession } from "./Session/disposeExpressSession";
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.disposeExpressSession = exports.saveExpressSession = exports.validateAndProcessJSON_RequestBody = exports.parseAndValidateJSON_RequestBody = void 0;
4
+ var parseAndValidateJSON_RequestBody_1 = require("./RequestBody/parseAndValidateJSON_RequestBody");
5
+ Object.defineProperty(exports, "parseAndValidateJSON_RequestBody", { enumerable: true, get: function () { return parseAndValidateJSON_RequestBody_1.parseAndValidateJSON_RequestBody; } });
6
+ var validateAndProcessJSON_RequestBody_1 = require("./RequestBody/validateAndProcessJSON_RequestBody");
7
+ Object.defineProperty(exports, "validateAndProcessJSON_RequestBody", { enumerable: true, get: function () { return validateAndProcessJSON_RequestBody_1.validateAndProcessJSON_RequestBody; } });
8
+ var saveExpressSession_1 = require("./Session/saveExpressSession");
9
+ Object.defineProperty(exports, "saveExpressSession", { enumerable: true, get: function () { return saveExpressSession_1.saveExpressSession; } });
10
+ var disposeExpressSession_1 = require("./Session/disposeExpressSession");
11
+ Object.defineProperty(exports, "disposeExpressSession", { enumerable: true, get: function () { return disposeExpressSession_1.disposeExpressSession; } });
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Yamato Daiwa Co., Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Yamato Daiwa Express Extensions
2
+
3
+ Additional functions for [express](https://www.npmjs.com/package/express) and its plugins, and also for
4
+ [routing-controllers](https://www.npmjs.com/package/routing-controllers).
5
+
6
+
7
+ ## Installation
8
+
9
+ ```Bash
10
+ npm i @yamato-daiwa/express-extensions -E
11
+ ```
12
+
13
+ Also, install the following peer dependencies if not installed yet.
14
+
15
+ + **body-parser**: ~1.20.0
16
+ + **express**: ~4.21.0
17
+ + **express-session**: ~1.18.0
18
+ + **routing-controllers**: ~0.10.0
19
+
20
+
21
+ ## Functionality
22
+
23
+ + Session
24
+
25
+ + [`saveExpressSession`](#saveexpresssession)
26
+ + [`disposeExpressSession`](#disposeexpresssession)
27
+
28
+ + Request Body
29
+
30
+ + [`parseAndValidateJSON_RequestBody`](#parseandvalidatejson_requestbody)
31
+ + [`validateAndProcessJSON_RequestBody`](#validateandprocessrequestbody)
32
+
33
+
34
+ ### Session
35
+ #### `saveExpressSession`
36
+
37
+ ```
38
+ saveExpressSession(session: Session): Promise<void>
39
+ ```
40
+
41
+ The promisfied version of [`session.save(callback)`](https://www.npmjs.com/package/express-session#user-content-sessionsavecallback).
42
+ The promise will reject if the callback of `session.save` will receive
43
+ neither undefined nor null parameter.
44
+
45
+
46
+ #### `disposeExpressSession`
47
+
48
+ ```
49
+ # === [ Overload 1 ] Must waint until completion
50
+ export function disposeExpressSession(session: Session, options: Readonly<{ mustWaitUntilCompletion: true; }>): Promise<void>;
51
+
52
+ # === [ Overload 2 ] Do not waint until completion
53
+ export function disposeExpressSession(session: Session, options: Readonly<{ mustWaitUntilCompletion: false; }>): void;
54
+ ```
55
+
56
+ The wrapper for [`session.destroy(callback)`](https://www.npmjs.com/package/express-session#user-content-sessiondestroycallback).
57
+
58
+ * If the second parameter has been set to `{ mustWaitUntilCompletion: true }`, this function will return the promise which
59
+ will be resolved when the disposal will successfully complete.
60
+ In this case, the `disposeExpressSession` is the promisfied version of `session.destroy(callback)`.
61
+ * If there is no need to wait the ending of disposal, set the second parameter to `{ mustWaitUntilCompletion: false }`
62
+ and this function will return nothing.
63
+
64
+
65
+ ### Request Body
66
+ #### `parseAndValidateJSON_RequestBody`
67
+
68
+ ```
69
+ <RequestData extends ReadonlyParsedJSON>(
70
+ settings: Readonly<{
71
+ requestBodySizeLimit__bytesPackageFormat: string | number;
72
+ validationAndProcessing: RawObjectDataProcessor.ObjectDataSpecification;
73
+ }>
74
+ ): ExpressMiddleware
75
+ ```
76
+
77
+ Parses expected to be the JSON-type request body, validates it by [RawObjectDataProcessor](https://github.com/TokugawaTakeshi/Yamato-Daiwa-ES-Extensions/blob/master/CoreLibrary/Package/Documentation/RawObjectDataProcessor/RawObjectDataProcessor.md)
78
+ and, if demanded, modifying it by same util.
79
+ The *alternative* to [class-transformer](https://github.com/typestack/class-transformer) which is transforming the
80
+ request body to the instances of classes for the cases when there is no need to turn the objects to the instances
81
+ of the classes.
82
+
83
+ * Requirements
84
+ 1. **body-parser** has _not_ been applied neither globally nor locally, by other words, the request body
85
+ has not been parsed yet.
86
+ 2. The *class-transformer* is disabled by `useExpressServer({ classTransformer: false })` where the `useExpressServer`
87
+ is the function from **routing-controllers**
88
+ * Intended to be used as one of parameters of **routing-controllers** middleware.
89
+
90
+
91
+ #### `validateAndProcessJSON_RequestBody`
92
+
93
+
94
+ ```
95
+ <RequestData extends ReadonlyParsedJSON>(
96
+ validRequestBodySpecification: RawObjectDataProcessor.ObjectDataSpecification
97
+ ): ExpressMiddleware
98
+ ```
99
+
100
+ Validates the pre-parsed JSON body by [RawObjectDataProcessor](https://github.com/TokugawaTakeshi/Yamato-Daiwa-ES-Extensions/blob/master/CoreLibrary/Package/Documentation/RawObjectDataProcessor/RawObjectDataProcessor.md)
101
+ and, if demanded, modifying it by same util.
102
+ The *alternative* to [class-transformer](https://github.com/typestack/class-transformer) which is transforming the
103
+ request body to the instances of classes for the cases when there is no need to turn the objects to the instances
104
+ of the classes.
105
+
106
+ * Requirements
107
+ 1. The JSON-type request body has been preliminary parsed by **body-parser**.
108
+ 2. The *class-transformer* is disabled by `useExpressServer({ classTransformer: false })` where the `useExpressServer`
109
+ is the function from **routing-controllers**
110
+ * Intended to be used as one of parameters of **routing-controllers** middleware.
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@yamato-daiwa/express-extensions",
3
+ "version": "0.0.0",
4
+ "description": "Additional functionality for Express.js and also \"routing-controllers\" aimed to reduce the routine code.",
5
+ "keywords": [
6
+ "nodejs",
7
+ "typescript",
8
+ "express"
9
+ ],
10
+ "author": {
11
+ "name": "Yamato Daiwa Co., Ltd."
12
+ },
13
+ "license": "MIT",
14
+ "homepage": "https://github.com/TokugawaTakeshi/YamatoDaiwaExpressExtensions",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/TokugawaTakeshi/YamatoDaiwaExpressExtensions.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/TokugawaTakeshi/YamatoDaiwaExpressExtensions/issues"
21
+ },
22
+ "main": "./Distributable/index.js",
23
+ "sideEffects": false,
24
+ "types": "./Distributable/index.d.ts",
25
+ "files": [
26
+ "Distributable"
27
+ ],
28
+ "engines": {
29
+ "node": ">=18.0.0"
30
+ },
31
+ "peerDependencies": {
32
+ "body-parser": "~1.20.0",
33
+ "express": "~4.21.0",
34
+ "express-session": "~1.18.0",
35
+ "routing-controllers": "~0.10.0"
36
+ },
37
+ "devDependencies": {
38
+ "@types/express-session": "1.18.1",
39
+ "@yamato-daiwa/style_guides": "0.6.2",
40
+ "body-parser": "1.20.3",
41
+ "express": "4.21.2",
42
+ "express-session": "1.18.1",
43
+ "rimraf": "6.0.1",
44
+ "routing-controllers": "0.10.4",
45
+ "typescript": "5.7.3"
46
+ },
47
+ "scripts": {
48
+ "Rebuild Distributable Without Preliminary Cleaning": "tsc",
49
+ "Rebuild Distributable": "rimraf Distributable && tsc",
50
+ "Rebuild Distributable and Realize Alpha": "npm run \"Rebuild Distributable\" && npm publish --tag alpha",
51
+ "Rebuild Distributable and Realize RC": "npm run \"Rebuild Distributable\" && npm publish --tag rc"
52
+ },
53
+ "dependencies": {
54
+ "@yamato-daiwa/es-extensions": "1.7.2"
55
+ }
56
+ }