Skip to content
Myr

feat(context): `c.redirect()` supports `TypedResponse` (#2908)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 5beac4e · Browse files · 3 files+61 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+61 added−4 removed

  • tests:+55 added−1 removed
  • other:+6 added−3 removed

3 files+61 added−4 removed

modified:src/client/client.test.ts+55 added-1 removed
8 unmodified lines
9import { parse } from '../utils/cookie'10import type { Equal, Expect } from '../utils/types'11import { validator } from '../validator'12import { hc } from './client'13import type { InferRequestType, InferResponseType } from './types'13import type { ClientResponse, InferRequestType, InferResponseType } from './types'1415describe('Basic - JSON', () => {16 const app = new Hono()17
1062 unmodified lines
1080 type Expected = Promise<string>1081 type verify = Expect<Equal<Expected, Actual>>1082 })1083})10841085describe('Redirect response - only types', () => {1086 const server = setupServer(1087 http.get('http://localhost/', async () => {1088 return HttpResponse.redirect('/')1089 })1090 )10911092 beforeAll(() => server.listen())1093 afterEach(() => server.resetHandlers())1094 afterAll(() => server.close())10951096 const condition = () => true1097 const app = new Hono().get('/', async (c) => {1098 const ok = condition()1099 const temporary = condition()1100 if (ok) {1101 return c.json({ ok: true }, 200)1102 }1103 if (temporary) {1104 return c.redirect('/302')1105 }1106 return c.redirect('/301', 301)1107 })11081109 const client = hc<typeof app>('http://localhost/')1110 const req = client.index.$get11111112 it('Should infer request type the type correctly', () => {1113 type Actual = InferResponseType<typeof req>1114 type Expected =1115 | {1116 ok: boolean1117 }1118 | undefined1119 type verify = Expect<Equal<Expected, Actual>>1120 })11211122 it('Should infer response type correctly', async () => {1123 const res = await req()1124 if (res.ok) {1125 const data = await res.json()1126 expectTypeOf(data).toMatchTypeOf({ ok: true })1127 }1128 if (res.status === 301) {1129 type Expected = ClientResponse<undefined, 301, 'redirect'>1130 type verify = Expect<Equal<Expected, typeof res>>1131 }1132 if (res.status === 302) {1133 type Expected = ClientResponse<undefined, 302, 'redirect'>1134 type verify = Expect<Equal<Expected, typeof res>>1135 }1136 })1137})
modified:src/context.ts+5 added-2 removed
714 unmodified lines
715 * return c.redirect('/', 301)716 * })717 * ```718 */719 redirect = (location: string, status: RedirectStatusCode = 302): Response => {719 redirect = <T extends RedirectStatusCode = 302>(720 location: string,721 status?: T722 ): Response & TypedResponse<undefined, T, 'redirect'> => {723 this.#headers ??= new Headers()724 this.#headers.set('Location', location)722 return this.newResponse(null, status)725 return this.newResponse(null, status ?? 302) as any726 }727728 /**729 * `.notFound()` can return the Not Found Response.
14 unmodified lines
modified:src/types.ts+1 added-1 removed
1739 unmodified lines
1740////// TypedResponse //////1741////// //////1742////////////////////////////////////////17431744export type KnownResponseFormat = 'json' | 'text'1744export type KnownResponseFormat = 'json' | 'text' | 'redirect'1745export type ResponseFormat = KnownResponseFormat | string17461747export type TypedResponse<1748 T = unknown,
103 unmodified lines