|
1 | 1 | import { instancePoll } from '../ioc'
|
2 | 2 | import { META_INJECT, META_CONTROLLER } from '../constants'
|
3 |
| -import { |
4 |
| - RuntimeOptionsInterface, |
5 |
| - InjectInterface, |
6 |
| - ControllerInterface |
7 |
| -} from '../interface' |
| 3 | +import { RuntimeOptions, InjectMetaType, ControllerInterface } from '../types' |
8 | 4 | import KoaRuntime from '../runtime/koa'
|
9 | 5 | import * as Koa from 'koa'
|
10 | 6 |
|
11 | 7 | /**
|
12 |
| - * |
| 8 | + * @description controller poll,所有的controller都会注册到这里,在创建服务的时候获取到。 |
13 | 9 | */
|
14 | 10 | const CONTROLLER_POLL = Object.create(null)
|
15 | 11 |
|
16 | 12 | /**
|
17 |
| - * |
| 13 | + * @description 初始化所有的controller并将service注入去调用KoaRuntime启动服务。 |
18 | 14 | * @param options
|
19 | 15 | */
|
20 |
| -type CreateApplicationType = (options: RuntimeOptionsInterface) => Koa |
| 16 | +type CreateApplicationType = (options: RuntimeOptions) => Koa |
21 | 17 | export const createApplication: CreateApplicationType = (options = {}) => {
|
22 | 18 | const controllers: Set<any> = new Set()
|
23 |
| - const controllerPoll: Array<ControllerInterface> = Reflect.getMetadata( |
24 |
| - META_CONTROLLER, |
25 |
| - CONTROLLER_POLL |
26 |
| - ) |
27 |
| - if (controllerPoll) { |
28 |
| - for (let controllerMeta of controllerPoll) { |
29 |
| - const { path, constructor } = controllerMeta |
30 |
| - const controller = new constructor() |
31 |
| - if (!('_isExtends' in controller)) { |
32 |
| - throw new Error(`class ${constructor.name} not extends BaseController`) |
33 |
| - } |
34 |
| - const injects: Array<InjectInterface> = Reflect.getMetadata( |
35 |
| - META_INJECT, |
36 |
| - controller |
37 |
| - ) |
38 |
| - if (injects) { |
39 |
| - for (let inject of injects) { |
40 |
| - const { propertyKey, tag } = inject |
41 |
| - const injectable: new () => {} = instancePoll.get(tag) |
42 |
| - if (injectable) { |
43 |
| - Object.defineProperty(controller, propertyKey, { |
44 |
| - value: new injectable(), |
45 |
| - writable: false, |
46 |
| - configurable: false, |
47 |
| - enumerable: true |
48 |
| - }) |
49 |
| - } else { |
50 |
| - throw new Error(`not ${String(tag)} model is injectable`) |
51 |
| - } |
52 |
| - } |
| 19 | + const controllerPoll: Array<ControllerInterface> = |
| 20 | + Reflect.getMetadata(META_CONTROLLER, CONTROLLER_POLL) || [] |
| 21 | + // 遍历所有的controller,并获取到依赖的service。 |
| 22 | + for (let controllerMeta of controllerPoll) { |
| 23 | + const { path, constructor } = controllerMeta |
| 24 | + const controller = new constructor() |
| 25 | + if (!('_isExtends' in controller)) { |
| 26 | + throw new Error(`class ${constructor.name} not extends BaseController`) |
| 27 | + } |
| 28 | + const injects: Array<InjectMetaType> = |
| 29 | + Reflect.getMetadata(META_INJECT, controller) || [] |
| 30 | + for (let inject of injects) { |
| 31 | + const { propertyKey, tag } = inject |
| 32 | + const injectable: new () => {} = instancePoll.get(tag) |
| 33 | + if (injectable) { |
| 34 | + Object.defineProperty(controller, propertyKey, { |
| 35 | + value: new injectable(), |
| 36 | + writable: false, |
| 37 | + configurable: false, |
| 38 | + enumerable: true |
| 39 | + }) |
| 40 | + } else { |
| 41 | + throw new Error(`not ${String(tag)} model is injectable`) |
53 | 42 | }
|
54 |
| - |
55 |
| - controllers.add({ |
56 |
| - controller, |
57 |
| - path |
58 |
| - }) |
59 | 43 | }
|
| 44 | + |
| 45 | + controllers.add({ |
| 46 | + controller, |
| 47 | + path |
| 48 | + }) |
60 | 49 | }
|
| 50 | + // 调用KoaRuntime启动服务 |
61 | 51 | const app = KoaRuntime(controllers, options)
|
62 | 52 | return app
|
63 | 53 | }
|
64 | 54 | /**
|
65 |
| - * |
| 55 | + * controller的标示之一,将修饰的类注册到controller poll里 |
66 | 56 | * @param path
|
67 | 57 | */
|
68 |
| -export const Controller = (path: string = '/'): Function => { |
69 |
| - return function<T extends { new (...args: any[]): {} }>(constructor: T) { |
| 58 | +export const Controller = (path: string = '/'): ClassDecorator => { |
| 59 | + return function(constructor) { |
70 | 60 | const controllerPoll: Array<ControllerInterface> = Reflect.getMetadata(
|
71 | 61 | META_CONTROLLER,
|
72 | 62 | CONTROLLER_POLL
|
|
0 commit comments