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
Provenance
- Commit
448a002- Tree
fc79b8b- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
5beac4e
3 files changed+61 added−4 removed
modified:src/client/client.test.ts+55 added-1 removed
8 unmodified lines9 import { parse } from '../utils/cookie'10 import type { Equal, Expect } from '../utils/types'11 import { validator } from '../validator'12 import { hc } from './client'13-import type { InferRequestType, InferResponseType } from './types'13+import type { ClientResponse, InferRequestType, InferResponseType } from './types'14 15 describe('Basic - JSON', () => {16 const app = new Hono()17 1062 unmodified lines1080 type Expected = Promise<string>1081 type verify = Expect<Equal<Expected, Actual>>1082 })1083 })1084+1085+describe('Redirect response - only types', () => {1086+ const server = setupServer(1087+ http.get('http://localhost/', async () => {1088+ return HttpResponse.redirect('/')1089+ })1090+ )1091+1092+ beforeAll(() => server.listen())1093+ afterEach(() => server.resetHandlers())1094+ afterAll(() => server.close())1095+1096+ 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+ })1108+1109+ const client = hc<typeof app>('http://localhost/')1110+ const req = client.index.$get1111+1112+ 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+ })1121+1122+ 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 lines715 * 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 }727 728 /**729 * `.notFound()` can return the Not Found Response.14 unmodified linesmodified:src/types.ts+1 added-1 removed
1739 unmodified lines1740 ////// TypedResponse //////1741 ////// //////1742 ////////////////////////////////////////1743 1744-export type KnownResponseFormat = 'json' | 'text'1744+export type KnownResponseFormat = 'json' | 'text' | 'redirect'1745 export type ResponseFormat = KnownResponseFormat | string1746 1747 export type TypedResponse<1748 T = unknown,103 unmodified lines