Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Implements inversify/InversifyJS/issues/478

* 3.1.0 release

* Added missing docs
  • Loading branch information
remojansen authored Jan 29, 2017
1 parent 0067b37 commit d000880
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ server
.listen(3000, 'localhost', callback);
```

## Using a custom Router
It is possible to pass a custom `Router` instance to `InversifyExpressServer`:

```ts
let container = new Container();

let router = express.Router({
caseSensitive: false,
mergeParams: false,
strict: false
});

let server = new InversifyExpressServer(container, router);
```

## Decorators

### `@Controller(path, [middleware, ...])`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inversify-express-utils",
"version": "3.0.0",
"version": "3.1.0",
"description": "Some utilities for the development of express applications with Inversify",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
38 changes: 22 additions & 16 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import { TYPE, METADATA_KEY } from "./constants";
* Wrapper for the express server.
*/
export class InversifyExpressServer {
private container: inversify.interfaces.Container;
private app: express.Application = express();
private configFn: interfaces.ConfigFunction;
private errorConfigFn: interfaces.ConfigFunction;

private _router: express.Router;
private _container: inversify.interfaces.Container;
private _app: express.Application = express();
private _configFn: interfaces.ConfigFunction;
private _errorConfigFn: interfaces.ConfigFunction;

/**
* Wrapper for the express server.
*
* @param container Container loaded with all controllers and their dependencies.
*/
constructor(container: inversify.interfaces.Container) {
this.container = container;
constructor(
container: inversify.interfaces.Container,
customRouter?: express.Router
) {
this._container = container;
this._router = customRouter || express.Router();
}

/**
Expand All @@ -30,7 +36,7 @@ export class InversifyExpressServer {
* @param fn Function in which app-level middleware can be registered.
*/
public setConfig(fn: interfaces.ConfigFunction): InversifyExpressServer {
this.configFn = fn;
this._configFn = fn;
return this;
}

Expand All @@ -43,7 +49,7 @@ export class InversifyExpressServer {
* @param fn Function in which app-level error handlers can be registered.
*/
public setErrorConfig(fn: interfaces.ConfigFunction): InversifyExpressServer {
this.errorConfigFn = fn;
this._errorConfigFn = fn;
return this;
}

Expand All @@ -52,23 +58,23 @@ export class InversifyExpressServer {
*/
public build(): express.Application {
// register server-level middleware before anything else
if (this.configFn) {
this.configFn.apply(undefined, [this.app]);
if (this._configFn) {
this._configFn.apply(undefined, [this._app]);
}

this.registerControllers();

// register error handlers after controllers
if (this.errorConfigFn) {
this.errorConfigFn.apply(undefined, [this.app]);
if (this._errorConfigFn) {
this._errorConfigFn.apply(undefined, [this._app]);
}

return this.app;
return this._app;
}

private registerControllers() {

let controllers: interfaces.Controller[] = this.container.getAll<interfaces.Controller>(TYPE.Controller);
let controllers: interfaces.Controller[] = this._container.getAll<interfaces.Controller>(TYPE.Controller);

controllers.forEach((controller: interfaces.Controller) => {

Expand All @@ -90,14 +96,14 @@ export class InversifyExpressServer {
router[metadata.method](metadata.path, ...metadata.middleware, handler);
});

this.app.use(controllerMetadata.path, ...controllerMetadata.middleware, router);
this._app.use(controllerMetadata.path, ...controllerMetadata.middleware, router);
}
});
}

private handlerFactory(controllerName: any, key: string): express.RequestHandler {
return (req: express.Request, res: express.Response, next: express.NextFunction) => {
let result: any = this.container.getNamed(TYPE.Controller, controllerName)[key](req, res, next);
let result: any = this._container.getNamed(TYPE.Controller, controllerName)[key](req, res, next);
// try to resolve promise
if (result && result instanceof Promise) {

Expand Down
19 changes: 19 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,23 @@ describe("Unit Test: InversifyExpressServer", () => {
expect(configFn.calledBefore(errorConfigFn)).to.be.true;
done();
});

it("Should allow to pass a custom Router instance as config", () => {

let container = new Container();

let customRouter = express.Router({
caseSensitive: false,
mergeParams: false,
strict: false
});

let serverWithDefaultRouter = new InversifyExpressServer(container);
let serverWithCustomRouter = new InversifyExpressServer(container, customRouter);

expect((serverWithDefaultRouter as any)._router === customRouter).to.be.false;
expect((serverWithCustomRouter as any)._router === customRouter).to.be.true;

});

});

0 comments on commit d000880

Please sign in to comment.