1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const common_1 = require("@nestjs/common");
|
4 | const constants_1 = require("@nestjs/common/constants");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const metadata_scanner_1 = require("../metadata-scanner");
|
7 | const router_explorer_1 = require("../router/router-explorer");
|
8 | class RoutesMapper {
|
9 | constructor(container) {
|
10 | this.routerExplorer = new router_explorer_1.RouterExplorer(new metadata_scanner_1.MetadataScanner(), container);
|
11 | }
|
12 | mapRouteToRouteInfo(route) {
|
13 | if (shared_utils_1.isString(route)) {
|
14 | return [
|
15 | {
|
16 | path: this.validateRoutePath(route),
|
17 | method: common_1.RequestMethod.ALL,
|
18 | },
|
19 | ];
|
20 | }
|
21 | const routePath = Reflect.getMetadata(constants_1.PATH_METADATA, route);
|
22 | if (this.isRouteInfo(routePath, route)) {
|
23 | return [
|
24 | {
|
25 | path: this.validateRoutePath(route.path),
|
26 | method: route.method,
|
27 | },
|
28 | ];
|
29 | }
|
30 | const paths = this.routerExplorer.scanForPaths(Object.create(route), route.prototype);
|
31 | const concatPaths = (acc, currentValue) => acc.concat(currentValue);
|
32 | return paths
|
33 | .map(item => item.path &&
|
34 | item.path.map(p => ({
|
35 | path: this.validateGlobalPath(routePath) + this.validateRoutePath(p),
|
36 | method: item.requestMethod,
|
37 | })))
|
38 | .reduce(concatPaths, []);
|
39 | }
|
40 | isRouteInfo(path, objectOrClass) {
|
41 | return shared_utils_1.isUndefined(path);
|
42 | }
|
43 | validateGlobalPath(path) {
|
44 | const prefix = shared_utils_1.validatePath(path);
|
45 | return prefix === '/' ? '' : prefix;
|
46 | }
|
47 | validateRoutePath(path) {
|
48 | return shared_utils_1.validatePath(path);
|
49 | }
|
50 | }
|
51 | exports.RoutesMapper = RoutesMapper;
|