Skip to content

Commit 2b0251c

Browse files
authored
Merge pull request #276 from 19majkel94/feature/custom-driver-support
Custom driver support
2 parents 9a7b3e4 + f252f66 commit 2b0251c

File tree

7 files changed

+76
-139
lines changed

7 files changed

+76
-139
lines changed

src/ActionParameterHandler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {plainToClass} from "class-transformer";
22
import {validateOrReject as validate, ValidationError} from "class-validator";
33
import {Action} from "./Action";
44
import {BadRequestError} from "./http-error/BadRequestError";
5-
import {Driver} from "./driver/Driver";
5+
import {BaseDriver} from "./driver/BaseDriver";
66
import {ParameterParseJsonError} from "./error/ParameterParseJsonError";
77
import {ParamMetadata} from "./metadata/ParamMetadata";
88
import {ParamRequiredError} from "./error/ParamRequiredError";
@@ -13,13 +13,13 @@ import {isPromiseLike} from "./util/isPromiseLike";
1313
/**
1414
* Handles action parameter.
1515
*/
16-
export class ActionParameterHandler {
16+
export class ActionParameterHandler<T extends BaseDriver> {
1717

1818
// -------------------------------------------------------------------------
1919
// Constructor
2020
// -------------------------------------------------------------------------
2121

22-
constructor(private driver: Driver) {
22+
constructor(private driver: T) {
2323
}
2424

2525
// -------------------------------------------------------------------------

src/RoutingControllers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import {Action} from "./Action";
22
import {ActionMetadata} from "./metadata/ActionMetadata";
33
import {ActionParameterHandler} from "./ActionParameterHandler";
4-
import {Driver} from "./driver/Driver";
4+
import {BaseDriver} from "./driver/BaseDriver";
55
import {InterceptorInterface} from "./InterceptorInterface";
66
import {InterceptorMetadata} from "./metadata/InterceptorMetadata";
77
import {MetadataBuilder} from "./metadata-builder/MetadataBuilder";
8-
import { RoutingControllersOptions } from "./RoutingControllersOptions";
8+
import {RoutingControllersOptions} from "./RoutingControllersOptions";
99
import {getFromContainer} from "./container";
1010
import {isPromiseLike} from "./util/isPromiseLike";
1111
import {runInSequence} from "./util/runInSequence";
1212

1313
/**
1414
* Registers controllers and middlewares in the given server framework.
1515
*/
16-
export class RoutingControllers {
16+
export class RoutingControllers<T extends BaseDriver> {
1717

1818
// -------------------------------------------------------------------------
1919
// Private properties
@@ -22,7 +22,7 @@ export class RoutingControllers {
2222
/**
2323
* Used to check and handle controller action parameters.
2424
*/
25-
private parameterHandler: ActionParameterHandler;
25+
private parameterHandler: ActionParameterHandler<T>;
2626

2727
/**
2828
* Used to build metadata objects for controllers and middlewares.
@@ -38,7 +38,7 @@ export class RoutingControllers {
3838
// Constructor
3939
// -------------------------------------------------------------------------
4040

41-
constructor(private driver: Driver, private options: RoutingControllersOptions) {
41+
constructor(private driver: T, private options: RoutingControllersOptions) {
4242
this.parameterHandler = new ActionParameterHandler(driver);
4343
this.metadataBuilder = new MetadataBuilder(options);
4444
}

src/driver/BaseDriver.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import {ValidatorOptions} from "class-validator";
2-
import {HttpError} from "../http-error/HttpError";
32
import {ClassTransformOptions} from "class-transformer";
3+
4+
import {HttpError} from "../http-error/HttpError";
45
import {CurrentUserChecker} from "../CurrentUserChecker";
56
import {AuthorizationChecker} from "../AuthorizationChecker";
7+
import {ActionMetadata} from "../metadata/ActionMetadata";
8+
import {ParamMetadata} from "../metadata/ParamMetadata";
9+
import {MiddlewareMetadata} from "../metadata/MiddlewareMetadata";
10+
import {Action} from "../Action";
611

712
/**
813
* Base driver functionality for all other drivers.
14+
* Abstract layer to organize controllers integration with different http server implementations.
915
*/
10-
export class BaseDriver {
16+
export abstract class BaseDriver {
1117

1218
// -------------------------------------------------------------------------
1319
// Public Properties
1420
// -------------------------------------------------------------------------
1521

22+
/**
23+
* Reference to the underlying framework app object.
24+
*/
25+
app: any;
26+
1627
/**
1728
* Indicates if class-transformer should be used or not.
1829
*/
@@ -77,6 +88,41 @@ export class BaseDriver {
7788
*/
7889
currentUserChecker?: CurrentUserChecker;
7990

91+
/**
92+
* Initializes the things driver needs before routes and middleware registration.
93+
*/
94+
abstract initialize(): void;
95+
96+
/**
97+
* Registers given middleware.
98+
*/
99+
abstract registerMiddleware(middleware: MiddlewareMetadata): void;
100+
101+
/**
102+
* Registers action in the driver.
103+
*/
104+
abstract registerAction(action: ActionMetadata, executeCallback: (options: Action) => any): void;
105+
106+
/**
107+
* Registers all routes in the framework.
108+
*/
109+
abstract registerRoutes(): void;
110+
111+
/**
112+
* Gets param from the request.
113+
*/
114+
abstract getParamFromRequest(actionOptions: Action, param: ParamMetadata): any;
115+
116+
/**
117+
* Defines an algorithm of how to handle error during executing controller action.
118+
*/
119+
abstract handleError(error: any, action: ActionMetadata, options: Action): any;
120+
121+
/**
122+
* Defines an algorithm of how to handle success result of executing controller action.
123+
*/
124+
abstract handleSuccess(result: any, action: ActionMetadata, options: Action): void;
125+
80126
// -------------------------------------------------------------------------
81127
// Protected Methods
82128
// -------------------------------------------------------------------------

src/driver/Driver.ts

Lines changed: 0 additions & 114 deletions
This file was deleted.

src/driver/express/ExpressDriver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {MiddlewareMetadata} from "../../metadata/MiddlewareMetadata";
33
import {ActionMetadata} from "../../metadata/ActionMetadata";
44
import {Action} from "../../Action";
55
import {classToPlain} from "class-transformer";
6-
import {Driver} from "../Driver";
76
import {ParamMetadata} from "../../metadata/ParamMetadata";
87
import {BaseDriver} from "../BaseDriver";
98
import {ExpressMiddlewareInterface} from "./ExpressMiddlewareInterface";
@@ -20,7 +19,7 @@ const templateUrl = require("template-url");
2019
/**
2120
* Integration with express framework.
2221
*/
23-
export class ExpressDriver extends BaseDriver implements Driver {
22+
export class ExpressDriver extends BaseDriver {
2423

2524
// -------------------------------------------------------------------------
2625
// Constructor
@@ -29,6 +28,7 @@ export class ExpressDriver extends BaseDriver implements Driver {
2928
constructor(public express?: any) {
3029
super();
3130
this.loadExpress();
31+
this.app = this.express;
3232
}
3333

3434
// -------------------------------------------------------------------------

src/driver/koa/KoaDriver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {Action} from "../../Action";
22
import {ActionMetadata} from "../../metadata/ActionMetadata";
33
import {BaseDriver} from "../BaseDriver";
4-
import {Driver} from "../Driver";
54
import {MiddlewareMetadata} from "../../metadata/MiddlewareMetadata";
65
import {ParamMetadata} from "../../metadata/ParamMetadata";
76
import {UseMetadata} from "../../metadata/UseMetadata";
@@ -20,7 +19,7 @@ const templateUrl = require("template-url");
2019
/**
2120
* Integration with koa framework.
2221
*/
23-
export class KoaDriver extends BaseDriver implements Driver {
22+
export class KoaDriver extends BaseDriver {
2423

2524
// -------------------------------------------------------------------------
2625
// Constructor
@@ -30,6 +29,7 @@ export class KoaDriver extends BaseDriver implements Driver {
3029
super();
3130
this.loadKoa();
3231
this.loadRouter();
32+
this.app = this.koa;
3333
}
3434

3535
// -------------------------------------------------------------------------

src/index.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {CustomParameterDecorator} from "./CustomParameterDecorator";
2-
import {Driver} from "./driver/Driver";
2+
import {BaseDriver} from "./driver/BaseDriver";
33
import {ExpressDriver} from "./driver/express/ExpressDriver";
44
import {KoaDriver} from "./driver/koa/KoaDriver";
55
import {MetadataArgsStorage} from "./metadata-builder/MetadataArgsStorage";
@@ -88,7 +88,6 @@ export * from "./RoleChecker";
8888
export * from "./Action";
8989
export * from "./InterceptorInterface";
9090

91-
export * from "./driver/Driver";
9291
export * from "./driver/BaseDriver";
9392
export * from "./driver/express/ExpressDriver";
9493
export * from "./driver/koa/KoaDriver";
@@ -112,40 +111,46 @@ export function getMetadataArgsStorage(): MetadataArgsStorage {
112111
* Registers all loaded actions in your express application.
113112
*/
114113
export function useExpressServer<T>(expressApp: T, options?: RoutingControllersOptions): T {
115-
createExecutor(new ExpressDriver(expressApp), options || {});
116-
return expressApp;
114+
const driver = new ExpressDriver(expressApp);
115+
return createServer(driver, options);
117116
}
118117

119118
/**
120119
* Registers all loaded actions in your express application.
121120
*/
122121
export function createExpressServer(options?: RoutingControllersOptions): any {
123122
const driver = new ExpressDriver();
124-
createExecutor(driver, options || {});
125-
return driver.express;
123+
return createServer(driver, options);
126124
}
127125

128126
/**
129127
* Registers all loaded actions in your koa application.
130128
*/
131129
export function useKoaServer<T>(koaApp: T, options?: RoutingControllersOptions): T {
132-
createExecutor(new KoaDriver(koaApp), options || {});
133-
return koaApp;
130+
const driver = new KoaDriver(koaApp);
131+
return createServer(driver, options);
134132
}
135133

136134
/**
137135
* Registers all loaded actions in your koa application.
138136
*/
139137
export function createKoaServer(options?: RoutingControllersOptions): any {
140138
const driver = new KoaDriver();
141-
createExecutor(driver, options || {});
142-
return driver.koa;
139+
return createServer(driver, options);
140+
}
141+
142+
/**
143+
* Registers all loaded actions in your application using selected driver.
144+
*/
145+
export function createServer<T extends BaseDriver>(driver: T, options?: RoutingControllersOptions): any {
146+
createExecutor(driver, options);
147+
return driver.app;
143148
}
144149

145150
/**
146151
* Registers all loaded actions in your express application.
147152
*/
148-
export function createExecutor(driver: Driver, options: RoutingControllersOptions): void {
153+
export function createExecutor<T extends BaseDriver>(driver: T, options: RoutingControllersOptions = {}): void {
149154

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

0 commit comments

Comments
 (0)