Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(context): single body overrides other returns #3800

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c.body() now has a type of Response & TypedResponse, so this change was made. The types are no longer compatible

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'>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yusukebe not sure if this should remain as unknown, could you advise pls?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@askorupskyy

unknown is OK. _data in TypedResponse should be unknown in this case.

<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>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is very good!

})
})
Loading