Skip to content
Myr

feat(context): `ResponseInit` accepts generics `StatusCode` for `status` (#3770)

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 b1335f9 · Browse files · 2 files+56 added−13 removed
This commit

State

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

Diff stats

2 files+56 added−13 removed

  • tests:+39 added−0 removed
  • other:+17 added−13 removed

2 files+56 added−13 removed

modified:src/context.ts+17 added-13 removed
106 unmodified lines
107 * Interface for creating a new response.108 */109interface NewResponse {110 (data: Data | null, status?: StatusCode, headers?: HeaderRecord): Response111 (data: Data | null, init?: ResponseInit): Response111 (data: Data | null, init?: ResponseOrInit): Response112}113114/**115 * Interface for responding with a body.
1 unmodified line
117interface BodyRespond {118 // if we return content, only allow the status codes that allow for returning the body119 (data: Data, status?: ContentfulStatusCode, headers?: HeaderRecord): Response120 (data: null, status?: StatusCode, headers?: HeaderRecord): Response121 (data: Data | null, init?: ResponseInit): Response121 (data: Data, init?: ResponseOrInit<ContentfulStatusCode>): Response122 (data: null, init?: ResponseOrInit): Response123}124125/**126 * Interface for responding with text.
15 unmodified lines
142 headers?: HeaderRecord143 ): Response & TypedResponse<T, U, 'text'>144 <T extends string, U extends ContentfulStatusCode = ContentfulStatusCode>(145 text: T,145 init?: ResponseInit146 init?: ResponseOrInit<U>147 ): Response & TypedResponse<T, U, 'text'>148}149150/**
22 unmodified lines
173 T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue,174 U extends ContentfulStatusCode = ContentfulStatusCode175 >(176 object: T,176 init?: ResponseInit177 init?: ResponseOrInit<U>178 ): JSONRespondReturn<T, U>179}180181/**
31 unmodified lines
213 html: T,214 status?: ContentfulStatusCode,215 headers?: HeaderRecord216 ): T extends string ? Response : Promise<Response>216 <T extends string | Promise<string>>(html: T, init?: ResponseInit): T extends string217 ? Response218 : Promise<Response>217 <T extends string | Promise<string>>(218 html: T,219 init?: ResponseOrInit<ContentfulStatusCode>220 ): T extends string ? Response : Promise<Response>221}222223/**224 * Options for configuring the context.
33 unmodified lines
258 | Record<ResponseHeader, string>259 | Record<string, string>260 | Headers261260interface ResponseInit {262interface ResponseInit<T extends StatusCode = StatusCode> {263 headers?: ResponseHeadersInit262 status?: number264 status?: T265 statusText?: string266}267268type ResponseOrInit<T extends StatusCode = StatusCode> = ResponseInit<T> | Response269270export const TEXT_PLAIN = 'text/plain; charset=UTF-8'271272/**273 * Sets the headers of a response.
361 unmodified lines
635 }636637 #newResponse(638 data: Data | null,635 arg?: StatusCode | ResponseInit,639 arg?: StatusCode | ResponseOrInit,640 headers?: HeaderRecord641 ): Response {642 // Optimized643 if (this.#isFresh && !headers && !arg && this.#status === 200) {
98 unmodified lines
742 * ```743 */744 text: TextRespond = (745 text: string,742 arg?: ContentfulStatusCode | ResponseInit,746 arg?: ContentfulStatusCode | ResponseOrInit,747 headers?: HeaderRecord748 ): ReturnType<TextRespond> => {749 // If the header is empty, return Response immediately.750 // Content-Type will be added automatically as `text/plain`.
29 unmodified lines
780 T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue,781 U extends ContentfulStatusCode = ContentfulStatusCode782 >(783 object: T,780 arg?: U | ResponseInit,784 arg?: U | ResponseOrInit<U>,785 headers?: HeaderRecord786 ): JSONRespondReturn<T, U> => {787 const body = JSON.stringify(object)788 this.#preparedHeaders ??= {}
5 unmodified lines
794 }795796 html: HTMLRespond = (797 html: string | Promise<string>,794 arg?: ContentfulStatusCode | ResponseInit,798 arg?: ContentfulStatusCode | ResponseOrInit<ContentfulStatusCode>,799 headers?: HeaderRecord800 ): Response | Promise<Response> => {801 this.#preparedHeaders ??= {}802 this.#preparedHeaders['content-type'] = 'text/html; charset=UTF-8'
55 unmodified lines
modified:src/types.test.ts+39 added
2245 unmodified lines
2246 }2247 type verify = Expect<Equal<Expected, Actual>>2248 })2249})22502251describe('generic typed variables', () => {2252 const okHelper = (c: Context) => {2253 return <TData>(data: TData) => c.json({ data })2254 }
16 unmodified lines
2271 type Expected = { data: string }2272 expectTypeOf<Actual>().toEqualTypeOf<Expected>()2273 })2274})22752276describe('status code', () => {2277 const app = new Hono()22782279 it('should only allow to return .json() with contentful status codes', async () => {2280 const route = app.get('/', async (c) => c.json({}))2281 type Actual = ExtractSchema<typeof route>['/']['$get']['status']2282 expectTypeOf<Actual>().toEqualTypeOf<ContentfulStatusCode>()2283 })22842285 it('should only allow to return .body(null) with all status codes', async () => {2286 const route = app.get('/', async (c) => c.body(null))2287 type Actual = ExtractSchema<typeof route>['/']['$get']['status']2288 expectTypeOf<Actual>().toEqualTypeOf<StatusCode>()2289 })22902291 it('should only allow to return .text() with contentful status codes', async () => {2292 const route = app.get('/', async (c) => c.text('whatever'))2293 type Actual = ExtractSchema<typeof route>['/']['$get']['status']2294 expectTypeOf<Actual>().toEqualTypeOf<ContentfulStatusCode>()2295 })22962297 it('should throw type error when .json({}) is used with contentless status codes', async () => {2298 // @ts-expect-error 204 is not contentful status code2299 app.get('/', async (c) => c.json({}, 204))2300 app.get('/', async (c) =>2301 c.json(2302 {},2303 // @ts-expect-error 204 is not contentful status code2304 {2305 status: 204,2306 }2307 )2308 )2309 })23102311 it('should throw type error when .body(content) is used with contentless status codes', async () => {2312 // @ts-expect-error 204 is not contentful status code2313 app.get('/', async (c) => c.body('content', 204))2314 // @ts-expect-error 204 is not contentful status code2315 app.get('/', async (c) => c.body('content', { status: 204 }))2316 })23172318 it('should throw type error when .text(content) is used with contentless status codes', async () => {2319 // @ts-expect-error 204 is not contentful status code2320 app.get('/', async (c) => c.text('content', 204))2321 // @ts-expect-error 204 is not contentful status code2322 app.get('/', async (c) => c.text('content', { status: 204 }))2323 })23242325 it('should throw type error when .html(content) is used with contentless status codes', async () => {2326 // @ts-expect-error 204 is not contentful status code2327 app.get('/', async (c) => c.html('<h1>title</h1>', 204))2328 // @ts-expect-error 204 is not contentful status code2329 app.get('/', async (c) => c.html('<h1>title</h1>', { status: 204 }))2330 })2331})