Skip to content

Commit

Permalink
Merge pull request #276 from 19majkel94/feature/custom-driver-support
Browse files Browse the repository at this point in the history
Custom driver support
  • Loading branch information
19majkel94 authored Sep 8, 2017
2 parents 9a7b3e4 + f252f66 commit 2b0251c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 139 deletions.
6 changes: 3 additions & 3 deletions src/ActionParameterHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -13,13 +13,13 @@ import {isPromiseLike} from "./util/isPromiseLike";
/**
* Handles action parameter.
*/
export class ActionParameterHandler {
export class ActionParameterHandler<T extends BaseDriver> {

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------

constructor(private driver: Driver) {
constructor(private driver: T) {
}

// -------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/RoutingControllers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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";

/**
* Registers controllers and middlewares in the given server framework.
*/
export class RoutingControllers {
export class RoutingControllers<T extends BaseDriver> {

// -------------------------------------------------------------------------
// Private properties
Expand All @@ -22,7 +22,7 @@ export class RoutingControllers {
/**
* Used to check and handle controller action parameters.
*/
private parameterHandler: ActionParameterHandler;
private parameterHandler: ActionParameterHandler<T>;

/**
* Used to build metadata objects for controllers and middlewares.
Expand All @@ -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);
}
Expand Down
50 changes: 48 additions & 2 deletions src/driver/BaseDriver.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
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
// -------------------------------------------------------------------------

/**
* Reference to the underlying framework app object.
*/
app: any;

/**
* Indicates if class-transformer should be used or not.
*/
Expand Down Expand Up @@ -77,6 +88,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
// -------------------------------------------------------------------------
Expand Down
114 changes: 0 additions & 114 deletions src/driver/Driver.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/driver/express/ExpressDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -29,6 +28,7 @@ export class ExpressDriver extends BaseDriver implements Driver {
constructor(public express?: any) {
super();
this.loadExpress();
this.app = this.express;
}

// -------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/driver/koa/KoaDriver.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
Expand All @@ -30,6 +29,7 @@ export class KoaDriver extends BaseDriver implements Driver {
super();
this.loadKoa();
this.loadRouter();
this.app = this.koa;
}

// -------------------------------------------------------------------------
Expand Down
27 changes: 16 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CustomParameterDecorator} from "./CustomParameterDecorator";
import {Driver} from "./driver/Driver";
import {BaseDriver} from "./driver/BaseDriver";
import {ExpressDriver} from "./driver/express/ExpressDriver";
import {KoaDriver} from "./driver/koa/KoaDriver";
import {MetadataArgsStorage} from "./metadata-builder/MetadataArgsStorage";
Expand Down Expand Up @@ -88,7 +88,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";
Expand All @@ -112,40 +111,46 @@ export function getMetadataArgsStorage(): MetadataArgsStorage {
* Registers all loaded actions in your express application.
*/
export function useExpressServer<T>(expressApp: T, options?: RoutingControllersOptions): T {
createExecutor(new ExpressDriver(expressApp), options || {});
return expressApp;
const driver = new ExpressDriver(expressApp);
return createServer(driver, options);
}

/**
* Registers all loaded actions in your express application.
*/
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<T>(koaApp: T, options?: RoutingControllersOptions): T {
createExecutor(new KoaDriver(koaApp), options || {});
return koaApp;
const driver = new KoaDriver(koaApp);
return createServer(driver, options);
}

/**
* Registers all loaded actions in your koa application.
*/
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<T extends BaseDriver>(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<T extends BaseDriver>(driver: T, options: RoutingControllersOptions = {}): void {

// import all controllers and middlewares and error handlers (new way)
let controllerClasses: Function[];
Expand Down

0 comments on commit 2b0251c

Please sign in to comment.