@gnosticdev/hono-actions 1.2.3 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -56
- package/dist/actions.d.ts +10 -1
- package/dist/index.js +8 -2332
- package/package.json +9 -4
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Astro Actions with Hono
|
|
1
|
+
# Astro Actions with Hono
|
|
2
2
|
|
|
3
3
|
Define server actions with built-in validation, error handling, and a pre-built hono client for calling the routes.
|
|
4
4
|
|
|
@@ -16,9 +16,7 @@ bun add @gnosticdev/hono-actions
|
|
|
16
16
|
|
|
17
17
|
This package requires:
|
|
18
18
|
|
|
19
|
-
- `astro`: ^5.13.
|
|
20
|
-
|
|
21
|
-
All other dependencies (`hono`, `valibot`, `@hono/valibot-validator`, etc.) are bundled with the integration.
|
|
19
|
+
- `astro`: ^5.13.0
|
|
22
20
|
|
|
23
21
|
## Setup
|
|
24
22
|
|
|
@@ -41,7 +39,7 @@ export default defineConfig({
|
|
|
41
39
|
|
|
42
40
|
### 2. Create your actions file
|
|
43
41
|
|
|
44
|
-
|
|
42
|
+
If not using a custom actions path, create a file at one of these locations:
|
|
45
43
|
|
|
46
44
|
- `src/server/actions.ts`
|
|
47
45
|
- `src/hono/actions.ts`
|
|
@@ -51,57 +49,61 @@ Create a file at one of these locations (the integration will auto-discover):
|
|
|
51
49
|
## Usage
|
|
52
50
|
|
|
53
51
|
```typescript
|
|
54
|
-
// src/
|
|
55
|
-
import { defineHonoAction
|
|
56
|
-
import
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
schema:
|
|
62
|
-
name:
|
|
52
|
+
// src/hono.ts (or any of the supported locations above)
|
|
53
|
+
import { defineHonoAction type HonoEnv } from '@gnosticdev/hono-actions/actions'
|
|
54
|
+
import { z } from 'astro/zod'
|
|
55
|
+
import { Hono } from 'hono'
|
|
56
|
+
|
|
57
|
+
// Define a POST action with Zod validation (no `path` option is used anymore)
|
|
58
|
+
export const myAction = defineHonoAction({
|
|
59
|
+
schema: z.object({
|
|
60
|
+
name: z.string()
|
|
63
61
|
}),
|
|
64
62
|
handler: async (input, ctx) => {
|
|
65
|
-
// input is automatically typed
|
|
63
|
+
// `input` is automatically typed from the schema
|
|
64
|
+
// `ctx` is a strongly-typed Hono Context with your `HonoEnv`
|
|
66
65
|
return { message: `Hello ${input.name}!` }
|
|
67
66
|
}
|
|
68
67
|
})
|
|
69
68
|
|
|
70
|
-
// Define
|
|
71
|
-
export const
|
|
72
|
-
|
|
73
|
-
schema: v.object({
|
|
74
|
-
name: v.string(),
|
|
75
|
-
email: v.pipe(v.string(), v.email())
|
|
76
|
-
}),
|
|
69
|
+
// Define another POST action
|
|
70
|
+
export const anotherAction = defineHonoAction({
|
|
71
|
+
schema: z.object({ name2: z.string() }),
|
|
77
72
|
handler: async (input, ctx) => {
|
|
78
|
-
// input is automatically typed based on schema
|
|
79
73
|
return {
|
|
80
|
-
|
|
81
|
-
email: input.email
|
|
74
|
+
message2: `Hello ${input.name2}!`
|
|
82
75
|
}
|
|
83
76
|
}
|
|
84
77
|
})
|
|
85
78
|
|
|
86
|
-
//
|
|
87
|
-
export const
|
|
88
|
-
path: '/error',
|
|
79
|
+
// Optional: Define an action without a schema (accepts any JSON)
|
|
80
|
+
export const noSchemaAction = defineHonoAction({
|
|
89
81
|
handler: async (input, ctx) => {
|
|
90
|
-
if (
|
|
82
|
+
if (!('name' in input)) {
|
|
91
83
|
throw new HonoActionError({
|
|
92
|
-
message: '
|
|
93
|
-
code: '
|
|
84
|
+
message: 'Name is required',
|
|
85
|
+
code: 'INPUT_VALIDATION_ERROR'
|
|
94
86
|
})
|
|
95
87
|
}
|
|
96
|
-
return {
|
|
88
|
+
return { message: `Hello ${String((input as any).name)}!` }
|
|
97
89
|
}
|
|
98
90
|
})
|
|
99
91
|
|
|
100
|
-
//
|
|
92
|
+
// You can also define standard Hono routes (GET/PATCH/etc.), not just POST actions.
|
|
93
|
+
// This is useful where standard Astro actions are POST-only.
|
|
94
|
+
const app = new Hono<HonoEnv>()
|
|
95
|
+
const getRoute = app.get('/', (c) => c.json({ message: 'Hi from a get route' }))
|
|
96
|
+
|
|
97
|
+
// Export all actions and routes in a single `honoActions` object.
|
|
98
|
+
// Each key becomes the route name under your basePath, e.g.:
|
|
99
|
+
// - POST /api/myAction
|
|
100
|
+
// - POST /api/anotherAction
|
|
101
|
+
// - GET /api/getRoute
|
|
101
102
|
export const honoActions = {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
myAction,
|
|
104
|
+
anotherAction,
|
|
105
|
+
noSchemaAction,
|
|
106
|
+
getRoute
|
|
105
107
|
}
|
|
106
108
|
```
|
|
107
109
|
|
|
@@ -110,22 +112,22 @@ export const honoActions = {
|
|
|
110
112
|
```typescript
|
|
111
113
|
// src/pages/example.astro or any .astro file
|
|
112
114
|
---
|
|
113
|
-
import { honoClient } from '@gnosticdev/hono-actions/client'
|
|
115
|
+
import { honoClient, parseResponse } from '@gnosticdev/hono-actions/client'
|
|
114
116
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
})
|
|
117
|
+
// Call a POST action
|
|
118
|
+
const { data: actionRes } = await parseResponse(
|
|
119
|
+
await honoClient.api.myAction.$post({ json: { name: 'John' } })
|
|
120
|
+
)
|
|
118
121
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
console.error(await response.text()) // Error message
|
|
124
|
-
}
|
|
122
|
+
// Call a GET route
|
|
123
|
+
const { message } = await parseResponse(
|
|
124
|
+
await honoClient.api.getRoute.$get()
|
|
125
|
+
)
|
|
125
126
|
---
|
|
126
127
|
|
|
127
128
|
<div>
|
|
128
|
-
{
|
|
129
|
+
{actionRes && <p>{actionRes.message}</p>}
|
|
130
|
+
<p>{message}</p>
|
|
129
131
|
</div>
|
|
130
132
|
```
|
|
131
133
|
|
|
@@ -137,10 +139,9 @@ import { honoClient } from '@gnosticdev/hono-actions/client'
|
|
|
137
139
|
|
|
138
140
|
// Make requests from the browser
|
|
139
141
|
const handleSubmit = async (formData: FormData) => {
|
|
140
|
-
const response = await honoClient.
|
|
142
|
+
const response = await honoClient.api.anotherAction.$post({
|
|
141
143
|
json: {
|
|
142
|
-
|
|
143
|
-
email: formData.get('email') as string
|
|
144
|
+
name2: formData.get('name') as string
|
|
144
145
|
}
|
|
145
146
|
})
|
|
146
147
|
|
|
@@ -156,11 +157,12 @@ const handleSubmit = async (formData: FormData) => {
|
|
|
156
157
|
|
|
157
158
|
## Package Structure
|
|
158
159
|
|
|
159
|
-
This package provides
|
|
160
|
+
This package provides these entry points:
|
|
160
161
|
|
|
161
|
-
- **`@gnosticdev/hono-actions
|
|
162
|
-
-
|
|
163
|
-
|
|
162
|
+
- **`@gnosticdev/hono-actions/actions`**: Action definition utilities (`defineHonoAction`, `HonoActionError`, `HonoEnv`)
|
|
163
|
+
- Used in your actions file(s)
|
|
164
|
+
- **`@gnosticdev/hono-actions/client`**: Pre-built Hono client and helpers (`honoClient`, `parseResponse`)
|
|
165
|
+
- Safe for browser and server environments
|
|
164
166
|
- **`@gnosticdev/hono-actions/integration`**: Astro integration
|
|
165
167
|
- Uses Node.js built-ins (fs, path)
|
|
166
168
|
- Only used in `astro.config.ts`
|
|
@@ -175,11 +177,12 @@ The integration accepts the following options:
|
|
|
175
177
|
## Features
|
|
176
178
|
|
|
177
179
|
- ✅ **Type-safe**: Full TypeScript support with automatic type inference
|
|
178
|
-
- ✅ **Validation**: Built-in request validation using
|
|
180
|
+
- ✅ **Validation**: Built-in request validation using Zod schemas
|
|
179
181
|
- ✅ **Error handling**: Custom error types and automatic error responses
|
|
180
182
|
- ✅ **Auto-discovery**: Automatically finds your actions file
|
|
181
183
|
- ✅ **Client generation**: Pre-built client with full type safety
|
|
182
184
|
- ✅ **Development**: Hot reload support during development
|
|
185
|
+
- ✅ **Flexible routing**: Define standard Hono routes (GET/PATCH/etc.) alongside POST actions
|
|
183
186
|
|
|
184
187
|
## Troubleshooting
|
|
185
188
|
|
|
@@ -188,7 +191,7 @@ The integration accepts the following options:
|
|
|
188
191
|
If you get an error that no actions were found, make sure:
|
|
189
192
|
|
|
190
193
|
1. Your actions file is in one of the supported locations
|
|
191
|
-
2. You export a `honoActions` object containing your actions
|
|
194
|
+
2. You export a `honoActions` object containing your actions and any Hono routes
|
|
192
195
|
3. The file path matches the `actionsPath` option if you specified one
|
|
193
196
|
|
|
194
197
|
### Type errors
|
package/dist/actions.d.ts
CHANGED
|
@@ -24,6 +24,15 @@ interface Bindings {
|
|
|
24
24
|
* HonoEnv is passed to the Hono context to provide types on `ctx.env`.
|
|
25
25
|
*
|
|
26
26
|
* We are using `HonoEnv` to avoid confusion with the Cloudflare types on `Env` -> which cooresponds to `Bindings`
|
|
27
|
+
*
|
|
28
|
+
* * **NOTE** For Cloudflare users, you can declare this in your src/env.d.ts file to get strong
|
|
29
|
+
* typing for `ctx.env`.
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* declare namespace App {
|
|
33
|
+
* interface Locals extends Runtime {}
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
27
36
|
*/
|
|
28
37
|
interface HonoEnv {
|
|
29
38
|
Bindings: Bindings;
|
|
@@ -58,7 +67,7 @@ type HonoActionParams<TSchema extends HonoActionSchema, TReturn, TEnv extends Ho
|
|
|
58
67
|
handler: (params: z.output<TSchema>, context: TContext extends infer Ctx ? Ctx : never) => Promise<TReturn>;
|
|
59
68
|
};
|
|
60
69
|
/**
|
|
61
|
-
* Defines a
|
|
70
|
+
* Defines a POST route with Zod validation for the request body.
|
|
62
71
|
*
|
|
63
72
|
* @param schema - The Zod schema for validation (optional).
|
|
64
73
|
* @param handler - The handler function for the action.
|