13 |
14 | > Server component for Astro (validation and state management)
15 |
16 |
17 | # Full feature server components for Astro.js
18 |
19 | This package is a framework for Astro.js that allows you to create forms and manage their state without any JavaScript.
20 |
21 | It also allows you to validate the form on the client side and server side, and protect against CSRF attacks.
22 |
23 | ### More features
24 | - JWT session management
25 | - Override response at runtime (useful for error handling)
26 | - Custom server validation with `zod`
27 | - Multiples app states at the same time
28 |
29 | # Show me the code
30 | ```astro
31 | ---
32 | import { Bind, BindForm, BButton, BInput } from "@astro-utils/forms/forms.js";
33 | import Layout from "../layouts/Layout.astro";
34 |
35 | const form = Bind();
36 | let showSubmitText: string;
37 |
38 | function formSubmit(){
39 | showSubmitText = `You name is ${form.name}, you are ${form.age} years old. `;
40 | }
41 | ---
42 |
43 |
44 | {showSubmitText}
45 |
46 |
What you name*
47 |
48 |
49 |
Enter age*
50 |
51 |
52 | Submit
53 |
54 |
55 | ```
56 |
57 | ## Usage
58 |
59 | ### Add the middleware to your server
60 |
61 | ```
62 | npm install @astro-utils/forms
63 | ```
64 |
65 | Add the middleware to your server
66 |
67 |
68 | `src/middleware.ts`
69 | ```ts
70 | import astroForms from "@astro-utils/forms";
71 | import {sequence} from "astro/middleware";
72 |
73 | export const onRequest = sequence(astroForms());
74 | ```
75 |
76 | ### Add to Layout
77 | Add the `WebForms` component in the layout
78 |
79 | `layouts/Layout.astro`
80 | ```astro
81 | ---
82 | import {WebForms} from '@astro-utils/forms/forms.js';
83 | ---
84 |
85 |
86 |
87 | ```
88 |
89 | ### Code Integration
90 | This changes astro behavior to allow the form to work, it ensure the components render by the order they are in the file.
91 |
92 | `astro.config.mjs`
93 | ```js
94 | import { defineConfig } from 'astro/config';
95 | import astroForms from "@astro-utils/forms/dist/integration.js";
96 |
97 | export default defineConfig({
98 | output: 'server',
99 | integrations: [astroForms]
100 | });
101 | ```
--------------------------------------------------------------------------------
/assets/logo.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/withastro-utils/utils/6cbdfd482d79d48fe00fc152bd5dcd13cb5ea607/assets/logo.jpeg
--------------------------------------------------------------------------------
/assets/logo.rounded.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/withastro-utils/utils/6cbdfd482d79d48fe00fc152bd5dcd13cb5ea607/assets/logo.rounded.png
--------------------------------------------------------------------------------
/examples/simple-form/.gitignore:
--------------------------------------------------------------------------------
1 | # build output
2 | dist/
3 |
4 | # generated types
5 | .astro/
6 |
7 | # dependencies
8 | node_modules/
9 |
10 | # logs
11 | npm-debug.log*
12 | yarn-debug.log*
13 | yarn-error.log*
14 | pnpm-debug.log*
15 |
16 | # environment variables
17 | .env
18 | .env.production
19 |
20 | # macOS-specific files
21 | .DS_Store
22 |
--------------------------------------------------------------------------------
/examples/simple-form/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 |
--------------------------------------------------------------------------------
/examples/simple-form/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/examples/simple-form/.idea/simple-form.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/examples/simple-form/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/examples/simple-form/.vscode/extensions.json:
--------------------------------------------------------------------------------
1 | {
2 | "recommendations": ["astro-build.astro-vscode"],
3 | "unwantedRecommendations": []
4 | }
5 |
--------------------------------------------------------------------------------
/examples/simple-form/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.2.0",
3 | "configurations": [
4 | {
5 | "command": "./node_modules/.bin/astro dev",
6 | "name": "Development server",
7 | "request": "launch",
8 | "type": "node-terminal"
9 | }
10 | ]
11 | }
12 |
--------------------------------------------------------------------------------
/examples/simple-form/README.md:
--------------------------------------------------------------------------------
1 | # Astro Starter Kit: Basics
2 |
3 | ```sh
4 | npm create astro@latest -- --template basics
5 | ```
6 |
7 | [](https://stackblitz.com/github/withastro/astro/tree/latest/examples/basics)
8 | [](https://codesandbox.io/p/sandbox/github/withastro/astro/tree/latest/examples/basics)
9 | [](https://codespaces.new/withastro/astro?devcontainer_path=.devcontainer/basics/devcontainer.json)
10 |
11 | > 🧑🚀 **Seasoned astronaut?** Delete this file. Have fun!
12 |
13 | 
14 |
15 | ## 🚀 Project Structure
16 |
17 | Inside of your Astro project, you'll see the following folders and files:
18 |
19 | ```text
20 | /
21 | ├── public/
22 | │ └── favicon.svg
23 | ├── src/
24 | │ ├── components/
25 | │ │ └── Card.astro
26 | │ ├── layouts/
27 | │ │ └── Layout.astro
28 | │ └── pages/
29 | │ └── index.astro
30 | └── package.json
31 | ```
32 |
33 | Astro looks for `.astro` or `.md` files in the `src/pages/` directory. Each page is exposed as a route based on its file name.
34 |
35 | There's nothing special about `src/components/`, but that's where we like to put any Astro/React/Vue/Svelte/Preact components.
36 |
37 | Any static assets, like images, can be placed in the `public/` directory.
38 |
39 | ## 🧞 Commands
40 |
41 | All commands are run from the root of the project, from a terminal:
42 |
43 | | Command | Action |
44 | | :------------------------ | :----------------------------------------------- |
45 | | `npm install` | Installs dependencies |
46 | | `npm run dev` | Starts local dev server at `localhost:4321` |
47 | | `npm run build` | Build your production site to `./dist/` |
48 | | `npm run preview` | Preview your build locally, before deploying |
49 | | `npm run astro ...` | Run CLI commands like `astro add`, `astro check` |
50 | | `npm run astro -- --help` | Get help using the Astro CLI |
51 |
52 | ## 👀 Want to learn more?
53 |
54 | Feel free to check [our documentation](https://docs.astro.build) or jump into our [Discord server](https://astro.build/chat).
55 |
--------------------------------------------------------------------------------
/examples/simple-form/astro.config.mjs:
--------------------------------------------------------------------------------
1 | import {defineConfig} from 'astro/config';
2 | import react from '@astrojs/react';
3 | import astroFormsDebug from "@astro-utils/forms/dist/integration.js";
4 |
5 | // https://astro.build/config
6 | export default defineConfig({
7 | output: "server",
8 | integrations: [react(), astroFormsDebug]
9 | });
10 |
--------------------------------------------------------------------------------
/examples/simple-form/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "simple-form",
3 | "description": "A simple form with validation",
4 | "type": "module",
5 | "version": "0.0.1",
6 | "scripts": {
7 | "dev": "astro dev",
8 | "start": "astro dev",
9 | "build": "astro check && astro build",
10 | "preview": "astro preview",
11 | "astro": "astro"
12 | },
13 | "dependencies": {
14 | "@astro-utils/express-endpoints": "^0.0.1",
15 | "@astro-utils/forms": "^0.0.1",
16 | "@astrojs/check": "^0.7.0",
17 | "@astrojs/react": "^3.6.0",
18 | "@types/react": "^18.3.3",
19 | "@types/react-dom": "^18.3.0",
20 | "astro": "^4.7.0",
21 | "bootstrap": "^5.3.2",
22 | "react": "^18.3.1",
23 | "react-dom": "^18.3.1",
24 | "reactstrap": "^9.2.1",
25 | "sleep-promise": "^9.1.0",
26 | "typescript": "^5.5.3",
27 | "zod": "^3.22.4"
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/examples/simple-form/public/favicon.svg:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/examples/simple-form/src/components/Counter.astro:
--------------------------------------------------------------------------------
1 | ---
2 | import {BButton} from '@astro-utils/forms/forms.js';
3 |
4 | function inc() {
5 | Astro.locals.session.count ??= 0;
6 | this.innerText = Astro.locals.session.count++;
7 | }
8 | ---
9 | Counter
10 |
--------------------------------------------------------------------------------
/examples/simple-form/src/components/ReactComponent.tsx:
--------------------------------------------------------------------------------
1 | export function ReactComponent(){
2 | return