Skip to content

Commit

Permalink
Merge pull request #6 from remojansen/master
Browse files Browse the repository at this point in the history
  • Loading branch information
remojansen authored Jul 5, 2016
2 parents dd97da3 + b5aa7bc commit 35241bb
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ Configure the inversify kernel in your composition root as usual.
Then, pass the kernel to the InversifyExpressServer constructor. This will allow it to register all controllers and their dependencies from your kernel and attach them to the express app.
Then just call server.build() to prepare your app.

In order for the InversifyExpressServer to find your controllers, you must bind them to the "Controller" service identifier and tag the binding with the controller's name.
In order for the InversifyExpressServer to find your controllers, you must bind them to the `TYPE.Controller` service identifier and tag the binding with the controller's name.
The `Controller` interface exported by inversify-express-utils is empty and solely for convenience, so feel free to implement your own if you want.

```ts
import { Kernel } from 'inversify';
import { InversifyExpressServer } from 'inversify-express-utils';
import { InversifyExpressServer, TYPE } from 'inversify-express-utils';

// set up kernel
let kernel = new Kernel();

// note that you *must* bind your controllers to Controller
kernel.bind<Controller>('Controller').to(FooController).whenTargetNamed('FooController');
kernel.bind<Controller>(TYPE.Controller).to(FooController).whenTargetNamed('FooController');
kernel.bind<FooService>('FooService').to(FooService);

// create server
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": "1.0.0-rc.1",
"version": "1.0.0-rc.2",
"description": "Some utilities for the development of express applications with Inversify",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
10 changes: 10 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const TYPE = {
Controller: Symbol("Controller")
};

const METADATA_KEY = {
controller: "_controller",
controllerMethod: "_controller-method"
};

export { TYPE, METADATA_KEY };
9 changes: 5 additions & 4 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as express from "express";
import interfaces from "./interfaces";
import { METADATA_KEY } from "./constants";

export function Controller(path: string, ...middleware: express.RequestHandler[]) {
return function (target: any) {
let metadata: interfaces.ControllerMetadata = {path, middleware, target};
Reflect.defineMetadata("_controller", metadata, target);
Reflect.defineMetadata(METADATA_KEY.controller, metadata, target);
};
}

Expand Down Expand Up @@ -41,10 +42,10 @@ export function Method(method: string, path: string, ...middleware: express.Requ
let metadata: interfaces.ControllerMethodMetadata = {path, middleware, method, target, key};
let metadataList: interfaces.ControllerMethodMetadata[] = [];

if (!Reflect.hasOwnMetadata("_controller-method", target.constructor)) {
Reflect.defineMetadata("_controller-method", metadataList, target.constructor);
if (!Reflect.hasOwnMetadata(METADATA_KEY.controllerMethod, target.constructor)) {
Reflect.defineMetadata(METADATA_KEY.controllerMethod, metadataList, target.constructor);
} else {
metadataList = Reflect.getOwnMetadata("_controller-method", target.constructor);
metadataList = Reflect.getOwnMetadata(METADATA_KEY.controllerMethod, target.constructor);
}

metadataList.push(metadata);
Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { InversifyExpressServer } from "./server";
import { Controller, Method, Get, Put, Post, Patch, Head, All, Delete } from "./decorators";
import { TYPE } from "./constants";

export { InversifyExpressServer, Controller, Method, Get, Put, Post, Patch, Head, All, Delete };
export {
InversifyExpressServer,
Controller,
Method,
Get,
Put,
Post,
Patch,
Head,
All,
Delete,
TYPE
};
10 changes: 6 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as express from "express";
import interfaces from "./interfaces";
import { TYPE, METADATA_KEY } from "./constants";

/**
* Wrapper for the express server.
Expand Down Expand Up @@ -65,17 +66,18 @@ export class InversifyExpressServer {
}

private registerControllers() {
let controllers: interfaces.Controller[] = this.kernel.getAll<interfaces.Controller>("Controller");

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

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

let controllerMetadata: interfaces.ControllerMetadata = Reflect.getOwnMetadata(
"_controller",
METADATA_KEY.controller,
controller.constructor
);

let methodMetadata: interfaces.ControllerMethodMetadata[] = Reflect.getOwnMetadata(
"_controller-method",
METADATA_KEY.controllerMethod,
controller.constructor
);

Expand All @@ -94,7 +96,7 @@ export class InversifyExpressServer {

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

Expand Down
25 changes: 13 additions & 12 deletions test/framework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { injectable, Kernel } from "inversify";
import interfaces from "../src/interfaces";
import { InversifyExpressServer } from "../src/server";
import { Controller, Method, Get, Post, Put, Patch, Head, Delete } from "../src/decorators";
import { TYPE } from "../src/constants";

describe("Integration Tests:", () => {
let server: InversifyExpressServer;
Expand All @@ -38,7 +39,7 @@ describe("Integration Tests:", () => {
}));
}
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -56,7 +57,7 @@ describe("Integration Tests:", () => {
}));
}
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -77,7 +78,7 @@ describe("Integration Tests:", () => {
return "GET";
}
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -103,7 +104,7 @@ describe("Integration Tests:", () => {
return "GET";
}
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -126,7 +127,7 @@ describe("Integration Tests:", () => {
}));
}
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -146,7 +147,7 @@ describe("Integration Tests:", () => {
@Head("/") public headTest(req: express.Request, res: express.Response) { res.send("HEAD"); }
@Delete("/") public deleteTest(req: express.Request, res: express.Response) { res.send("DELETE"); }
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
let agent = request(server.build());
Expand All @@ -168,7 +169,7 @@ describe("Integration Tests:", () => {
class TestController {
@Method("propfind", "/") public getTest(req: express.Request, res: express.Response) { res.send("PROPFIND"); }
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -185,7 +186,7 @@ describe("Integration Tests:", () => {
class TestController {
@Get("/") public getTest(req: express.Request, res: express.Response) { return result; }
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand Down Expand Up @@ -229,7 +230,7 @@ describe("Integration Tests:", () => {
class TestController {
@Get("/", spyA, spyB, spyC) public getTest(req: express.Request, res: express.Response) { res.send("GET"); }
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -250,7 +251,7 @@ describe("Integration Tests:", () => {
class TestController {
@Get("/") public getTest(req: express.Request, res: express.Response) { res.send("GET"); }
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);
request(server.build())
Expand All @@ -271,7 +272,7 @@ describe("Integration Tests:", () => {
class TestController {
@Get("/") public getTest(req: express.Request, res: express.Response) { res.send("GET"); }
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);

Expand Down Expand Up @@ -299,7 +300,7 @@ describe("Integration Tests:", () => {
class TestController {
@Get("/", spyC) public getTest(req: express.Request, res: express.Response) { res.send("GET"); }
}
kernel.bind<interfaces.Controller>("Controller").to(TestController).whenTargetNamed("TestController");
kernel.bind<interfaces.Controller>(TYPE.Controller).to(TestController).whenTargetNamed("TestController");

server = new InversifyExpressServer(kernel);

Expand Down
3 changes: 2 additions & 1 deletion test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as sinon from "sinon";
import * as express from "express";
import { InversifyExpressServer } from "../src/server";
import { Kernel, injectable } from "inversify";
import { TYPE } from "../src/constants";

describe("Unit Test: InversifyExpressServer", () => {

Expand All @@ -18,7 +19,7 @@ describe("Unit Test: InversifyExpressServer", () => {
@injectable()
class TestController {}

kernel.bind("Controller").to(TestController);
kernel.bind(TYPE.Controller).to(TestController);
let server = new InversifyExpressServer(kernel);

server.setConfig(configFn)
Expand Down

0 comments on commit 35241bb

Please sign in to comment.