Skip to content

Commit

Permalink
fix: router type (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr authored Jan 23, 2023
1 parent 641cd16 commit b32d9b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
9 changes: 4 additions & 5 deletions plugin/controller/lib/impl/http/HTTPControllerRegister.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from 'assert';
import KoaRouter from 'koa-router';
import { Application, Context } from 'egg';
import { Application, Router } from 'egg';
import {
CONTROLLER_META_DATA,
ControllerMetadata,
Expand All @@ -17,8 +16,8 @@ import { RootProtoManager } from '../../RootProtoManager';
export class HTTPControllerRegister implements ControllerRegister {
static instance?: HTTPControllerRegister;

private readonly router: KoaRouter<any, Context>;
private readonly checkRouters: Map<string, KoaRouter<any, Context>>;
private readonly router: Router;
private readonly checkRouters: Map<string, Router>;
private readonly eggContainerFactory: typeof EggContainerFactory;
private controllerProtos: EggPrototype[] = [];

Expand All @@ -31,7 +30,7 @@ export class HTTPControllerRegister implements ControllerRegister {
return HTTPControllerRegister.instance;
}

constructor(router: KoaRouter<any, Context>, eggContainerFactory: typeof EggContainerFactory) {
constructor(router: Router, eggContainerFactory: typeof EggContainerFactory) {
this.router = router;
this.checkRouters = new Map();
this.checkRouters.set('default', router);
Expand Down
15 changes: 7 additions & 8 deletions plugin/controller/lib/impl/http/HTTPMethodRegister.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from 'assert';
import KoaRouter from 'koa-router';
import { Context } from 'egg';
import { Context, Router } from 'egg';
import {
EggContext,
HTTPControllerMeta,
Expand All @@ -25,8 +24,8 @@ const noop = () => {
};

export class HTTPMethodRegister {
private readonly router: KoaRouter<any, Context>;
private readonly checkRouters: Map<string, KoaRouter<any, Context>>;
private readonly router: Router;
private readonly checkRouters: Map<string, Router>;
private readonly controllerMeta: HTTPControllerMeta;
private readonly methodMeta: HTTPMethodMeta;
private readonly proto: EggPrototype;
Expand All @@ -36,8 +35,8 @@ export class HTTPMethodRegister {
proto: EggPrototype,
controllerMeta: HTTPControllerMeta,
methodMeta: HTTPMethodMeta,
router: KoaRouter<any, Context>,
checkRouters: Map<string, KoaRouter<any, Context>>,
router: Router,
checkRouters: Map<string, Router>,
eggContainerFactory: typeof EggContainerFactory,
) {
this.proto = proto;
Expand Down Expand Up @@ -133,14 +132,14 @@ export class HTTPMethodRegister {
});
}

private registerToRouter(router: KoaRouter<any, Context>) {
private registerToRouter(router: Router) {
const routerFunc = router[this.methodMeta.method.toLowerCase()];
const methodRealPath = this.controllerMeta.getMethodRealPath(this.methodMeta);
const methodName = this.controllerMeta.getMethodName(this.methodMeta);
Reflect.apply(routerFunc, router, [ methodName, methodRealPath, noop ]);
}

private checkDuplicateInRouter(router: KoaRouter<any, Context>) {
private checkDuplicateInRouter(router: Router) {
const methodRealPath = this.controllerMeta.getMethodRealPath(this.methodMeta);
const matched = router.match(methodRealPath, this.methodMeta.method);
const methodName = this.controllerMeta.getMethodName(this.methodMeta);
Expand Down
2 changes: 0 additions & 2 deletions plugin/controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@eggjs/tegg-loader": "^3.2.4",
"@eggjs/tegg-metadata": "^3.2.4",
"@eggjs/tegg-runtime": "^3.2.4",
"@types/koa-router": "^7.0.40",
"egg-errors": "^2.3.0",
"globby": "^10.0.2",
"koa-compose": "^3.2.1",
Expand All @@ -71,7 +70,6 @@
"egg-mock": "^5.5.0",
"egg-tracer": "^2.0.0",
"globby": "^11.1.0",
"koa-router": "^8.0.8",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
Expand Down

0 comments on commit b32d9b8

Please sign in to comment.