1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.MiddlewareBuilder = void 0;
|
4 | const dependencies_decorator_1 = require("@nestjs/common/decorators/core/dependencies.decorator");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const iterare_1 = require("iterare");
|
7 | const utils_1 = require("./utils");
|
8 | class MiddlewareBuilder {
|
9 | constructor(routesMapper, httpAdapter, routeInfoPathExtractor) {
|
10 | this.routesMapper = routesMapper;
|
11 | this.httpAdapter = httpAdapter;
|
12 | this.routeInfoPathExtractor = routeInfoPathExtractor;
|
13 | this.middlewareCollection = new Set();
|
14 | }
|
15 | apply(...middleware) {
|
16 | return new MiddlewareBuilder.ConfigProxy(this, (0, dependencies_decorator_1.flatten)(middleware), this.routeInfoPathExtractor);
|
17 | }
|
18 | build() {
|
19 | return [...this.middlewareCollection];
|
20 | }
|
21 | getHttpAdapter() {
|
22 | return this.httpAdapter;
|
23 | }
|
24 | }
|
25 | exports.MiddlewareBuilder = MiddlewareBuilder;
|
26 | MiddlewareBuilder.ConfigProxy = class {
|
27 | constructor(builder, middleware, routeInfoPathExtractor) {
|
28 | this.builder = builder;
|
29 | this.middleware = middleware;
|
30 | this.routeInfoPathExtractor = routeInfoPathExtractor;
|
31 | this.excludedRoutes = [];
|
32 | }
|
33 | getExcludedRoutes() {
|
34 | return this.excludedRoutes;
|
35 | }
|
36 | exclude(...routes) {
|
37 | this.excludedRoutes = this.getRoutesFlatList(routes).reduce((excludedRoutes, route) => {
|
38 | for (const routePath of this.routeInfoPathExtractor.extractPathFrom(route)) {
|
39 | excludedRoutes.push({
|
40 | ...route,
|
41 | path: routePath,
|
42 | });
|
43 | }
|
44 | return excludedRoutes;
|
45 | }, []);
|
46 | return this;
|
47 | }
|
48 | forRoutes(...routes) {
|
49 | const { middlewareCollection } = this.builder;
|
50 | const flattedRoutes = this.getRoutesFlatList(routes);
|
51 | const forRoutes = this.removeOverlappedRoutes(flattedRoutes);
|
52 | const configuration = {
|
53 | middleware: (0, utils_1.filterMiddleware)(this.middleware, this.excludedRoutes, this.builder.getHttpAdapter()),
|
54 | forRoutes,
|
55 | };
|
56 | middlewareCollection.add(configuration);
|
57 | return this.builder;
|
58 | }
|
59 | getRoutesFlatList(routes) {
|
60 | const { routesMapper } = this.builder;
|
61 | return (0, iterare_1.iterate)(routes)
|
62 | .map(route => routesMapper.mapRouteToRouteInfo(route))
|
63 | .flatten()
|
64 | .toArray();
|
65 | }
|
66 | removeOverlappedRoutes(routes) {
|
67 | const regexMatchParams = /(:[^\/]*)/g;
|
68 | const wildcard = '([^/]*)';
|
69 | const routesWithRegex = routes
|
70 | .filter(route => route.path.includes(':'))
|
71 | .map(route => ({
|
72 | method: route.method,
|
73 | path: route.path,
|
74 | regex: new RegExp('^(' + route.path.replace(regexMatchParams, wildcard) + ')$', 'g'),
|
75 | }));
|
76 | return routes.filter(route => {
|
77 | const isOverlapped = (item) => {
|
78 | if (route.method !== item.method) {
|
79 | return false;
|
80 | }
|
81 | const normalizedRoutePath = (0, shared_utils_1.stripEndSlash)(route.path);
|
82 | return (normalizedRoutePath !== item.path &&
|
83 | item.regex.test(normalizedRoutePath));
|
84 | };
|
85 | const routeMatch = routesWithRegex.find(isOverlapped);
|
86 | return routeMatch === undefined;
|
87 | });
|
88 | }
|
89 | };
|