@discover-cloud/shared 1.0.4 → 1.0.5
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.
package/dist/middleware/index.js
CHANGED
|
@@ -18,3 +18,4 @@ __exportStar(require("./error-handler.middleware"), exports);
|
|
|
18
18
|
__exportStar(require("./validate.middleware"), exports);
|
|
19
19
|
__exportStar(require("./request-id.middleware"), exports);
|
|
20
20
|
__exportStar(require("./authorize.middleware"), exports);
|
|
21
|
+
__exportStar(require("./validated-merge.middleware"), exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
/**
|
|
3
|
+
* VALIDATED MERGE HELPERS
|
|
4
|
+
* ─────────────────────────
|
|
5
|
+
* Used in routes that run two Validator.validate calls (e.g. params + body,
|
|
6
|
+
* or params + query). Each call overwrites req.validated, so values from
|
|
7
|
+
* the first validator must be captured before the second runs.
|
|
8
|
+
*
|
|
9
|
+
* Pattern:
|
|
10
|
+
* Validator.validate(paramsSchema, "params"), // req.validated = { id }
|
|
11
|
+
* captureValidated("id", "_capturedId"), // stashes id
|
|
12
|
+
* Validator.validate(bodySchema, "body"), // req.validated = { role }
|
|
13
|
+
* mergeValidated("id", "_capturedId"), // req.validated = { id, role }
|
|
14
|
+
*
|
|
15
|
+
* The cast through unknown is required — TypeScript won't allow a direct
|
|
16
|
+
* cast from Request to Record<string, unknown> because they don't overlap
|
|
17
|
+
* enough. Casting to unknown first tells TypeScript we know what we're doing.
|
|
18
|
+
*/
|
|
19
|
+
export declare const captureValidated: (key: string, tempKey: string) => (req: Request, _res: Response, next: NextFunction) => void;
|
|
20
|
+
export declare const mergeValidated: (key: string, tempKey: string) => (req: Request, _res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeValidated = exports.captureValidated = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* VALIDATED MERGE HELPERS
|
|
6
|
+
* ─────────────────────────
|
|
7
|
+
* Used in routes that run two Validator.validate calls (e.g. params + body,
|
|
8
|
+
* or params + query). Each call overwrites req.validated, so values from
|
|
9
|
+
* the first validator must be captured before the second runs.
|
|
10
|
+
*
|
|
11
|
+
* Pattern:
|
|
12
|
+
* Validator.validate(paramsSchema, "params"), // req.validated = { id }
|
|
13
|
+
* captureValidated("id", "_capturedId"), // stashes id
|
|
14
|
+
* Validator.validate(bodySchema, "body"), // req.validated = { role }
|
|
15
|
+
* mergeValidated("id", "_capturedId"), // req.validated = { id, role }
|
|
16
|
+
*
|
|
17
|
+
* The cast through unknown is required — TypeScript won't allow a direct
|
|
18
|
+
* cast from Request to Record<string, unknown> because they don't overlap
|
|
19
|
+
* enough. Casting to unknown first tells TypeScript we know what we're doing.
|
|
20
|
+
*/
|
|
21
|
+
const captureValidated = (key, tempKey) => (req, _res, next) => {
|
|
22
|
+
req[tempKey] =
|
|
23
|
+
req.validated[key];
|
|
24
|
+
next();
|
|
25
|
+
};
|
|
26
|
+
exports.captureValidated = captureValidated;
|
|
27
|
+
const mergeValidated = (key, tempKey) => (req, _res, next) => {
|
|
28
|
+
req.validated = {
|
|
29
|
+
...req.validated,
|
|
30
|
+
[key]: req[tempKey],
|
|
31
|
+
};
|
|
32
|
+
next();
|
|
33
|
+
};
|
|
34
|
+
exports.mergeValidated = mergeValidated;
|