From d30e72fe524045b6085b2c6955a1dd3341e92549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=5BDesktop=5D?= Date: Mon, 4 Sep 2017 15:28:44 +0200 Subject: [PATCH 1/3] Remove Driver interface abstraction --- src/driver/BaseDriver.ts | 45 ++++++++++- src/driver/Driver.ts | 114 ---------------------------- src/driver/express/ExpressDriver.ts | 3 +- src/driver/koa/KoaDriver.ts | 3 +- src/index.ts | 2 - 5 files changed, 45 insertions(+), 122 deletions(-) delete mode 100644 src/driver/Driver.ts diff --git a/src/driver/BaseDriver.ts b/src/driver/BaseDriver.ts index e1523834..46e0601a 100644 --- a/src/driver/BaseDriver.ts +++ b/src/driver/BaseDriver.ts @@ -1,13 +1,19 @@ import {ValidatorOptions} from "class-validator"; -import {HttpError} from "../http-error/HttpError"; import {ClassTransformOptions} from "class-transformer"; + +import {HttpError} from "../http-error/HttpError"; import {CurrentUserChecker} from "../CurrentUserChecker"; import {AuthorizationChecker} from "../AuthorizationChecker"; +import {ActionMetadata} from "../metadata/ActionMetadata"; +import {ParamMetadata} from "../metadata/ParamMetadata"; +import {MiddlewareMetadata} from "../metadata/MiddlewareMetadata"; +import {Action} from "../Action"; /** * Base driver functionality for all other drivers. + * Abstract layer to organize controllers integration with different http server implementations. */ -export class BaseDriver { +export abstract class BaseDriver { // ------------------------------------------------------------------------- // Public Properties @@ -77,6 +83,41 @@ export class BaseDriver { */ currentUserChecker?: CurrentUserChecker; + /** + * Initializes the things driver needs before routes and middleware registration. + */ + abstract initialize(): void; + + /** + * Registers given middleware. + */ + abstract registerMiddleware(middleware: MiddlewareMetadata): void; + + /** + * Registers action in the driver. + */ + abstract registerAction(action: ActionMetadata, executeCallback: (options: Action) => any): void; + + /** + * Registers all routes in the framework. + */ + abstract registerRoutes(): void; + + /** + * Gets param from the request. + */ + abstract getParamFromRequest(actionOptions: Action, param: ParamMetadata): any; + + /** + * Defines an algorithm of how to handle error during executing controller action. + */ + abstract handleError(error: any, action: ActionMetadata, options: Action): any; + + /** + * Defines an algorithm of how to handle success result of executing controller action. + */ + abstract handleSuccess(result: any, action: ActionMetadata, options: Action): void; + // ------------------------------------------------------------------------- // Protected Methods // ------------------------------------------------------------------------- diff --git a/src/driver/Driver.ts b/src/driver/Driver.ts deleted file mode 100644 index eefa7c3a..00000000 --- a/src/driver/Driver.ts +++ /dev/null @@ -1,114 +0,0 @@ -import {ValidatorOptions} from "class-validator"; -import {ActionMetadata} from "../metadata/ActionMetadata"; -import {ParamMetadata} from "../metadata/ParamMetadata"; -import {MiddlewareMetadata} from "../metadata/MiddlewareMetadata"; -import {Action} from "../Action"; -import {ClassTransformOptions} from "class-transformer"; -import {AuthorizationChecker} from "../AuthorizationChecker"; -import {CurrentUserChecker} from "../CurrentUserChecker"; - -/** - * Abstract layer to organize controllers integration with different http server implementations. - */ -export interface Driver { - - /** - * Indicates if constructor-utils should be used to perform serialization / deserialization. - */ - useClassTransformer: boolean; - - /** - * Indicates if class-validator should be used to auto validate objects injected into params. - */ - enableValidation: boolean; - - /** - * Global class-validator options passed during validate operation. - */ - validationOptions: ValidatorOptions; - - /** - * Global class transformer options passed to class-transformer during classToPlain operation. - * This operation is being executed when server returns response to user. - */ - classToPlainTransformOptions: ClassTransformOptions; - - /** - * Global class transformer options passed to class-transformer during plainToClass operation. - * This operation is being executed when parsing user parameters. - */ - plainToClassTransformOptions: ClassTransformOptions; - - /** - * Indicates if default routing-controller's error handling is enabled or not. - */ - isDefaultErrorHandlingEnabled: boolean; - - /** - * Indicates if debug mode is enabled or not. In debug mode additional information may be exposed. - */ - developmentMode: boolean; - - /** - * Map of error overrides. - */ - errorOverridingMap: { [key: string]: any }; - - /** - * Route prefix. eg '/api' - */ - routePrefix: string; - - /** - * Indicates if cors are enabled. - * This requires installation of additional module (cors for express and kcors for koa). - */ - cors?: boolean|Object; - - /** - * Special function used to check user authorization roles per request. - * Must return true or promise with boolean true resolved for authorization to succeed. - */ - authorizationChecker?: AuthorizationChecker; - - /** - * Special function used to get currently authorized user. - */ - currentUserChecker?: CurrentUserChecker; - - /** - * Initializes the things driver needs before routes and middleware registration. - */ - initialize(): void; - - /** - * Registers given middleware. - */ - registerMiddleware(middleware: MiddlewareMetadata): void; - - /** - * Registers action in the driver. - */ - registerAction(action: ActionMetadata, executeCallback: (options: Action) => any): void; - - /** - * Registers all routes in the framework. - */ - registerRoutes(): void; - - /** - * Gets param from the request. - */ - getParamFromRequest(actionOptions: Action, param: ParamMetadata): any; - - /** - * Defines an algorithm of how to handle error during executing controller action. - */ - handleError(error: any, action: ActionMetadata, options: Action): any; - - /** - * Defines an algorithm of how to handle success result of executing controller action. - */ - handleSuccess(result: any, action: ActionMetadata, options: Action): void; - -} \ No newline at end of file diff --git a/src/driver/express/ExpressDriver.ts b/src/driver/express/ExpressDriver.ts index 8ca65fe2..3599da67 100644 --- a/src/driver/express/ExpressDriver.ts +++ b/src/driver/express/ExpressDriver.ts @@ -3,7 +3,6 @@ import {MiddlewareMetadata} from "../../metadata/MiddlewareMetadata"; import {ActionMetadata} from "../../metadata/ActionMetadata"; import {Action} from "../../Action"; import {classToPlain} from "class-transformer"; -import {Driver} from "../Driver"; import {ParamMetadata} from "../../metadata/ParamMetadata"; import {BaseDriver} from "../BaseDriver"; import {ExpressMiddlewareInterface} from "./ExpressMiddlewareInterface"; @@ -20,7 +19,7 @@ const templateUrl = require("template-url"); /** * Integration with express framework. */ -export class ExpressDriver extends BaseDriver implements Driver { +export class ExpressDriver extends BaseDriver { // ------------------------------------------------------------------------- // Constructor diff --git a/src/driver/koa/KoaDriver.ts b/src/driver/koa/KoaDriver.ts index 40b48cd6..549db227 100644 --- a/src/driver/koa/KoaDriver.ts +++ b/src/driver/koa/KoaDriver.ts @@ -1,7 +1,6 @@ import {Action} from "../../Action"; import {ActionMetadata} from "../../metadata/ActionMetadata"; import {BaseDriver} from "../BaseDriver"; -import {Driver} from "../Driver"; import {MiddlewareMetadata} from "../../metadata/MiddlewareMetadata"; import {ParamMetadata} from "../../metadata/ParamMetadata"; import {UseMetadata} from "../../metadata/UseMetadata"; @@ -20,7 +19,7 @@ const templateUrl = require("template-url"); /** * Integration with koa framework. */ -export class KoaDriver extends BaseDriver implements Driver { +export class KoaDriver extends BaseDriver { // ------------------------------------------------------------------------- // Constructor diff --git a/src/index.ts b/src/index.ts index bbd7cc3f..d71798c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ import {CustomParameterDecorator} from "./CustomParameterDecorator"; -import {Driver} from "./driver/Driver"; import {ExpressDriver} from "./driver/express/ExpressDriver"; import {KoaDriver} from "./driver/koa/KoaDriver"; import {MetadataArgsStorage} from "./metadata-builder/MetadataArgsStorage"; @@ -88,7 +87,6 @@ export * from "./RoleChecker"; export * from "./Action"; export * from "./InterceptorInterface"; -export * from "./driver/Driver"; export * from "./driver/BaseDriver"; export * from "./driver/express/ExpressDriver"; export * from "./driver/koa/KoaDriver"; From 834cdaa894110f964b1e8b23ba6277398fe83fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=5BDesktop=5D?= Date: Mon, 4 Sep 2017 15:30:46 +0200 Subject: [PATCH 2/3] Create generic createServer function --- src/driver/BaseDriver.ts | 5 +++++ src/driver/express/ExpressDriver.ts | 1 + src/driver/koa/KoaDriver.ts | 1 + src/index.ts | 25 ++++++++++++++++--------- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/driver/BaseDriver.ts b/src/driver/BaseDriver.ts index 46e0601a..4282574d 100644 --- a/src/driver/BaseDriver.ts +++ b/src/driver/BaseDriver.ts @@ -19,6 +19,11 @@ export abstract class BaseDriver { // Public Properties // ------------------------------------------------------------------------- + /** + * Reference to the underlying framework app object. + */ + app: any; + /** * Indicates if class-transformer should be used or not. */ diff --git a/src/driver/express/ExpressDriver.ts b/src/driver/express/ExpressDriver.ts index 3599da67..16c50fb0 100644 --- a/src/driver/express/ExpressDriver.ts +++ b/src/driver/express/ExpressDriver.ts @@ -28,6 +28,7 @@ export class ExpressDriver extends BaseDriver { constructor(public express?: any) { super(); this.loadExpress(); + this.app = this.express; } // ------------------------------------------------------------------------- diff --git a/src/driver/koa/KoaDriver.ts b/src/driver/koa/KoaDriver.ts index 549db227..58d66350 100644 --- a/src/driver/koa/KoaDriver.ts +++ b/src/driver/koa/KoaDriver.ts @@ -29,6 +29,7 @@ export class KoaDriver extends BaseDriver { super(); this.loadKoa(); this.loadRouter(); + this.app = this.koa; } // ------------------------------------------------------------------------- diff --git a/src/index.ts b/src/index.ts index d71798c9..e143ab93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import {CustomParameterDecorator} from "./CustomParameterDecorator"; +import {BaseDriver} from "./driver/BaseDriver"; import {ExpressDriver} from "./driver/express/ExpressDriver"; import {KoaDriver} from "./driver/koa/KoaDriver"; import {MetadataArgsStorage} from "./metadata-builder/MetadataArgsStorage"; @@ -110,8 +111,8 @@ export function getMetadataArgsStorage(): MetadataArgsStorage { * Registers all loaded actions in your express application. */ export function useExpressServer(expressApp: T, options?: RoutingControllersOptions): T { - createExecutor(new ExpressDriver(expressApp), options || {}); - return expressApp; + const driver = new ExpressDriver(expressApp); + return createServer(driver, options); } /** @@ -119,16 +120,15 @@ export function useExpressServer(expressApp: T, options?: RoutingControllersO */ export function createExpressServer(options?: RoutingControllersOptions): any { const driver = new ExpressDriver(); - createExecutor(driver, options || {}); - return driver.express; + return createServer(driver, options); } /** * Registers all loaded actions in your koa application. */ export function useKoaServer(koaApp: T, options?: RoutingControllersOptions): T { - createExecutor(new KoaDriver(koaApp), options || {}); - return koaApp; + const driver = new KoaDriver(koaApp); + return createServer(driver, options); } /** @@ -136,14 +136,21 @@ export function useKoaServer(koaApp: T, options?: RoutingControllersOptions): */ export function createKoaServer(options?: RoutingControllersOptions): any { const driver = new KoaDriver(); - createExecutor(driver, options || {}); - return driver.koa; + return createServer(driver, options); +} + +/** + * Registers all loaded actions in your application using selected driver. + */ +export function createServer(driver: T, options?: RoutingControllersOptions): any { + createExecutor(driver, options); + return driver.app; } /** * Registers all loaded actions in your express application. */ -export function createExecutor(driver: Driver, options: RoutingControllersOptions): void { +export function createExecutor(driver: T, options: RoutingControllersOptions = {}): void { // import all controllers and middlewares and error handlers (new way) let controllerClasses: Function[]; From f252f66b0d21c0054c46b65f720a072c2104debc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=5BDesktop=5D?= Date: Mon, 4 Sep 2017 15:39:58 +0200 Subject: [PATCH 3/3] Fix missed Driver type dependency in codebase --- src/ActionParameterHandler.ts | 6 +++--- src/RoutingControllers.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ActionParameterHandler.ts b/src/ActionParameterHandler.ts index 6291e53a..1c5d364e 100644 --- a/src/ActionParameterHandler.ts +++ b/src/ActionParameterHandler.ts @@ -2,7 +2,7 @@ import {plainToClass} from "class-transformer"; import {validateOrReject as validate, ValidationError} from "class-validator"; import {Action} from "./Action"; import {BadRequestError} from "./http-error/BadRequestError"; -import {Driver} from "./driver/Driver"; +import {BaseDriver} from "./driver/BaseDriver"; import {ParameterParseJsonError} from "./error/ParameterParseJsonError"; import {ParamMetadata} from "./metadata/ParamMetadata"; import {ParamRequiredError} from "./error/ParamRequiredError"; @@ -13,13 +13,13 @@ import {isPromiseLike} from "./util/isPromiseLike"; /** * Handles action parameter. */ -export class ActionParameterHandler { +export class ActionParameterHandler { // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- - constructor(private driver: Driver) { + constructor(private driver: T) { } // ------------------------------------------------------------------------- diff --git a/src/RoutingControllers.ts b/src/RoutingControllers.ts index da682338..0fb3cf6b 100644 --- a/src/RoutingControllers.ts +++ b/src/RoutingControllers.ts @@ -1,11 +1,11 @@ import {Action} from "./Action"; import {ActionMetadata} from "./metadata/ActionMetadata"; import {ActionParameterHandler} from "./ActionParameterHandler"; -import {Driver} from "./driver/Driver"; +import {BaseDriver} from "./driver/BaseDriver"; import {InterceptorInterface} from "./InterceptorInterface"; import {InterceptorMetadata} from "./metadata/InterceptorMetadata"; import {MetadataBuilder} from "./metadata-builder/MetadataBuilder"; -import { RoutingControllersOptions } from "./RoutingControllersOptions"; +import {RoutingControllersOptions} from "./RoutingControllersOptions"; import {getFromContainer} from "./container"; import {isPromiseLike} from "./util/isPromiseLike"; import {runInSequence} from "./util/runInSequence"; @@ -13,7 +13,7 @@ import {runInSequence} from "./util/runInSequence"; /** * Registers controllers and middlewares in the given server framework. */ -export class RoutingControllers { +export class RoutingControllers { // ------------------------------------------------------------------------- // Private properties @@ -22,7 +22,7 @@ export class RoutingControllers { /** * Used to check and handle controller action parameters. */ - private parameterHandler: ActionParameterHandler; + private parameterHandler: ActionParameterHandler; /** * Used to build metadata objects for controllers and middlewares. @@ -38,7 +38,7 @@ export class RoutingControllers { // Constructor // ------------------------------------------------------------------------- - constructor(private driver: Driver, private options: RoutingControllersOptions) { + constructor(private driver: T, private options: RoutingControllersOptions) { this.parameterHandler = new ActionParameterHandler(driver); this.metadataBuilder = new MetadataBuilder(options); }