@tsed/di
Advanced tools
Comparing version 5.18.2 to 5.19.0
@@ -1,4 +0,8 @@ | ||
import { IInterceptorContext } from "./IInterceptorContext"; | ||
import { IInterceptorContext, IInterceptorNextHandler } from "./IInterceptorContext"; | ||
export interface IInterceptor { | ||
aroundInvoke: (ctx: IInterceptorContext<any>, options?: any) => any; | ||
/** | ||
* @deprecated Use intercept instead. | ||
*/ | ||
aroundInvoke?(context: IInterceptorContext<any>, options?: any): any; | ||
intercept?(context: IInterceptorContext<any>, next?: IInterceptorNextHandler): any; | ||
} |
@@ -0,3 +1,10 @@ | ||
export interface IInterceptorNextHandler { | ||
<T>(err?: Error): T; | ||
} | ||
export interface IInterceptorContext<T> { | ||
target: T; | ||
propertyKey: string; | ||
args: any[]; | ||
next: IInterceptorNextHandler; | ||
options?: any; | ||
/** | ||
@@ -7,5 +14,6 @@ * @deprecated | ||
method: string; | ||
propertyKey: string; | ||
args: any[]; | ||
proceed: <T>(err?: Error) => T; | ||
/** | ||
* @deprecated | ||
*/ | ||
proceed<T>(err?: Error): T; | ||
} |
@@ -160,4 +160,3 @@ import { IProvider } from "../interfaces"; | ||
* export default class MyInterceptor { | ||
* constructor(){} | ||
* aroundInvoke() { | ||
* intercept() { | ||
* return "test"; | ||
@@ -175,3 +174,3 @@ * } | ||
* const myInterceptor = injector.get<MyInterceptor>(MyInterceptor); | ||
* myInterceptor.aroundInvoke(); // test | ||
* myInterceptor.intercept(); // test | ||
* ``` | ||
@@ -178,0 +177,0 @@ * |
@@ -221,4 +221,3 @@ "use strict"; | ||
* export default class MyInterceptor { | ||
* constructor(){} | ||
* aroundInvoke() { | ||
* intercept() { | ||
* return "test"; | ||
@@ -236,3 +235,3 @@ * } | ||
* const myInterceptor = injector.get<MyInterceptor>(MyInterceptor); | ||
* myInterceptor.aroundInvoke(); // test | ||
* myInterceptor.intercept(); // test | ||
* ``` | ||
@@ -239,0 +238,0 @@ * |
@@ -6,2 +6,3 @@ "use strict"; | ||
const core_1 = require("@tsed/core"); | ||
const util = require("util"); | ||
const Container_1 = require("../class/Container"); | ||
@@ -286,2 +287,8 @@ const LocalsContainer_1 = require("../class/LocalsContainer"); | ||
instance[propertyKey] = (...args) => { | ||
const next = (err) => { | ||
if (!err) { | ||
return originalMethod.apply(instance, args); | ||
} | ||
throw err; | ||
}; | ||
const context = { | ||
@@ -292,10 +299,12 @@ target, | ||
args, | ||
proceed(err) { | ||
if (!err) { | ||
return originalMethod.apply(instance, args); | ||
} | ||
throw err; | ||
} | ||
options, | ||
proceed: util.deprecate(next, "context.proceed() is deprecated. Use context.next() or next() parameters instead."), | ||
next | ||
}; | ||
return this.get(useType).aroundInvoke(context, options); | ||
const interceptor = this.get(useType); | ||
if (interceptor.aroundInvoke) { | ||
interceptor.aroundInvoke = util.deprecate(interceptor.aroundInvoke.bind(interceptor), "interceptor.aroundInvoke is deprecated. Use interceptor.intercept instead."); | ||
return interceptor.aroundInvoke(context, options); | ||
} | ||
return interceptor.intercept(Object.assign({}, context, { options }), next); | ||
}; | ||
@@ -302,0 +311,0 @@ } |
{ | ||
"name": "@tsed/di", | ||
"version": "5.18.2", | ||
"version": "5.19.0", | ||
"description": "DI module for Ts.ED Framework", | ||
@@ -11,3 +11,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@tsed/core": "5.18.2" | ||
"@tsed/core": "5.19.0" | ||
}, | ||
@@ -28,3 +28,3 @@ "devDependencies": {}, | ||
"license": "MIT", | ||
"gitHead": "a91a46278a1c344e84309812feb799d0a4bcc661" | ||
"gitHead": "93c7d1e0ecd8d6bac14bd5b18da83297d7fc03c7" | ||
} |
@@ -1,5 +0,10 @@ | ||
import {IInterceptorContext} from "./IInterceptorContext"; | ||
import {IInterceptorContext, IInterceptorNextHandler} from "./IInterceptorContext"; | ||
export interface IInterceptor { | ||
aroundInvoke: (ctx: IInterceptorContext<any>, options?: any) => any; | ||
/** | ||
* @deprecated Use intercept instead. | ||
*/ | ||
aroundInvoke?(context: IInterceptorContext<any>, options?: any): any; | ||
intercept?(context: IInterceptorContext<any>, next?: IInterceptorNextHandler): any; | ||
} |
@@ -0,3 +1,11 @@ | ||
export interface IInterceptorNextHandler { | ||
<T>(err?: Error): T; | ||
} | ||
export interface IInterceptorContext<T> { | ||
target: T; | ||
propertyKey: string; | ||
args: any[]; | ||
next: IInterceptorNextHandler; | ||
options?: any; | ||
/** | ||
@@ -7,5 +15,6 @@ * @deprecated | ||
method: string; | ||
propertyKey: string; | ||
args: any[]; | ||
proceed: <T>(err?: Error) => T; | ||
/** | ||
* @deprecated | ||
*/ | ||
proceed<T>(err?: Error): T; | ||
} |
@@ -235,4 +235,3 @@ import {Provider} from "../class/Provider"; | ||
* export default class MyInterceptor { | ||
* constructor(){} | ||
* aroundInvoke() { | ||
* intercept() { | ||
* return "test"; | ||
@@ -250,3 +249,3 @@ * } | ||
* const myInterceptor = injector.get<MyInterceptor>(MyInterceptor); | ||
* myInterceptor.aroundInvoke(); // test | ||
* myInterceptor.intercept(); // test | ||
* ``` | ||
@@ -253,0 +252,0 @@ * |
import {deepClone, getClass, getClassOrSymbol, isFunction, Metadata, nameOf, prototypeOf, Store} from "@tsed/core"; | ||
import * as util from "util"; | ||
import {Container} from "../class/Container"; | ||
@@ -344,2 +345,10 @@ import {LocalsContainer} from "../class/LocalsContainer"; | ||
instance[propertyKey] = (...args: any[]) => { | ||
const next = (err?: Error) => { | ||
if (!err) { | ||
return originalMethod.apply(instance, args); | ||
} | ||
throw err; | ||
}; | ||
const context: IInterceptorContext<any> = { | ||
@@ -350,12 +359,25 @@ target, | ||
args, | ||
proceed(err?: Error) { | ||
if (!err) { | ||
return originalMethod.apply(instance, args); | ||
} | ||
throw err; | ||
} | ||
options, | ||
proceed: util.deprecate(next, "context.proceed() is deprecated. Use context.next() or next() parameters instead."), | ||
next | ||
}; | ||
return this.get<IInterceptor>(useType)!.aroundInvoke(context, options); | ||
const interceptor = this.get<IInterceptor>(useType)!; | ||
if (interceptor.aroundInvoke) { | ||
interceptor.aroundInvoke = util.deprecate( | ||
interceptor.aroundInvoke.bind(interceptor), | ||
"interceptor.aroundInvoke is deprecated. Use interceptor.intercept instead." | ||
); | ||
return interceptor.aroundInvoke!(context, options); | ||
} | ||
return interceptor.intercept!( | ||
{ | ||
...context, | ||
options | ||
}, | ||
next | ||
); | ||
}; | ||
@@ -362,0 +384,0 @@ } |
@@ -8,3 +8,3 @@ import {Store} from "@tsed/core"; | ||
class TestInterceptor implements IInterceptor { | ||
aroundInvoke(ctx: IInterceptorContext<any>, options?: any) { | ||
intercept(ctx: IInterceptorContext<any>) { | ||
@@ -11,0 +11,0 @@ return ""; |
@@ -18,7 +18,7 @@ import { | ||
aroundInvoke(ctx: IInterceptorContext<any>, opts?: string) { | ||
const r = typeof ctx.args[0] === "string" ? undefined : new Error(`Error message`); | ||
const retValue = ctx.proceed(r); | ||
intercept(context: IInterceptorContext<any>) { | ||
const r = typeof context.args[0] === "string" ? undefined : new Error(`Error message`); | ||
const retValue = context.next(r); | ||
return `${retValue} - ${opts || ""} - intercepted`; | ||
return `${retValue} - ${context.options || ""} - intercepted`; | ||
} | ||
@@ -25,0 +25,0 @@ } |
@@ -571,3 +571,3 @@ import {Store} from "@tsed/core"; | ||
it("should bind the method", async () => { | ||
it("should bind the method with aroundInvoke", async () => { | ||
// GIVEN | ||
@@ -605,3 +605,36 @@ class InterceptorTest { | ||
}); | ||
it("should bind the method with intercept", async () => { | ||
// GIVEN | ||
class InterceptorTest { | ||
intercept(ctx: any) { | ||
return ctx.next() + " intercepted"; | ||
} | ||
} | ||
const injector = new InjectorService(); | ||
injector.addProvider(InterceptorTest); | ||
await injector.load(); | ||
const instance = new Test(); | ||
const originalMethod = instance["test"]; | ||
sandbox.spy(injector, "get"); | ||
// WHEN | ||
injector.bindInterceptor(instance, { | ||
bindingType: "interceptor", | ||
propertyKey: "test3", | ||
useType: InterceptorTest | ||
} as any); | ||
const result = (instance as any).test3("test"); | ||
// THEN | ||
expect(originalMethod).should.not.eq(instance.test3); | ||
injector.get.should.have.been.calledWithExactly(InterceptorTest); | ||
expect(result).to.eq("test called intercepted"); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
304475
5272