zenweb 1.21.0 → 2.0.0
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.md +25 -54
- package/index.d.ts +3 -3
- package/index.js +5 -13
- package/package.json +21 -9
- package/.eslintrc +0 -14
- package/example/app/controller/index.js +0 -87
- package/example/app/service/hello_service.js +0 -17
- package/example/app.js +0 -19
package/README.md
CHANGED
|
@@ -3,15 +3,29 @@ Modular lightweight web framework based on Koa
|
|
|
3
3
|
|
|
4
4
|
## Quick start
|
|
5
5
|
|
|
6
|
+
package.json
|
|
7
|
+
```json
|
|
8
|
+
{
|
|
9
|
+
"name": "app",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"private": true,
|
|
12
|
+
"exports": "app/**",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"dev": "cross-env DEBUG=* NODE_ENV=development nodemon app",
|
|
15
|
+
"start": "node app"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
6
20
|
```bash
|
|
7
|
-
$ npm i zenweb
|
|
21
|
+
$ npm i zenweb cross-env nodemon
|
|
8
22
|
```
|
|
9
23
|
|
|
10
24
|
app/index.js
|
|
11
25
|
```js
|
|
12
|
-
|
|
26
|
+
import { create } from 'zenweb';
|
|
13
27
|
|
|
14
|
-
const app =
|
|
28
|
+
const app = create({
|
|
15
29
|
// add optional module
|
|
16
30
|
// $ npm i @zenweb/sentry @zenweb/cors @zenweb/validation
|
|
17
31
|
sentry: { dsn: 'xxxxx' },
|
|
@@ -24,16 +38,14 @@ app.start();
|
|
|
24
38
|
|
|
25
39
|
app/controller/hello.js
|
|
26
40
|
```js
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const app = require('..');
|
|
30
|
-
const router = app.router;
|
|
41
|
+
import { Router } from 'zenweb';
|
|
42
|
+
export const router = Router();
|
|
31
43
|
|
|
32
|
-
|
|
44
|
+
router.get('/', ctx => {
|
|
33
45
|
ctx.success('Hello');
|
|
34
46
|
});
|
|
35
47
|
|
|
36
|
-
|
|
48
|
+
router.get('/hello', ctx => {
|
|
37
49
|
ctx.success({
|
|
38
50
|
say1: ctx.service.helloService.say(),
|
|
39
51
|
say2: ctx.service.helloService.say(),
|
|
@@ -41,7 +53,7 @@ app.router.get('/hello', ctx => {
|
|
|
41
53
|
});
|
|
42
54
|
});
|
|
43
55
|
|
|
44
|
-
|
|
56
|
+
router.get('/error', ctx => {
|
|
45
57
|
ctx.fail('error info');
|
|
46
58
|
console.log('Will not output');
|
|
47
59
|
});
|
|
@@ -49,11 +61,9 @@ app.router.get('/error', ctx => {
|
|
|
49
61
|
|
|
50
62
|
app/service/hello_service.js
|
|
51
63
|
```js
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const { Service } = require('zenweb');
|
|
64
|
+
import { Service } from 'zenweb';
|
|
55
65
|
|
|
56
|
-
class HelloService extends Service {
|
|
66
|
+
export default class HelloService extends Service {
|
|
57
67
|
constructor(ctx) {
|
|
58
68
|
super(ctx);
|
|
59
69
|
this.i = 0;
|
|
@@ -64,49 +74,10 @@ class HelloService extends Service {
|
|
|
64
74
|
return `Hello: ${this.ctx.path}, ${this.i}`;
|
|
65
75
|
}
|
|
66
76
|
}
|
|
67
|
-
|
|
68
|
-
module.exports = HelloService;
|
|
69
77
|
```
|
|
70
78
|
|
|
71
79
|
```bash
|
|
72
|
-
$
|
|
80
|
+
$ npm run dev
|
|
73
81
|
boot time: 2 ms
|
|
74
82
|
server on: 7001
|
|
75
83
|
```
|
|
76
|
-
|
|
77
|
-
## doc
|
|
78
|
-
|
|
79
|
-
### ctx.helper
|
|
80
|
-
#### query(), body(), params()
|
|
81
|
-
```js
|
|
82
|
-
ctx.body = ctx.helper.query('kw', {
|
|
83
|
-
count: 'int',
|
|
84
|
-
is: 'bool',
|
|
85
|
-
list:'int[]',
|
|
86
|
-
trim:'trim',
|
|
87
|
-
trimList:'trim[]',
|
|
88
|
-
});
|
|
89
|
-
```
|
|
90
|
-
```bash
|
|
91
|
-
curl --location --request GET '127.0.0.1:7001/typecast?kw=%20111%20&count=222&is=y&list=1,2,3&trim=%20%20aaaa%20&trimList=asd,sdd,%20%20ddd%20,d,,1'
|
|
92
|
-
```
|
|
93
|
-
```json
|
|
94
|
-
{
|
|
95
|
-
"kw": " 111 ",
|
|
96
|
-
"count": 222,
|
|
97
|
-
"is": true,
|
|
98
|
-
"list": [
|
|
99
|
-
1,
|
|
100
|
-
2,
|
|
101
|
-
3
|
|
102
|
-
],
|
|
103
|
-
"trim": "aaaa",
|
|
104
|
-
"trimList": [
|
|
105
|
-
"asd",
|
|
106
|
-
"sdd",
|
|
107
|
-
"ddd",
|
|
108
|
-
"d",
|
|
109
|
-
"1"
|
|
110
|
-
]
|
|
111
|
-
}
|
|
112
|
-
```
|
package/index.d.ts
CHANGED
|
@@ -2,9 +2,6 @@ import '@zenweb/log';
|
|
|
2
2
|
import '@zenweb/router';
|
|
3
3
|
import '@zenweb/helper';
|
|
4
4
|
import '@zenweb/service';
|
|
5
|
-
export { Core } from '@zenweb/core';
|
|
6
|
-
export { ApiFail, ApiFailDetail } from '@zenweb/api';
|
|
7
|
-
export { Service } from '@zenweb/service';
|
|
8
5
|
import { CoreOptions } from '@zenweb/core';
|
|
9
6
|
import { ServiceOptions } from '@zenweb/service';
|
|
10
7
|
import { ApiOptions } from '@zenweb/api';
|
|
@@ -20,6 +17,9 @@ import { ViewOptions } from '@zenweb/view';
|
|
|
20
17
|
import { ScheduleOptions } from '@zenweb/schedule';
|
|
21
18
|
import { MessageCodeOption } from '@zenweb/messagecode';
|
|
22
19
|
import { FormOption } from '@zenweb/form';
|
|
20
|
+
export { Router } from '@zenweb/router';
|
|
21
|
+
export { ApiFail } from '@zenweb/api';
|
|
22
|
+
export { Service } from '@zenweb/service';
|
|
23
23
|
|
|
24
24
|
interface CreateOptions {
|
|
25
25
|
core?: CoreOptions;
|
package/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { Service } = require('@zenweb/service');
|
|
1
|
+
import { Core } from '@zenweb/core';
|
|
2
|
+
export { Router } from '@zenweb/router';
|
|
3
|
+
export { ApiFail } from '@zenweb/api';
|
|
4
|
+
export { Service } from '@zenweb/service';
|
|
6
5
|
|
|
7
6
|
// 可选模块
|
|
8
7
|
const OPTIONAL_MODULES = {
|
|
@@ -20,7 +19,7 @@ const OPTIONAL_MODULES = {
|
|
|
20
19
|
* @param {object} [options] 配置项
|
|
21
20
|
* @returns {Core}
|
|
22
21
|
*/
|
|
23
|
-
function create(options) {
|
|
22
|
+
export function create(options) {
|
|
24
23
|
options = options || {};
|
|
25
24
|
const core = new Core(options.core);
|
|
26
25
|
core.setup('@zenweb/meta');
|
|
@@ -43,10 +42,3 @@ function create(options) {
|
|
|
43
42
|
}
|
|
44
43
|
return core;
|
|
45
44
|
}
|
|
46
|
-
|
|
47
|
-
module.exports = {
|
|
48
|
-
Core,
|
|
49
|
-
ApiFail,
|
|
50
|
-
Service,
|
|
51
|
-
create,
|
|
52
|
-
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zenweb",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Modular lightweight web framework based on Koa",
|
|
5
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./index.js",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=16.0.0"
|
|
9
|
+
},
|
|
10
|
+
"typings": "index.d.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"index.d.ts"
|
|
14
|
+
],
|
|
6
15
|
"scripts": {
|
|
7
|
-
"
|
|
16
|
+
"dev": "cd example && cross-env DEBUG=* NODE_ENV=development node app"
|
|
8
17
|
},
|
|
9
18
|
"author": {
|
|
10
19
|
"name": "YeFei",
|
|
@@ -25,7 +34,7 @@
|
|
|
25
34
|
"bugs": {
|
|
26
35
|
"url": "https://github.com/yefei/zenweb/issues"
|
|
27
36
|
},
|
|
28
|
-
"
|
|
37
|
+
"optionalDependencies": {
|
|
29
38
|
"@zenweb/cors": "^1.1.0",
|
|
30
39
|
"@zenweb/form": "^1.6.1",
|
|
31
40
|
"@zenweb/metric": "^1.3.2",
|
|
@@ -39,12 +48,15 @@
|
|
|
39
48
|
"dependencies": {
|
|
40
49
|
"@zenweb/api": "^1.2.0",
|
|
41
50
|
"@zenweb/body": "^1.1.0",
|
|
42
|
-
"@zenweb/core": "^
|
|
51
|
+
"@zenweb/core": "^2.0.1",
|
|
43
52
|
"@zenweb/helper": "^1.2.0",
|
|
44
|
-
"@zenweb/log": "^
|
|
53
|
+
"@zenweb/log": "^2.0.1",
|
|
45
54
|
"@zenweb/messagecode": "^1.0.1",
|
|
46
|
-
"@zenweb/meta": "^
|
|
47
|
-
"@zenweb/router": "^
|
|
48
|
-
"@zenweb/service": "^
|
|
55
|
+
"@zenweb/meta": "^2.0.0",
|
|
56
|
+
"@zenweb/router": "^2.0.1",
|
|
57
|
+
"@zenweb/service": "^2.0.0"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"cross-env": "^7.0.3"
|
|
49
61
|
}
|
|
50
62
|
}
|
package/.eslintrc
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const app = require('../../app');
|
|
4
|
-
const router = app.router;
|
|
5
|
-
|
|
6
|
-
router.get('/', ctx => {
|
|
7
|
-
ctx.body = {
|
|
8
|
-
hello: 'world',
|
|
9
|
-
time: Date.now(),
|
|
10
|
-
};
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
router.get('/name/:name', ctx => {
|
|
14
|
-
ctx.body = {
|
|
15
|
-
name: ctx.params.name,
|
|
16
|
-
};
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
router.get('/error', ctx => {
|
|
20
|
-
// 错误输出终止执行
|
|
21
|
-
ctx.fail('error demo');
|
|
22
|
-
console.log('后续代码不会执行');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
router.get('/success', ctx => {
|
|
26
|
-
// 使用 success 统一包装返回格式
|
|
27
|
-
ctx.success('ok');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
router.get('/log', ctx => {
|
|
31
|
-
app.log.info('App log');
|
|
32
|
-
ctx.log.info('Context log');
|
|
33
|
-
ctx.body = 'hello';
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
router.get('/service', ctx => {
|
|
37
|
-
console.log( 'helloService' in ctx.service ); // 可以用 in 判断 service 是否已注册
|
|
38
|
-
const helloService = ctx.service.helloService; // 获取时会被初始化
|
|
39
|
-
const helloService2 = ctx.service.helloService; // 再次获取使用之前已经初始化的
|
|
40
|
-
const { helloService: s3 } = ctx.service; // 可以用解构
|
|
41
|
-
ctx.body = {
|
|
42
|
-
1: helloService.say(),
|
|
43
|
-
2: helloService2.say(),
|
|
44
|
-
3: s3.say(),
|
|
45
|
-
};
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
function sleep(ms) {
|
|
49
|
-
return new Promise((res) => {
|
|
50
|
-
setTimeout(res, ms);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
router.get('/x-process-time', async ctx => {
|
|
55
|
-
await sleep(1000);
|
|
56
|
-
ctx.body = 'ok';
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
router.get('/typecast', ctx => {
|
|
61
|
-
ctx.body = ctx.helper.query('kw', {
|
|
62
|
-
count: 'int',
|
|
63
|
-
is: 'bool',
|
|
64
|
-
list:'int[]',
|
|
65
|
-
trim:'trim',
|
|
66
|
-
trimList:'trim[]',
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
router.post('/typecast', ctx => {
|
|
71
|
-
ctx.body = ctx.helper.body('kw', {
|
|
72
|
-
count: 'int',
|
|
73
|
-
is: 'bool',
|
|
74
|
-
list:'int[]',
|
|
75
|
-
trim:'trim',
|
|
76
|
-
trimList:'trim[]',
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
router.post('/post', ctx => {
|
|
81
|
-
ctx.body = ctx.request.body;
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
router.post('/file', ctx => {
|
|
85
|
-
console.log('file:', ctx.request.files);
|
|
86
|
-
ctx.body = ctx.request.body;
|
|
87
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { Service } = require('../../..');
|
|
4
|
-
|
|
5
|
-
class HelloService extends Service {
|
|
6
|
-
constructor(ctx) {
|
|
7
|
-
super(ctx);
|
|
8
|
-
this.i = 0;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
say() {
|
|
12
|
-
this.i++;
|
|
13
|
-
return `Hello: ${this.ctx.path}, ${this.i}`;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = HelloService;
|
package/example/app.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
process.env.NODE_ENV = 'development';
|
|
4
|
-
process.env.DEBUG = '*';
|
|
5
|
-
|
|
6
|
-
const app = module.exports = require('..').create({
|
|
7
|
-
body: {
|
|
8
|
-
multipart: true,
|
|
9
|
-
},
|
|
10
|
-
api: {
|
|
11
|
-
failCode: 500,
|
|
12
|
-
failStatus: 200,
|
|
13
|
-
success(data) {
|
|
14
|
-
return { path: this.path, code: 200, data };
|
|
15
|
-
},
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
app.start();
|