jj.js 0.9.0 → 0.11.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/lib/view.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const path = require('path');
2
- const {readFile, isFileSync} = require('./utils/fs');
2
+ const {isFileSync} = require('./utils/fs');
3
+ const {readFile} = require('fs').promises;
3
4
  const {app: cfg_app, view: cfg_view} = require('./config');
4
5
  const {toLine} = require('./utils/str');
5
6
  const Context = require('./context');
@@ -12,7 +13,7 @@ class View extends Context
12
13
  /**
13
14
  * Initialize a new `View`
14
15
  * @public
15
- * @param {import('./types').Context} ctx
16
+ * @param {import('../types').KoaCtx} ctx
16
17
  */
17
18
  constructor(ctx) {
18
19
  super(ctx);
@@ -28,7 +29,7 @@ class View extends Context
28
29
  */
29
30
  async load(template) {
30
31
  const file_name = this.parseTplName(template);
31
- return await readFile(file_name);
32
+ return (await readFile(file_name)).toString();
32
33
  }
33
34
 
34
35
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jj.js",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "A super simple lightweight NodeJS MVC framework(一个超级简单轻量的NodeJS MVC框架)",
5
5
  "main": "jj.js",
6
6
  "scripts": {},
@@ -29,6 +29,9 @@
29
29
  "koa-static": "^5.0.0",
30
30
  "mysql": "^2.18.1"
31
31
  },
32
+ "devDependencies": {
33
+ "watch": "^1.0.2"
34
+ },
32
35
  "engines": {
33
36
  "node": ">= 12.17.0"
34
37
  }
