koa-ts-core 0.1.0-dev.0 → 0.2.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 +287 -123
- package/dist/global.d.ts +2 -0
- package/dist/index.cjs.js +8 -8
- package/dist/index.d.ts +2 -1
- package/dist/index.esm.js +8 -8
- package/dist/init/index.d.ts +4 -17
- package/dist/init/register_log.d.ts +2 -2
- package/dist/init/register_middleware.d.ts +14 -0
- package/dist/middleware/create_trackId_middleware.d.ts +6 -0
- package/dist/middleware/exception_middleware.d.ts +3 -3
- package/dist/middleware/router_middleware.d.ts +6 -0
- package/dist/types/core.d.ts +88 -0
- package/package.json +5 -2
- /package/dist/{init → utils}/address.d.ts +0 -0
package/dist/init/index.d.ts
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
|
-
import { TErrorCallback } from '../middleware/exception_middleware';
|
|
2
1
|
import { appRouter } from '../router';
|
|
3
|
-
import
|
|
2
|
+
import { CreateKoaOptions } from '../types/core';
|
|
4
3
|
import Koa from "koa";
|
|
5
|
-
import { TLog4 } from "./register_log";
|
|
6
|
-
export type TInitOPtions = Partial<{
|
|
7
|
-
koaInstance: Koa;
|
|
8
|
-
corsMiddleware: Koa.Middleware;
|
|
9
|
-
authCheckCallback: AuthRouterCallback;
|
|
10
|
-
catchErrorCallback: TErrorCallback;
|
|
11
|
-
registerHighPriorityMiddleware: (app: Koa) => void;
|
|
12
|
-
registerHook: (ctx: Koa.Context, type: "request" | "response" | "error") => void;
|
|
13
|
-
log4: TLog4;
|
|
14
|
-
runtimeLog: boolean;
|
|
15
|
-
}>;
|
|
16
4
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
19
|
-
* @returns {[Koa, typeof appRouter]} [app, appRouter]
|
|
5
|
+
* 创建并初始化 Koa 应用
|
|
6
|
+
* @returns [app 实例, 路由对象]
|
|
20
7
|
*/
|
|
21
|
-
export declare const
|
|
8
|
+
export declare const createKoaApp: (rawOptions: Partial<CreateKoaOptions>) => Promise<[Koa, typeof appRouter]>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ErrorConfig, Koa2CorsFn, PhaseMiddlewareMap, TrackIdConfig } from '../types/core';
|
|
2
|
+
import Koa from "koa";
|
|
3
|
+
/**
|
|
4
|
+
* 注册中间件
|
|
5
|
+
* @param app
|
|
6
|
+
* @param phaseMiddlewares
|
|
7
|
+
* @param options
|
|
8
|
+
*/
|
|
9
|
+
declare const registerMiddleware: (app: Koa, phaseMiddlewares: PhaseMiddlewareMap, options: {
|
|
10
|
+
errorConfig?: ErrorConfig;
|
|
11
|
+
koa2Cors?: Koa2CorsFn;
|
|
12
|
+
trackConfig: TrackIdConfig;
|
|
13
|
+
}) => void;
|
|
14
|
+
export default registerMiddleware;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import BaseException from '../base/exception';
|
|
2
2
|
import type Koa from "koa";
|
|
3
|
-
export type
|
|
3
|
+
export type ErrorFn = (error: Error | BaseException, ctx: Koa.Context) => void;
|
|
4
4
|
/**
|
|
5
5
|
* 错误处理中间件
|
|
6
|
-
* @param {
|
|
6
|
+
* @param {ErrorFn} catchCallback
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
|
-
declare const catchErrorMiddleware: (catchCallback?:
|
|
9
|
+
declare const catchErrorMiddleware: (catchCallback?: ErrorFn) => (ctx: Koa.Context, next: Koa.Next) => Promise<void>;
|
|
10
10
|
export default catchErrorMiddleware;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type Koa from "koa";
|
|
2
|
+
import cors from "koa2-cors";
|
|
3
|
+
import type { Log4js } from "log4js";
|
|
4
|
+
import { AuthRouterCallback } from "./route";
|
|
5
|
+
/**
|
|
6
|
+
* 中间件分阶段
|
|
7
|
+
*/
|
|
8
|
+
export declare enum MiddlewarePhase {
|
|
9
|
+
AsyncContext = "AsyncContext",
|
|
10
|
+
ErrorHandling = "ErrorHandling",
|
|
11
|
+
Logging = "Logging",
|
|
12
|
+
Security = "Security",
|
|
13
|
+
BodyParsing = "BodyParsing",
|
|
14
|
+
Auth = "Auth",
|
|
15
|
+
Routing = "Routing"
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 阶段中间件配置
|
|
19
|
+
*/
|
|
20
|
+
export interface PhaseMiddlewareConfig {
|
|
21
|
+
before?: Koa.Middleware[];
|
|
22
|
+
use?: Koa.Middleware[];
|
|
23
|
+
after?: Koa.Middleware[];
|
|
24
|
+
}
|
|
25
|
+
export type TrackIdGenerator = false | ((ctx: Koa.Context) => string | Promise<string>);
|
|
26
|
+
export interface TrackIdConfig {
|
|
27
|
+
/**
|
|
28
|
+
* 生成 trackId 的函数:
|
|
29
|
+
* - 默认:从 header 透传或生成 uuid.v4
|
|
30
|
+
* - 传入 false 则不生成、不透传
|
|
31
|
+
*/
|
|
32
|
+
generator?: TrackIdGenerator;
|
|
33
|
+
/**
|
|
34
|
+
* 是否将 trackId 写回响应头
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
exposeHeader?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* 使用的 header 名称
|
|
40
|
+
* @default "x-request-id"
|
|
41
|
+
*/
|
|
42
|
+
headerName?: string;
|
|
43
|
+
}
|
|
44
|
+
export type ErrorFn = (err: unknown, ctx: Koa.Context) => Promise<void> | void;
|
|
45
|
+
export type TLog4 = boolean | ((instance: Log4js) => Log4js);
|
|
46
|
+
export type Koa2CorsFn = (koa2Cors: typeof cors) => Koa.Middleware;
|
|
47
|
+
export type HookType = "request" | "response" | "error";
|
|
48
|
+
export type RegisterHookFn = (ctx: Koa.Context, type: HookType) => void;
|
|
49
|
+
export type PhaseMiddlewareMap = Map<MiddlewarePhase, PhaseMiddlewareConfig>;
|
|
50
|
+
export interface AuthConfig {
|
|
51
|
+
handler: AuthRouterCallback;
|
|
52
|
+
}
|
|
53
|
+
export interface ErrorConfig {
|
|
54
|
+
handler?: ErrorFn;
|
|
55
|
+
/** 是否在响应中暴露堆栈信息(默认 dev=true, prod=false) */
|
|
56
|
+
exposeStack?: boolean;
|
|
57
|
+
}
|
|
58
|
+
export interface LogConfig {
|
|
59
|
+
/** log4 配置 */
|
|
60
|
+
log4?: TLog4;
|
|
61
|
+
/** 是否记录每次请求的运行日志(旧的 runtimeLog) */
|
|
62
|
+
runtimeLog?: boolean;
|
|
63
|
+
}
|
|
64
|
+
export interface HookConfig {
|
|
65
|
+
/** hook 注册函数(旧的 registerHook) */
|
|
66
|
+
register?: RegisterHookFn;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 初始化配置
|
|
70
|
+
*/
|
|
71
|
+
export interface CreateKoaOptions {
|
|
72
|
+
/** 支持自定义 koa 实例 */
|
|
73
|
+
koaInstance?: Koa;
|
|
74
|
+
/** 基于 koa2-cors */
|
|
75
|
+
koa2Cors?: Koa2CorsFn;
|
|
76
|
+
/** 鉴权配置(替代 authCheckCallback) */
|
|
77
|
+
auth?: AuthConfig;
|
|
78
|
+
/** 错误处理配置(替代 catchErrorCallback) */
|
|
79
|
+
error?: ErrorConfig;
|
|
80
|
+
/** 日志相关配置(替代 log4 + runtimeLog) */
|
|
81
|
+
log?: LogConfig;
|
|
82
|
+
/** hook 配置(替代 registerHook) */
|
|
83
|
+
hooks?: HookConfig;
|
|
84
|
+
/** 分阶段中间件扩展 */
|
|
85
|
+
phaseMiddlewares?: PhaseMiddlewareMap;
|
|
86
|
+
/** trackId 配置 */
|
|
87
|
+
trackId?: TrackIdConfig;
|
|
88
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa-ts-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "koa-ts-core",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -24,14 +24,17 @@
|
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@koa/router": "^15.2.0",
|
|
27
|
+
"@types/koa2-cors": "^2.0.6",
|
|
27
28
|
"dotenv": "^17.2.3",
|
|
28
29
|
"koa": "^3.1.1",
|
|
29
30
|
"koa-bodyparser": "^4.4.1",
|
|
31
|
+
"koa2-cors": "^2.0.6",
|
|
30
32
|
"log4js": "^6.9.1",
|
|
31
33
|
"portfinder": "^1.0.38",
|
|
32
34
|
"pug": "^3.0.3",
|
|
33
35
|
"rollup-plugin-copy-and-reference-dts": "0.0.2",
|
|
34
|
-
"rollup-plugin-pug": "^1.1.1"
|
|
36
|
+
"rollup-plugin-pug": "^1.1.1",
|
|
37
|
+
"uuid": "^13.0.0"
|
|
35
38
|
},
|
|
36
39
|
"devDependencies": {
|
|
37
40
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
File without changes
|