-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @fallthrough() decorator as well as global option to enable autom…
…atic fallthrough. By default, fallthrough is now off.
- Loading branch information
1 parent
2765a22
commit 93bb6fc
Showing
12 changed files
with
130 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {getMetadataArgsStorage} from "../index"; | ||
|
||
/** | ||
* Indicates that this route should fall through to further route handlers or middleware after being executed. | ||
* This is equivalent to a route handler calling next() with no parameters in Express. | ||
*/ | ||
export function Fallthrough(): Function { | ||
return function (object: Object, methodName: string) { | ||
getMetadataArgsStorage().responseHandlers.push({ | ||
type: "fallthrough", | ||
target: object.constructor, | ||
method: methodName | ||
}); | ||
}; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import "reflect-metadata"; | ||
import {Controller} from "../../src/decorator/Controller"; | ||
import {Fallthrough} from "../../src/decorator/Fallthrough"; | ||
import {UseAfter} from "../../src/decorator/UseAfter"; | ||
import {Get} from "../../src/decorator/Get"; | ||
import {createExpressServer, createKoaServer, getMetadataArgsStorage} from "../../src/index"; | ||
import {assertRequest} from "./test-utils"; | ||
const chakram = require("chakram"); | ||
const expect = chakram.expect; | ||
|
||
describe("fallthrough-decorator behavior", () => { | ||
let useAfter: boolean; | ||
|
||
beforeEach(() => { | ||
useAfter = undefined; | ||
}); | ||
|
||
before(() => { | ||
// reset metadata args storage | ||
getMetadataArgsStorage().reset(); | ||
|
||
@Controller() | ||
class FallthroughController { | ||
@Get("/books") | ||
@Fallthrough() | ||
@UseAfter(function(req: any, res: any, next: (err?: any) => any) { | ||
useAfter = true; | ||
}) | ||
getAll() { | ||
return "<html><body>All books</body></html>"; | ||
} | ||
} | ||
}); | ||
|
||
let expressApp: any, koaApp: any; | ||
before(done => expressApp = createExpressServer().listen(3001, done)); | ||
after(done => expressApp.close(done)); | ||
before(done => koaApp = createKoaServer().listen(3002, done)); | ||
after(done => koaApp.close(done)); | ||
|
||
describe("get should respond with proper status code, headers and body content, and should call after middleware", () => { | ||
assertRequest([3001, 3002], "get", "books", response => { | ||
expect(response).to.have.status(200); | ||
expect(response).to.have.header("content-type", "text/html; charset=utf-8"); | ||
expect(response.body).to.be.equal("<html><body>All books</body></html>"); | ||
expect(useAfter).to.be.equal(true); | ||
}); | ||
}); | ||
|
||
}); |
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