Skip to content

Commit b51608f

Browse files
[feature] add redirect method to context
1 parent 3fd31ed commit b51608f

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

__test__/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,13 @@ it("should throw timeout error", async () => {
8787
TimeoutError
8888
)
8989
})
90+
91+
it("should redirect to baidu", async () => {
92+
const { status, headers } = await test(async (ctx) => {
93+
ctx.redirect("https://baidu.com")
94+
})({
95+
method: "POST"
96+
})
97+
expect(status).toBe(301)
98+
expect(headers).toHaveProperty("Location", "https://baidu.com")
99+
})

src/context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ export class Context {
7575
public finish() {
7676
return this.res.finish()
7777
}
78+
79+
public redirect(newUrl: string) {
80+
this.status = 301
81+
this.setHeader("Location", newUrl)
82+
}
7883
}
7984

8085
export const createContext: CreateContextFunction = async (req, res, ctx) => {

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,7 @@ delayed-stream@~1.0.0:
17061706
delegates@^1.0.0:
17071707
version "1.0.0"
17081708
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
1709+
integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=
17091710

17101711
depd@~1.1.2:
17111712
version "1.1.2"

0 commit comments

Comments
 (0)