@gnosticdev/hono-actions 2.0.6 → 2.0.7
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 +89 -0
- package/dist/actions.d.ts +6 -2
- package/dist/actions.js +6 -7
- package/dist/index.js +55 -40
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -239,6 +239,95 @@ const handleSubmit = async (formData: FormData) => {
|
|
|
239
239
|
}
|
|
240
240
|
```
|
|
241
241
|
|
|
242
|
+
## Advanced Usage
|
|
243
|
+
|
|
244
|
+
### Using AsyncLocalStorage with Custom Hono Instances
|
|
245
|
+
|
|
246
|
+
For advanced use cases involving request-scoped data and custom Hono instances, see [docs/async_hooks.md](./docs/async_hooks.md).
|
|
247
|
+
|
|
248
|
+
## Augmenting Type Interfaces
|
|
249
|
+
|
|
250
|
+
When using this library, you may need to augment the type interfaces to add custom types for your environment bindings, Hono context variables, or Astro locals. This is especially important when using the Cloudflare adapter.
|
|
251
|
+
|
|
252
|
+
### Cloudflare Runtime Types
|
|
253
|
+
|
|
254
|
+
When using the `@astrojs/cloudflare` adapter, the library automatically exports Cloudflare runtime types. However, it assumes that Cloudflare types have been generated by Wrangler via the `wrangler types` command, which creates an `Env` interface that is automatically added to `HonoEnv['Bindings']`.
|
|
255
|
+
|
|
256
|
+
To generate Cloudflare types:
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
bunx --bun wrangler types
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
This creates a `worker-configuration.d.ts` file (or similar) containing the `Env` interface based on your `wrangler.jsonc` configuration.
|
|
263
|
+
|
|
264
|
+
### Extending HonoEnv Types
|
|
265
|
+
|
|
266
|
+
To add additional types to your Hono environment, create a type declaration file (e.g., `src/env.d.ts`) and augment the module:
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
// src/env.d.ts
|
|
270
|
+
import '@gnosticdev/hono-actions/actions'
|
|
271
|
+
|
|
272
|
+
// Augmenting the actions types for use with cloudflare adapter
|
|
273
|
+
declare module '@gnosticdev/hono-actions/actions' {
|
|
274
|
+
// 1) Extend existing Bindings, with Env from worker-configuration.d.ts
|
|
275
|
+
interface Bindings extends Env {
|
|
276
|
+
anotherVar: string
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// 2) Add Variables available on `ctx.var.db` or `ctx.var.db.get('randomKey')` in hono actions
|
|
280
|
+
interface HonoEnv {
|
|
281
|
+
Variables: {
|
|
282
|
+
db: Map<string, any>
|
|
283
|
+
}
|
|
284
|
+
Bindings: Bindings
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Extend Astro Locals if you want to use in middleware
|
|
289
|
+
// need to add this to global scope bc we have an import in the file
|
|
290
|
+
declare global {
|
|
291
|
+
type Runtime = import('@astrojs/cloudflare').Runtime<Env>
|
|
292
|
+
declare namespace App {
|
|
293
|
+
interface Locals extends Runtime {
|
|
294
|
+
db: Map<string, any> // this will now be available on both `ctx.var.db` and `Astro.locals.db`
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
Now in your actions, you'll have full type safety:
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
// src/hono.ts
|
|
306
|
+
import { defineHonoAction, type HonoEnv } from '@gnosticdev/hono-actions/actions'
|
|
307
|
+
|
|
308
|
+
export const myAction = defineHonoAction({
|
|
309
|
+
handler: async (input, ctx) => {
|
|
310
|
+
// ctx.env has type: Bindings (includes Env + your custom bindings)
|
|
311
|
+
const kv = ctx.env.CUSTOM_KV
|
|
312
|
+
|
|
313
|
+
// ctx.var has type: HonoEnv['Variables']
|
|
314
|
+
const user = kv.get('user')
|
|
315
|
+
|
|
316
|
+
return { success: true }
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
And in your Astro pages:
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
// src/pages/index.astro
|
|
325
|
+
---
|
|
326
|
+
// Astro.locals has type: App.Locals
|
|
327
|
+
const user = Astro.locals.user
|
|
328
|
+
---
|
|
329
|
+
```
|
|
330
|
+
|
|
242
331
|
## Package Structure
|
|
243
332
|
|
|
244
333
|
This package provides these entry points:
|
package/dist/actions.d.ts
CHANGED
|
@@ -20,6 +20,10 @@ declare class HonoActionError<TMessage extends string, TCode extends ActionError
|
|
|
20
20
|
|
|
21
21
|
interface Bindings {
|
|
22
22
|
}
|
|
23
|
+
interface Variables {
|
|
24
|
+
/** Variables */
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
23
27
|
/**
|
|
24
28
|
* HonoEnv is passed to the Hono context to provide types on `ctx.env`.
|
|
25
29
|
*
|
|
@@ -36,7 +40,7 @@ interface Bindings {
|
|
|
36
40
|
*/
|
|
37
41
|
interface HonoEnv {
|
|
38
42
|
Bindings: Bindings;
|
|
39
|
-
Variables:
|
|
43
|
+
Variables: Variables;
|
|
40
44
|
}
|
|
41
45
|
type HonoActionSchema = z.ZodTypeAny;
|
|
42
46
|
/**
|
|
@@ -153,4 +157,4 @@ declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActi
|
|
|
153
157
|
};
|
|
154
158
|
}, "/">;
|
|
155
159
|
|
|
156
|
-
export { type Bindings, HonoActionError, type HonoEnv, type MergeActionKeyIntoPath, defineHonoAction };
|
|
160
|
+
export { type Bindings, HonoActionError, type HonoEnv, type MergeActionKeyIntoPath, type Variables, defineHonoAction };
|
package/dist/actions.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { zValidator } from '@hono/zod-validator';
|
|
2
|
+
import { z } from 'astro/zod';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
|
|
1
5
|
// src/actions.ts
|
|
2
|
-
import { zValidator } from "@hono/zod-validator";
|
|
3
|
-
import { z } from "astro/zod";
|
|
4
|
-
import { Hono } from "hono";
|
|
5
6
|
|
|
6
7
|
// src/error.ts
|
|
7
8
|
var HonoActionError = class extends Error {
|
|
@@ -78,7 +79,5 @@ function defineHonoAction({ schema, handler }) {
|
|
|
78
79
|
);
|
|
79
80
|
return route;
|
|
80
81
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
defineHonoAction
|
|
84
|
-
};
|
|
82
|
+
|
|
83
|
+
export { HonoActionError, defineHonoAction };
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
+
import { defineIntegration, createResolver, addVirtualImports } from 'astro-integration-kit';
|
|
2
|
+
import { z } from 'astro/zod';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { glob } from 'tinyglobby';
|
|
6
|
+
|
|
1
7
|
// src/integration.ts
|
|
2
|
-
import {
|
|
3
|
-
addVirtualImports,
|
|
4
|
-
createResolver,
|
|
5
|
-
defineIntegration
|
|
6
|
-
} from "astro-integration-kit";
|
|
7
|
-
import { z } from "astro/zod";
|
|
8
|
-
import fs from "node:fs/promises";
|
|
9
|
-
import path from "node:path";
|
|
10
|
-
import { glob } from "tinyglobby";
|
|
11
8
|
|
|
12
9
|
// src/integration-files.ts
|
|
13
10
|
function generateRouter(opts) {
|
|
@@ -127,6 +124,48 @@ export { parseResponse, hc }
|
|
|
127
124
|
export type { DetailedError }
|
|
128
125
|
export const honoClient = hc<HonoRouter>(getBaseUrl())
|
|
129
126
|
`;
|
|
127
|
+
var generateIntegrationTypes = (adapter) => {
|
|
128
|
+
let actionTypes = `
|
|
129
|
+
// Generated by Hono Actions Integration
|
|
130
|
+
declare module '@gnosticdev/hono-actions/actions' {
|
|
131
|
+
interface Bindings { [key: string]: unknown }
|
|
132
|
+
interface Variables { [key: string]: unknown }
|
|
133
|
+
interface HonoEnv { Bindings: Bindings, Variables: Variables }
|
|
134
|
+
}
|
|
135
|
+
export {}
|
|
136
|
+
`;
|
|
137
|
+
let clientTypes = `
|
|
138
|
+
// Generated by Hono Actions Integration
|
|
139
|
+
declare module '@gnosticdev/hono-actions/client' {
|
|
140
|
+
export const honoClient: typeof import('./client').honoClient
|
|
141
|
+
export const parseResponse: typeof import('./client').parseResponse
|
|
142
|
+
export type DetailedError = import('./client').DetailedError
|
|
143
|
+
}
|
|
144
|
+
`;
|
|
145
|
+
switch (adapter) {
|
|
146
|
+
// cloudflare uses Bindings and Variables passed in from the route handler
|
|
147
|
+
case "@astrojs/cloudflare":
|
|
148
|
+
actionTypes = `
|
|
149
|
+
// Generated by Hono Actions Integration
|
|
150
|
+
// keeping separate from the main types.d.ts to avoid clobbering package exports
|
|
151
|
+
declare module '@gnosticdev/hono-actions/actions' {
|
|
152
|
+
interface Bindings extends Env { ASTRO_LOCALS: App.Locals }
|
|
153
|
+
interface Variables { [key: string]: unknown }
|
|
154
|
+
interface HonoEnv { Bindings: Bindings, Variables: Variables }
|
|
155
|
+
}
|
|
156
|
+
export {}
|
|
157
|
+
`;
|
|
158
|
+
clientTypes += `
|
|
159
|
+
type Runtime = import('@astrojs/cloudflare').Runtime<Env>
|
|
160
|
+
|
|
161
|
+
declare namespace App {
|
|
162
|
+
interface Locals extends Runtime {}
|
|
163
|
+
}
|
|
164
|
+
`;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
return { actionTypes, clientTypes };
|
|
168
|
+
};
|
|
130
169
|
|
|
131
170
|
// src/lib/utils.ts
|
|
132
171
|
var reservedRoutes = ["_astro", "_actions", "_server_islands"];
|
|
@@ -261,26 +300,6 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
|
|
|
261
300
|
config,
|
|
262
301
|
logger
|
|
263
302
|
}) => {
|
|
264
|
-
injectTypes({
|
|
265
|
-
filename: "actions.d.ts",
|
|
266
|
-
content: `
|
|
267
|
-
// Generated by Hono Actions Integration
|
|
268
|
-
// keeping separate from the main types.d.ts to avoid clobbering package exports
|
|
269
|
-
declare module '@gnosticdev/hono-actions/actions' {
|
|
270
|
-
interface Bindings extends Env { ASTRO_LOCALS: App.Locals }
|
|
271
|
-
interface HonoEnv { Bindings: Bindings }
|
|
272
|
-
}
|
|
273
|
-
export {}
|
|
274
|
-
`
|
|
275
|
-
});
|
|
276
|
-
let clientTypes = `
|
|
277
|
-
// Generated by Hono Actions Integration
|
|
278
|
-
declare module '@gnosticdev/hono-actions/client' {
|
|
279
|
-
export const honoClient: typeof import('./client').honoClient
|
|
280
|
-
export const parseResponse: typeof import('./client').parseResponse
|
|
281
|
-
export type DetailedError = import('./client').DetailedError
|
|
282
|
-
}
|
|
283
|
-
`;
|
|
284
303
|
const adapter = config.adapter?.name;
|
|
285
304
|
if (!adapter) {
|
|
286
305
|
logger.warn("No adapter found...");
|
|
@@ -292,14 +311,11 @@ declare module '@gnosticdev/hono-actions/client' {
|
|
|
292
311
|
);
|
|
293
312
|
return;
|
|
294
313
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
`;
|
|
302
|
-
}
|
|
314
|
+
const { actionTypes, clientTypes } = generateIntegrationTypes(adapter);
|
|
315
|
+
injectTypes({
|
|
316
|
+
filename: "actions.d.ts",
|
|
317
|
+
content: actionTypes
|
|
318
|
+
});
|
|
303
319
|
injectTypes({
|
|
304
320
|
filename: "types.d.ts",
|
|
305
321
|
content: clientTypes
|
|
@@ -312,6 +328,5 @@ declare module '@gnosticdev/hono-actions/client' {
|
|
|
312
328
|
|
|
313
329
|
// src/index.ts
|
|
314
330
|
var src_default = integration_default;
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
};
|
|
331
|
+
|
|
332
|
+
export { src_default as default };
|
package/package.json
CHANGED
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"@astrojs/netlify": "catalog:",
|
|
20
20
|
"@astrojs/node": "catalog:",
|
|
21
21
|
"@astrojs/vercel": "catalog:",
|
|
22
|
-
"astro": "catalog:"
|
|
22
|
+
"astro": "catalog:",
|
|
23
|
+
"@biomejs/biome": "catalog:"
|
|
23
24
|
},
|
|
24
25
|
"exports": {
|
|
25
26
|
".": {
|
|
@@ -61,5 +62,5 @@
|
|
|
61
62
|
},
|
|
62
63
|
"type": "module",
|
|
63
64
|
"types": "./dist/index.d.ts",
|
|
64
|
-
"version": "2.0.
|
|
65
|
+
"version": "2.0.7"
|
|
65
66
|
}
|