koa-ts-core 0.0.16 → 0.0.18

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 CHANGED
@@ -8,12 +8,6 @@
8
8
  npm i koa-ts-core
9
9
  ```
10
10
 
11
- 宿主项目需要安装库
12
-
13
- ```shell
14
- npm i koa @koa/router dotenv
15
- ```
16
-
17
11
  ## 初始化
18
12
 
19
13
  ```typescript
@@ -28,6 +22,13 @@ const [app] = await initializeKoaApp({
28
22
  registerHighPriorityMiddleware: (app: Koa) => void,
29
23
  // 异常处理
30
24
  catchErrorCallback: (error: Error) => void,
25
+ // hook
26
+ registerHook: (
27
+ ctx: Koa.Context,
28
+ type: "request" | "response" | "error"
29
+ ) => void;
30
+ // 是否挂载log4到context
31
+ log4: TLog4;
31
32
  });
32
33
 
33
34
  app.listen();
@@ -47,6 +48,10 @@ app.listen();
47
48
 
48
49
  - registerHighPriorityMiddleware ((app: Koa) => void)) : 高优先级中间件注册函数,用于注册一些高优先级中间件。
49
50
 
51
+ - registerHook ((ctx: Koa.Context, type: "request" | "response" | "error") => void) : hook 注册函数,用于注册一些 hook
52
+
53
+ - log4 (TLog4) : 是否挂载 log4 到 context
54
+
50
55
  ## 能力
51
56
 
52
57
  ### 约定式路由
@@ -267,6 +272,49 @@ export default class Test {
267
272
 
268
273
  打开`/doc`路由地址,渲染文档
269
274
 
275
+ ### 挂载 log4
276
+
277
+ 使用[log4js](https://www.npmjs.com/package/log4js)库对日志进行预加载
278
+
279
+ ```typescript
280
+ type TLog4 = boolean | ((instance: Log4js) => Log4js);
281
+ ```
282
+
283
+ 如果传入值为`true`,则使用默认配置
284
+
285
+ ```js
286
+ log4js.configure({
287
+ appenders: {
288
+ console: { type: "console" }, // 控制台输出
289
+ file: {
290
+ type: "dateFile",
291
+ filename: "logs/app.log",
292
+ pattern: ".yyyy-MM-dd",
293
+ compress: true,
294
+ numBackups: 7,
295
+ alwaysIncludePattern: true,
296
+ },
297
+ },
298
+ categories: {
299
+ default: { appenders: ["console", "file"], level: "info" },
300
+ },
301
+ });
302
+ ```
303
+
304
+ 如需覆盖默认配置,传入函数
305
+
306
+ ```typescript
307
+ initializeKoaApp({
308
+ log4: (instance) => {
309
+ return instance.configure({
310
+ appenders: {
311
+ console: { type: "console" }, // 控制台输出
312
+ },
313
+ });
314
+ },
315
+ });
316
+ ```
317
+
270
318
  ### 配置项
271
319
 
272
320
  #### process.env.APP_PORT
@@ -0,0 +1,13 @@
1
+ import "koa";
2
+
3
+ declare module "koa" {
4
+ interface DefaultContext {
5
+ log4?: import("log4js").Log4js;
6
+ }
7
+ }
8
+
9
+ declare namespace NodeJS {
10
+ interface ProcessEnv {
11
+ NODE_ENV: "development" | "production" | "test"; // 环境类型
12
+ }
13
+ }