@gnosticdev/hono-actions 1.1.1 → 1.2.0

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/dist/actions.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import * as hono_hono_base from 'hono/hono-base';
2
2
  import * as hono_utils_types from 'hono/utils/types';
3
- import * as zod_v4 from 'zod/v4';
4
- import * as zod_v4_core from 'zod/v4/core';
5
3
  import { z } from 'astro/zod';
6
- import { Context } from 'hono';
4
+ import { Hono, Context } from 'hono';
5
+ import { MergeSchemaPath } from 'hono/types';
7
6
 
8
7
  /**
9
8
  * Standard error codes for actions
@@ -31,6 +30,29 @@ interface HonoEnv {
31
30
  Variables: Record<string, unknown>;
32
31
  }
33
32
  type HonoActionSchema = z.ZodTypeAny;
33
+ /**
34
+ * Merge each action key into its route path.
35
+ *
36
+ * Given a map of actions where each `Hono` app defines handlers at `"/"`, this
37
+ * transforms the schema so each action's path becomes `"/${key}"`.
38
+ *
39
+ * Example:
40
+ * ```ts
41
+ * declare const honoActions: {
42
+ * myAction: Hono<HonoEnv, { '/': { $post: any } }, '/'>
43
+ * anotherAction: Hono<HonoEnv, { '/': { $post: any } }, '/'>
44
+ * }
45
+ *
46
+ * type ActionsWithKeyedPaths = MergeActionKeyIntoPath<typeof honoActions>
47
+ * // => {
48
+ * // myAction: Hono<HonoEnv, { '/myAction': { $post: any } }, '/'>
49
+ * // anotherAction: Hono<HonoEnv, { '/anotherAction': { $post: any } }, '/'>
50
+ * // }
51
+ * ```
52
+ */
53
+ type MergeActionKeyIntoPath<TActions extends Record<string, Hono<any, any, any>>> = {
54
+ [K in keyof TActions]: TActions[K] extends Hono<infer TEnv, infer TSchema, infer TBase> ? Hono<TEnv, MergeSchemaPath<TSchema, `/${Extract<K, string>}`>, TBase> : never;
55
+ };
34
56
  interface HonoActionContext<TEnv extends HonoEnv, TSchema extends HonoActionSchema> extends Context<TEnv, '/', {
35
57
  input: z.input<TSchema>;
36
58
  output: z.output<TSchema>;
@@ -52,14 +74,14 @@ type HonoActionParams<TSchema extends HonoActionSchema, TReturn, TEnv extends Ho
52
74
  declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActionSchema, TReturn>({ schema, handler }: HonoActionParams<TSchema, TReturn, TEnv>): hono_hono_base.HonoBase<TEnv, {
53
75
  "/": {
54
76
  $post: {
55
- input: unknown extends ((undefined extends {} | (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? true : false) extends true ? {
56
- json?: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T ? T extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T extends any ? T : { [K2 in keyof T]?: any; } : never : never) | undefined;
77
+ input: unknown extends ((undefined extends z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> ? true : false) extends true ? {
78
+ json?: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> | undefined;
57
79
  } : {
58
- json: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T_1 ? T_1 extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T_1 extends any ? T_1 : { [K2_1 in keyof T_1]: any; } : never : never);
59
- }) ? {} : (undefined extends {} | (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? true : false) extends true ? {
60
- json?: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T_2 ? T_2 extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T_2 extends any ? T_2 : { [K2_2 in keyof T_2]?: any; } : never : never) | undefined;
80
+ json: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>;
81
+ }) ? {} : (undefined extends z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> ? true : false) extends true ? {
82
+ json?: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> | undefined;
61
83
  } : {
62
- json: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T_3 ? T_3 extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T_3 extends any ? T_3 : { [K2_3 in keyof T_3]: any; } : never : never);
84
+ json: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>;
63
85
  };
64
86
  output: unknown extends ({
65
87
  data: Awaited<TReturn>;
@@ -70,16 +92,16 @@ declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActi
70
92
  } as ({
71
93
  data: Awaited<TReturn>;
72
94
  error: null;
73
- }[K] extends infer T_4 ? T_4 extends {
95
+ }[K] extends infer T ? T extends {
74
96
  data: Awaited<TReturn>;
75
97
  error: null;
76
- }[K] ? T_4 extends hono_utils_types.InvalidJSONValue ? true : false : never : never) extends true ? never : K]: boolean extends ({
98
+ }[K] ? T extends hono_utils_types.InvalidJSONValue ? true : false : never : never) extends true ? never : K]: boolean extends ({
77
99
  data: Awaited<TReturn>;
78
100
  error: null;
79
- }[K] extends infer T_5 ? T_5 extends {
101
+ }[K] extends infer T_1 ? T_1 extends {
80
102
  data: Awaited<TReturn>;
81
103
  error: null;
82
- }[K] ? T_5 extends hono_utils_types.InvalidJSONValue ? true : false : never : never) ? hono_utils_types.JSONParsed<{
104
+ }[K] ? T_1 extends hono_utils_types.InvalidJSONValue ? true : false : never : never) ? hono_utils_types.JSONParsed<{
83
105
  data: Awaited<TReturn>;
84
106
  error: null;
85
107
  }[K]> | undefined : hono_utils_types.JSONParsed<{
@@ -94,16 +116,16 @@ declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActi
94
116
  } as ({
95
117
  data: Awaited<TReturn>;
96
118
  error: null;
97
- }[K] extends infer T_4 ? T_4 extends {
119
+ }[K] extends infer T ? T extends {
98
120
  data: Awaited<TReturn>;
99
121
  error: null;
100
- }[K] ? T_4 extends hono_utils_types.InvalidJSONValue ? true : false : never : never) extends true ? never : K]: boolean extends ({
122
+ }[K] ? T extends hono_utils_types.InvalidJSONValue ? true : false : never : never) extends true ? never : K]: boolean extends ({
101
123
  data: Awaited<TReturn>;
102
124
  error: null;
103
- }[K] extends infer T_5 ? T_5 extends {
125
+ }[K] extends infer T_1 ? T_1 extends {
104
126
  data: Awaited<TReturn>;
105
127
  error: null;
106
- }[K] ? T_5 extends hono_utils_types.InvalidJSONValue ? true : false : never : never) ? hono_utils_types.JSONParsed<{
128
+ }[K] ? T_1 extends hono_utils_types.InvalidJSONValue ? true : false : never : never) ? hono_utils_types.JSONParsed<{
107
129
  data: Awaited<TReturn>;
108
130
  error: null;
109
131
  }[K]> | undefined : hono_utils_types.JSONParsed<{
@@ -113,14 +135,14 @@ declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActi
113
135
  outputFormat: "json";
114
136
  status: 200;
115
137
  } | {
116
- input: unknown extends ((undefined extends {} | (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? true : false) extends true ? {
117
- json?: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T_4 ? T_4 extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T_4 extends any ? T_4 : { [K2_4 in keyof T_4]?: any; } : never : never) | undefined;
138
+ input: unknown extends ((undefined extends z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> ? true : false) extends true ? {
139
+ json?: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> | undefined;
118
140
  } : {
119
- json: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T_5 ? T_5 extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T_5 extends any ? T_5 : { [K2_5 in keyof T_5]: any; } : never : never);
120
- }) ? {} : (undefined extends {} | (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? true : false) extends true ? {
121
- json?: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T_6 ? T_6 extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T_6 extends any ? T_6 : { [K2_6 in keyof T_6]?: any; } : never : never) | undefined;
141
+ json: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>;
142
+ }) ? {} : (undefined extends z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> ? true : false) extends true ? {
143
+ json?: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>> | undefined;
122
144
  } : {
123
- json: {} | ((TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) extends infer T_7 ? T_7 extends (TSchema extends z.ZodType<any, z.ZodTypeDef, any> ? z.input<TSchema> : TSchema extends zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>> ? zod_v4.input<TSchema> : never) ? T_7 extends any ? T_7 : { [K2_7 in keyof T_7]: any; } : never : never);
145
+ json: z.input<TSchema | z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>;
124
146
  };
125
147
  output: {
126
148
  data: null;
@@ -135,4 +157,4 @@ declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActi
135
157
  };
136
158
  }, "/">;
