Astro 4.15.0
Astro Actions が安定版になった。
https://astro.build/blog/astro-4150/
Astro Actions は、簡素な記述で型安全なデータ取得、JSON 解析、入力検証を自動的に処理する。次のように利用できる。
ts
// src/actions/index.ts
import { defineAction } from "astro:actions";
import { z } from "astro:schema";
export const server = {
newsletter: defineAction({
input: z.object({ email: z.string().email() }),
handler: async (input) => {
return {
error: null,
};
},
}),
};astro
---
// src/pages/newsletter.astro
import { actions } from "astro:actions";
const result = Astro.getActionResult(actions.newsletter);
---
{result && !result.error && <p>Thanks for signing up!</p>}
<form method="POST" action={actions.newsletter}>
<input type="email" name="email" />
<button>Sign up</button>
</form>