From 581c31f92308728164f8f622a1e7716724307682 Mon Sep 17 00:00:00 2001 From: "Remo H. Jansen" Date: Sun, 17 Dec 2017 00:59:55 +0000 Subject: [PATCH] Implements #633 and #576 (#719) * Implements 633 and 576 * Preparing release --- package.json | 2 +- src/interfaces/interfaces.ts | 2 ++ src/planning/context.ts | 9 ++++++++- src/resolution/resolver.ts | 2 ++ test/bugs/issue_633.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/bugs/issue_633.test.ts diff --git a/package.json b/package.json index d33943339..b2a5f7d9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "inversify", - "version": "4.7.0", + "version": "4.8.0", "description": "A powerful and lightweight inversion of control container for JavaScript and Node.js apps powered by TypeScript.", "main": "lib/inversify.js", "jsnext:main": "es/inversify.js", diff --git a/src/interfaces/interfaces.ts b/src/interfaces/interfaces.ts index e0ba6c313..4107f6f53 100644 --- a/src/interfaces/interfaces.ts +++ b/src/interfaces/interfaces.ts @@ -88,7 +88,9 @@ namespace interfaces { guid: string; container: Container; plan: Plan; + currentRequest: Request; addPlan(plan: Plan): void; + setCurrentRequest(request: Request): void; } export interface ReflectResult { diff --git a/src/planning/context.ts b/src/planning/context.ts index 371182ea9..4a712feeb 100644 --- a/src/planning/context.ts +++ b/src/planning/context.ts @@ -6,8 +6,10 @@ class Context implements interfaces.Context { public guid: string; public container: interfaces.Container; public plan: interfaces.Plan; + public currentRequest: interfaces.Request; - public constructor(container: interfaces.Container) { + public constructor( + container: interfaces.Container) { this.guid = guid(); this.container = container; } @@ -15,6 +17,11 @@ class Context implements interfaces.Context { public addPlan(plan: interfaces.Plan) { this.plan = plan; } + + public setCurrentRequest(currentRequest: interfaces.Request) { + this.currentRequest = currentRequest; + } + } export { Context }; diff --git a/src/resolution/resolver.ts b/src/resolution/resolver.ts index e9fdbed1e..1c5c76ac3 100644 --- a/src/resolution/resolver.ts +++ b/src/resolution/resolver.ts @@ -28,6 +28,8 @@ const invokeFactory = ( const _resolveRequest = (requestScope: interfaces.RequestScope) => (request: interfaces.Request): any => { + request.parentContext.setCurrentRequest(request); + const bindings = request.bindings; const childRequests = request.childRequests; diff --git a/test/bugs/issue_633.test.ts b/test/bugs/issue_633.test.ts new file mode 100644 index 000000000..cb4f8ea4a --- /dev/null +++ b/test/bugs/issue_633.test.ts @@ -0,0 +1,36 @@ +import { expect } from "chai"; +import { Container, injectable } from "../../src/inversify"; + +describe("Issue 633", () => { + + it("Should expose metadata through context", () => { + + @injectable() + class Logger { + public named: string; + public constructor(named: string) { + this.named = named; + } + } + + const container = new Container(); + + const TYPE = { + Logger: Symbol.for("Logger") + }; + + container.bind(TYPE.Logger).toDynamicValue((context) => { + const namedMetadata = context.currentRequest.target.getNamedTag(); + const named = namedMetadata ? namedMetadata.value : "default"; + return new Logger(named); + }); + + const logger1 = container.getNamed(TYPE.Logger, "Name1"); + const logger2 = container.getNamed(TYPE.Logger, "Name2"); + + expect(logger1.named).to.eq("Name1"); + expect(logger2.named).to.eq("Name2"); + + }); + +});