Skip to content

Commit f9b2530

Browse files
authored
Merge pull request #20 from Antpolis/master
To allow use function with directory path
2 parents 4baa8b8 + bc1ef28 commit f9b2530

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/SocketControllerExecutor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export class SocketControllerExecutor {
5252
// Public Methods
5353
// -------------------------------------------------------------------------
5454

55-
execute() {
56-
this.registerControllers();
57-
this.registerMiddlewares();
55+
execute(controllerClasses?: Function[], middlewareClasses?: Function[]) {
56+
this.registerControllers(controllerClasses);
57+
this.registerMiddlewares(middlewareClasses);
5858
}
5959

6060
// -------------------------------------------------------------------------

src/SocketControllersOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ export interface SocketControllersOptions {
88
/**
99
* List of directories from where to "require" all your controllers.
1010
*/
11-
controllers?: string[];
11+
controllers?: Function[] | string[];
1212

1313
/**
1414
* List of directories from where to "require" all your middlewares.
1515
*/
16-
middlewares?: string[];
16+
middlewares?: Function[] | string[];
1717

1818
/**
1919
* Indicates if class-transformer package should be used to perform message body serialization / deserialization.

src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ function createExecutor(io: any, options: SocketControllersOptions): void {
3131
const executor = new SocketControllerExecutor(io);
3232

3333
// second import all controllers and middlewares and error handlers
34+
let controllerClasses: Function[];
3435
if (options && options.controllers && options.controllers.length)
35-
importClassesFromDirectories(options.controllers);
36+
controllerClasses = (options.controllers as any[]).filter(controller => controller instanceof Function);
37+
const controllerDirs = (options.controllers as any[]).filter(controller => typeof controller === "string");
38+
controllerClasses.push(...importClassesFromDirectories(controllerDirs));
3639

37-
if (options && options.middlewares && options.middlewares.length)
38-
importClassesFromDirectories(options.middlewares);
40+
let middlewareClasses: Function[];
41+
if (options && options.middlewares && options.middlewares.length) {
42+
middlewareClasses = (options.middlewares as any[]).filter(controller => controller instanceof Function);
43+
const middlewareDirs = (options.middlewares as any[]).filter(controller => typeof controller === "string");
44+
middlewareClasses.push(...importClassesFromDirectories(middlewareDirs));
45+
}
3946

4047
if (options.useClassTransformer !== undefined) {
4148
executor.useClassTransformer = options.useClassTransformer;
@@ -47,7 +54,7 @@ function createExecutor(io: any, options: SocketControllersOptions): void {
4754
executor.plainToClassTransformOptions = options.plainToClassTransformOptions;
4855

4956
// run socket controller register and other operations
50-
executor.execute();
57+
executor.execute(controllerClasses, middlewareClasses);
5158
}
5259

5360
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)