package/types.js ADDED
@@ -0,0 +1,308 @@
1
+ //------------------系统核心库------------------
2
+ /**
3
+ * @typedef {typeof import('./lib/app')} App
4
+ * @typedef {typeof import('./lib/cache')} Cache
5
+ * @typedef {typeof import('./lib/context')} Context
6
+ * @typedef {typeof import('./lib/controller')} Controller
7
+ * @typedef {typeof import('./lib/cookie')} Cookie
8
+ * @typedef {typeof import('./lib/db')} Db
9
+ * @typedef {typeof import('./lib/logger')} Logger
10
+ * @typedef {typeof import('./lib/middleware')} Middleware
11
+ * @typedef {typeof import('./lib/model')} Model
12
+ * @typedef {typeof import('./lib/pagination')} Pagination
13
+ * @typedef {typeof import('./lib/response')} Response
14
+ * @typedef {typeof import('./lib/storage')} Storage
15
+ * @typedef {typeof import('./lib/upload')} Upload
16
+ * @typedef {typeof import('./lib/url')} Url
17
+ * @typedef {typeof import('./lib/view')} View
18
+ */
19
+
20
+ /**
21
+ * @typedef {typeof import('./lib/context').prototype} ContextInstance
22
+ * @typedef {typeof import('./lib/controller').prototype} ControllerInstance
23
+ * @typedef {typeof import('./lib/cookie').prototype} CookieInstance
24
+ * @typedef {typeof Ctx.prototype} CtxInstance
25
+ * @typedef {typeof import('./lib/db').prototype} DbInstance
26
+ * @typedef {typeof import('./lib/middleware').prototype} MiddlewareInstance
27
+ * @typedef {typeof import('./lib/model').prototype} ModelInstance
28
+ * @typedef {typeof import('./lib/pagination').prototype} PaginationInstance
29
+ * @typedef {typeof import('./lib/response').prototype} ResponseInstance
30
+ * @typedef {typeof import('./lib/upload').prototype} UploadInstance
31
+ * @typedef {typeof import('./lib/url').prototype} UrlInstance
32
+ * @typedef {typeof import('./lib/view').prototype} ViewInstance
33
+ */
34
+
35
+ /**
36
+ * @typedef {Object} Core
37
+ * @property {App} App
38
+ * @property {Cache} Cache
39
+ * @property {Context} Context
40
+ * @property {Controller} Controller
41
+ * @property {Cookie} Cookie
42
+ * @property {Ctx} Ctx
43
+ * @property {Db} Db
44
+ * @property {Logger} Logger
45
+ * @property {Middleware} Middleware
46
+ * @property {Model} Model
47
+ * @property {Pagination} Pagination
48
+ * @property {Response} Response
49
+ * @property {Upload} Upload
50
+ * @property {Url} Url
51
+ * @property {View} View
52
+ * @property {Utils} utils
53
+ * @property {Config} config
54
+ * @property {Storage} storage
55
+ */
56
+
57
+ /**
58
+ * @typedef {Object} $
59
+ * @property {Cache} cache
60
+ * @property {Config} config
61
+ * @property {ContextInstance} context
62
+ * @property {ControllerInstance} controller
63
+ * @property {CookieInstance} cookie
64
+ * @property {CtxInstance} ctx
65
+ * @property {DbInstance} db
66
+ * @property {Logger} logger
67
+ * @property {MiddlewareInstance} middleware
68
+ * @property {ModelInstance} model
69
+ * @property {PaginationInstance} pagination
70
+ * @property {ResponseInstance} response
71
+ * @property {UploadInstance} upload
72
+ * @property {UrlInstance} url
73
+ * @property {ViewInstance} view
74
+ * @property {Utils} utils
75
+ */
76
+
77
+
78
+ //------------------系统App配置---------------------
79
+ /**
80
+ * @typedef {Object} AppConfig - 系统配置
81
+ * @property {boolean} [app_debug=true] - 调试模式
82
+ * @property {boolean} [app_multi=false] - 是否开启多应用
83
+ * @property {string} [default_app=app] - 默认应用
84
+ * @property {string} [default_controller=index] - 默认控制器
85
+ * @property {string} [default_action=index] - 默认方法
86
+ * @property {string} [common_app=common] - 公共应用,存放公共模型及逻辑
87
+ * @property {string} [controller_folder=controller] - 控制器目录名
88
+ * @property {string} [static_dir=''] - 静态文件目录,相对于应用根目录,为空时,关闭静态访问
89
+ * @property {?object} [koa_body=null] - koa-body配置参数,为null或空时,关闭koa-body
90
+ * @property {string} [base_dir] - 应用根目录(会自动计算)
91
+ */
92
+
93
+ /**
94
+ * @typedef {Object} ViewConfig - 模板配置
95
+ * @property {string} [view_folder=view] - 模板目录名
96
+ * @property {string} [view_depr='/'] - 模版文件名分割符,'/'代表二级目录
97
+ * @property {string} [view_ext='.htm'] - 模版文件后缀
98
+ * @property {string} [view_engine=art-template] - 默认模版引擎,字符串或引擎类
99
+ * @property {object} [view_filte={}] - 模版函数
100
+ */
101
+
102
+ /**
103
+ * @typedef {Object} DbConfigItem - 数据库参数
104
+ * @property {string} [type=mysql] - 数据库类型
105
+ * @property {string} [host='127.0.0.1'] - 服务器地址
106
+ * @property {string} [database=jj] - 数据库名
107
+ * @property {string} [user=root] - 数据库用户名
108
+ * @property {string} [password=''] - 数据库密码
109
+ * @property {string} [port=''] - 数据库连接端口
110
+ * @property {string} [charset=utf8] - 数据库编码默认采用utf8
111
+ * @property {string} [prefix=jj_] - 数据库表前缀
112
+ */
113
+
114
+ /**
115
+ * @typedef {object} DbConfig - 数据库配置
116
+ * @property {DbConfigItem} default - 数据库参数
117
+ */
118
+
119
+ /**
120
+ * @callback LogHandle - 日志驱动
121
+ * @param {string} level - 日志级别
122
+ * @param {...any} args - 日志数据,支持多个,支持对象
123
+ */
124
+
125
+ /**
126
+ * @typedef {object} LogConfig - 日志配置
127
+ * @property {array} [log_level] - 允许输出的日志级别
128
+ * @property {LogHandle} [log_handle] - 日志驱动
129
+ */
130
+
131
+ /**
132
+ * @typedef {Object} CacheConfig - 缓存配置
133
+ * @property {number} [cache_time='60 * 60 * 24'] - 缓存时间,默认1天,为空或false则为10年
134
+ * @property {number} [clear_time=undefined] - 缓存自动清理周期,undefined: 清理一次, 0: 关闭自动清理, >0: 为周期时间,单位秒
135
+ */
136
+
137
+ /**
138
+ * @typedef {Object} PageConfig - 分页配置
139
+ * @property {string} [page_key=page] - 分页标识
140
+ * @property {string} [key_origin=query] - page_key来源
141
+ * @property {number} [page_size=10] - page_key来源
142
+ * @property {number} [page_length=5] - page_key来源
143
+ * @property {string} [url_page] - page_key来源
144
+ * @property {string} [url_index] - page_key来源
145
+ * @property {string} [index_tpl] - 首页模板
146
+ * @property {string} [end_tpl] - 末页模板
147
+ * @property {string} [prev_tpl] - 上一页模板
148
+ * @property {string} [next_tpl] - 下一页模板
149
+ * @property {string} [list_tpl] - 数字页模板
150
+ * @property {string} [active_tpl] - 当前页模板
151
+ * @property {string} [info_tpl] - 分页信息模板
152
+ * @property {string} [template] - 渲染模板
153
+ */
154
+
155
+ /**
156
+ * @typedef {Object} RouteConfigItem - 路由配置
157
+ * @property {string} [method=all] - 请求方法,支持['all', 'get', 'put', 'post', 'patch', 'delete', 'del']
158
+ * @property {string} url - 请求url,支持变量正则,详细参考@koa/router
159
+ * @property {(string|Middleware)} path - 响应地址(支持智能解析)或中间件函数,如果为中间件函数,则不会再执行后续代码
160
+ * @property {string} [type='AppConfig.controller_folder'] - 响应类型,即path对应的类型,支持controller、middleware、view(ViewConfig.view_folder)
161
+ * @property {string} [name] - 路由命名,命一个名字后,可以使用Url类反向编译路由url
162
+ */
163
+
164
+ /**
165
+ * @typedef {RouteConfigItem[]} RouteConfig - 路由配置
166
+ */
167
+
168
+ /**
169
+ * @typedef {import('cookies').SetOption} CookieConfig - Cookie配置,一般不用设置
170
+ */
171
+
172
+ /**
173
+ * @typedef {Object} TplConfig - 跳转、调试模板配置
174
+ * @property {string} [jump] - 跳转模板,默认require('./tpl/jump')
175
+ * @property {string} [exception] - 调试异常输出模板,默认require('./tpl/exception')
176
+ */
177
+
178
+ /**
179
+ * @typedef {Object} Config - 系统配置
180
+ * @property {AppConfig} [app]
181
+ * @property {ViewConfig} [view]
182
+ * @property {DbConfig} [db]
183
+ * @property {LogConfig} [log]
184
+ * @property {CacheConfig} [cache]
185
+ * @property {PageConfig} [page]
186
+ * @property {RouteConfig} [routes]
187
+ * @property {CookieConfig} [cookie]
188
+ * @property {TplConfig} [tpl]
189
+ */
190
+
191
+
192
+ //------------------数据库类--------------------
193
+ /**
194
+ * @typedef {import('mysql').Pool} Pool - 连接池
195
+ * @typedef {import('mysql').PoolConfig} PoolConfig - 连接池配置
196
+ * @typedef {import('mysql').PoolConnection} PoolConnection - 连接池连接
197
+ * @typedef {import('mysql').QueryOptions} QueryOptions - 查询参数
198
+ * @typedef {import('mysql').OkPacket} OkPacket - 数据库查询结果
199
+ * @typedef {Object} RowData - 单条数据
200
+ * @typedef {Array<RowData>} ListData - 多条数据
201
+ * @typedef {Object} FieldInfo - 字段信息
202
+ * @property {string} Field
203
+ * @property {string} Type
204
+ * @property {string} Null
205
+ * @property {string} Key
206
+ * @property {string} Default
207
+ * @property {string} Extra
208
+ * @typedef {Map<Pool>} PoolMap
209
+ */
210
+
211
+
212
+ //------------------上传类--------------------
213
+ /**
214
+ * @typedef {Object} UploadData
215
+ * @property {string} [filename]
216
+ * @property {string} [extname]
217
+ * @property {string} [savename]
218
+ * @property {string} [filepath]
219
+ * @property {string} [name]
220
+ * @property {number} [size]
221
+ * @property {string} [mimetype]
222
+ * @property {string} [hash]
223
+ * /
224
+ *
225
+ /**
226
+ * @typedef {Object} ValidateRule
227
+ * @property {number} [size] - 文件大小
228
+ * @property {string} [ext] - 文件名后缀,多个用','隔开
229
+ * @property {string} [type] - 文件mimetype,多个用','隔开
230
+ */
231
+
232
+
233
+ //------------------系统工具--------------------
234
+ /**
235
+ * @typedef {Object} Utils
236
+ * @property {typeof import('./lib/utils/date')} date
237
+ * @property {typeof import('./lib/utils/error')} error
238
+ * @property {typeof import('./lib/utils/fs')} fs
239
+ * @property {typeof import('./lib/utils/md5')} md5
240
+ * @property {typeof import('./lib/utils/str')} str
241
+ */
242
+
243
+
244
+ //------------------Koa Context--------------------
245
+ /**
246
+ * @typedef {import('koa').Context} KoaCtx - Koa ctx
247
+ * @callback AsyncNext - 中间件函数
248
+ */
249
+
250
+
251
+ //------------------Ctx系统基类--------------------
252
+ /**
253
+ * @class Ctx
254
+ */
255
+ class Ctx {
256
+ /** @type {$} */
257
+ $;
258
+
259
+ /** @type {Cache} */
260
+ $cache;
261
+
262
+ /** @type {(Context & ContextInstance)} */
263
+ $context;
264
+
265
+ /** @type {(Controller & ControllerInstance)} */
266
+ $controller;
267
+
268
+ /** @type {(Cookie & CookieInstance)} */
269
+ $cookie;
270
+
271
+ /** @type {(Db & DbInstance)} */
272
+ $db;
273
+
274
+ /** @type {Logger} */
275
+ $logger;
276
+
277
+ /** @type {(Middleware & MiddlewareInstance)} */
278
+ $middleware;
279
+
280
+ /** @type {(Model & ModelInstance)} */
281
+ $model;
282
+
283
+ /** @type {(Pagination & PaginationInstance)} */
284
+ $pagination;
285
+
286
+ /** @type {(Response & ResponseInstance)} */
287
+ $response;
288
+
289
+ /** @type {(Upload & UploadInstance)} */
290
+ $upload;
291
+
292
+ /** @type {(Url & UrlInstance)} */
293
+ $url;
294
+
295
+ /** @type {(View & ViewInstance)} */
296
+ $view;
297
+
298
+ /** @type {Utils} */
299
+ $utils;
300
+
301
+ /** @type {Config} */
302
+ $config;
303
+ }
304
+
305
+ /**
306
+ * @module Types
307
+ */
308
+ module.exports = Ctx;