Skip to content

Commit

Permalink
feat(types): export and use type MiddlewareParamArray
Browse files Browse the repository at this point in the history
- update types of parameters of MidwayWebLoader.handlerWebMiddleware()
- fix `no-shadow` error relative to variable `middlewares`
  • Loading branch information
waitingsong committed Jul 8, 2019
1 parent 5911955 commit 8fbf356
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
8 changes: 5 additions & 3 deletions packages/midway-decorator/src/web/requestMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { attachClassMetadata } from 'injection';
import { WEB_ROUTER_KEY } from '../constant';
import { KoaMiddleware } from '../interface';

export type MiddlewareParamArray = Array<string | KoaMiddleware>

export interface RouterOption {
path?: string;
requestMethod: string;
routerName?: string;
method: string;
middleware?: Array<string | KoaMiddleware>;
middleware?: MiddlewareParamArray;
}

export const RequestMethod = {
Expand Down Expand Up @@ -40,7 +42,7 @@ export interface RequestMappingMetadata {
[PATH_METADATA]?: string;
[METHOD_METADATA]: string;
[ROUTER_NAME_METADATA]?: string;
[ROUTER_MIDDLEWARE]?: Array<string | KoaMiddleware>;
[ROUTER_MIDDLEWARE]?: MiddlewareParamArray;
}

export const RequestMapping = (
Expand Down Expand Up @@ -68,7 +70,7 @@ const createMappingDecorator = (method: string) => (
path?: string,
routerOptions: {
routerName?: string;
middleware?: Array<string | KoaMiddleware>;
middleware?: MiddlewareParamArray;
} = {middleware: []}
): MethodDecorator => {
return RequestMapping({
Expand Down
3 changes: 2 additions & 1 deletion packages/midway-web/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {EggLoaderOptions} from 'egg-core';
import {IApplicationContext} from 'injection';
import {KoaMiddleware} from '@midwayjs/decorator';

export interface WebMiddleware {
resolve(): (context: any, next: () => Promise<any>) => any;
resolve(): KoaMiddleware;
}

export interface MidwayLoaderOptions extends EggLoaderOptions {
Expand Down
17 changes: 9 additions & 8 deletions packages/midway-web/src/loader/webLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CONTROLLER_KEY,
ControllerOption,
KoaMiddleware,
MiddlewareParamArray,
PRIORITY_KEY,
RouterOption,
WEB_ROUTER_KEY,
Expand Down Expand Up @@ -194,7 +195,7 @@ export class MidwayWebLoader extends EggLoader {

if (newRouter) {
// implement middleware in controller
const middlewares = controllerOption.routerOptions.middleware;
const middlewares: MiddlewareParamArray | void = controllerOption.routerOptions.middleware;
await this.handlerWebMiddleware(middlewares, (middlewareImpl: KoaMiddleware) => {
newRouter.use(middlewareImpl);
});
Expand All @@ -205,10 +206,10 @@ export class MidwayWebLoader extends EggLoader {
if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
for (const webRouter of webRouterInfo) {
// get middleware
const middlewares = webRouter.middleware;
const middlewares2: MiddlewareParamArray | void = webRouter.middleware;
const methodMiddlwares = [];

await this.handlerWebMiddleware(middlewares, (middlewareImpl: KoaMiddleware) => {
await this.handlerWebMiddleware(middlewares2, (middlewareImpl: KoaMiddleware) => {
methodMiddlwares.push(middlewareImpl);
});

Expand Down Expand Up @@ -241,9 +242,9 @@ export class MidwayWebLoader extends EggLoader {
}


private async handlerWebMiddleware<T extends any = any>(
middlewares: T[],
handlerCallback: (ps: T | ReturnType<WebMiddleware['resolve']>) => any,
private async handlerWebMiddleware(
middlewares: MiddlewareParamArray | void,
handlerCallback: (middlewareImpl: KoaMiddleware) => void,
): Promise<void> {

if (middlewares && middlewares.length) {
Expand All @@ -252,8 +253,8 @@ export class MidwayWebLoader extends EggLoader {
// web function middleware
handlerCallback(middleware);
} else {
const middlewareImpl: WebMiddleware = await this.applicationContext.getAsync(middleware);
if (middlewareImpl && middlewareImpl.resolve) {
const middlewareImpl: WebMiddleware | void = await this.applicationContext.getAsync(middleware);
if (middlewareImpl && typeof middlewareImpl.resolve === 'function') {
handlerCallback(middlewareImpl.resolve());
}
}
Expand Down

0 comments on commit 8fbf356

Please sign in to comment.