Skip to content

Commit

Permalink
Issue 590 (#97)
Browse files Browse the repository at this point in the history
* Added test case

* WIP implementation

* Implemented issue 590
  • Loading branch information
remojansen authored Dec 16, 2017
1 parent 6083280 commit 01448b5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ describe("Some Component", () => {

You can find an example of this in [our unit tests](https://github.com/inversify/inversify-express-utils/blob/master/test/framework.test.ts#L25-L29).

Inversidy express utils will throw an exception if your application doesn't have controllers. You can disable this behaviour using the `forceControllers` option. You can find some examples of `forceControllers` in [our unit tests](https://github.com/inversify/inversify-express-utils/blob/master/test/issue_590.test.ts).

## InversifyExpressServer

A wrapper for an express Application.
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": "5.1.2",
"version": "5.2.0",
"description": "Some utilities for the development of express applications with Inversify",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
6 changes: 5 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export enum PARAMETER_TYPE {
NEXT
}

export const DUPLICATED_CONTROLLER_NAME = (name: string) => `Two controllers cannot have the same name: ${name}`;
export const DUPLICATED_CONTROLLER_NAME = (name: string) =>
`Two controllers cannot have the same name: ${name}`;

export const NO_CONTROLLERS_FOUND = "No controllers have been found! " +
"Please ensure that you have register at least one Controller.";

const DEFAULT_ROUTING_ROOT_PATH = "/";

Expand Down
2 changes: 1 addition & 1 deletion src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function getRouteInfo(container: inversifyInterfaces.Container) {

export function getRawMetadata(container: inversifyInterfaces.Container) {

const controllers = getControllersFromContainer(container);
const controllers = getControllersFromContainer(container, true);

const raw = controllers.map((controller) => {

Expand Down
16 changes: 11 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export class InversifyExpressServer {
private _configFn: interfaces.ConfigFunction;
private _errorConfigFn: interfaces.ConfigFunction;
private _routingConfig: interfaces.RoutingConfig;
private _AuthProvider: { new(): interfaces.AuthProvider}|undefined;
private _AuthProvider: { new(): interfaces.AuthProvider};
private _forceControllers: boolean;

/**
* Wrapper for the express server.
Expand All @@ -37,16 +38,18 @@ export class InversifyExpressServer {
customRouter?: express.Router|null,
routingConfig?: interfaces.RoutingConfig|null,
customApp?: express.Application| null,
authProvider?: { new(): interfaces.AuthProvider}
authProvider?: { new(): interfaces.AuthProvider} | null,
forceControllers = true
) {
this._container = container;
this._forceControllers = forceControllers;
this._router = customRouter || express.Router();
this._routingConfig = routingConfig || {
rootPath: DEFAULT_ROUTING_ROOT_PATH
};
this._app = customApp || express();
this._AuthProvider = authProvider;
if (this._AuthProvider) {
if (authProvider) {
this._AuthProvider = authProvider;
container.bind<interfaces.AuthProvider>(TYPE.AuthProvider)
.to(this._AuthProvider);
}
Expand Down Expand Up @@ -139,7 +142,10 @@ export class InversifyExpressServer {
.whenTargetNamed(name);
});

let controllers = getControllersFromContainer(this._container);
let controllers = getControllersFromContainer(
this._container,
this._forceControllers
);

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

Expand Down
15 changes: 12 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { interfaces as inversifyInterfaces } from "inversify";
import { METADATA_KEY } from "./constants";
import { METADATA_KEY, NO_CONTROLLERS_FOUND } from "./constants";
import { interfaces } from "./interfaces";
import { TYPE } from "./constants";

export function getControllersFromContainer(container: inversifyInterfaces.Container) {
return container.getAll<interfaces.Controller>(TYPE.Controller);
export function getControllersFromContainer(
container: inversifyInterfaces.Container,
forceControllers: boolean
) {
if (container.isBound(TYPE.Controller)) {
return container.getAll<interfaces.Controller>(TYPE.Controller);
} else if (forceControllers) {
throw new Error(NO_CONTROLLERS_FOUND);
} else {
return [];
}
}

export function getControllersFromMetadata() {
Expand Down
26 changes: 26 additions & 0 deletions test/issue_590.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from "chai";
import { Container } from "inversify";
import { InversifyExpressServer, cleanUpMetadata } from "../src/index";
import { NO_CONTROLLERS_FOUND } from "../src/constants";

describe("Issue 590", () => {

beforeEach(() => {
cleanUpMetadata();
});

it("should throw if no bindings for controllers are declared", () => {
let container = new Container();
let server = new InversifyExpressServer(container);
const throws = () => server.build();
expect(throws).to.throw(NO_CONTROLLERS_FOUND);
});

it("should not throw if forceControllers is false and no bindings for controllers are declared", () => {
let container = new Container();
let server = new InversifyExpressServer(container, null, null, null, null, false);
const throws = () => server.build();
expect(throws).not.to.throw();
});

});

0 comments on commit 01448b5

Please sign in to comment.