0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 02:07:30 +01:00

feat(context): allow URL object on c.redirect() (#3609)

* feat(context): allow URL object on `c.redirect()`

* Update src/context.ts

Co-authored-by: EdamAmex <121654029+EdamAme-x@users.noreply.github.com>

---------

Co-authored-by: EdamAmex <121654029+EdamAme-x@users.noreply.github.com>
This commit is contained in:
Hyeseong Kim 2024-11-01 18:04:33 +09:00 committed by GitHub
parent 224ec367f3
commit 01277aa389
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -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')

View File

@ -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
}