1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.isMiddlewareRouteExcluded = exports.assignToken = exports.isMiddlewareClass = exports.mapToClass = exports.filterMiddleware = exports.mapToExcludeRoute = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const iterare_1 = require("iterare");
|
7 | const pathToRegexp = require("path-to-regexp");
|
8 | const uuid_1 = require("uuid");
|
9 | const utils_1 = require("../router/utils");
|
10 | const mapToExcludeRoute = (routes) => {
|
11 | return routes.map(({ path, method }) => ({
|
12 | pathRegex: pathToRegexp(path),
|
13 | requestMethod: method,
|
14 | }));
|
15 | };
|
16 | exports.mapToExcludeRoute = mapToExcludeRoute;
|
17 | const filterMiddleware = (middleware, routes, httpAdapter) => {
|
18 | const excludedRoutes = (0, exports.mapToExcludeRoute)(routes);
|
19 | return (0, iterare_1.iterate)([])
|
20 | .concat(middleware)
|
21 | .filter(shared_utils_1.isFunction)
|
22 | .map((item) => (0, exports.mapToClass)(item, excludedRoutes, httpAdapter))
|
23 | .toArray();
|
24 | };
|
25 | exports.filterMiddleware = filterMiddleware;
|
26 | const mapToClass = (middleware, excludedRoutes, httpAdapter) => {
|
27 | if (isMiddlewareClass(middleware)) {
|
28 | if (excludedRoutes.length <= 0) {
|
29 | return middleware;
|
30 | }
|
31 | const MiddlewareHost = class extends middleware {
|
32 | use(...params) {
|
33 | const [req, _, next] = params;
|
34 | const isExcluded = isMiddlewareRouteExcluded(req, excludedRoutes, httpAdapter);
|
35 | if (isExcluded) {
|
36 | return next();
|
37 | }
|
38 | return super.use(...params);
|
39 | }
|
40 | };
|
41 | return assignToken(MiddlewareHost, middleware.name);
|
42 | }
|
43 | return assignToken(class {
|
44 | constructor() {
|
45 | this.use = (...params) => {
|
46 | const [req, _, next] = params;
|
47 | const isExcluded = isMiddlewareRouteExcluded(req, excludedRoutes, httpAdapter);
|
48 | if (isExcluded) {
|
49 | return next();
|
50 | }
|
51 | return middleware(...params);
|
52 | };
|
53 | }
|
54 | });
|
55 | };
|
56 | exports.mapToClass = mapToClass;
|
57 | function isMiddlewareClass(middleware) {
|
58 | var _a, _b;
|
59 | const middlewareStr = middleware.toString();
|
60 | if (middlewareStr.substring(0, 5) === 'class') {
|
61 | return true;
|
62 | }
|
63 | const middlewareArr = middlewareStr.split(' ');
|
64 | return (middlewareArr[0] === 'function' &&
|
65 | /[A-Z]/.test((_a = middlewareArr[1]) === null || _a === void 0 ? void 0 : _a[0]) &&
|
66 | (0, shared_utils_1.isFunction)((_b = middleware.prototype) === null || _b === void 0 ? void 0 : _b.use));
|
67 | }
|
68 | exports.isMiddlewareClass = isMiddlewareClass;
|
69 | function assignToken(metatype, token = (0, uuid_1.v4)()) {
|
70 | Object.defineProperty(metatype, 'name', { value: token });
|
71 | return metatype;
|
72 | }
|
73 | exports.assignToken = assignToken;
|
74 | function isMiddlewareRouteExcluded(req, excludedRoutes, httpAdapter) {
|
75 | if (excludedRoutes.length <= 0) {
|
76 | return false;
|
77 | }
|
78 | const reqMethod = httpAdapter.getRequestMethod(req);
|
79 | const originalUrl = httpAdapter.getRequestUrl(req);
|
80 | const queryParamsIndex = originalUrl && originalUrl.indexOf('?');
|
81 | const pathname = queryParamsIndex >= 0
|
82 | ? originalUrl.slice(0, queryParamsIndex)
|
83 | : originalUrl;
|
84 | return (0, utils_1.isRouteExcluded)(excludedRoutes, pathname, common_1.RequestMethod[reqMethod]);
|
85 | }
|
86 | exports.isMiddlewareRouteExcluded = isMiddlewareRouteExcluded;
|