137
159
 
138
- export { type Bindings, HonoActionError, type HonoEnv, defineHonoAction };
160
+ export { type Bindings, HonoActionError, type HonoEnv, type MergeActionKeyIntoPath, defineHonoAction };
package/dist/actions.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/actions.ts
2
2
  import { zValidator } from "@hono/zod-validator";
3
3
  import { z } from "astro/zod";
4
+ import { Hono } from "hono/quick";
4
5
 
5
6
  // src/error.ts
6
7
  var HonoActionError = class extends Error {
@@ -19,7 +20,6 @@ var HonoActionError = class extends Error {
19
20
  };
20
21
 
21
22
  // src/actions.ts
22
- import { Hono } from "hono/quick";
23
23
  function defineHonoAction({ schema, handler }) {
24
24
  const app = new Hono();
25
25
  const route = app.post(
package/dist/index.d.ts CHANGED
@@ -25,7 +25,7 @@ declare const optionsSchema: z.ZodOptional<z.ZodObject<{
25
25
  basePath?: string | undefined;
26
26
  actionsPath?: string | undefined;
27
27
  }>>;
28
- type IntegrationOptions = z.infer<typeof optionsSchema>;
28
+ type IntegrationOptions = z.output<typeof optionsSchema>;
29
29
  /**
30
30
  * Astro integration for Hono Actions
31
31
  *
package/dist/index.js CHANGED
@@ -2282,7 +2282,7 @@ async function glob(patternsOrOptions, options) {
2282
2282
  // src/integration-files.ts
2283
2283
  function generateRouter(opts) {
2284
2284
  const { basePath, relativeActionsPath } = opts;
2285
- return `import type { HonoEnv } from '@gnosticdev/hono-actions/actions'
2285
+ return `import type { HonoEnv, MergeActionKeyIntoPath } from '@gnosticdev/hono-actions/actions'
2286
2286
  import { Hono } from 'hono'
2287
2287
  import { cors } from 'hono/cors'
2288
2288
  import { showRoutes } from 'hono/dev'
@@ -2291,9 +2291,10 @@ import { prettyJSON } from 'hono/pretty-json'
2291
2291
  import type { ExtractSchema, MergeSchemaPath } from 'hono/types'
2292
2292
 
2293
2293
  async function buildRouter(){
2294
- type ActionSchema = ExtractSchema<typeof honoActions[keyof typeof honoActions]>
2294
+ type ActionsWithKeyedPaths = MergeActionKeyIntoPath<typeof honoActions>
2295
+ type ActionSchema = ExtractSchema<ActionsWithKeyedPaths[keyof ActionsWithKeyedPaths]>
2295
2296
  const { honoActions} = await import('${relativeActionsPath}')
2296
- const app = new Hono<HonoEnv, MergeSchemaPath<ActionSchema, '${basePath}/keyof typeof honoActions'>>().basePath('${basePath}')
2297
+ const app = new Hono<HonoEnv, MergeSchemaPath<ActionSchema, \`${basePath}\`>>().basePath('${basePath}')
2297
2298
 
2298
2299
  app.use('*', cors(), logger(), prettyJSON())
2299
2300
 
@@ -2314,7 +2315,7 @@ export default app`;
2314
2315
  }
2315
2316
  var getAstroHandler = (adapter) => {
2316
2317
  switch (adapter) {
2317
- case "cloudflare":
2318
+ case "@astrojs/cloudflare":
2318
2319
  return `
2319
2320
  // Generated by Hono Actions Integration
2320
2321
  import router from './router.js'
@@ -2387,6 +2388,10 @@ var ACTION_PATTERNS = [
2387
2388
  "src/hono/index.ts",
2388
2389
  "src/hono.ts"
2389
2390
  ];
2391
+ var SUPPORTED_ADAPTERS = ["@astrojs/cloudflare"];
2392
+ function isSupportedAdapter(adapter) {
2393
+ return SUPPORTED_ADAPTERS.includes(adapter);
2394
+ }
2390
2395
  var integration_default = defineIntegration({
2391
2396
  name: "@gnosticdev/hono-actions",
2392
2397
  optionsSchema,
@@ -2437,9 +2442,20 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
2437
2442
  codeGenDir.pathname,
2438
2443
  "api.ts"
2439
2444
  );
2440
- if (!params.config.adapter?.name)
2445
+ const adapter = params.config.adapter?.name;
2446
+ if (!adapter) {
2441
2447
  throw new Error("No Astro adapter found");
2442
- const astroHandlerContent = getAstroHandler("cloudflare");
2448
+ }
2449
+ let astroHandlerContent;
2450
+ if (isSupportedAdapter(adapter)) {
2451
+ astroHandlerContent = getAstroHandler(adapter);
2452
+ } else {
2453
+ throw new Error(`Unsupported adapter: ${adapter}`, {
2454
+ cause: `Only ${SUPPORTED_ADAPTERS.join(
2455
+ ", "
2456
+ )} are supported for now`
2457
+ });
2458
+ }
2443
2459
  await fs.writeFile(
2444
2460
  astroHandlerPathAbs,
2445
2461
  astroHandlerContent,
@@ -2468,32 +2484,48 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
2468
2484
  `\u2705 Hono Actions route mounted at ${basePath}/[...slug]`
2469
2485
  );
2470
2486
  },
2471
- "astro:config:done": async ({ injectTypes }) => {
2487
+ "astro:config:done": async ({
2488
+ injectTypes,
2489
+ config,
2490
+ logger
2491
+ }) => {
2472
2492
  injectTypes({
2473
2493
  filename: "actions.d.ts",
2474
2494
  content: `
2475
- declare module '@gnosticdev/hono-actions/actions' {
2495
+ // Generated by Hono Actions Integration
2496
+ // keeping separate from the main types.d.ts to avoid clobbering package exports
2497
+ declare module '@gnosticdev/hono-actions/actions' {
2476
2498
  interface Bindings extends Env { ASTRO_LOCALS: App.Locals }
2477
2499
  interface HonoEnv { Bindings: Bindings }
2478
- }
2479
- export {}
2480
- `
2500
+ }
2501
+ export {}
2502
+ `
2481
2503
  });
2504
+ let clientTypes = `
2505
+ // Generated by Hono Actions Integration
2506
+ // keeping separate from the main types.d.ts to avoid clobbering package exports
2507
+ declare module '@gnosticdev/hono-actions/client' {
2508
+ export const honoClient: typeof import('./client').honoClient
2509
+ export const parseResponse: typeof import('./client').parseResponse
2510
+ }
2511
+ `;
2512
+ if (!config.adapter?.name) {
2513
+ logger.warn("No adapter found...");
2514
+ return;
2515
+ }
2516
+ if (config.adapter.name !== "@astrojs/cloudflare") {
2517
+ logger.warn("Unsupported adapter...");
2518
+ return;
2519
+ }
2520
+ clientTypes += `
2521
+ type Runtime = import('@astrojs/cloudflare').Runtime<Env>
2522
+ declare namespace App {
2523
+ interface Locals extends Runtime {}
2524
+ }
2525
+ `;
2482
2526
  injectTypes({
2483
2527
  filename: "types.d.ts",
2484
- content: `
2485
- // Generated by Hono Actions Integration
2486
-
2487
- declare module 'virtual:hono-actions/router' {
2488
- export type HonoRouter = import('./router.ts').HonoRouter
2489
- const app: typeof import('./router.ts').default
2490
- export default app
2491
- }
2492
-
2493
- declare module '@gnosticdev/hono-actions/client' {
2494
- export const honoClient: typeof import('./client').honoClient
2495
- }
2496
- `
2528
+ content: clientTypes
2497
2529
  });
2498
2530
  }
2499
2531
  }
package/package.json CHANGED
@@ -5,7 +5,6 @@
5
5
  "url": "https://github.com/gnosticdev"
6
6
  },
7
7
  "dependencies": {
8
- "@astrojs/cloudflare": "^12.6.7",
9
8
  "@hono/zod-validator": "^0.2.2",
10
9
  "astro-integration-kit": "^0.19.0",
11
10
  "hono": "^4.9.5",
@@ -54,5 +53,5 @@
54
53
  },
55
54
  "type": "module",
56
55
  "types": "./dist/index.d.ts",
57
- "version": "1.1.1"
56
+ "version": "1.2.0"
58
57
  }