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
Provenance
- Commit
efc8a46- Tree
5e50321- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
b1335f9
2 files changed+56 added−13 removed
modified:src/context.ts+17 added-13 removed
106 unmodified lines107 * Interface for creating a new response.108 */109 interface NewResponse {110 (data: Data | null, status?: StatusCode, headers?: HeaderRecord): Response111- (data: Data | null, init?: ResponseInit): Response111+ (data: Data | null, init?: ResponseOrInit): Response112 }113 114 /**115 * Interface for responding with a body.1 unmodified line117 interface 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 }124 125 /**126 * Interface for responding with text.15 unmodified lines142 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 }149 150 /**22 unmodified lines173 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 }180 181 /**31 unmodified lines213 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 }222 223 /**224 * Options for configuring the context.33 unmodified lines258 | Record<ResponseHeader, string>259 | Record<string, string>260 | Headers261 260-interface ResponseInit {262+interface ResponseInit<T extends StatusCode = StatusCode> {263 headers?: ResponseHeadersInit262- status?: number264+ status?: T265 statusText?: string266 }267 268+type ResponseOrInit<T extends StatusCode = StatusCode> = ResponseInit<T> | Response269+270 export const TEXT_PLAIN = 'text/plain; charset=UTF-8'271 272 /**273 * Sets the headers of a response.361 unmodified lines635 }636 637 #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 lines742 * ```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 lines780 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 lines794 }795 796 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 linesmodified:src/types.test.ts+39 added
2245 unmodified lines2246 }2247 type verify = Expect<Equal<Expected, Actual>>2248 })2249 })2250+2251 describe('generic typed variables', () => {2252 const okHelper = (c: Context) => {2253 return <TData>(data: TData) => c.json({ data })2254 }16 unmodified lines2271 type Expected = { data: string }2272 expectTypeOf<Actual>().toEqualTypeOf<Expected>()2273 })2274 })2275+2276 describe('status code', () => {2277 const app = new Hono()2278 2279 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 })2284+2285 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 })2290+2291 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 })2296+2297+ 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+ })2310+2311+ 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+ })2317+2318+ 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+ })2324+2325+ 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 })