Skip to content

Commit 0eba305

Browse files
committed
refactor: use hostList internal
1 parent 0378f16 commit 0eba305

File tree

7 files changed

+25
-33
lines changed

7 files changed

+25
-33
lines changed

core/controller-decorator/src/decorator/http/Host.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import { HostType } from '../../model';
66

77
export function Host(host: HostType) {
88
function classHost(constructor: EggProtoImplClass) {
9-
ControllerInfoUtil.addControllerHost(host, constructor);
9+
const hostList = Array.isArray(host) ? host : [ host ];
10+
ControllerInfoUtil.addControllerHost(hostList, constructor);
1011
}
1112

1213
function methodHost(target: any, propertyKey: PropertyKey) {
1314
assert(typeof propertyKey === 'string',
1415
`[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
1516
const controllerClazz = target.constructor as EggProtoImplClass;
1617
const methodName = propertyKey as string;
17-
18-
MethodInfoUtil.setMethodHost(host, controllerClazz, methodName);
18+
const hostList = Array.isArray(host) ? host : [ host ];
19+
MethodInfoUtil.setMethodHost(hostList, controllerClazz, methodName);
1920
}
2021

2122
return function(target: any, propertyKey?: PropertyKey) {

core/controller-decorator/src/model/HTTPControllerMeta.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22
import { ControllerMetadata } from './ControllerMetadata';
3-
import { ControllerType, HostType, MiddlewareFunc } from './types';
3+
import { ControllerType, MiddlewareFunc } from './types';
44
import { HTTPMethodMeta } from './HTTPMethodMeta';
55
import { EggPrototypeName } from '@eggjs/core-decorator';
66

@@ -14,7 +14,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
1414
public readonly methods: readonly HTTPMethodMeta[];
1515
public readonly needAcl: boolean;
1616
public readonly aclCode?: string;
17-
public readonly host?: HostType;
17+
public readonly host?: string[];
1818

1919
constructor(
2020
className: string,
@@ -25,7 +25,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
2525
methods: HTTPMethodMeta[],
2626
needAcl: boolean,
2727
aclCode: string | undefined,
28-
host: HostType | undefined,
28+
host: string[] | undefined,
2929
) {
3030
this.protoName = protoName;
3131
this.controllerName = controllerName;
@@ -45,7 +45,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
4545
return method.path;
4646
}
4747

48-
getMethodHost(method: HTTPMethodMeta): HostType | undefined {
48+
getMethodHost(method: HTTPMethodMeta): string[] | undefined {
4949
if (this.host) {
5050
return this.host;
5151
}

core/controller-decorator/src/model/HTTPMethodMeta.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'assert';
22
import pathToRegexp from 'path-to-regexp';
33
import { MethodMeta } from './MethodMeta';
4-
import { HostType, HTTPMethodEnum, HTTPParamType, MiddlewareFunc } from './types';
4+
import { HTTPMethodEnum, HTTPParamType, MiddlewareFunc } from './types';
55

66
export abstract class ParamMeta {
77
type: HTTPParamType;
@@ -73,7 +73,7 @@ export class HTTPMethodMeta implements MethodMeta {
7373
public readonly priority: number;
7474
public readonly needAcL: boolean;
7575
public readonly aclCode: string | undefined;
76-
public readonly host: HostType | undefined;
76+
public readonly host: string[] | undefined;
7777

7878
constructor(
7979
name: string,
@@ -85,7 +85,7 @@ export class HTTPMethodMeta implements MethodMeta {
8585
priority: number,
8686
needAcl: boolean,
8787
aclCode: string | undefined,
88-
host: HostType | undefined,
88+
host: string[] | undefined,
8989
) {
9090
this.name = name;
9191
this.path = path;

core/controller-decorator/src/util/ControllerInfoUtil.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ControllerTypeLike, HostType, MiddlewareFunc } from '../model';
1+
import { ControllerTypeLike, MiddlewareFunc } from '../model';
22
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
33

44
export const CONTROLLER_TYPE = Symbol.for('EggPrototype#controllerType');
@@ -45,11 +45,11 @@ export default class ControllerInfoUtil {
4545
return MetadataUtil.getMetaData(CONTROLLER_ACL, clazz);
4646
}
4747

48-
static addControllerHost(host: HostType, clazz: EggProtoImplClass) {
48+
static addControllerHost(host: string[], clazz: EggProtoImplClass) {
4949
MetadataUtil.defineMetaData(CONTROLLER_HOST, host, clazz);
5050
}
5151

52-
static getControllerHost(clazz: EggProtoImplClass): string | undefined {
52+
static getControllerHost(clazz: EggProtoImplClass): string[] | undefined {
5353
return MetadataUtil.getMetaData(CONTROLLER_HOST, clazz);
5454
}
5555
}

core/controller-decorator/src/util/MethodInfoUtil.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
2-
import { ControllerTypeLike, HostType, MiddlewareFunc } from '../model';
2+
import { ControllerTypeLike, MiddlewareFunc } from '../model';
33
import { MapUtil } from '@eggjs/tegg-common-util';
44

55
const METHOD_CONTROLLER_TYPE_MAP = Symbol.for('EggPrototype#controller#mthods');
@@ -8,7 +8,7 @@ const METHOD_CONTEXT_INDEX = Symbol.for('EggPrototype#controller#method#context'
88
const METHOD_MIDDLEWARES = Symbol.for('EggPrototype#method#middlewares');
99
const METHOD_ACL = Symbol.for('EggPrototype#method#acl');
1010

11-
type METHOD_MAP = Map<string, ControllerTypeLike | HostType>;
11+
type METHOD_MAP = Map<string, ControllerTypeLike | string[]>;
1212
type MethodContextIndexMap = Map<string, number>;
1313
type MethodMiddlewareMap = Map<string, MiddlewareFunc[]>;
1414
type MethodAclMap = Map<string, string | undefined>;
@@ -60,13 +60,13 @@ export default class MethodInfoUtil {
6060
return methodAclMap?.get(methodName);
6161
}
6262

63-
static setMethodHost(host: HostType, clazz: EggProtoImplClass, methodName: string) {
63+
static setMethodHost(host: string[], clazz: EggProtoImplClass, methodName: string) {
6464
const methodControllerMap: METHOD_MAP = MetadataUtil.initOwnMapMetaData(METHOD_CONTROLLER_HOST, clazz, new Map());
6565
methodControllerMap.set(methodName, host);
6666
}
6767

68-
static getMethodHost(clazz: EggProtoImplClass, methodName: string): HostType | undefined {
68+
static getMethodHost(clazz: EggProtoImplClass, methodName: string): string[] | undefined {
6969
const methodControllerMap: METHOD_MAP | undefined = MetadataUtil.getMetaData(METHOD_CONTROLLER_HOST, clazz);
70-
return methodControllerMap?.get(methodName) as HostType | undefined;
70+
return methodControllerMap?.get(methodName) as string[] | undefined;
7171
}
7272
}

core/controller-decorator/test/http/Host.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import MethodInfoUtil from '../../src/util/MethodInfoUtil';
66
describe('test/Host.test.ts', () => {
77
it('controller Host work', () => {
88
const controllerHost = ControllerInfoUtil.getControllerHost(HostController);
9-
assert(controllerHost === 'foo.eggjs.com');
9+
assert(controllerHost![0] === 'foo.eggjs.com');
1010
});
1111

1212
it('method Host work', () => {
1313
const methodHost = MethodInfoUtil.getMethodHost(HostController, 'bar');
14-
assert(methodHost === 'bar.eggjs.com');
14+
assert(methodHost![0] === 'bar.eggjs.com');
1515
});
1616
});

plugin/controller/lib/impl/http/HTTPMethodRegister.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import KoaRouter from 'koa-router';
33
import { Context } from 'egg';
44
import {
55
EggContext,
6-
HostType,
76
HTTPControllerMeta,
87
HTTPMethodMeta,
98
HTTPParamType,
@@ -50,22 +49,15 @@ export class HTTPMethodRegister {
5049
this.eggContainerFactory = eggContainerFactory;
5150
}
5251

53-
private hostMatch(host: HostType | undefined, target: string) {
54-
if (Array.isArray(host)) {
55-
return host.includes(target);
56-
}
57-
return host === target;
58-
}
59-
60-
private createHandler(methodMeta: HTTPMethodMeta, host: HostType | undefined) {
52+
private createHandler(methodMeta: HTTPMethodMeta, host: string[] | undefined) {
6153
const argsLength = methodMeta.paramMap.size;
6254
const hasContext = methodMeta.contextParamIndex !== undefined;
6355
const contextIndex = methodMeta.contextParamIndex;
6456
const methodArgsLength = argsLength + (hasContext ? 1 : 0);
6557
const self = this;
6658
return async function(ctx: Context, next: Next) {
6759
// if hosts is not empty and host is not matched, not execute
68-
if (host && !self.hostMatch(host, ctx.host)) {
60+
if (host && !host.includes(ctx.host)) {
6961
return await next();
7062
}
7163
// HTTP decorator core implement
@@ -170,9 +162,8 @@ export class HTTPMethodRegister {
170162
if (aclMiddleware) {
171163
methodMiddlewares.push(aclMiddleware);
172164
}
173-
const host = this.controllerMeta.getMethodHost(this.methodMeta);
174-
const hostList = Array.isArray(host) ? host : [ host ];
175-
hostList.forEach(h => {
165+
const host = this.controllerMeta.getMethodHost(this.methodMeta) || [ undefined ];
166+
host.forEach(h => {
176167
const handler = this.createHandler(this.methodMeta, h);
177168
Reflect.apply(routerFunc, this.router,
178169
[ methodName, methodRealPath, ...methodMiddlewares, handler ]);

0 commit comments

Comments
 (0)