This repository was archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes-resolver.ts
127 lines (120 loc) · 4.19 KB
/
routes-resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { NestContainer, ApplicationConfig } from '@nestjs/core';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { Injector } from '@nestjs/core/injector/injector';
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { SIGNATURE, DESCRIPTION, OPTIONS } from './constants';
import { KhalaApplication } from './khala-application';
import { isUndefined } from '@nestjs/common/utils/shared.utils';
export class RoutesResolver {
// private readonly routerBuilder: RouterExplorer;
private readonly metadataScanner: MetadataScanner;
constructor(
private readonly container: NestContainer,
private readonly config: ApplicationConfig,
private readonly injector: Injector,
) {
// this.routerExceptionsFilter = new RouterExceptionFilters(
// container,
// config,
// container.getHttpAdapterRef(),
// );
this.metadataScanner = new MetadataScanner();
// this.routerBuilder = new RouterExplorer(
// metadataScanner,
// this.container,
// this.injector,
// this.routerProxy,
// this.routerExceptionsFilter,
// this.config,
// );
// this.routerBuilder = new RouterExplorer(
// metadataScanner,
// this.container,
// this.injector,
// this.routerProxy,
// this.routerExceptionsFilter,
// this.config,
// );
}
public resolve(app: KhalaApplication) {
const modules = this.container.getModules();
modules.forEach(({ controllers, metatype }, moduleName) => {
// let path = metatype
// ? Reflect.getMetadata(MODULE_PATH, metatype)
// : undefined;
// path = path ? basePath + path : basePath;
this.registerRouters(controllers, moduleName, app);
});
}
public registerRouters(
routes: Map<string, InstanceWrapper<Controller>>,
moduleName: string,
app: KhalaApplication,
) {
routes.forEach(instanceWrapper => {
const { instance, metatype } = instanceWrapper;
const prefix = Reflect.getMetadata(SIGNATURE, metatype);
this.scanForPaths(instance).forEach(route => {
const controllerName = metatype.name;
app.addCommand({ ...route, prefix }, controllerName);
});
});
// routes.forEach(instanceWrapper => {
// const { metatype } = instanceWrapper;
// let signature = Reflect.getMetadata(SIGNATURE, metatype);
// // const path = this.routerBuilder.extractRouterPath(
// // metatype as Type<any>,
// // basePath,
// // );
// const controllerName = metatype.name;
// // console.log({ signature, controllerName });
// // app.command(signature).action(k)
// app.addCommand(signature, controllerName);
// // this.logger.log(CONTROLLER_MAPPING_MESSAGE(controllerName, path));
// // this.routerBuilder.explore(
// // instanceWrapper,
// // moduleName,
// // applicationRef,
// // path,
// // );
// });
}
public scanForPaths(instance: Controller, prototype?: any): any[] {
const instancePrototype = isUndefined(prototype)
? Object.getPrototypeOf(instance)
: prototype;
return this.metadataScanner.scanFromPrototype(
instance,
instancePrototype,
method => this.exploreMethodMetadata(instance, instancePrototype, method),
);
}
public exploreMethodMetadata(
instance: Controller,
instancePrototype: any,
methodName: string,
) {
const targetCallback = instancePrototype[methodName];
const routePath = Reflect.getMetadata(SIGNATURE, targetCallback);
const description = Reflect.getMetadata(DESCRIPTION, targetCallback);
const options = Reflect.getMetadata(OPTIONS, targetCallback);
if (isUndefined(routePath)) {
return null;
}
// const requestMethod: RequestMethod = Reflect.getMetadata(
// METHOD_METADATA,
// targetCallback,
// );
// const path = isString(routePath)
// ? [this.validateRoutePath(routePath)]
// : routePath.map(p => this.validateRoutePath(p));
return {
routePath,
description,
targetCallback,
methodName,
options,
};
}
}