UNPKG

3.76 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 versionOrVersions = this.getVersion(metadata);
15 if (versionOrVersions &&
16 ((_a = metadata.versioningOptions) === null || _a === void 0 ? void 0 : _a.type) === common_1.VersioningType.URI) {
17 const versionPrefix = this.getVersionPrefix(metadata.versioningOptions);
18 if (Array.isArray(versionOrVersions)) {
19 paths = common_1.flatten(paths.map(path => versionOrVersions.map(version =>
20 // Version Neutral - Do not include version in URL
21 version === common_1.VERSION_NEUTRAL
22 ? path
23 : `${path}/${versionPrefix}${version}`)));
24 }
25 else {
26 // Version Neutral - Do not include version in URL
27 if (versionOrVersions !== common_1.VERSION_NEUTRAL) {
28 paths = paths.map(path => `${path}/${versionPrefix}${versionOrVersions}`);
29 }
30 }
31 }
32 paths = this.appendToAllIfDefined(paths, metadata.modulePath);
33 paths = this.appendToAllIfDefined(paths, metadata.ctrlPath);
34 paths = this.appendToAllIfDefined(paths, metadata.methodPath);
35 if (metadata.globalPrefix) {
36 paths = paths.map(path => {
37 if (this.isExcludedFromGlobalPrefix(path, requestMethod)) {
38 return path;
39 }
40 return shared_utils_1.stripEndSlash(metadata.globalPrefix || '') + path;
41 });
42 }
43 return paths
44 .map(path => shared_utils_1.addLeadingSlash(path || '/'))
45 .map(path => (path !== '/' ? shared_utils_1.stripEndSlash(path) : path));
46 }
47 getVersion(metadata) {
48 // The version will be either the path version or the controller version,
49 // with the pathVersion taking priority.
50 return metadata.methodVersion || metadata.controllerVersion;
51 }
52 getVersionPrefix(versioningOptions) {
53 const defaultPrefix = 'v';
54 if (versioningOptions.type === common_1.VersioningType.URI) {
55 if (versioningOptions.prefix === false) {
56 return '';
57 }
58 else if (versioningOptions.prefix !== undefined) {
59 return versioningOptions.prefix;
60 }
61 }
62 return defaultPrefix;
63 }
64 appendToAllIfDefined(paths, fragmentToAppend) {
65 if (!fragmentToAppend) {
66 return paths;
67 }
68 const concatPaths = (a, b) => shared_utils_1.stripEndSlash(a) + shared_utils_1.addLeadingSlash(b);
69 if (Array.isArray(fragmentToAppend)) {
70 const paths2dArray = paths.map(path => fragmentToAppend.map(fragment => concatPaths(path, fragment)));
71 return common_1.flatten(paths2dArray);
72 }
73 return paths.map(path => concatPaths(path, fragmentToAppend));
74 }
75 isExcludedFromGlobalPrefix(path, requestMethod) {
76 if (shared_utils_1.isUndefined(requestMethod)) {
77 return false;
78 }
79 const options = this.applicationConfig.getGlobalPrefixOptions();
80 const excludedRoutes = options.exclude;
81 return (Array.isArray(excludedRoutes) &&
82 utils_1.isRouteExcluded(excludedRoutes, path, requestMethod));
83 }
84}
85exports.RoutePathFactory = RoutePathFactory;