-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: Add ActionInputSchema utility type
#14698
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7a6f7de
feat: Add ActionInputSchema utility type
mauriciabad 1b69b4d
Update changeset
mauriciabad 398c61c
Merge branch 'main' into patch-1
mauriciabad 7c9dce5
Merge branch 'main' into patch-1
mauriciabad dcf7aaa
Merge branch 'main' into patch-1
florian-lefebvre 4aa9846
Add tests
mauriciabad f3703f0
Merge branch 'main' into patch-1
mauriciabad 60d37df
Add JS Doc
mauriciabad 24ac968
fix
florian-lefebvre 8d2c050
Merge branch 'main' into patch-1
mauriciabad 1a962d7
Update .changeset/poor-loops-boil.md
florian-lefebvre 7a7a8bd
Update .changeset/poor-loops-boil.md
florian-lefebvre 4ddb707
Apply suggestion from @florian-lefebvre
florian-lefebvre 81e0caa
Update .changeset/poor-loops-boil.md
florian-lefebvre e7cc58a
Apply suggestion from @florian-lefebvre
florian-lefebvre 67c9bb5
Apply suggestion from @florian-lefebvre
florian-lefebvre 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| 'astro': minor | ||
| --- | ||
|
|
||
| Adds the `ActionInputSchema` utility type to automatically infer the TypeScript type of an action's input based on its Zod schema | ||
|
|
||
| For example, this type can be used to retrieve the input type of a form action: | ||
|
|
||
| ```ts | ||
| import { type ActionInputSchema, defineAction } from 'astro:actions'; | ||
| import { z } from 'astro/zod'; | ||
|
|
||
| const action = defineAction({ | ||
| accept: 'form', | ||
| input: z.object({ name: z.string() }), | ||
| handler: ({ name }) => ({ message: `Welcome, ${name}!` }), | ||
| }); | ||
|
|
||
| type Schema = ActionInputSchema<typeof action>; | ||
| // typeof z.object({ name: z.string() }) | ||
|
|
||
| type Input = z.input<Schema>; | ||
| // { name: string } | ||
| ``` |
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,51 @@ | ||
| import { describe, it } from 'node:test'; | ||
| import { expectTypeOf } from 'expect-type'; | ||
| import { type ActionInputSchema, defineAction } from '../../dist/actions/runtime/server.js'; | ||
| import { z } from '../../dist/zod.js'; | ||
|
|
||
| describe('ActionInputSchema', () => { | ||
| const acceptVariants = ['form', 'json', undefined] as const; | ||
|
|
||
| for (const accept of acceptVariants) { | ||
| describe(`accept = ${typeof accept === 'string' ? `'${accept}'` : accept}`, () => { | ||
| it('Infers action input schema', async () => { | ||
| const inputSchema = z.object({ | ||
| name: z.string(), | ||
| age: z.number(), | ||
| }); | ||
|
|
||
| const _action = defineAction({ | ||
| accept, | ||
| input: inputSchema, | ||
| handler: () => undefined, | ||
| }); | ||
|
|
||
| expectTypeOf<ActionInputSchema<typeof _action>>().toEqualTypeOf<typeof inputSchema>(); | ||
| }); | ||
|
|
||
| it('Infers action input value', async () => { | ||
| const schema = z.object({ | ||
| name: z.string(), | ||
| age: z.number(), | ||
| }); | ||
| const _action = defineAction({ | ||
| accept, | ||
| input: schema, | ||
| handler: () => undefined, | ||
| }); | ||
| expectTypeOf<z.input<ActionInputSchema<typeof _action>>>().toEqualTypeOf<{ | ||
| name: string; | ||
| age: number; | ||
| }>(); | ||
| }); | ||
|
|
||
| it('Infers action input schema when input is omitted', async () => { | ||
| const _action = defineAction({ | ||
| accept, | ||
| handler: () => undefined, | ||
| }); | ||
| expectTypeOf<ActionInputSchema<typeof _action>>().toBeNever; | ||
| }); | ||
| }); | ||
| } | ||
| }); |
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.