UNPKG

3.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.RoutePathFactory = void 0;
4const common_1 = require("@nestjs/common");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const utils_1 = require("./utils");
7class RoutePathFactory {
8 constructor(applicationConfig) {
9 this.applicationConfig = applicationConfig;
10 }
11 create(metadata, requestMethod) {
12 var _a;
13 let paths = [''];
14 const version = this.getVersion(metadata);
15 if (version && ((_a = metadata.versioningOptions) === null || _a === void 0 ? void 0 : _a.type) === common_1.VersioningType.URI) {
16 const versionPrefix = this.getVersionPrefix(metadata.versioningOptions);
17 // Version Neutral - Do not include version in URL
18 if (version !== common_1.VERSION_NEUTRAL) {
19 if (Array.isArray(version)) {
20 paths = common_1.flatten(paths.map(path => version.map(v => path + `/${versionPrefix}${v}`)));
21 }
22 else {
23 paths = paths.map(path => path + `/${versionPrefix}${version}`);
24 }
25 }
26 }
27 paths = this.appendToAllIfDefined(paths, metadata.modulePath);
28 paths = this.appendToAllIfDefined(paths, metadata.ctrlPath);
29 paths = this.appendToAllIfDefined(paths, metadata.methodPath);
30 if (metadata.globalPrefix) {
31 paths = paths.map(path => {
32 if (this.isExcludedFromGlobalPrefix(path, requestMethod)) {
33 return path;
34 }
35 return shared_utils_1.stripEndSlash(metadata.globalPrefix || '') + path;
36 });
37 }
38 return paths
39 .map(path => shared_utils_1.addLeadingSlash(path || '/'))
40 .map(path => (path !== '/' ? shared_utils_1.stripEndSlash(path) : path));
41 }
42 getVersion(metadata) {
43 // The version will be either the path version or the controller version,
44 // with the pathVersion taking priority.
45 return metadata.methodVersion || metadata.controllerVersion;
46 }
47 getVersionPrefix(versioningOptions) {
48 const defaultPrefix = 'v';
49 if (versioningOptions.type === common_1.VersioningType.URI) {
50 if (versioningOptions.prefix === false) {
51 return '';
52 }
53 else if (versioningOptions.prefix !== undefined) {
54 return versioningOptions.prefix;
55 }
56 }
57 return defaultPrefix;
58 }
59 appendToAllIfDefined(paths, fragmentToAppend) {
60 if (!fragmentToAppend) {
61 return paths;
62 }
63 const concatPaths = (a, b) => shared_utils_1.stripEndSlash(a) + shared_utils_1.addLeadingSlash(b);
64 if (Array.isArray(fragmentToAppend)) {
65 const paths2dArray = paths.map(path => fragmentToAppend.map(fragment => concatPaths(path, fragment)));
66 return common_1.flatten(paths2dArray);
67 }
68 return paths.map(path => concatPaths(path, fragmentToAppend));
69 }
70 isExcludedFromGlobalPrefix(path, requestMethod) {
71 if (shared_utils_1.isUndefined(requestMethod)) {
72 return false;
73 }
74 const options = this.applicationConfig.getGlobalPrefixOptions();
75 const excludedRoutes = options.exclude;
76 return (Array.isArray(excludedRoutes) &&
77 utils_1.isRouteExcluded(excludedRoutes, path, requestMethod));
78 }
79}
80exports.RoutePathFactory = RoutePathFactory;