Skip to content
Open
Changes from 2 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
28 changes: 28 additions & 0 deletions src/content/docs/en/reference/modules/astro-actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
**Type:** <code>(\{ accept, input, handler \}) => <a href="#actionclient">ActionClient</a></code>
</p>

A utility to define new actions in the `src/actions/index.ts` file. This accepts a [`handler()`](#handler-property) function containing the server logic to run, and an optional [`input`](#input-validator) property to validate input parameters at runtime.

Check failure on line 41 in src/content/docs/en/reference/modules/astro-actions.mdx

View workflow job for this annotation

GitHub Actions / Check Links

Broken page link in src/content/docs/en/reference/modules/astro-actions.mdx, line 41: (#input-validator)

```ts title="src/actions/index.ts"
import { defineAction } from 'astro:actions';
Expand All @@ -65,7 +65,7 @@

A required function containing the server logic to run when the action is called. Data returned from the `handler()` is automatically serialized and sent to the caller.

The `handler()` is called with user input as its first argument. If an [`input`](#input-validator) validator is set, the user input will be validated before being passed to the handler. The second argument is [a subset of Astro's `context` object](#actionapicontext).

Check failure on line 68 in src/content/docs/en/reference/modules/astro-actions.mdx

View workflow job for this annotation

GitHub Actions / Check Links

Broken page link in src/content/docs/en/reference/modules/astro-actions.mdx, line 68: (#input-validator)

Return values are parsed using the [devalue library](https://github.com/Rich-Harris/devalue). This supports JSON values and instances of `Date()`, `Map()`, `Set()`, and `URL()`.

Expand Down Expand Up @@ -575,3 +575,31 @@
Represents the result of an action call:
* on success, `data` contains the output of the action and `error` is `undefined`.
* on failure, `error` contains an [`ActionError`](#actionerror) with validation errors or runtime errors, and `data` is `undefined`.

### `ActionInputSchema`

<p>

**Type:** `ZodType`
<Since v="5.15.4" />
</p>

A utility type that extracts the input's **zod schema type** from [an action handler](#defineaction). This can be useful if you need to reference an [action's validator]((#input-validator)) input type in your own type definitions.

Check failure on line 587 in src/content/docs/en/reference/modules/astro-actions.mdx

View workflow job for this annotation

GitHub Actions / Check Links

Broken page link in src/content/docs/en/reference/modules/astro-actions.mdx, line 587: (#input-validator)

Even if the action [accepts `form`](#accept-property), `ActionInputSchema` lets you get the input type as `object` rather than [`FormData`](https://developer.mozilla.org/docs/Web/API/FormData).

If the [`input` validator](#input-validator) is omitted, it returns `never`.

Check failure on line 591 in src/content/docs/en/reference/modules/astro-actions.mdx

View workflow job for this annotation

GitHub Actions / Check Links

Broken page link in src/content/docs/en/reference/modules/astro-actions.mdx, line 591: (#input-validator)

The following example uses `ActionInputSchema` on an action named `contact` to:
* Retrieve the **zod schema type** for the input of the action.
* Retrieve the expected **input type** of the action's validator.

```astro title="src/components/Form.astro" {4}
---
import { actions, ActionInputSchema } from 'astro:actions';
import { z } from 'astro/zod';

type ContactSchema = ActionInputSchema<typeof actions.contact>;
type ContactInput = z.input<ContactSchema>;
---
```
Loading