From 01277aa389aff302992805c1b9f03d03cb70f6df Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Fri, 1 Nov 2024 18:04:33 +0900 Subject: [PATCH] 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> --- src/context.test.ts | 6 ++++++ src/context.ts | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/context.test.ts b/src/context.test.ts index ee02296b..62285260 100644 --- a/src/context.test.ts +++ b/src/context.test.ts @@ -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') diff --git a/src/context.ts b/src/context.ts index af47d958..b8f0e4b4 100644 --- a/src/context.ts +++ b/src/context.ts @@ -819,11 +819,11 @@ export class Context< * ``` */ redirect = ( - location: string, + location: string | URL, status?: T ): Response & TypedResponse => { this.#headers ??= new Headers() - this.#headers.set('Location', location) + this.#headers.set('Location', String(location)) return this.newResponse(null, status ?? 302) as any }