From b5aa7bc05b7a2b955ca23760a92c549899b5a345 Mon Sep 17 00:00:00 2001 From: remojansen Date: Tue, 5 Jul 2016 01:06:50 +0100 Subject: [PATCH] Working on https://github.com/inversify/InversifyJS/issues/283 --- README.md | 6 +++--- package.json | 2 +- src/constants.ts | 10 ++++++++++ src/decorators.ts | 9 +++++---- src/index.ts | 15 ++++++++++++++- src/server.ts | 10 ++++++---- test/framework.test.ts | 25 +++++++++++++------------ test/server.test.ts | 3 ++- 8 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 src/constants.ts diff --git a/README.md b/README.md index 1232464b..1fab47a5 100644 --- a/README.md +++ b/README.md @@ -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').to(FooController).whenTargetNamed('FooController'); +kernel.bind(TYPE.Controller).to(FooController).whenTargetNamed('FooController'); kernel.bind('FooService').to(FooService); // create server diff --git a/package.json b/package.json index 6b498670..574d2386 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 00000000..5b42c516 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,10 @@ +const TYPE = { + Controller: Symbol("Controller") +}; + +const METADATA_KEY = { + controller: "_controller", + controllerMethod: "_controller-method" +}; + +export { TYPE, METADATA_KEY }; diff --git a/src/decorators.ts b/src/decorators.ts index 181fe452..1fe21e90 100644 --- a/src/decorators.ts +++ b/src/decorators.ts @@ -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); }; } @@ -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); diff --git a/src/index.ts b/src/index.ts index 2cd833c3..f7872974 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 +}; diff --git a/src/server.ts b/src/server.ts index 43e69af0..1da524a5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,5 +1,6 @@ import * as express from "express"; import interfaces from "./interfaces"; +import { TYPE, METADATA_KEY } from "./constants"; /** * Wrapper for the express server. @@ -65,17 +66,18 @@ export class InversifyExpressServer { } private registerControllers() { - let controllers: interfaces.Controller[] = this.kernel.getAll("Controller"); + + let controllers: interfaces.Controller[] = this.kernel.getAll(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 ); @@ -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) { diff --git a/test/framework.test.ts b/test/framework.test.ts index 6486416d..ddac21f9 100644 --- a/test/framework.test.ts +++ b/test/framework.test.ts @@ -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; @@ -38,7 +39,7 @@ describe("Integration Tests:", () => { })); } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -56,7 +57,7 @@ describe("Integration Tests:", () => { })); } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -77,7 +78,7 @@ describe("Integration Tests:", () => { return "GET"; } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -103,7 +104,7 @@ describe("Integration Tests:", () => { return "GET"; } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -126,7 +127,7 @@ describe("Integration Tests:", () => { })); } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -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("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); let agent = request(server.build()); @@ -168,7 +169,7 @@ describe("Integration Tests:", () => { class TestController { @Method("propfind", "/") public getTest(req: express.Request, res: express.Response) { res.send("PROPFIND"); } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -185,7 +186,7 @@ describe("Integration Tests:", () => { class TestController { @Get("/") public getTest(req: express.Request, res: express.Response) { return result; } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -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("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -250,7 +251,7 @@ describe("Integration Tests:", () => { class TestController { @Get("/") public getTest(req: express.Request, res: express.Response) { res.send("GET"); } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); request(server.build()) @@ -271,7 +272,7 @@ describe("Integration Tests:", () => { class TestController { @Get("/") public getTest(req: express.Request, res: express.Response) { res.send("GET"); } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); @@ -299,7 +300,7 @@ describe("Integration Tests:", () => { class TestController { @Get("/", spyC) public getTest(req: express.Request, res: express.Response) { res.send("GET"); } } - kernel.bind("Controller").to(TestController).whenTargetNamed("TestController"); + kernel.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); server = new InversifyExpressServer(kernel); diff --git a/test/server.test.ts b/test/server.test.ts index c280d15c..0cd9fe27 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -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", () => { @@ -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)