Skip to content

Commit 72efc75

Browse files
committed
feat: init types
1 parent e19ae3e commit 72efc75

File tree

14 files changed

+122
-162
lines changed

14 files changed

+122
-162
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"main": "./lib/index.js",
66
"types": "./dts/index.d.ts",
77
"scripts": {
8-
"build": "tsc",
9-
"test": "echo text",
8+
"build": "node ./scripts/build.js",
109
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add ./CHANGELOG.md",
1110
"format": "eslint --fix . --ext .js,.ts"
1211
},

scripts/build.js

Whitespace-only changes.

src/constants/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @description 各可反射的常量标识
2+
* @description 各个可反射的常量标识
33
*/
44
export const META_ROUTER = Symbol.for('dectorator#meta_router')
55

src/core/controller.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import * as Koa from 'koa'
22

33
/**
4-
*
4+
* @description controller的基础可继承类,继承自Koa。
55
*/
6-
export default class BaseController extends Koa {
6+
export class BaseController extends Koa {
77
/**
8-
*
8+
* @description 使用继承的方式显示的让每一个controller可以从实例上获取到ctx和next
99
*/
1010
public ctx: Koa.Context
11-
/**
12-
*
13-
*/
1411
public next: Koa.Next
1512
/**
16-
*
13+
* @description controller继承baseController的标识
1714
*/
1815
public _isExtends: symbol = Symbol.for('BaseController#isExtends')
1916
}

src/core/index.ts

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,62 @@
11
import { instancePoll } from '../ioc'
22
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'
84
import KoaRuntime from '../runtime/koa'
95
import * as Koa from 'koa'
106

117
/**
12-
*
8+
* @description controller poll,所有的controller都会注册到这里,在创建服务的时候获取到。
139
*/
1410
const CONTROLLER_POLL = Object.create(null)
1511

1612
/**
17-
*
13+
* @description 初始化所有的controller并将service注入去调用KoaRuntime启动服务。
1814
* @param options
1915
*/
20-
type CreateApplicationType = (options: RuntimeOptionsInterface) => Koa
16+
type CreateApplicationType = (options: RuntimeOptions) => Koa
2117
export const createApplication: CreateApplicationType = (options = {}) => {
2218
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`)
5342
}
54-
55-
controllers.add({
56-
controller,
57-
path
58-
})
5943
}
44+
45+
controllers.add({
46+
controller,
47+
path
48+
})
6049
}
50+
// 调用KoaRuntime启动服务
6151
const app = KoaRuntime(controllers, options)
6252
return app
6353
}
6454
/**
65-
*
55+
* controller的标示之一,将修饰的类注册到controller poll里
6656
* @param path
6757
*/
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) {
7060
const controllerPoll: Array<ControllerInterface> = Reflect.getMetadata(
7161
META_CONTROLLER,
7262
CONTROLLER_POLL

src/decorator/httpRouter.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
import { META_ROUTER } from '../constants'
2-
import { RouteAopInterface } from '../interface'
2+
import { RouteAop } from '../types'
33
import { Middleware } from 'koa'
44
// create http request method decorator
55
/**
6-
*
6+
* @description get router
77
*/
88
export const Get = createHTTPMethodDecorator('get')
99
/**
10-
*
10+
* @description post router
1111
*/
1212
export const Post = createHTTPMethodDecorator('post')
1313
/**
14-
*
14+
* @description put router
1515
*/
1616
export const Put = createHTTPMethodDecorator('put')
1717
/**
18-
*
18+
* @description delete router
1919
*/
2020
export const Delete = createHTTPMethodDecorator('delete')
2121
/**
22-
*
22+
* @description all router
2323
*/
2424
export const All = createHTTPMethodDecorator('get', 'post', 'put', 'delete')
2525

2626
/**
27-
*
27+
* @description 路由装饰器生成器
2828
* @param methods
2929
*/
3030
type HttpMethodReturnType = (
3131
path: string,
32-
aopPlugins?: RouteAopInterface
32+
aopPlugins?: RouteAop
3333
) => MethodDecorator
3434
function createHTTPMethodDecorator(
3535
...methods: Array<string>
3636
): HttpMethodReturnType {
3737
return (path, aopPlugins = {}): MethodDecorator => (
38-
target: any,
39-
propertyKey: string,
40-
descripator: PropertyDescriptor
38+
target,
39+
propertyKey,
40+
descripator
4141
) => {
4242
let beforePlugins: Set<Middleware> = new Set()
4343
let afterPlugins: Set<Middleware> = new Set()

src/decorator/params/headers.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { META_HEADERS } from '../../constants'
2-
import { ParamsMetaInterface } from '../../interface'
2+
import { ParamsMetaInterface } from '../../types'
33
import { Context } from 'koa'
44
/**
5-
*
5+
* @description 路由函数参数装饰器,可直接获取到请求的header
66
* @param headers
77
*/
8-
export const Headers = (tags: Array<string> = []) => (
9-
target: any,
10-
propertyKey: string,
11-
paramIndex: number
8+
type HeadersType = (tags: Array<string>) => ParameterDecorator
9+
export const Headers: HeadersType = (tags = []) => (
10+
target,
11+
propertyKey,
12+
paramIndex
1213
) => {
1314
Reflect.defineMetadata(
1415
META_HEADERS,
@@ -21,14 +22,16 @@ export const Headers = (tags: Array<string> = []) => (
2122
}
2223

2324
/**
24-
*
25+
* @description 获取header的辅助函数
2526
* @param ctx
2627
* @param paramMeta
2728
*/
28-
export const getHeadersHandler = (
29+
type HeadersHandlerType = (
2930
ctx: Context,
3031
paramMeta: ParamsMetaInterface
31-
): Array<any> => {
32+
) => Array<any>
33+
34+
export const getHeadersHandler: HeadersHandlerType = (ctx, paramMeta) => {
3235
const { index, tags } = paramMeta
3336
const rawHeaders = ctx.req.headers
3437
let result

src/decorator/params/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import { META_REQ, META_RES, META_COOKIE } from '../../constants'
22
import { Headers, getHeadersHandler } from './headers'
33
import { Params, getParamsHeandler } from './params'
4+
/**
5+
* get request
6+
*/
47
const Req = createParamDecorator(META_REQ)
58

9+
/**
10+
* get response
11+
*/
612
const Res = createParamDecorator(META_RES)
7-
13+
/**
14+
* get cookie
15+
*/
816
const Cookie = createParamDecorator(META_COOKIE)
917

1018
/**
11-
*
19+
* @description 基础参数装饰器生成器
1220
* @param key
13-
*
1421
*/
1522
function createParamDecorator(key: symbol) {
16-
return () => (target: any, propertyKey: string, paramIndex: number) => {
23+
return (): ParameterDecorator => (target, propertyKey, paramIndex) => {
1724
Reflect.defineMetadata(key, paramIndex, target[propertyKey])
1825
}
1926
}

src/decorator/params/params.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { Context } from 'koa'
22
import { META_PARAMS } from '../../constants'
3-
import { ParamsMetaInterface } from '../../interface'
4-
export const Params = (tags: Array<string> = []) => (
5-
target: any,
6-
propertyKey: string,
7-
paramIndex: number
3+
import { ParamsMetaInterface } from '../../types'
4+
5+
/**
6+
* @description 请求参数装饰器
7+
* @param tags
8+
*/
9+
type ParamsType = (tags: Array<string>) => ParameterDecorator
10+
export const Params: ParamsType = (tags = []) => (
11+
target,
12+
propertyKey,
13+
paramIndex
814
) => {
915
Reflect.defineMetadata(
1016
META_PARAMS,
@@ -16,10 +22,16 @@ export const Params = (tags: Array<string> = []) => (
1622
)
1723
}
1824

19-
export const getParamsHeandler = (
25+
/**
26+
* @description 请求参数装饰器服务函数
27+
* @param ctx
28+
* @param paramMeta
29+
*/
30+
type ParamsHeandlerType = (
2031
ctx: Context,
2132
paramMeta: ParamsMetaInterface
22-
): Array<any> => {
33+
) => Array<any>
34+
export const getParamsHeandler: ParamsHeandlerType = (ctx, paramMeta) => {
2335
const { index, tags } = paramMeta
2436
const allParams = {
2537
body: ctx.request.body,

src/index.ts

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
import 'reflect-metadata'
2-
/**
3-
*
4-
*/
5-
import { Inject, Injectable } from './ioc'
6-
/**
7-
*
8-
*/
9-
import { Get, Post, Put, Delete, All } from './decorator/httpRouter'
10-
/**
11-
*
12-
*/
13-
import { createApplication, Controller } from './core'
14-
/**
15-
*
16-
*/
17-
import BaseController from './core/controller'
182

19-
import { Headers, Params, Cookie, Req, Res } from './decorator/params'
3+
export * from './ioc'
204

21-
export {
22-
createApplication,
23-
BaseController,
24-
Controller,
25-
Inject,
26-
Injectable,
27-
Get,
28-
Post,
29-
Put,
30-
Delete,
31-
All,
32-
Headers,
33-
Params,
34-
Cookie,
35-
Req,
36-
Res
37-
}
5+
export * from './decorator/httpRouter'
6+
7+
export * from './core'
8+
9+
export * from './core/controller'
10+
11+
export * from './decorator/params'

0 commit comments

Comments
 (0)