-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): Add UseBeforeEach decorator.
- Loading branch information
Showing
15 changed files
with
391 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/common/src/mvc/decorators/method/useBeforeEach.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {decorateMethodsOf, DecoratorParameters, getDecoratorType, UnsupportedDecoratorType} from "@tsed/core"; | ||
import {UseBefore} from "./UseBefore"; | ||
|
||
/** | ||
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when | ||
* the base of the requested path matches `path. | ||
* | ||
* ```typescript | ||
* @Controller('/') | ||
* @UseBeforeEach(Middleware1) // Called before each endpoint | ||
* export class Ctrl { | ||
* | ||
* @Get('/') | ||
* get() { } | ||
* } | ||
* | ||
* ``` | ||
* | ||
* @returns {Function} | ||
* @param args | ||
* @decorator | ||
* @endpoint | ||
*/ | ||
export function UseBeforeEach(...args: any[]): Function { | ||
return <T>(...decoratorArgs: DecoratorParameters): TypedPropertyDescriptor<T> | void => { | ||
switch (getDecoratorType(decoratorArgs, true)) { | ||
case "method": | ||
return UseBefore(...args)(...decoratorArgs); | ||
|
||
case "class": | ||
decorateMethodsOf(decoratorArgs[0], UseBefore(...args)); | ||
break; | ||
|
||
default: | ||
throw new UnsupportedDecoratorType(UseBeforeEach, decoratorArgs); | ||
} | ||
}; | ||
} |
93 changes: 72 additions & 21 deletions
93
packages/common/test/mvc/decorators/method/useAfter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,100 @@ | ||
import {decoratorArgs, descriptorOf, Store} from "@tsed/core"; | ||
import {expect} from "chai"; | ||
import {prototypeOf, Store, UnsupportedDecoratorType} from "@tsed/core"; | ||
import * as Sinon from "sinon"; | ||
import {EndpointRegistry, UseAfter} from "../../../../src/mvc"; | ||
|
||
class Test { | ||
test() { | ||
class CustomMiddleware { | ||
use() { | ||
} | ||
} | ||
|
||
describe("UseAfter()", () => { | ||
describe("when the decorator is use on a method", () => { | ||
before(() => { | ||
this.endpointRegistryStub = Sinon.stub(EndpointRegistry, "useAfter"); | ||
describe("when the decorator is use on a class", () => { | ||
class Test { | ||
test() { | ||
} | ||
} | ||
|
||
this.returns = UseAfter(() => { | ||
})(...decoratorArgs(Test, "test")); | ||
before(() => { | ||
Sinon.stub(EndpointRegistry, "useAfter"); | ||
}); | ||
|
||
after(() => { | ||
this.endpointRegistryStub.restore(); | ||
// @ts-ignore | ||
EndpointRegistry.useAfter.restore(); | ||
}); | ||
|
||
afterEach(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useAfter.resetHistory(); | ||
}); | ||
|
||
it("should add the middleware on the use stack", () => { | ||
this.endpointRegistryStub.should.be.calledWithExactly(Test, "test", [Sinon.match.func]); | ||
// WHEN | ||
UseAfter(CustomMiddleware)(Test); | ||
|
||
// THEN | ||
Store.from(Test).get("middlewares").should.deep.eq({useAfter: [CustomMiddleware]}); | ||
}); | ||
}); | ||
describe("when the decorator is use on a method", () => { | ||
before(() => { | ||
Sinon.stub(EndpointRegistry, "useAfter"); | ||
}); | ||
|
||
after(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useAfter.restore(); | ||
}); | ||
|
||
afterEach(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useAfter.resetHistory(); | ||
}); | ||
|
||
it("should return a descriptor", () => { | ||
this.returns.should.be.deep.eq(descriptorOf(Test, "test")); | ||
it("should add the middleware on the use stack", () => { | ||
// WHEN | ||
class Test { | ||
@UseAfter(CustomMiddleware) | ||
test() { | ||
} | ||
} | ||
|
||
// THEN | ||
EndpointRegistry.useAfter.should.be.calledWithExactly(prototypeOf(Test), "test", [CustomMiddleware]); | ||
}); | ||
}); | ||
describe("when the decorator is use in another way", () => { | ||
class Test { | ||
test() { | ||
} | ||
} | ||
|
||
describe("when the decorator is use on a class", () => { | ||
before(() => { | ||
this.returns = UseAfter(() => { | ||
})(Test); | ||
Sinon.stub(EndpointRegistry, "useAfter"); | ||
}); | ||
|
||
after(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useAfter.restore(); | ||
}); | ||
|
||
this.store = Store.from(Test).get("middlewares"); | ||
afterEach(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useAfter.resetHistory(); | ||
}); | ||
|
||
it("should add the middleware on the use stack", () => { | ||
expect(this.store.useAfter[0]).to.be.a("function"); | ||
}); | ||
// WHEN | ||
let actualError; | ||
try { | ||
UseAfter(CustomMiddleware)(Test, "property"); | ||
} catch (er) { | ||
actualError = er; | ||
} | ||
|
||
it("should return nothing", () => { | ||
expect(this.returns).to.eq(undefined); | ||
// THEN | ||
actualError.should.instanceOf(UnsupportedDecoratorType); | ||
actualError.message.should.eq("UseAfter cannot used as property.static at Test.property"); | ||
}); | ||
}); | ||
}); |
95 changes: 74 additions & 21 deletions
95
packages/common/test/mvc/decorators/method/useBefore.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,100 @@ | ||
import {decoratorArgs, descriptorOf, Store} from "@tsed/core"; | ||
import {expect} from "chai"; | ||
import {prototypeOf, Store, UnsupportedDecoratorType} from "@tsed/core"; | ||
import * as Sinon from "sinon"; | ||
import {EndpointRegistry, UseBefore} from "../../../../src/mvc"; | ||
|
||
class Test { | ||
test() { | ||
class CustomMiddleware { | ||
use() { | ||
} | ||
} | ||
|
||
describe("UseBefore()", () => { | ||
describe("when the decorator is use on a method", () => { | ||
before(() => { | ||
this.endpointRegistryStub = Sinon.stub(EndpointRegistry, "useBefore"); | ||
describe("when the decorator is use on a class", () => { | ||
class Test { | ||
test() { | ||
} | ||
} | ||
|
||
this.returns = UseBefore(() => { | ||
})(...decoratorArgs(Test, "test")); | ||
before(() => { | ||
Sinon.stub(EndpointRegistry, "useBefore"); | ||
}); | ||
|
||
after(() => { | ||
this.endpointRegistryStub.restore(); | ||
// @ts-ignore | ||
EndpointRegistry.useBefore.restore(); | ||
}); | ||
|
||
afterEach(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useBefore.resetHistory(); | ||
}); | ||
|
||
it("should add the middleware on the use stack", () => { | ||
this.endpointRegistryStub.should.be.calledWithExactly(Test, "test", [Sinon.match.func]); | ||
// WHEN | ||
UseBefore(CustomMiddleware)(Test); | ||
|
||
// THEN | ||
Store.from(Test).get("middlewares").should.deep.eq({useBefore: [CustomMiddleware]}); | ||
}); | ||
}); | ||
describe("when the decorator is use on a method", () => { | ||
before(() => { | ||
Sinon.stub(EndpointRegistry, "useBefore"); | ||
}); | ||
|
||
after(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useBefore.restore(); | ||
}); | ||
|
||
it("should return a descriptor", () => { | ||
this.returns.should.be.deep.eq(descriptorOf(Test, "test")); | ||
afterEach(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useBefore.resetHistory(); | ||
}); | ||
|
||
it("should add the middleware on the use stack", () => { | ||
// WHEN | ||
class Test { | ||
@UseBefore(CustomMiddleware) | ||
test() { | ||
} | ||
} | ||
|
||
// THEN | ||
EndpointRegistry.useBefore.should.be.calledWithExactly(prototypeOf(Test), "test", [CustomMiddleware]); | ||
}); | ||
}); | ||
describe("when the decorator is use in another way", () => { | ||
class Test { | ||
test() { | ||
} | ||
} | ||
|
||
describe("when the decorator is use on a class", () => { | ||
before(() => { | ||
this.returns = UseBefore(() => { | ||
})(Test); | ||
Sinon.stub(EndpointRegistry, "useBefore"); | ||
}); | ||
|
||
this.store = Store.from(Test).get("middlewares"); | ||
after(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useBefore.restore(); | ||
}); | ||
it("should add the middleware on the use stack", () => { | ||
expect(this.store.useBefore[0]).to.be.a("function"); | ||
|
||
afterEach(() => { | ||
// @ts-ignore | ||
EndpointRegistry.useBefore.resetHistory(); | ||
}); | ||
it("should return nothing", () => { | ||
expect(this.returns).to.eq(undefined); | ||
|
||
it("should add the middleware on the use stack", () => { | ||
// WHEN | ||
let actualError; | ||
try { | ||
UseBefore(CustomMiddleware)(Test, "property"); | ||
} catch (er) { | ||
actualError = er; | ||
} | ||
|
||
// THEN | ||
actualError.should.instanceOf(UnsupportedDecoratorType); | ||
actualError.message.should.eq("UseBefore cannot used as property.static at Test.property"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.