fix(context): single body overrides other returns (#3800)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Anthony Skorupskyy · committed by GitHub · parent fa6f51b · Browse files · 3 files·+36 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: askorupskyy <50280805+rcbxd@users.noreply.github.com>
Diff stats
3 files+36 added−11 removed
- tests:+20 added−3 removed
- other:+16 added−8 removed
Provenance
- Commit
5af7b4f- Tree
fdbb29d- Author
Anthony Skorupskyy <50280805+askorupskyy@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fa6f51b
3 files changed+36 added−11 removed
modified:src/context.test.ts+3 added-3 removed
149 unmodified lines150 it('c.header() - clear the header', async () => {151 c.header('X-Foo', 'Bar')152 c.header('X-Foo', undefined)153 c.header('X-Foo2', 'Bar')154- let res = c.body('Hi')154+ const res = c.body('Hi')155 expect(res.headers.get('X-Foo')).toBe(null)156 c.header('X-Foo2', undefined)157- res = c.res158- expect(res.headers.get('X-Foo2')).toBe(null)157+ const res2 = c.body('Hi')158+ expect(res2.headers.get('X-Foo2')).toBe(null)159 })160 161 it('c.header() - clear the header when append is true', async () => {162 c.header('X-Foo', 'Bar', { append: true })354 unmodified linesmodified:src/context.ts+16 added-8 removed
114 unmodified lines115 * Interface for responding with a body.116 */117 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, init?: ResponseOrInit<ContentfulStatusCode>): Response122- (data: null, init?: ResponseOrInit): Response119+ <U extends ContentfulStatusCode>(data: Data, status?: U, headers?: HeaderRecord): Response &120+ TypedResponse<unknown, U, 'body'>121+ <U extends StatusCode>(data: null, status?: U, headers?: HeaderRecord): Response &122+ TypedResponse<null, U, 'body'>123+ <U extends ContentfulStatusCode>(data: Data, init?: ResponseOrInit<U>): Response &124+ TypedResponse<unknown, U, 'body'>125+ <U extends StatusCode>(data: null, init?: ResponseOrInit<U>): Response &126+ TypedResponse<null, U, 'body'>127 }128 129 /**130 * Interface for responding with text.595 unmodified lines726 * return c.body('Thank you for coming')727 * })728 * ```729 */726- body: BodyRespond = (data, arg?: StatusCode | RequestInit, headers?: HeaderRecord) => {727- return typeof arg === 'number'728- ? this.#newResponse(data, arg, headers)729- : this.#newResponse(data, arg)730+ body: BodyRespond = (731+ data: Data | null,732+ arg?: StatusCode | RequestInit,733+ headers?: HeaderRecord734+ ): ReturnType<BodyRespond> => {735+ return (736+ typeof arg === 'number' ? this.#newResponse(data, arg, headers) : this.#newResponse(data, arg)737+ ) as ReturnType<BodyRespond>738 }739 740 /**741 * `.text()` can render text as `Content-Type:text/plain`.124 unmodified linesmodified:src/types.test.ts+17 added
2326 unmodified lines2327 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+2332+ it('.body() not override other responses in hono client', async () => {2333+ const router = app.get('/', async (c) => {2334+ if (c.req.header('Content-Type') === 'application/json') {2335+ return c.text('Hello', 200)2336+ }2337+2338+ if (c.req.header('Content-Type') === 'application/x-www-form-urlencoded') {2339+ return c.body('Hello', 201)2340+ }2341+2342+ return c.body(null, 204)2343+ })2344+2345+ type Actual = ExtractSchema<typeof router>['/']['$get']['status']2346+ expectTypeOf<Actual>().toEqualTypeOf<204 | 201 | 200>()2347+ })2348 })