1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.RoutePathFactory = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const utils_1 = require("./utils");
|
7 | class 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 = (0, common_1.flatten)(paths.map(path => versionOrVersions.map(version =>
|
20 |
|
21 | version === common_1.VERSION_NEUTRAL
|
22 | ? path
|
23 | : `${path}/${versionPrefix}${version}`)));
|
24 | }
|
25 | else {
|
26 |
|
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 (0, shared_utils_1.stripEndSlash)(metadata.globalPrefix || '') + path;
|
41 | });
|
42 | }
|
43 | return paths
|
44 | .map(path => (0, shared_utils_1.addLeadingSlash)(path || '/'))
|
45 | .map(path => (path !== '/' ? (0, shared_utils_1.stripEndSlash)(path) : path));
|
46 | }
|
47 | getVersion(metadata) {
|
48 |
|
49 |
|
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) => (0, shared_utils_1.stripEndSlash)(a) + (0, shared_utils_1.addLeadingSlash)(b);
|
69 | if (Array.isArray(fragmentToAppend)) {
|
70 | const paths2dArray = paths.map(path => fragmentToAppend.map(fragment => concatPaths(path, fragment)));
|
71 | return (0, common_1.flatten)(paths2dArray);
|
72 | }
|
73 | return paths.map(path => concatPaths(path, fragmentToAppend));
|
74 | }
|
75 | isExcludedFromGlobalPrefix(path, requestMethod) {
|
76 | if ((0, 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 | (0, utils_1.isRouteExcluded)(excludedRoutes, path, requestMethod));
|
83 | }
|
84 | }
|
85 | exports.RoutePathFactory = RoutePathFactory;
|