kvl只是简单封装了express,而更丰满得功能皆是通过插件来实现,你可以来使用express插件,也可以创建kvl插件来丰富kvl。
kvl目前主要内置了validator(数据验证器),receive(数据接收器),interceptor(拦截器),Injectable(注入器)等插件
插件 | 功能 |
interceptor | 用于进行接口拦截,权限控制 |
receive | 接收通过post方式传进来得参数 |
validator | 验证数据完整性 |
Injectable | 注入器,路由间数据共享 |
interceptor(拦截器)
在接口访问中,避免不了进行各种权限认证,正是各种权限保护着系统的正常运行,因此kvl内置了一个拦截器插件,用于保护接口的安全性
import kvl from 'kvl';
import { Main ,Router, config } from 'kvl';
@Router({
interceptor(req: kvl.Request, res: kvl.Response){
if(!req.query.id){
res.status(400).json({ code: 0, msg: '参数缺省' })
//如果拦截住,禁止继续执行,设置return false
return false;
}
}
})
class hello {
@config({ type: 'get', url: '/getArticle' })
private getArticle(req: kvl.Request, res: kvl.Response): void{
res.send(`
<h1>Hello world</h1>
`)
}
}
//拦截器支持async 模式,来进行异步操作
@Router({
async interceptor(){}
})
//数组形式传参
@Router({
interceptor: [function(){}]
})
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
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
@Router.interceptorLevel(拦截器级别)
- interceptorLevel: 0 (相当与默认规则)
- interceptorLevel: 1 (抛弃全局得拦截器)
- interceptorLevel: 2 (抛弃之前所有通过class继承到的拦截器)
- interceptorLevel: 3 (抛弃父class的拦截器,但是保留全局得拦截器)
- interceptorLevel: 4 (抛弃父class的拦截器,和全局拦截器)
@config.interceptorLevel(拦截器级别)
- interceptorLevel: 0 (相当与默认规则)
- interceptorLevel: 1 (抛弃全局得拦截器)
- interceptorLevel: 2 (仅保留自身拦截器)
- interceptorLevel: 3 (抛弃所在class拦截器,但是保留全局得拦截器)
- interceptorLevel: 4 (抛弃全部class拦截器,但是保留全局得拦截器)
receive(数据接收器)
kvl扩展了一个接收post等参数得方法:receive(通过formidable插件实现)。
注:receive仅实现了接收参数功能,未实现文件接收功能
import kvl from 'kvl';
import { Main ,Router, config } from 'kvl';
@Router({})
class hello {
@config({ type: 'get', url: '/getArticle' })
private getArticle(req: kvl.Request, res: kvl.Response): void{
//post等方式接收得参数默认存在req.body中
res.json(req.body)
}
}
//替换默认post接收方式
Main({
async getData(request: kvl.Response){
return { ... }
},
})
//为接口单独设置解析方式
@config({ type: 'post', url: '/getArticle', getData(request: kvl.Response){ return {} } })
private getArticle(req: kvl.Request, res: kvl.Response): void{
res.json(req.body)
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
创建一个文件接收功能
const UploadRpost = (req: kvl.Request, res: kvl.Response) => {
return new Promise((resolve, reject) => {
const form = new formidable.IncomingForm();
form.uploadDir='tmp'
form.parse(req, function(err: any, fields: any, files: any) {
if(err){
throw err
}
const extname: string = path.extname(files.upload.name);
const filename: any = new Date().getTime() + Math.random();
const oldpath: string = files.upload.path;
const newpath: string = path.resolve(process.cwd(), 'assets', filename+extname)
fs.rename(oldpath,newpath,function (err) {
if(err){
res.json({ code: 0 })
return;
}
fields.upload = newpath;
resolve(fields);
});
});
})
}
@config({ url: '/upload', name: '测试上传', type: 'post', getData: UploadRpost })
private upload(req: kvl.Request, res: kvl.Response): void{
res.end('success')
}
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
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
validator(数据验证器)
用于验证接口接收得参数完整性及是否符合要求
import kvl from 'kvl';
import { Main ,Router, config } from 'kvl';
const articleValidation: kvl.Validation = {
phone: {
type: 'phone',
required: true,
name: '手机号',
done(errpe: kvl.ValidationError, response: kvl.Response){
response.statue(400).json({
code: 0,
msg: '发生了错误',
err
})
}
},
/**
* 如果属性值没有done则执行articleValidation内的done,默认执行全局得done
*/
//done(errpe: kvl.ValidationError, response: kvl.Response){}
}
@Router({})
class hello {
@config({ type: 'get', url: '/getUser', validation: articleValidation })
private getArticle(req: kvl.Request, res: kvl.Response): void{
res.json(req.query)
}
}
/**
* 为验证器添加type:phone类型
*/
Main({
validationType: {
phone: /^[1][3,4,5,6,7,8,9][0-9]{9}$/
},
//自定义错误默认回调
validatorDone(error: kvl.ValidationError, response: kvl.Response){
res.statue(400).json({ code: 0, error })
}
})
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
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
验证器,get时候会自动判断req.query内的参数,post时候判断req.body内参数,你可以为@config添加validType字段,来改变默认的参数检查对象
interface kvl.config {
/**
* query -> req.query
* body -> req.body
* param -> req.params
* all -> { ...request.params, ...request.query, ...request.body }
*/
validType?: 'query' | 'body' | 'param' | 'all'
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Injectable(注入器)
注入器,通过new Class方式,注入到不同router中
import kvl from 'kvl';
import { Main ,Router, config } from 'kvl';
class UserInjectable{
private config: any;
constructor(config: any){
this.config = config;
}
public name2: string = 'cc';
}
@Router({})
class hello extends kvl {
@Injectable('text-injectable') private textInjectable: any;
onInit(){
//这里的this.textInjectable为UserInjectable实例化对象
console.log(this.textInjectable)
}
}
Main({
router: [ hello ],
injectable: [ { class: UserInjectable, name: 'text-injectable', data: { name: 'tom' } } ]
})
//Function为注入得class对象,name为名字,data为class初始化时传进来的值
const injectable = [ { class: Function, name: 'xx', data: {} } ]
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
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