Skip to content

Commit ba57fa9

Browse files
committed
feat: @controller support sub route.
1 parent 7cd0e1f commit ba57fa9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

lib/controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ function initController(target: any, config?: EggAppConfig) {
143143
route.method = [...new Set([].concat(methodAndPath[0] || []).concat(route.method || []))];
144144
route.url = methodAndPath[1];
145145
}
146+
if (!route.url.startsWith('/')) {
147+
route.url = `${prefix}/${route.url}`;
148+
}
146149
}
147150
if (!route.method) {
148151
route.method = parsedPath.method;

test/ctrl.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ describe('ctrl', () => {
2222
.expect(200);
2323
});
2424

25+
it('subRoute', () => {
26+
return request(app.callback())
27+
.put('/api/ctrl/ss/a?type=sub')
28+
.expect('sub')
29+
.expect(200);
30+
});
31+
2532
it('middleware', () => {
2633
return request(app.callback())
2734
.get('/api/ctrl/hi?type=mw')
2835
.expect('ctrl:middleware')
2936
.expect(200);
3037
});
31-
3238
});

test/fixtures/example/app/controller/ctrl.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ export class CtrlController extends Controller {
1818
hi(type: string) {
1919
return type;
2020
}
21+
22+
@route('PUT ss/a')
23+
subRoute(type: string) {
24+
return type;
25+
}
2126
}

test/fixtures/openapi/example.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66
"contact": {}
77
},
88
"paths": {
9+
"/api/ctrl/ss/a": {
10+
"put": {
11+
"operationId": "subRoute",
12+
"tags": ["CtrlController"],
13+
"parameters": [
14+
{
15+
"name": "type",
16+
"in": "query",
17+
"schema": {
18+
"type": "string"
19+
}
20+
}
21+
],
22+
"responses": {
23+
"default": {
24+
"description": "default",
25+
"content": {
26+
"application/json": {
27+
"schema": {}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
},
934
"/api/ctrl/hi": {
1035
"get": {
1136
"operationId": "hi",

0 commit comments

Comments
 (0)