-
-
Notifications
You must be signed in to change notification settings - Fork 635
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
feat(utils): specify detailed return type for parseBody #2771
Conversation
Uh, it wasn't easy. I'll think about it a bit. |
src/utils/body.test.ts
Outdated
@@ -1,4 +1,9 @@ | |||
import { parseBody } from './body' | |||
import type { BodyData } from './body' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! That's certainly better.
I made the change in eecfc43.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yusukebe How about this? |
Also added type to |
Hi @usualoma ! It would be nice to have the type definition determined by what the value of the option is. But I think there are two problems: 1. Annotations by IDE are not friendly.As you can see, it's not easy to understand type definitions for the user, though it should annotate 2. I intended the first argument for the generic be parsed object typesI designed it so that the first argument of const result = await parseBody<{ key1: string, key2: string }>()
// ^ result should be { key1: string, key2: string } But with this PR, it will be typed for the options. This will break the spec. I'm investigating how to fix these problems, but I can't find a good way now 😦 FYI You may already know that https://github.com/honojs/hono/blob/main/src/utils/types.ts#L40 https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts |
@yusukebe Thank you!
Sorry, the very first push, 288e050, had that problem, but I think it was fixed in the subsequent 3b178fc. I also added an explicit test at a6c300d. That should work, right?
This is indeed true. I don't know how to solve it and will investigate. |
a2b9d42
to
e6aa84e
Compare
Awesome!! Let's go with this! |
In 1c18227, variable names were changed to appropriate names. My work is now complete! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@usualoma Great! Merging now. |
What is this?
More restrictive on the type of value returned from
parseBody()
.Specification Changes
In v4.3.x, BodyData was defined as follows
hono/src/utils/body.ts
Line 3 in 75a7a09
For applications using it directly, this change results in the following (
(string | File)[]
is no longer included)To get the same result as in v4.3.x, you need to do the following.
Although there are some changes from v4.3.x as described above, I think it is useful to restrict the type of
parseBody()
, so I think it is better to include this change.The author should do the following, if applicable
bun denoify
to generate files for Denobun run format:fix && bun run lint:fix
to format the code