topbit 3.1.3 → 3.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.cn.md CHANGED
@@ -2008,7 +2008,7 @@ const {ParamCheck} = Topbit.extensions
2008
2008
  // Query 参数检测
2009
2009
  let pck = new ParamCheck({
2010
2010
  key: 'query',
2011
- data : {
2011
+ rule: {
2012
2012
  // 严格限制值
2013
2013
  say: 'hello',
2014
2014
  // 类型转换与范围限制
@@ -2026,7 +2026,7 @@ let paramck = new ParamCheck({
2026
2026
  key: 'param',
2027
2027
  deny: ['x-key'], // 禁止提交的字段
2028
2028
  deleteDeny: true,
2029
- data : {
2029
+ rule: {
2030
2030
  errorMessage: '参数错误', // 自定义错误信息
2031
2031
  mobile: {
2032
2032
  callback: (obj, k, method) => {
@@ -2046,7 +2046,7 @@ app.use(pck, {method: 'GET'})
2046
2046
  ```javascript
2047
2047
  let pmbody = new ParamCheck({
2048
2048
  key: 'body',
2049
- data: {
2049
+ rule: {
2050
2050
  username: { must: true },
2051
2051
  passwd: { must: true }
2052
2052
  }
package/README.md CHANGED
@@ -1960,7 +1960,7 @@ const {ParamCheck} = Topbit.extensions
1960
1960
  // Query parameter check
1961
1961
  let pck = new ParamCheck({
1962
1962
  key: 'query',
1963
- data : {
1963
+ rule: {
1964
1964
  // Strictly restrict value
1965
1965
  say: 'hello',
1966
1966
  // Type conversion and range restriction
@@ -1978,7 +1978,7 @@ let paramck = new ParamCheck({
1978
1978
  key: 'param',
1979
1979
  deny: ['x-key'], // Fields forbidden to submit
1980
1980
  deleteDeny: true,
1981
- data : {
1981
+ rule: {
1982
1982
  errorMessage: 'Parameter Error', // Custom error message
1983
1983
  mobile: {
1984
1984
  callback: (obj, k, method) => {
@@ -1998,7 +1998,7 @@ app.use(pck, {method: 'GET'})
1998
1998
  ```javascript
1999
1999
  let pmbody = new ParamCheck({
2000
2000
  key: 'body',
2001
- data: {
2001
+ rule: {
2002
2002
  username: { must: true },
2003
2003
  passwd: { must: true }
2004
2004
  }
@@ -3,7 +3,7 @@
3
3
  module.exports = [
4
4
  {
5
5
  middleware: async (ctx, next) => {
6
- console.log(`global ${ctx.path} start`)
6
+ console.log(`global group:${ctx.group} path:${ctx.path} start`)
7
7
  await next(ctx)
8
8
  console.log(`global ${ctx.path} end`)
9
9
  }
@@ -11,6 +11,14 @@ class Test {
11
11
  ])
12
12
  }
13
13
 
14
+ async post(ctx) {
15
+ ctx.ok(ctx.body)
16
+ }
17
+
18
+ async list(ctx) {
19
+ ctx.ok(ctx.query)
20
+ }
21
+
14
22
  __mid() {
15
23
  return [
16
24
  {
@@ -19,7 +27,44 @@ class Test {
19
27
  await next(ctx)
20
28
  console.log(`test ${ctx.method} end`)
21
29
  }
22
- }
30
+ },
31
+
32
+ {
33
+ middleware: async (ctx, next) => {
34
+ console.log('use for get list method')
35
+ await next(ctx)
36
+ console.log('end for get list method')
37
+ },
38
+
39
+ handler: [
40
+ 'get', 'list'
41
+ ]
42
+ },
43
+
44
+ {
45
+ middleware: async (ctx, next) => {
46
+ console.log('use for get method', (new Date).toLocaleString())
47
+ await next(ctx)
48
+ console.log('end for get method', (new Date).toLocaleString())
49
+ },
50
+
51
+ handler: [
52
+ 'get'
53
+ ]
54
+ },
55
+
56
+ {
57
+ middleware: async (ctx, next) => {
58
+ console.log(' -- use for post method', (new Date).toLocaleString())
59
+ ctx.body.tag = Math.random()
60
+ await next(ctx)
61
+ console.log(' -- end for post method', (new Date).toLocaleString())
62
+ },
63
+
64
+ handler: [
65
+ 'post'
66
+ ]
67
+ },
23
68
  ]
24
69
  }
25
70
  }
package/demo/loader.js CHANGED
@@ -27,3 +27,4 @@ app.autoWorker(12)
27
27
  app.daemon(1234, 5)
28
28
 
29
29
  //console.log(app.midware.midGroup)
30
+ //console.log({...app.router})
package/demo/rule.js ADDED
@@ -0,0 +1,115 @@
1
+ 'use strict'
2
+
3
+ let Topbit = require('../src/topbit.js')
4
+
5
+ let {ParamCheck} = Topbit.extensions
6
+
7
+ let app = new Topbit({
8
+ debug: true
9
+ })
10
+
11
+ let pck = new ParamCheck({
12
+ //支持query、param、body,对应于请求上下文的ctx.query、ctx.param、ctx.body。
13
+ key: 'query',
14
+
15
+ //要验证的数据,key值即为属性名称,验证规则可以是string|number|object。
16
+ //string会严格判等,number仅仅数据判等,object是最强大的功能。
17
+ rule: {
18
+ //严格限制say的值必须是hello。
19
+ say: 'hello',
20
+ offset: {
21
+ //如果c.query.offset是undefined,则会赋值为0。
22
+ default: 0,
23
+ //要转换的类型,只能是int、float、boolean
24
+ to: 'int',
25
+ //最小值,>=
26
+ min: 0,
27
+ //最大值,<=
28
+ max: 100
29
+ },
30
+ test: {
31
+ default: false,
32
+ // 转换为布尔类型,若字符串为true则转换为布尔值true,否则转换为布尔值false。
33
+ to: 'boolean'
34
+ }
35
+ }
36
+
37
+ })
38
+
39
+ let paramck = new ParamCheck({
40
+ //支持query或param,对应于请求上下文的ctx.query和ctx.param。
41
+ key: 'param',
42
+
43
+ //禁止提交的字段
44
+ deny: ['x-key', 'test'],
45
+
46
+ //检测到存在禁止提交的属性则自动删除,默认会返回400错误。
47
+ deleteDeny: true,
48
+
49
+ //要验证的数据,key值即为属性名称,验证规则可以是string|number|object。
50
+ //string会严格判等,number仅仅数据判等,object是最强大的功能。
51
+ rule: {
52
+ //自定义错误返回的消息,每个属性都可以有自己的错误消息提示。
53
+ name: {
54
+ errorMessage: 'name长度必须在2~8范围内。',
55
+
56
+ //obj是c.query或c.param,k是属性名称,method是当前请求方法
57
+ callback: (obj, k, method) => {
58
+ if (obj[k].length < 2 || obj[k].length > 8) {
59
+ return false
60
+ }
61
+ return true
62
+ }
63
+ },
64
+
65
+ age: {
66
+ errorMessage: '年龄必须在12~65',
67
+ to: 'int',
68
+ min: 12,
69
+ max: 65
70
+ },
71
+
72
+ mobile: {
73
+ errorMessage: '手机号不符合要求',
74
+ //利用callback,可以实现完全自主的自定义规则。
75
+ callback: (obj, k, method) => {
76
+ let preg = /^(12|13|15|16|17|18|19)[0-9]{9}$/
77
+ if (!preg.test(obj[k])) {
78
+ return false
79
+ }
80
+ return true
81
+ }
82
+ }
83
+ }
84
+
85
+ })
86
+
87
+ let pmbody = new ParamCheck({
88
+ key: 'body',
89
+ rule: {
90
+ username: {
91
+ //必须有这个属性。
92
+ must: true
93
+ },
94
+ passwd: {
95
+ must: true
96
+ }
97
+ }
98
+ })
99
+
100
+ app.use(pck, {method: 'GET'})
101
+ .use(paramck, {method: 'GET'})
102
+ .use(pmbody, {method: ['POST', 'PUT'], name: 'login'})
103
+
104
+ app.get('/user/:name/:age/:mobile', async c => {
105
+ c.ok({
106
+ query: c.query,
107
+ param: c.param
108
+ })
109
+ })
110
+
111
+ app.post('/login', async c => {
112
+ c.ok(c.body)
113
+ }, {name: 'login'})
114
+
115
+ app.run(1234)
@@ -206,10 +206,12 @@ module.exports = [
206
206
 
207
207
  ```js
208
208
  // Inside any controller file
209
- static __mid() {
209
+ __mid() {
210
210
  return [
211
211
  { name: '@vip-auth', pre: true },
212
- { name: 'log', method: 'POST' }
212
+ { name: 'log', method: 'POST' },
213
+ //use for controller method: get list
214
+ { name: 'check', handler: ['get', 'list'] }
213
215
  ]
214
216
  }
215
217
  ```
@@ -205,10 +205,13 @@ module.exports = [
205
205
 
206
206
  ```js
207
207
  // 在 controller/user.js 中
208
- static __mid() {
208
+ // 只在本文件生效
209
+ __mid() {
209
210
  return [
210
- { name: '@vip-auth', pre: true }, // 只在本文件生效
211
- { name: 'log', method: 'POST' }
211
+ { name: '@vip-auth', pre: true },
212
+ { name: 'log', method: 'POST' },
213
+ //只对控制器方法get list 启用中间件
214
+ { name: 'check', handler: ['get', 'list'] }
212
215
  ]
213
216
  }
214
217
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topbit",
3
- "version": "3.1.3",
3
+ "version": "3.1.5",
4
4
  "description": "A Server-side web framework support http/1.1 and http/2",
5
5
  "main": "src/topbit.js",
6
6
  "directories": {
@@ -13,7 +13,7 @@ class ParamCheck {
13
13
  constructor(options = {}) {
14
14
  this.type = ['query', 'param', 'body']
15
15
  this.key = 'param'
16
- this.data = {}
16
+ this.rule = {}
17
17
  this.errorMessage = "提交数据不符合要求"
18
18
  //设置禁止提交的字段
19
19
  this.deny = null
@@ -28,9 +28,9 @@ class ParamCheck {
28
28
  }
29
29
  break
30
30
 
31
- case 'data':
31
+ case 'rule':
32
32
  if (typeof options[k] === 'object') {
33
- this.data = options[k]
33
+ this.rule = options[k]
34
34
  }
35
35
  break
36
36
 
@@ -57,25 +57,25 @@ class ParamCheck {
57
57
  }
58
58
 
59
59
  let data_type = ''
60
- for (let k in this.data) {
61
- data_type = typeof this.data[k]
60
+ for (let k in this.rule) {
61
+ data_type = typeof this.rule[k]
62
62
 
63
63
  if (data_type === 'string' || data_type === 'number') {
64
- this.data[k] = {
64
+ this.rule[k] = {
65
65
  __is_value__: true,
66
- __value__: this.data[k],
66
+ __value__: this.rule[k],
67
67
  __type__: data_type === 'string' ? TYPE_STRING : TYPE_NUMBER
68
68
  }
69
69
 
70
70
  continue
71
71
  }
72
72
 
73
- this.data[k].__is_value__ = false
73
+ this.rule[k].__is_value__ = false
74
74
 
75
- if (this.data[k].callback && typeof this.data[k].callback === 'function') {
76
- this.data[k].__is_call__ = true
75
+ if (this.rule[k].callback && typeof this.rule[k].callback === 'function') {
76
+ this.rule[k].__is_call__ = true
77
77
  } else {
78
- this.data[k].__is_call__ = false
78
+ this.rule[k].__is_call__ = false
79
79
  }
80
80
  }
81
81
 
@@ -194,8 +194,8 @@ class ParamCheck {
194
194
  let ost = {ok: true, key: ''}
195
195
 
196
196
  if (this.key !== 'body' || (c.body !== c.rawBody && typeof c.body === 'object')) {
197
- for (let k in this.data) {
198
- if (!this.checkData(d, k, this.data[k], c.method, ost)) {
197
+ for (let k in this.rule) {
198
+ if (!this.checkData(d, k, this.rule[k], c.method, ost)) {
199
199
  return ost
200
200
  }
201
201
  }
@@ -206,7 +206,7 @@ class ParamCheck {
206
206
 
207
207
  mid() {
208
208
  let self = this
209
- let dataObject = this.data
209
+ let dataObject = this.rule
210
210
 
211
211
  if (!Array.isArray(this.deny) || this.deny.length === 0) this.deny = null
212
212
 
@@ -56,7 +56,7 @@ class TopbitLoader {
56
56
  multi: false,
57
57
  optionsRoute: true,
58
58
  fileAsGroup: true,
59
-
59
+
60
60
  beforeController: null,
61
61
  afterController: null,
62
62
 
@@ -566,22 +566,22 @@ class TopbitLoader {
566
566
  let opts = { group };
567
567
  f = `${this.config.prePath}${f}`;
568
568
 
569
- if (!this.fileAsGroup && m.path === undefined) {
570
- m.path = [
569
+ if (!this.config.fileAsGroup && m.handler === undefined) {
570
+ m.handler = [
571
571
  'get', 'list', 'post', 'put', 'delete',
572
572
  'options', 'patch', 'head', 'trace'
573
573
  ];
574
574
  }
575
575
 
576
- if (m.path && typeof m.path === 'string') m.path = [ m.path ];
576
+ if (m.handler && typeof m.handler === 'string') m.handler = [ m.handler ];
577
577
 
578
- if (m.path && Array.isArray(m.path)) {
578
+ if (m.handler && Array.isArray(m.handler)) {
579
579
  opts.name = [];
580
- let path_num;
581
- for (let p of m.path) {
582
- path_num = this.methodNumber[p.toLowerCase()];
583
- if (path_num === undefined) continue;
584
- opts.name.push(`${f}/${path_num}`);
580
+ let handler_num;
581
+ for (let p of m.handler) {
582
+ handler_num = this.methodNumber[p.toLowerCase()];
583
+ if (handler_num === undefined) continue;
584
+ opts.name.push(`${f}/${handler_num}`);
585
585
  }
586
586
  }
587
587