koa-ts-core 0.2.1 → 0.3.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 +149 -496
- package/dist/{init → core}/register_middleware.d.ts +2 -0
- package/dist/{init → core}/register_route.d.ts +2 -1
- package/dist/core/render_doc.d.ts +12 -0
- package/dist/core/swagger_collector.d.ts +22 -0
- package/dist/decorators/index.d.ts +3 -0
- package/dist/{decorate/router_decorate.d.ts → decorators/router.d.ts} +2 -10
- package/dist/decorators/swagger.d.ts +51 -0
- package/dist/decorators/validate.d.ts +9 -0
- package/dist/{base/exception.d.ts → exceptions/base.d.ts} +1 -0
- package/dist/exceptions/index.d.ts +1 -0
- package/dist/global.d.ts +5 -1
- package/dist/index.cjs.js +1 -39
- package/dist/index.d.ts +4 -3
- package/dist/index.esm.js +1 -39
- package/dist/{middleware/exception_middleware.d.ts → middlewares/exception.d.ts} +1 -1
- package/dist/types/core.d.ts +12 -0
- package/dist/utils/address.d.ts +2 -3
- package/dist/utils/functions.d.ts +1 -0
- package/dist/utils/path.d.ts +7 -4
- package/package.json +8 -6
- package/dist/base/index.d.ts +0 -1
- package/dist/decorate/index.d.ts +0 -1
- package/dist/init/render_doc.d.ts +0 -8
- /package/dist/{constant.d.ts → constants/index.d.ts} +0 -0
- /package/dist/{init/index.d.ts → core/app.d.ts} +0 -0
- /package/dist/{init → core}/register_controller.d.ts +0 -0
- /package/dist/{init → core}/register_env.d.ts +0 -0
- /package/dist/{init → core}/register_log.d.ts +0 -0
- /package/dist/{middleware/context_middleware.d.ts → middlewares/context.d.ts} +0 -0
- /package/dist/{middleware/logger_middleware.d.ts → middlewares/logger.d.ts} +0 -0
- /package/dist/{middleware/request_params_middleware.d.ts → middlewares/request_params.d.ts} +0 -0
- /package/dist/{middleware/router_middleware.d.ts → middlewares/router.d.ts} +0 -0
- /package/dist/{middleware/create_trackId_middleware.d.ts → middlewares/track_id.d.ts} +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ErrorConfig, Koa2CorsFn, PhaseMiddlewareMap, TrackIdConfig } from '../types/core';
|
|
2
2
|
import Koa from "koa";
|
|
3
|
+
import { Options as BodyParserOptions } from "koa-bodyparser";
|
|
3
4
|
/**
|
|
4
5
|
* 注册中间件
|
|
5
6
|
* @param app
|
|
@@ -10,5 +11,6 @@ declare const registerMiddleware: (app: Koa, phaseMiddlewares: PhaseMiddlewareMa
|
|
|
10
11
|
errorConfig?: ErrorConfig;
|
|
11
12
|
koa2Cors?: Koa2CorsFn;
|
|
12
13
|
trackConfig: TrackIdConfig;
|
|
14
|
+
bodyParser?: BodyParserOptions;
|
|
13
15
|
}) => void;
|
|
14
16
|
export default registerMiddleware;
|
|
@@ -3,8 +3,9 @@ import { AuthRouterHandler, FilePathMeta, RouteConfig } from '../types/route';
|
|
|
3
3
|
* 注册路由
|
|
4
4
|
* @param validateModulePath validate 根目录路径
|
|
5
5
|
* @param filePath 控制器文件路径元信息
|
|
6
|
+
* @param controllerClass 控制器类
|
|
6
7
|
* @param routesMap 路由映射
|
|
7
8
|
* @param authCheckResult 鉴权结果处理函数
|
|
8
9
|
*/
|
|
9
|
-
declare const registerRoute: (validateModulePath: string, filePath: FilePathMeta, routesMap: Map<string, RouteConfig>, authCheckResult: AuthRouterHandler) => void;
|
|
10
|
+
declare const registerRoute: (validateModulePath: string, filePath: FilePathMeta, controllerClass: any, routesMap: Map<string, RouteConfig>, authCheckResult: AuthRouterHandler) => void;
|
|
10
11
|
export default registerRoute;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IMetaData } from '../types/meta_data';
|
|
2
|
+
export type TClassMeta = {
|
|
3
|
+
configs: IMetaData[];
|
|
4
|
+
desc: string;
|
|
5
|
+
name: string;
|
|
6
|
+
apiPrePath: string;
|
|
7
|
+
pathPrefix: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* 扫描 doc 目录并提取元数据
|
|
11
|
+
*/
|
|
12
|
+
export declare const scanDocMetadata: () => TClassMeta[];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare class SwaggerCollector {
|
|
2
|
+
private static instance;
|
|
3
|
+
private paths;
|
|
4
|
+
static getInstance(): SwaggerCollector;
|
|
5
|
+
/**
|
|
6
|
+
* 添加路由定义到 Swagger Spec
|
|
7
|
+
*/
|
|
8
|
+
addRoute(controllerClass: any, functionName: string, method: string, fullPath: string): void;
|
|
9
|
+
loadDocMetadata(): void;
|
|
10
|
+
generateSpec(): {
|
|
11
|
+
openapi: string;
|
|
12
|
+
info: {
|
|
13
|
+
title: string;
|
|
14
|
+
version: string;
|
|
15
|
+
description: string;
|
|
16
|
+
};
|
|
17
|
+
paths: any;
|
|
18
|
+
components: {
|
|
19
|
+
schemas: {};
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import { HttpMethod } from '../types/route';
|
|
2
|
+
import "reflect-metadata";
|
|
2
3
|
export declare const INDEX_ROUTE = "index";
|
|
3
|
-
|
|
4
|
-
* 注册路由
|
|
5
|
-
* @param method HTTP 请求方法
|
|
6
|
-
* @param path 路由地址,可选,默认根据函数名推断
|
|
7
|
-
*/
|
|
4
|
+
export declare const ROUTE_METADATA_KEY: unique symbol;
|
|
8
5
|
export declare function Router(method?: HttpMethod, path?: string): (target: any, functionName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
9
|
-
/**
|
|
10
|
-
* 注册「需要鉴权」的路由
|
|
11
|
-
* @param method HTTP 请求方法
|
|
12
|
-
* @param path 路由地址,可选,默认根据函数名推断
|
|
13
|
-
*/
|
|
14
6
|
export declare function AuthRouter(method?: HttpMethod, path?: string): (target: any, functionName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
export declare const SWAGGER_TAGS_KEY: unique symbol;
|
|
3
|
+
export declare const SWAGGER_OPERATION_KEY: unique symbol;
|
|
4
|
+
export declare const SWAGGER_PARAMETERS_KEY: unique symbol;
|
|
5
|
+
export declare const SWAGGER_BODY_KEY: unique symbol;
|
|
6
|
+
export declare const SWAGGER_RESPONSES_KEY: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* 定义控制器标签
|
|
9
|
+
* @param tags 标签列表
|
|
10
|
+
*/
|
|
11
|
+
export declare function ApiTags(tags: string[]): (target: any) => void;
|
|
12
|
+
/**
|
|
13
|
+
* 定义接口摘要和描述
|
|
14
|
+
* @param summary 摘要
|
|
15
|
+
* @param description 详细描述
|
|
16
|
+
*/
|
|
17
|
+
export declare function ApiOperation(summary: string, description?: string): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
18
|
+
export interface ApiParamOptions {
|
|
19
|
+
name: string;
|
|
20
|
+
required?: boolean;
|
|
21
|
+
type?: string;
|
|
22
|
+
description?: string;
|
|
23
|
+
example?: any;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 定义 Query 参数
|
|
27
|
+
*/
|
|
28
|
+
export declare function ApiQuery(options: ApiParamOptions): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
29
|
+
/**
|
|
30
|
+
* 定义 Path 参数
|
|
31
|
+
*/
|
|
32
|
+
export declare function ApiParam(options: ApiParamOptions): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
33
|
+
export interface ApiBodyOptions {
|
|
34
|
+
description?: string;
|
|
35
|
+
required?: boolean;
|
|
36
|
+
schema?: any;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 定义 Request Body
|
|
40
|
+
*/
|
|
41
|
+
export declare function ApiBody(options: ApiBodyOptions): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
42
|
+
export interface ApiResponseOptions {
|
|
43
|
+
status: number;
|
|
44
|
+
description?: string;
|
|
45
|
+
type?: any;
|
|
46
|
+
schema?: any;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 定义响应
|
|
50
|
+
*/
|
|
51
|
+
export declare function ApiResponse(options: ApiResponseOptions): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { Context } from "koa";
|
|
3
|
+
export declare const VALIDATE_METADATA_KEY: unique symbol;
|
|
4
|
+
export type ValidatorFn = (ctx: Context) => void | Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* 参数校验装饰器
|
|
7
|
+
* @param validator 校验函数
|
|
8
|
+
*/
|
|
9
|
+
export declare function Validate(validator: ValidatorFn): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as BaseException } from "./base";
|
package/dist/global.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import "koa";
|
|
2
2
|
|
|
3
3
|
declare module "koa" {
|
|
4
|
+
interface Request {
|
|
5
|
+
body?: any;
|
|
6
|
+
}
|
|
7
|
+
|
|
4
8
|
interface DefaultContext {
|
|
5
9
|
// trackId
|
|
6
10
|
trackId?: string;
|
|
7
11
|
// log4js
|
|
8
12
|
log4?: import("log4js").Log4js;
|
|
9
13
|
// hook
|
|
10
|
-
hook?: (ctx:
|
|
14
|
+
hook?: (ctx: Context, type: "request" | "response" | "error") => void;
|
|
11
15
|
// runtimeLog
|
|
12
16
|
runtimeLog: boolean;
|
|
13
17
|
// 验证过的参数
|