-
Notifications
You must be signed in to change notification settings - Fork 317
feat: add event handler generics for typed request body and query #417
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9ba3da5
feat: add event handler generic for typed input body/query
danielroe 1d34f8f
style: lint
danielroe 5d72410
Merge branch 'main' (excluding body.ts)
pi0 ac3fceb
add example readTypedBody from same impl (wip)
pi0 37126b8
[autofix.ci] apply automated fixes
autofix-ci[bot] 4d10c08
small updates
pi0 f0fb84c
Merge branch 'main' into feat/input-types
pi0 db9a0b7
add type safety for readValidatedBody
pi0 b52066c
gitignore tsconfig.vitest-temp.json
pi0 0d84a82
update body.ts
pi0 414d97a
update request.ts
pi0 aa3fcaa
update type tests
pi0 db28d00
refactor: InferEventInput utility
pi0 7d1939b
fix query default
pi0 0519793
update (unsed) default for H3Event generic
pi0 dd5ecf3
refactor: input => request
pi0 532df6c
refactor: return ~> response
pi0 721932f
use EventHandlerResponse when possible
pi0 3a9fd8b
add typed validator tests
pi0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { describe, it, expectTypeOf } from "vitest"; | ||
| import { eventHandler, H3Event, readBody, getQuery } from "../src"; | ||
|
|
||
| describe("types for event handlers", () => { | ||
| it("return type test", () => { | ||
| const handler = eventHandler(() => { | ||
| return { | ||
| foo: "bar", | ||
| }; | ||
| }); | ||
|
|
||
| expectTypeOf(handler({} as H3Event)).toEqualTypeOf< | ||
| { foo: string } | Promise<{ foo: string }> | ||
| >(); | ||
| }); | ||
|
|
||
| it("input type test", () => { | ||
| eventHandler<{ body: { id: string } }>(async (event) => { | ||
| const body = await readBody(event); | ||
| expectTypeOf(body).toEqualTypeOf<{ id: string }>(); | ||
| expectTypeOf(getQuery(event)).toBeUnknown(); | ||
|
|
||
| return null; | ||
| }); | ||
|
|
||
| eventHandler<{ query: { id: string } }>((event) => { | ||
| const query = getQuery(event); | ||
| expectTypeOf(query).toEqualTypeOf<{ id: string }>(); | ||
|
|
||
| return null; | ||
| }); | ||
| }); | ||
|
|
||
| it("allows backwards compatible generic for eventHandler definition", () => { | ||
| const handler = eventHandler<string>(() => { | ||
| return ""; | ||
| }); | ||
| expectTypeOf(handler({} as H3Event)).toEqualTypeOf< | ||
| string | Promise<string> | ||
| >(); | ||
| }); | ||
|
|
||
| // For backwards compatibility - this should likely become `unknown` in future | ||
| it("input types aren't applied when omitted", () => { | ||
| eventHandler(async (event) => { | ||
| const body = await readBody(event); | ||
| expectTypeOf(body).toBeAny(); | ||
| expectTypeOf(getQuery(event)).toBeAny(); | ||
|
|
||
| return null; | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.