Skip to content

Commit fac3903

Browse files
committed
feat(0.0.5): 拦截器和请求处理器可配置多个method,发布0.0.5
1 parent a32657d commit fac3903

File tree

7 files changed

+74
-59
lines changed

7 files changed

+74
-59
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
> 次版本:每季度发布一次,向下兼容的功能性新增
44
> 修订版本:每周发布一次(紧急版本随时发布),向下兼容的问题修正
55
6-
## 0.0.4 [Current]
6+
## 0.0.5 [Current]
7+
###### 发布日期:2023-06-27
8+
###### 兼容性:0.x.x
9+
10+
+ 拦截器和请求处理器可配置多个method
11+
12+
13+
## 0.0.4
714
###### 发布日期:2023-05-23
815
###### 兼容性:0.x.x
916

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "brisk-controller",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "fast light brisk controller in nodejs(koa)",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
@@ -35,9 +35,9 @@
3535
},
3636
"homepage": "https://github.com/ruixiaozi/brisk-controller#readme",
3737
"dependencies": {
38-
"brisk-ioc": "0.0.4",
39-
"brisk-log": "0.0.4",
40-
"brisk-ts-extends": "0.0.4",
38+
"brisk-ioc": "0.0.5",
39+
"brisk-log": "0.0.5",
40+
"brisk-ts-extends": "0.0.5",
4141
"co-body": "^6.1.0",
4242
"koa": "2.14.1",
4343
"koa-cors": "0.0.16",

src/core/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export async function forward<T>(targetPath: string, method = BRISK_CONTROLLER_M
8080
}
8181

8282
/**
83-
* 返回结果工程
83+
* 返回结果工厂
8484
* @param result 返回对象
8585
* @returns 返回一个工厂对象
8686
*/

src/core/router.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,23 @@ export function addRoute(path: string, handler: BriskControllerRouterHandler, op
6565
routers.set(realPath, methodMap);
6666
}
6767

68-
let typeMap = methodMap.get(option?.method || BRISK_CONTROLLER_METHOD_E.GET);
69-
if (!typeMap) {
70-
typeMap = new Map();
71-
methodMap.set(option?.method || BRISK_CONTROLLER_METHOD_E.GET, typeMap);
72-
}
68+
const methods = Array.isArray(option?.method) ? option!.method : [option?.method || BRISK_CONTROLLER_METHOD_E.GET];
7369

74-
let handlers = typeMap.get(option?.type || BRISK_CONTROLLER_ROUTER_TYPE_E.REQUEST);
75-
if (!handlers) {
76-
handlers = [];
77-
typeMap.set(option?.type || BRISK_CONTROLLER_ROUTER_TYPE_E.REQUEST, handlers);
78-
}
70+
methods.forEach((method) => {
71+
let typeMap = methodMap!.get(method);
72+
if (!typeMap) {
73+
typeMap = new Map();
74+
methodMap!.set(method, typeMap);
75+
}
76+
77+
let handlers = typeMap.get(option?.type || BRISK_CONTROLLER_ROUTER_TYPE_E.REQUEST);
78+
if (!handlers) {
79+
handlers = [];
80+
typeMap.set(option?.type || BRISK_CONTROLLER_ROUTER_TYPE_E.REQUEST, handlers);
81+
}
7982

80-
handlers.push(handler);
83+
handlers.push(handler);
84+
});
8185
}
8286

