@gnosticdev/hono-actions 1.2.4 → 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 CHANGED
@@ -1,4 +1,4 @@
1
- # Astro Actions with Hono and Valibot
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.3
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
- Create a file at one of these locations (the integration will auto-discover):
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/server/actions.ts
55
- import { defineHonoAction, HonoActionError } from '@gnosticdev/hono-actions'
56
- import * as v from 'valibot'
57
-
58
- // Define a simple action
59
- export const simpleAction = defineHonoAction({
60
- path: '/simple',
61
- schema: v.object({
62
- name: v.string()
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 based on schema
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 an action with validation
71
- export const validatedAction = defineHonoAction({
72
- path: '/validated',
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
- message: `Hello ${input.name}!`,
81
- email: input.email
74
+ message2: `Hello ${input.name2}!`
82
75
  }
83
76
  }
84
77
  })
85
78
 
86
- // Use custom error handling
87
- export const errorAction = defineHonoAction({
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 (someCondition) {
82
+ if (!('name' in input)) {
91
83
  throw new HonoActionError({
92
- message: 'Custom error message',
93
- code: 'EXTERNAL_API_ERROR'
84
+ message: 'Name is required',
85
+ code: 'INPUT_VALIDATION_ERROR'
94
86
  })
95
87
  }
96
- return { success: true }
88
+ return { message: `Hello ${String((input as any).name)}!` }
97
89
  }
98
90
  })
99
91
 
100
- // Export all actions in a honoActions object
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
- simpleAction,
103
- validatedAction,
104
- errorAction
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
- const response = await honoClient.simpleAction.$post({
116
- json: { name: 'John' }
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
- let result = null
120
- if (response.ok) {
121
- result = await response.json() // { message: 'Hello John!' }
122
- } else {
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
- {result && <p>{result.message}</p>}
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.validatedAction.$post({
142
+ const response = await honoClient.api.anotherAction.$post({
141
143
  json: {
142
- name: formData.get('name') as string,
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 two main entry points:
160
+ This package provides these entry points:
160
161
 
161
- - **`@gnosticdev/hono-actions`** (default): Action definition utilities (`defineHonoAction`, `HonoActionError`, types)
162
- - Safe for browser environments
163
- - Used in your action files and client-side code
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 Valibot schemas
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 type-safe Hono action using Zod for input validation.
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.
package/package.json CHANGED
@@ -56,5 +56,5 @@
56
56
  },
57
57
  "type": "module",
58
58
  "types": "./dist/index.d.ts",
59
- "version": "1.2.4"
59
+ "version": "1.2.5"
60
60
  }