Skip to content

Commit

Permalink
fix(context): single body overrides other returns (#3800)
Browse files Browse the repository at this point in the history
Co-authored-by: askorupskyy <[email protected]>
  • Loading branch information
askorupskyy and askorupskyy authored Jan 5, 2025
1 parent fa6f51b commit 5af7b4f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ describe('Context', () => {
c.header('X-Foo', 'Bar')
c.header('X-Foo', undefined)
c.header('X-Foo2', 'Bar')
let res = c.body('Hi')
const res = c.body('Hi')
expect(res.headers.get('X-Foo')).toBe(null)
c.header('X-Foo2', undefined)
res = c.res
expect(res.headers.get('X-Foo2')).toBe(null)
const res2 = c.body('Hi')
expect(res2.headers.get('X-Foo2')).toBe(null)
})

it('c.header() - clear the header when append is true', async () => {
Expand Down
24 changes: 16 additions & 8 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ interface NewResponse {
*/
interface BodyRespond {
// if we return content, only allow the status codes that allow for returning the body
(data: Data, status?: ContentfulStatusCode, headers?: HeaderRecord): Response
(data: null, status?: StatusCode, headers?: HeaderRecord): Response
(data: Data, init?: ResponseOrInit<ContentfulStatusCode>): Response
(data: null, init?: ResponseOrInit): Response
<U extends ContentfulStatusCode>(data: Data, status?: U, headers?: HeaderRecord): Response &
TypedResponse<unknown, U, 'body'>
<U extends StatusCode>(data: null, status?: U, headers?: HeaderRecord): Response &
TypedResponse<null, U, 'body'>
<U extends ContentfulStatusCode>(data: Data, init?: ResponseOrInit<U>): Response &
TypedResponse<unknown, U, 'body'>
<U extends StatusCode>(data: null, init?: ResponseOrInit<U>): Response &
TypedResponse<null, U, 'body'>
}

/**
Expand Down Expand Up @@ -723,10 +727,14 @@ export class Context<
* })
* ```
*/
body: BodyRespond = (data, arg?: StatusCode | RequestInit, headers?: HeaderRecord) => {
return typeof arg === 'number'
? this.#newResponse(data, arg, headers)
: this.#newResponse(data, arg)
body: BodyRespond = (
data: Data | null,
arg?: StatusCode | RequestInit,
headers?: HeaderRecord
): ReturnType<BodyRespond> => {
return (
typeof arg === 'number' ? this.#newResponse(data, arg, headers) : this.#newResponse(data, arg)
) as ReturnType<BodyRespond>
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2328,4 +2328,21 @@ describe('status code', () => {
// @ts-expect-error 204 is not contentful status code
app.get('/', async (c) => c.html('<h1>title</h1>', { status: 204 }))
})

it('.body() not override other responses in hono client', async () => {
const router = app.get('/', async (c) => {
if (c.req.header('Content-Type') === 'application/json') {
return c.text('Hello', 200)
}

if (c.req.header('Content-Type') === 'application/x-www-form-urlencoded') {
return c.body('Hello', 201)
}

return c.body(null, 204)
})

type Actual = ExtractSchema<typeof router>['/']['$get']['status']
expectTypeOf<Actual>().toEqualTypeOf<204 | 201 | 200>()
})
})

0 comments on commit 5af7b4f

Please sign in to comment.