Skip to content

Commit 834cdaa

Browse files
committed
Create generic createServer function
1 parent d30e72f commit 834cdaa

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/driver/BaseDriver.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export abstract class BaseDriver {
1919
// Public Properties
2020
// -------------------------------------------------------------------------
2121

22+
/**
23+
* Reference to the underlying framework app object.
24+
*/
25+
app: any;
26+
2227
/**
2328
* Indicates if class-transformer should be used or not.
2429
*/

src/driver/express/ExpressDriver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class ExpressDriver extends BaseDriver {
2828
constructor(public express?: any) {
2929
super();
3030
this.loadExpress();
31+
this.app = this.express;
3132
}
3233

3334
// -------------------------------------------------------------------------

src/driver/koa/KoaDriver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class KoaDriver extends BaseDriver {
2929
super();
3030
this.loadKoa();
3131
this.loadRouter();
32+
this.app = this.koa;
3233
}
3334

3435
// -------------------------------------------------------------------------

src/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {CustomParameterDecorator} from "./CustomParameterDecorator";
2+
import {BaseDriver} from "./driver/BaseDriver";
23
import {ExpressDriver} from "./driver/express/ExpressDriver";
34
import {KoaDriver} from "./driver/koa/KoaDriver";
45
import {MetadataArgsStorage} from "./metadata-builder/MetadataArgsStorage";
@@ -110,40 +111,46 @@ export function getMetadataArgsStorage(): MetadataArgsStorage {
110111
* Registers all loaded actions in your express application.
111112
*/
112113
export function useExpressServer<T>(expressApp: T, options?: RoutingControllersOptions): T {
113-
createExecutor(new ExpressDriver(expressApp), options || {});
114-
return expressApp;
114+
const driver = new ExpressDriver(expressApp);
115+
return createServer(driver, options);
115116
}
116117

117118
/**
118119
* Registers all loaded actions in your express application.
119120
*/
120121
export function createExpressServer(options?: RoutingControllersOptions): any {
121122
const driver = new ExpressDriver();
122-
createExecutor(driver, options || {});
123-
return driver.express;
123+
return createServer(driver, options);
124124
}
125125

126126
/**
127127
* Registers all loaded actions in your koa application.
128128
*/
129129
export function useKoaServer<T>(koaApp: T, options?: RoutingControllersOptions): T {
130-
createExecutor(new KoaDriver(koaApp), options || {});
131-
return koaApp;
130+
const driver = new KoaDriver(koaApp);
131+
return createServer(driver, options);
132132
}
133133

134134
/**
135135
* Registers all loaded actions in your koa application.
136136
*/
137137
export function createKoaServer(options?: RoutingControllersOptions): any {
138138
const driver = new KoaDriver();
139-
createExecutor(driver, options || {});
140-
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;
141148
}
142149

143150
/**
144151
* Registers all loaded actions in your express application.
145152
*/
146-
export function createExecutor(driver: Driver, options: RoutingControllersOptions): void {
153+
export function createExecutor<T extends BaseDriver>(driver: T, options: RoutingControllersOptions = {}): void {
147154

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

0 commit comments

Comments
 (0)