From bfcfc9a377f01026a459aaed35a3f0fdf0530f26 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 15 Sep 2020 23:43:10 +0800 Subject: [PATCH] feat: add generateMiddleware for express and faas --- packages/decorator/src/annotation/check.ts | 7 ++-- packages/decorator/src/common/constant.ts | 3 ++ packages/faas/src/framework.ts | 42 +++++++++++++--------- packages/faas/src/interface.ts | 21 ++++++++--- packages/web-express/src/framework.ts | 15 +++++--- 5 files changed, 59 insertions(+), 29 deletions(-) diff --git a/packages/decorator/src/annotation/check.ts b/packages/decorator/src/annotation/check.ts index e2ddd67ef81f..12a4bfe4cc7f 100644 --- a/packages/decorator/src/annotation/check.ts +++ b/packages/decorator/src/annotation/check.ts @@ -1,10 +1,10 @@ -import 'reflect-metadata'; import * as joi from 'joi'; + export function Check(failValue?: any) { return function (target, propertyKey: string | symbol, descriptor: PropertyDescriptor) { const origin = descriptor.value; descriptor.value = function (...args: any[]) { - const paramTypes = Reflect.getMetadata('design:paramtypes', target, propertyKey); + const paramTypes = Reflect.getMetadata('design:paramTypes', target, propertyKey); for (let i = 0; i < paramTypes.length; i++) { const item = paramTypes[i]; const rules = Reflect.getMetadata('rules', item.prototype); @@ -15,8 +15,7 @@ export function Check(failValue?: any) { } } } - const result = origin.call(this, ...arguments); - return result; + return origin.call(this, ...arguments); }; }; } diff --git a/packages/decorator/src/common/constant.ts b/packages/decorator/src/common/constant.ts index a31ee4f2b8e9..095c96b1522f 100644 --- a/packages/decorator/src/common/constant.ts +++ b/packages/decorator/src/common/constant.ts @@ -1,3 +1,6 @@ +// got all value with no property name +export const ALL_VALUE= 'common:all_value_key'; + // common export const PRIORITY_KEY = 'common:priority'; export const SCHEDULE_KEY = 'common:schedule'; diff --git a/packages/faas/src/framework.ts b/packages/faas/src/framework.ts index ee3e2e0a3a6b..7c5616711ae5 100644 --- a/packages/faas/src/framework.ts +++ b/packages/faas/src/framework.ts @@ -1,32 +1,32 @@ import { - IFaaSConfigurationOptions, - IFaaSApplication, FaaSContext, + FaaSMiddleware, + IFaaSConfigurationOptions, + IMidwayFaaSApplication, + WebMiddleware } from './interface'; import { - IMidwayApplication, - IMidwayBootstrapOptions, + BaseFramework, getClassMetadata, IMiddleware, + IMidwayApplication, + IMidwayBootstrapOptions, listModule, listPreloadModule, + MidwayFrameworkType, MidwayProcessTypeEnum, MidwayRequestContainer, REQUEST_OBJ_CTX_KEY, - BaseFramework, MidwayFrameworkType, } from '@midwayjs/core'; import { dirname, resolve } from 'path'; -import { - FUNC_KEY, - LOGGER_KEY, - PLUGIN_KEY, -} from '@midwayjs/decorator'; +import { FUNC_KEY, LOGGER_KEY, PLUGIN_KEY, } from '@midwayjs/decorator'; import SimpleLock from '@midwayjs/simple-lock'; import * as compose from 'koa-compose'; import { MidwayHooks } from './hooks'; const LOCK_KEY = '_faas_starter_start_key'; + // const MIDWAY_FAAS_KEY = '__midway_faas__'; export class MidwayFaaSFramework extends BaseFramework { @@ -35,7 +35,7 @@ export class MidwayFaaSFramework extends BaseFramework = new Map(); protected logger; private lock = new SimpleLock(); - private webApplication: IFaaSApplication; + private webApplication: IMidwayFaaSApplication; protected async beforeDirectoryLoad(options: Partial) { this.logger = options.logger || console; @@ -80,9 +80,9 @@ export class MidwayFaaSFramework extends BaseFramework {} + public async stop(): Promise { + } public getApplication(): IMidwayApplication { return this.webApplication; @@ -155,6 +156,11 @@ export class MidwayFaaSFramework extends BaseFramework { + const mwIns = await this.getApplicationContext().getAsync(middlewareId); + return mwIns.resolve(); + } + protected getContext(context) { if (!context.env) { context.env = this.getApplicationContext() @@ -205,7 +211,7 @@ export class MidwayFaaSFramework extends BaseFramework { return this.baseDir; @@ -292,6 +298,10 @@ export class MidwayFaaSFramework extends BaseFramework { + return this.generateMiddleware(middlewareId); + } }); } diff --git a/packages/faas/src/interface.ts b/packages/faas/src/interface.ts index 2629bca725c7..fd1630fb4077 100644 --- a/packages/faas/src/interface.ts +++ b/packages/faas/src/interface.ts @@ -2,14 +2,21 @@ import { MidwayRequestContainer, IMidwayApplication, IMidwayLogger } from '@midw import { FaaSHTTPContext } from '@midwayjs/faas-typings'; import type { MidwayHooks } from './hooks'; -export interface IFaaSApplication extends IMidwayApplication { +export type FaaSMiddleware = (() => (context: FaaSContext, next: () => Promise) => any) | string; + +export interface IMidwayFaaSApplication extends IMidwayApplication { getInitializeContext(); - use( - middleware: (() => (context: any, next: () => Promise) => any) | string - ); + use(middleware: FaaSMiddleware); useMiddleware(mw: string[]); + generateMiddleware(middlewareId: string): Promise; } +/** + * @deprecated + * use IMidwayFaaSApplication instead + */ +export type IFaaSApplication = IMidwayFaaSApplication; + /** * @deprecated */ @@ -30,6 +37,10 @@ export interface IFaaSConfigurationOptions { middleware?: string[]; initializeContext?: object; applicationAdapter?: { - getApplication(): IFaaSApplication; + getApplication(): IMidwayFaaSApplication; }; } + +export interface WebMiddleware { + resolve(): FaaSMiddleware; +} diff --git a/packages/web-express/src/framework.ts b/packages/web-express/src/framework.ts index 5280c5882668..531ad0955e3d 100644 --- a/packages/web-express/src/framework.ts +++ b/packages/web-express/src/framework.ts @@ -169,6 +169,11 @@ export class MidwayExpressFramework extends BaseFramework(middlewareId); + return mwIns.resolve(); + } + protected async preRegisterRouter( target: any, controllerId: string @@ -312,10 +317,12 @@ export class MidwayExpressFramework extends BaseFramework { - return this.generateController(controllerMapping, routeArgsInfo, routerResponseData); + generateController: (controllerMapping: string) => { + return this.generateController(controllerMapping); + }, + + generateMiddleware: async (middlewareId: string) => { + return this.generateMiddleware(middlewareId); } }); }