Skip to content

Commit

Permalink
feat(context): allow URL object on c.redirect() (#3609)
Browse files Browse the repository at this point in the history
* feat(context): allow URL object on `c.redirect()`

* Update src/context.ts

Co-authored-by: EdamAmex <[email protected]>

---------

Co-authored-by: EdamAmex <[email protected]>
  • Loading branch information
cometkim and EdamAme-x authored Nov 1, 2024
1 parent 224ec36 commit 01277aa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ describe('Context', () => {
expect(res.headers.get('Location')).toBe('https://example.com/destination')
})

it('c.redirect() w/ URL', async () => {
const res = c.redirect(new URL('/destination', 'https://example.com'))
expect(res.status).toBe(302)
expect(res.headers.get('Location')).toBe('https://example.com/destination')
})

it('c.header()', async () => {
c.header('X-Foo', 'Bar')
const res = c.body('Hi')
Expand Down
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,11 +819,11 @@ export class Context<
* ```
*/
redirect = <T extends RedirectStatusCode = 302>(
location: string,
location: string | URL,
status?: T
): Response & TypedResponse<undefined, T, 'redirect'> => {
this.#headers ??= new Headers()
this.#headers.set('Location', location)
this.#headers.set('Location', String(location))
return this.newResponse(null, status ?? 302) as any
}

Expand Down

0 comments on commit 01277aa

Please sign in to comment.