jj.js 0.11.0 → 0.13.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/CHANGELOG.md +8 -0
- package/jj.js +1 -1
- package/lib/ctx.js +2 -2
- package/lib/request.js +205 -0
- package/lib/response.js +2 -15
- package/lib/run.js +0 -1
- package/lib/types.js +16 -11
- package/package.json +2 -4
- package/types.js +11 -4
package/CHANGELOG.md
CHANGED
package/jj.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* jj.js核心库<br/>
|
|
3
|
-
* {App, Cache, Context, Controller, Cookie, Ctx, Db, Logger, Middleware, Model, Pagination, Response, Upload, Url, View, utils, config}
|
|
3
|
+
* {App, Cache, Context, Controller, Cookie, Ctx, Db, Logger, Middleware, Model, Pagination, Request, Response, Upload, Url, View, utils, config}
|
|
4
4
|
* @module core
|
|
5
5
|
* @type {import('./types').Core}
|
|
6
6
|
*/
|
package/lib/ctx.js
CHANGED
|
@@ -42,8 +42,8 @@ const Ctx = new Proxy(class {}, {
|
|
|
42
42
|
} else {
|
|
43
43
|
_[APP][$prop] = null;
|
|
44
44
|
}
|
|
45
|
-
// lib
|
|
46
|
-
if(['logger', 'pagination', 'response', 'url', 'view'].includes(prop) && _[APP][$prop] && _[APP][$prop].__node && !_[APP][$prop].__node.isClass && $[prop]) {
|
|
45
|
+
// lib内有调用,防止覆盖
|
|
46
|
+
if(['logger', 'pagination', 'request', 'response', 'url', 'view'].includes(prop) && _[APP][$prop] && _[APP][$prop].__node && !_[APP][$prop].__node.isClass && $[prop]) {
|
|
47
47
|
_[APP][$prop] = $[prop];
|
|
48
48
|
}
|
|
49
49
|
}
|
package/lib/request.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
const Context = require('./context');
|
|
2
|
+
|
|
3
|
+
class Request extends Context
|
|
4
|
+
{
|
|
5
|
+
/**
|
|
6
|
+
* Initialize a new `Request`
|
|
7
|
+
* @public
|
|
8
|
+
* @param {import('../types').KoaCtx} ctx
|
|
9
|
+
*/
|
|
10
|
+
constructor(ctx) {
|
|
11
|
+
super(ctx);
|
|
12
|
+
/**
|
|
13
|
+
* @type {import('querystring').ParsedUrlQuery}
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
16
|
+
this._get = {...this.ctx.query};
|
|
17
|
+
/**
|
|
18
|
+
* @private
|
|
19
|
+
*/
|
|
20
|
+
this._post = {...this.ctx.request.body};
|
|
21
|
+
/**
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
this._param = {...this.ctx.params};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 智能获取请求数据
|
|
29
|
+
* @param {string} key
|
|
30
|
+
* @param {*} [default_value='']
|
|
31
|
+
* @returns {*}
|
|
32
|
+
*/
|
|
33
|
+
query(key, default_value='') {
|
|
34
|
+
return this.post(key) || this.get(key) || this.param(key) || default_value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 获取param请求数据
|
|
39
|
+
* @param {string} key
|
|
40
|
+
* @param {*} default_value
|
|
41
|
+
* @returns {*}
|
|
42
|
+
*/
|
|
43
|
+
param(key, default_value='') {
|
|
44
|
+
return this._param[key] || default_value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 获取get请求数据
|
|
49
|
+
* @param {string} key
|
|
50
|
+
* @param {*} default_value
|
|
51
|
+
* @returns {*}
|
|
52
|
+
*/
|
|
53
|
+
get(key, default_value='') {
|
|
54
|
+
return Array.isArray(this._get[key]) ? this._get[key][0] : this._get[key] || default_value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 获取post请求数据
|
|
59
|
+
* @param {string} key
|
|
60
|
+
* @param {*} default_value
|
|
61
|
+
* @returns {*}
|
|
62
|
+
*/
|
|
63
|
+
post(key, default_value='') {
|
|
64
|
+
return this._post[key] || default_value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 获取所有请求数据
|
|
70
|
+
* @returns {Object}
|
|
71
|
+
*/
|
|
72
|
+
queryAll() {
|
|
73
|
+
return {...this._param, ...this._get, ...this._post};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 获取所有param数据
|
|
78
|
+
* @returns {Object}
|
|
79
|
+
*/
|
|
80
|
+
paramAll() {
|
|
81
|
+
return this._param;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 获取所有get数据
|
|
86
|
+
* @returns {Object}
|
|
87
|
+
*/
|
|
88
|
+
getAll() {
|
|
89
|
+
return this._get;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 获取所有post数据
|
|
94
|
+
* @returns {Object}
|
|
95
|
+
*/
|
|
96
|
+
postAll() {
|
|
97
|
+
return this._post;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 获取上传file数据
|
|
102
|
+
* @param {string} key
|
|
103
|
+
* @returns {import('formidable').File | import('formidable').File[]}
|
|
104
|
+
*/
|
|
105
|
+
file(key) {
|
|
106
|
+
return this.ctx.request.files[key];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 获取上传file数据
|
|
111
|
+
* @returns {import('formidable').Files}
|
|
112
|
+
*/
|
|
113
|
+
fileAll() {
|
|
114
|
+
return this.ctx.request.files;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 获取请求应用名
|
|
119
|
+
* @returns {string}
|
|
120
|
+
*/
|
|
121
|
+
app() {
|
|
122
|
+
return this.ctx.APP;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 获取请求控制器名
|
|
127
|
+
* @returns {string}
|
|
128
|
+
*/
|
|
129
|
+
controller() {
|
|
130
|
+
return this.ctx.CONTROLLER;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 获取请求方法名
|
|
135
|
+
* @returns {string}
|
|
136
|
+
*/
|
|
137
|
+
action() {
|
|
138
|
+
return this.ctx.ACTION;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 获取请求url
|
|
143
|
+
* @returns {string}
|
|
144
|
+
*/
|
|
145
|
+
url() {
|
|
146
|
+
return this.ctx.url;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 获取请求方式
|
|
151
|
+
* @returns {string}
|
|
152
|
+
*/
|
|
153
|
+
method() {
|
|
154
|
+
return this.ctx.method;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 判断是否get请求
|
|
159
|
+
* @returns {boolean}
|
|
160
|
+
*/
|
|
161
|
+
isGet() {
|
|
162
|
+
return this.ctx.method == 'get';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 判断是否post请求
|
|
167
|
+
* @returns {boolean}
|
|
168
|
+
*/
|
|
169
|
+
isPost() {
|
|
170
|
+
return this.ctx.method == 'post';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 判断是否ajax请求
|
|
175
|
+
* @public
|
|
176
|
+
* @returns {boolean}
|
|
177
|
+
*/
|
|
178
|
+
isAjax() {
|
|
179
|
+
return this.ctx.headers['x-requested-with'] && String(this.ctx.headers['x-requested-with']).toLowerCase() == 'xmlhttprequest'
|
|
180
|
+
|| this.ctx.request.type.toLowerCase() == 'application/json'
|
|
181
|
+
|| this.ctx.query.is_ajax
|
|
182
|
+
|| this.ctx.request.body && this.ctx.request.body.is_ajax
|
|
183
|
+
? true : false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 获取header请求数据
|
|
188
|
+
* @param {string} key
|
|
189
|
+
* @param {*} default_value
|
|
190
|
+
* @returns {*}
|
|
191
|
+
*/
|
|
192
|
+
header(key, default_value='') {
|
|
193
|
+
return this.ctx.headers[key] || default_value;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 获取所有header请求数据
|
|
198
|
+
* @returns {Object}
|
|
199
|
+
*/
|
|
200
|
+
headerAll() {
|
|
201
|
+
return this.ctx.headers;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
module.exports = Request;
|
package/lib/response.js
CHANGED
|
@@ -72,7 +72,7 @@ class Response extends Context
|
|
|
72
72
|
*/
|
|
73
73
|
jump(msg, url, state=1) {
|
|
74
74
|
const tplData = {state, msg, url, wait: this._wait};
|
|
75
|
-
this.ctx.body = this.isAjax() ? {state, msg, data: url} : this._tpl_jump.replace(/\{\$(\w+)\}/g, (...args) => {
|
|
75
|
+
this.ctx.body = this.$request.isAjax() ? {state, msg, data: url} : this._tpl_jump.replace(/\{\$(\w+)\}/g, (...args) => {
|
|
76
76
|
return tplData[args[1]];
|
|
77
77
|
});
|
|
78
78
|
}
|
|
@@ -99,23 +99,10 @@ class Response extends Context
|
|
|
99
99
|
tplData.msg = escapeHtml(tplData.msg);
|
|
100
100
|
tplData.code = '<span class="line">' + tplData.code.map(str => escapeHtml(str)).join('</span><span class="line">') + '</span>';
|
|
101
101
|
tplData.stack = tplData.stack.map(str => escapeHtml(str)).join('<br>');
|
|
102
|
-
this.ctx.body = this.isAjax() ? {state: 0, msg: tplData.msg} : this._tpl_exception.replace(/\{\$(\w+)\}/g, (...args) => {
|
|
102
|
+
this.ctx.body = this.$request.isAjax() ? {state: 0, msg: tplData.msg} : this._tpl_exception.replace(/\{\$(\w+)\}/g, (...args) => {
|
|
103
103
|
return tplData[args[1]];
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 判断是否ajax请求
|
|
109
|
-
* @public
|
|
110
|
-
* @returns {boolean}
|
|
111
|
-
*/
|
|
112
|
-
isAjax() {
|
|
113
|
-
return this.ctx.headers['x-requested-with'] && String(this.ctx.headers['x-requested-with']).toLowerCase() == 'xmlhttprequest'
|
|
114
|
-
|| this.ctx.request.type.toLowerCase() == 'application/json'
|
|
115
|
-
|| this.ctx.query.is_ajax
|
|
116
|
-
|| this.ctx.request.body && this.ctx.request.body.is_ajax
|
|
117
|
-
? true : false;
|
|
118
|
-
}
|
|
119
106
|
}
|
|
120
107
|
|
|
121
108
|
module.exports = Response;
|
package/lib/run.js
CHANGED
|
@@ -10,7 +10,6 @@ const storage = require('./storage');
|
|
|
10
10
|
|
|
11
11
|
async function run(ctx, next, control_type=cfg_app.controller_folder) {
|
|
12
12
|
if(cfg_app.app_debug) {
|
|
13
|
-
Logger.http('Request:', ctx.request);
|
|
14
13
|
Logger.http({Router: control_type, APP: ctx.APP, CONTROLLER: ctx.CONTROLLER, ACTION: ctx.ACTION});
|
|
15
14
|
Object.keys(ctx.params).length && Logger.http('Params:', ctx.params);
|
|
16
15
|
Object.keys(ctx.query).length && Logger.http('Get:', ctx.query);
|
package/lib/types.js
CHANGED
|
@@ -21,6 +21,7 @@ function start() {
|
|
|
21
21
|
Object.keys(f).forEach(file => {
|
|
22
22
|
Logger.system("wacth:", file);
|
|
23
23
|
});
|
|
24
|
+
createFile();
|
|
24
25
|
} else if(prev === null) {
|
|
25
26
|
// f is a new file
|
|
26
27
|
if(prevFile.file != f || prevFile.action != "created") {
|
|
@@ -55,18 +56,22 @@ function start() {
|
|
|
55
56
|
* createTypesFile
|
|
56
57
|
*/
|
|
57
58
|
async function createFile(f) {
|
|
58
|
-
if(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
59
|
+
if(f) {
|
|
60
|
+
if(f == this._f) {
|
|
61
|
+
return require.cache[f] && delete(require.cache[f]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this._f && require.cache[this._f] && delete(require.cache[this._f]);
|
|
65
|
+
this._f = f;
|
|
66
|
+
|
|
67
|
+
const f_info = f.replace(cfg_app.base_dir + path.sep, '').split(path.sep);
|
|
68
|
+
if(f_info[0] && f_info[1] && f_info[0] != cfg_app.common_app && !~f_info[0].indexOf('.')) {
|
|
69
|
+
this._APP = f_info[0];
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
this._APP = cfg_app.default_app;
|
|
69
73
|
}
|
|
74
|
+
|
|
70
75
|
|
|
71
76
|
const ignore = ['node_modules', 'docker', 'package-lock.json', 'types.js', '.git', '.gitignore'];
|
|
72
77
|
ignore.push(path.basename(module.parent.parent.parent.filename));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jj.js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "A super simple lightweight NodeJS MVC framework(一个超级简单轻量的NodeJS MVC框架)",
|
|
5
5
|
"main": "jj.js",
|
|
6
6
|
"scripts": {},
|
|
@@ -27,9 +27,7 @@
|
|
|
27
27
|
"koa": "^2.15.0",
|
|
28
28
|
"koa-body": "^5.0.0",
|
|
29
29
|
"koa-static": "^5.0.0",
|
|
30
|
-
"mysql": "^2.18.1"
|
|
31
|
-
},
|
|
32
|
-
"devDependencies": {
|
|
30
|
+
"mysql": "^2.18.1",
|
|
33
31
|
"watch": "^1.0.2"
|
|
34
32
|
},
|
|
35
33
|
"engines": {
|
package/types.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* @typedef {typeof import('./lib/middleware')} Middleware
|
|
11
11
|
* @typedef {typeof import('./lib/model')} Model
|
|
12
12
|
* @typedef {typeof import('./lib/pagination')} Pagination
|
|
13
|
+
* @typedef {typeof import('./lib/request')} Request
|
|
13
14
|
* @typedef {typeof import('./lib/response')} Response
|
|
14
15
|
* @typedef {typeof import('./lib/storage')} Storage
|
|
15
16
|
* @typedef {typeof import('./lib/upload')} Upload
|
|
@@ -26,6 +27,7 @@
|
|
|
26
27
|
* @typedef {typeof import('./lib/middleware').prototype} MiddlewareInstance
|
|
27
28
|
* @typedef {typeof import('./lib/model').prototype} ModelInstance
|
|
28
29
|
* @typedef {typeof import('./lib/pagination').prototype} PaginationInstance
|
|
30
|
+
* @typedef {typeof import('./lib/request').prototype} RequestInstance
|
|
29
31
|
* @typedef {typeof import('./lib/response').prototype} ResponseInstance
|
|
30
32
|
* @typedef {typeof import('./lib/upload').prototype} UploadInstance
|
|
31
33
|
* @typedef {typeof import('./lib/url').prototype} UrlInstance
|
|
@@ -45,6 +47,7 @@
|
|
|
45
47
|
* @property {Middleware} Middleware
|
|
46
48
|
* @property {Model} Model
|
|
47
49
|
* @property {Pagination} Pagination
|
|
50
|
+
* @property {Request} Request
|
|
48
51
|
* @property {Response} Response
|
|
49
52
|
* @property {Upload} Upload
|
|
50
53
|
* @property {Url} Url
|
|
@@ -67,6 +70,7 @@
|
|
|
67
70
|
* @property {MiddlewareInstance} middleware
|
|
68
71
|
* @property {ModelInstance} model
|
|
69
72
|
* @property {PaginationInstance} pagination
|
|
73
|
+
* @property {RequestInstance} request
|
|
70
74
|
* @property {ResponseInstance} response
|
|
71
75
|
* @property {UploadInstance} upload
|
|
72
76
|
* @property {UrlInstance} url
|
|
@@ -86,7 +90,7 @@
|
|
|
86
90
|
* @property {string} [common_app=common] - 公共应用,存放公共模型及逻辑
|
|
87
91
|
* @property {string} [controller_folder=controller] - 控制器目录名
|
|
88
92
|
* @property {string} [static_dir=''] - 静态文件目录,相对于应用根目录,为空时,关闭静态访问
|
|
89
|
-
* @property {
|
|
93
|
+
* @property {import('koa-body').IKoaBodyOptions} [koa_body=null] - koa-body配置参数,为null或空时,关闭koa-body
|
|
90
94
|
* @property {string} [base_dir] - 应用根目录(会自动计算)
|
|
91
95
|
*/
|
|
92
96
|
|
|
@@ -96,7 +100,7 @@
|
|
|
96
100
|
* @property {string} [view_depr='/'] - 模版文件名分割符,'/'代表二级目录
|
|
97
101
|
* @property {string} [view_ext='.htm'] - 模版文件后缀
|
|
98
102
|
* @property {string} [view_engine=art-template] - 默认模版引擎,字符串或引擎类
|
|
99
|
-
* @property {
|
|
103
|
+
* @property {Object} [view_filte={}] - 模版函数
|
|
100
104
|
*/
|
|
101
105
|
|
|
102
106
|
/**
|
|
@@ -112,7 +116,7 @@
|
|
|
112
116
|
*/
|
|
113
117
|
|
|
114
118
|
/**
|
|
115
|
-
* @typedef {
|
|
119
|
+
* @typedef {Object.<string, Object>} DbConfig - 数据库配置
|
|
116
120
|
* @property {DbConfigItem} default - 数据库参数
|
|
117
121
|
*/
|
|
118
122
|
|
|
@@ -123,7 +127,7 @@
|
|
|
123
127
|
*/
|
|
124
128
|
|
|
125
129
|
/**
|
|
126
|
-
* @typedef {
|
|
130
|
+
* @typedef {Object} LogConfig - 日志配置
|
|
127
131
|
* @property {array} [log_level] - 允许输出的日志级别
|
|
128
132
|
* @property {LogHandle} [log_handle] - 日志驱动
|
|
129
133
|
*/
|
|
@@ -283,6 +287,9 @@ class Ctx {
|
|
|
283
287
|
/** @type {(Pagination & PaginationInstance)} */
|
|
284
288
|
$pagination;
|
|
285
289
|
|
|
290
|
+
/** @type {(Request & RequestInstance)} */
|
|
291
|
+
$request;
|
|
292
|
+
|
|
286
293
|
/** @type {(Response & ResponseInstance)} */
|
|
287
294
|
$response;
|
|
288
295
|
|