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:
parent
224ec367f3
commit
01277aa389
@ -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')
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user