UNPKG

2.81 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.RouteInfoPathExtractor = void 0;
4const common_1 = require("@nestjs/common");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const utils_1 = require("../router/utils");
7const route_path_factory_1 = require("./../router/route-path-factory");
8class RouteInfoPathExtractor {
9 constructor(applicationConfig) {
10 this.applicationConfig = applicationConfig;
11 this.routePathFactory = new route_path_factory_1.RoutePathFactory(applicationConfig);
12 this.prefixPath = (0, shared_utils_1.stripEndSlash)((0, shared_utils_1.addLeadingSlash)(this.applicationConfig.getGlobalPrefix()));
13 this.excludedGlobalPrefixRoutes =
14 this.applicationConfig.getGlobalPrefixOptions().exclude;
15 this.versioningConfig = this.applicationConfig.getVersioning();
16 }
17 extractPathsFrom({ path, method, version }) {
18 const versionPath = this.extractVersionPathFrom(version);
19 if (this.isAWildcard(path)) {
20 return Array.isArray(this.excludedGlobalPrefixRoutes)
21 ? [
22 this.prefixPath + versionPath + (0, shared_utils_1.addLeadingSlash)(path),
23 ...this.excludedGlobalPrefixRoutes.map(route => versionPath + (0, shared_utils_1.addLeadingSlash)(route.path)),
24 ]
25 : [this.prefixPath + versionPath + (0, shared_utils_1.addLeadingSlash)(path)];
26 }
27 return [this.extractNonWildcardPathFrom({ path, method, version })];
28 }
29 extractPathFrom(route) {
30 if (this.isAWildcard(route.path) && !route.version) {
31 return (0, shared_utils_1.addLeadingSlash)(route.path);
32 }
33 return this.extractNonWildcardPathFrom(route);
34 }
35 isAWildcard(path) {
36 return ['*', '/*', '/*/', '(.*)', '/(.*)'].includes(path);
37 }
38 extractNonWildcardPathFrom({ path, method, version, }) {
39 const versionPath = this.extractVersionPathFrom(version);
40 if (Array.isArray(this.excludedGlobalPrefixRoutes) &&
41 (0, utils_1.isRouteExcluded)(this.excludedGlobalPrefixRoutes, path, method)) {
42 return versionPath + (0, shared_utils_1.addLeadingSlash)(path);
43 }
44 return this.prefixPath + versionPath + (0, shared_utils_1.addLeadingSlash)(path);
45 }
46 extractVersionPathFrom(version) {
47 var _a;
48 if (!version || ((_a = this.versioningConfig) === null || _a === void 0 ? void 0 : _a.type) !== common_1.VersioningType.URI)
49 return '';
50 const versionPrefix = this.routePathFactory.getVersionPrefix(this.versioningConfig);
51 return (0, shared_utils_1.addLeadingSlash)(versionPrefix + version.toString());
52 }
53}
54exports.RouteInfoPathExtractor = RouteInfoPathExtractor;