8387
const jsonTypes = [

src/core/swagger.ts

+42-38
Original file line numberDiff line numberDiff line change
@@ -217,49 +217,53 @@ export function addSwaggerRoute(routePath: string, option?: BriskControllerReque
217217
if (!swaggerConfig.paths[transRoutePath]) {
218218
swaggerConfig.paths[transRoutePath] = {};
219219
}
220-
swaggerConfig.paths[transRoutePath][option?.method || BRISK_CONTROLLER_METHOD_E.GET] = {
221-
operationId: option?.name,
222-
tags: [option?.tag?.name || defaultTag.name],
223-
summary: option?.title,
224-
description: option?.description,
225-
parameters: option?.params
226-
?.filter((item) => [
227-
BRISK_CONTROLLER_PARAMETER_IS_E.PATH,
228-
BRISK_CONTROLLER_PARAMETER_IS_E.QUERY,
229-
BRISK_CONTROLLER_PARAMETER_IS_E.HEADER,
230-
BRISK_CONTROLLER_PARAMETER_IS_E.COOKIE,
231-
].includes(item.is))
232-
.map((item) => ({
233-
in: item.is,
234-
name: item.name,
235-
required: Boolean(item.required),
236-
schema: item.type ? transforSwaggerSchema(item.type) : undefined,
237-
description: item.is === BRISK_CONTROLLER_PARAMETER_IS_E.COOKIE ? `${item?.description || ''}\n<b>本页面无法发送cookie</b>` : item?.description,
238-
})),
239-
requestBody: transforSwaggerReqBody(option?.params),
240-
responses: option?.redirect
241-
? {
242-
[`${option.redirect.status || 301}`]: {
243-
description: 'redirect',
244-
headers: {
245-
Location: {
246-
description: `${JSON.stringify(option.redirect.urls)}`,
247-
schema: { type: BRISK_CONTROLLER_PARAMTYPE_E.String },
220+
221+
const methods = Array.isArray(option?.method) ? option!.method : [option?.method || BRISK_CONTROLLER_METHOD_E.GET];
222+
methods.forEach((method) => {
223+
swaggerConfig.paths[transRoutePath][method] = {
224+
operationId: option?.name,
225+
tags: [option?.tag?.name || defaultTag.name],
226+
summary: option?.title,
227+
description: option?.description,
228+
parameters: option?.params
229+
?.filter((item) => [
230+
BRISK_CONTROLLER_PARAMETER_IS_E.PATH,
231+
BRISK_CONTROLLER_PARAMETER_IS_E.QUERY,
232+
BRISK_CONTROLLER_PARAMETER_IS_E.HEADER,
233+
BRISK_CONTROLLER_PARAMETER_IS_E.COOKIE,
234+
].includes(item.is))
235+
.map((item) => ({
236+
in: item.is,
237+
name: item.name,
238+
required: Boolean(item.required),
239+
schema: item.type ? transforSwaggerSchema(item.type) : undefined,
240+
description: item.is === BRISK_CONTROLLER_PARAMETER_IS_E.COOKIE ? `${item?.description || ''}\n<b>本页面无法发送cookie</b>` : item?.description,
241+
})),
242+
requestBody: transforSwaggerReqBody(option?.params),
243+
responses: option?.redirect
244+
? {
245+
[`${option.redirect.status || 301}`]: {
246+
description: 'redirect',
247+
headers: {
248+
Location: {
249+
description: `${JSON.stringify(option.redirect.urls)}`,
250+
schema: { type: BRISK_CONTROLLER_PARAMTYPE_E.String },
251+
},
248252
},
249253
},
250-
},
251-
}
252-
: {
253-
'200': {
254-
description: 'OK',
255-
content: {
256-
'application/json': {
257-
schema: option?.successResponseType ? transforSwaggerSchema(option.successResponseType) : {},
254+
}
255+
: {
256+
'200': {
257+
description: 'OK',
258+
content: {
259+
'application/json': {
260+
schema: option?.successResponseType ? transforSwaggerSchema(option.successResponseType) : {},
261+
},
258262
},
259263
},
260264
},
261-
},
262-
};
265+
};
266+
});
263267
}
264268

265269
const packageInfo = require(path.join(process.cwd(), 'package.json'));

src/types/base.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface BriskControllerRouterOption {
2424
// 默认是request
2525
type?: BRISK_CONTROLLER_ROUTER_TYPE_E;
2626
// 默认是get
27-
method?: BRISK_CONTROLLER_METHOD_E;
27+
method?: BRISK_CONTROLLER_METHOD_E | BRISK_CONTROLLER_METHOD_E[];
2828
}
2929

3030
export interface BriskControllerHeaders {
@@ -88,7 +88,7 @@ export interface BriskControllerParameter {
8888

8989
export interface BriskControllerInterceptorOption {
9090
// 默认为GET
91-
method?: BRISK_CONTROLLER_METHOD_E;
91+
method?: BRISK_CONTROLLER_METHOD_E | BRISK_CONTROLLER_METHOD_E[];
9292
// 基础地址,默认相对于globalBaseUrl的根路径
9393
baseUrl?: string;
9494
}

src/types/decorator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export interface BriskControllerDecoratorRequest {
1414
// 描述,用于swagger显示
1515
description?: string;
1616
// 默认为get
17-
method?: BRISK_CONTROLLER_METHOD_E;
17+
method?: BRISK_CONTROLLER_METHOD_E | BRISK_CONTROLLER_METHOD_E[];
1818
// 跳转信息,仅当返回redrect方法时
1919
redirect?: BriskControllerRedirectInfo;
2020
}
2121

2222
export interface BriskControllerDecoratorInterceptor {
2323
// 默认为get
24-
method?: BRISK_CONTROLLER_METHOD_E;
24+
method?: BRISK_CONTROLLER_METHOD_E | BRISK_CONTROLLER_METHOD_E[];
2525
}
2626

2727
export interface BriskControllerDecoratorParam {

0 commit comments

Comments
 (0)