1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PathsExplorer = void 0;
|
4 | const constants_1 = require("@nestjs/common/constants");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | class PathsExplorer {
|
7 | constructor(metadataScanner) {
|
8 | this.metadataScanner = metadataScanner;
|
9 | }
|
10 | scanForPaths(instance, prototype) {
|
11 | const instancePrototype = (0, shared_utils_1.isUndefined)(prototype)
|
12 | ? Object.getPrototypeOf(instance)
|
13 | : prototype;
|
14 | return this.metadataScanner
|
15 | .getAllMethodNames(instancePrototype)
|
16 | .reduce((acc, method) => {
|
17 | const route = this.exploreMethodMetadata(instance, instancePrototype, method);
|
18 | if (route) {
|
19 | acc.push(route);
|
20 | }
|
21 | return acc;
|
22 | }, []);
|
23 | }
|
24 | exploreMethodMetadata(instance, prototype, methodName) {
|
25 | const instanceCallback = instance[methodName];
|
26 | const prototypeCallback = prototype[methodName];
|
27 | const routePath = Reflect.getMetadata(constants_1.PATH_METADATA, prototypeCallback);
|
28 | if ((0, shared_utils_1.isUndefined)(routePath)) {
|
29 | return null;
|
30 | }
|
31 | const requestMethod = Reflect.getMetadata(constants_1.METHOD_METADATA, prototypeCallback);
|
32 | const version = Reflect.getMetadata(constants_1.VERSION_METADATA, prototypeCallback);
|
33 | const path = (0, shared_utils_1.isString)(routePath)
|
34 | ? [(0, shared_utils_1.addLeadingSlash)(routePath)]
|
35 | : routePath.map((p) => (0, shared_utils_1.addLeadingSlash)(p));
|
36 | return {
|
37 | path,
|
38 | requestMethod,
|
39 | targetCallback: instanceCallback,
|
40 | methodName,
|
41 | version,
|
42 | };
|
43 | }
|
44 | }
|
45 | exports.PathsExplorer = PathsExplorer;
|