Skip to content

Commit

Permalink
allow URL object on c.redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Nov 1, 2024
1 parent ae99d86 commit 94d3d86
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const makeResponseHeaderImmutable = (res: Response) => {
},
get(target, prop) {
if (prop === 'set') {
return function () {
return function() {
throw new TypeError('Cannot modify headers: Headers are immutable')
}
}
Expand Down 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 Expand Up @@ -306,7 +312,7 @@ describe('event and executionCtx', () => {
expect(() => c.executionCtx).not.toThrowError()
c.executionCtx.passThroughOnException()
expect(pathThroughOnException).toHaveBeenCalled()
const asyncFunc = async () => {}
const asyncFunc = async () => { }
c.executionCtx.waitUntil(asyncFunc())
expect(waitUntil).toHaveBeenCalled()
})
Expand Down
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,11 +820,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', location.toString())
return this.newResponse(null, status ?? 302) as any
}

Expand Down

0 comments on commit 94d3d86

Please sign in to comment.