@gnosticdev/hono-actions 2.0.10 → 2.1.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/index.d.ts CHANGED
@@ -2,32 +2,9 @@ import * as astro from 'astro';
2
2
  import { z } from 'astro/zod';
3
3
 
4
4
  declare const optionsSchema: z.ZodOptional<z.ZodObject<{
5
- /**
6
- * The base path for the API routes
7
- *
8
- * @default '/api'
9
- */
10
5
  basePath: z.ZodOptional<z.ZodString>;
11
- /**
12
- * The path to the actions file. If not provided, the integration will automatically discover the actions file by searching for one of the following patterns:
13
- * - `src/server/actions.ts`
14
- * - `src/hono/actions.ts`
15
- * - `src/hono/index.ts`
16
- * - `src/hono.ts`
17
- * - `src/hono-actions.ts`
18
- *
19
- * **NOTE** `src/actions.ts` is reserved for Astro Actions and will be ignored.
20
- *
21
- * @default 'src/server/actions.ts'
22
- */
23
6
  actionsPath: z.ZodOptional<z.ZodString>;
24
- }, "strip", z.ZodTypeAny, {
25
- basePath?: string | undefined;
26
- actionsPath?: string | undefined;
27
- }, {
28
- basePath?: string | undefined;
29
- actionsPath?: string | undefined;
30
- }>>;
7
+ }, z.core.$strip>>;
31
8
  type IntegrationOptions = z.output<typeof optionsSchema>;
32
9
  /**
33
10
  * Astro integration for Hono Actions
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ import { z } from 'astro/zod';
3
3
  import fs from 'node:fs/promises';
4
4
  import path from 'node:path';
5
5
  import { glob } from 'tinyglobby';
6
+ import { createRequire } from 'node:module';
6
7
 
7
8
  // src/integration.ts
8
9
 
@@ -45,9 +46,32 @@ showRoutes(app)
45
46
  console.log('---------------------------')
46
47
  ${exportedApp}`;
47
48
  }
48
- var generateAstroHandler = (adapter) => {
49
+ var generateAstroHandler = (adapter, astroMajorVersion = 5) => {
49
50
  switch (adapter) {
50
51
  case "@astrojs/cloudflare":
52
+ if (astroMajorVersion >= 6) {
53
+ return `
54
+ /// <reference types="./types.d.ts" />
55
+ // Generated by Hono Actions Integration
56
+ // adapter: ${adapter}
57
+ import { env } from 'cloudflare:workers'
58
+ import type { APIContext, APIRoute } from 'astro'
59
+ import router from './router.js'
60
+
61
+ const handler: APIRoute<APIContext> = async (ctx) => {
62
+ return router.fetch(
63
+ ctx.request,
64
+ {
65
+ ...env,
66
+ ASTRO_LOCALS: ctx.locals,
67
+ },
68
+ ctx.locals.cfContext,
69
+ )
70
+ }
71
+
72
+ export { handler as ALL }
73
+ `;
74
+ }
51
75
  return `
52
76
  /// <reference types="./types.d.ts" />
53
77
  // Generated by Hono Actions Integration
@@ -58,7 +82,10 @@ import router from './router.js'
58
82
  const handler: APIRoute<APIContext> = async (ctx) => {
59
83
  return router.fetch(
60
84
  ctx.request,
61
- ctx.locals.runtime.env, // required for cloudflare adapter
85
+ {
86
+ ...ctx.locals.runtime.env,
87
+ ASTRO_LOCALS: ctx.locals,
88
+ },
62
89
  ctx.locals.runtime.ctx, // required for cloudflare adapter
63
90
  )
64
91
  }
@@ -132,7 +159,7 @@ export function createHonoClient<T extends HonoRouter = HonoRouter>(basePath: st
132
159
  return hc<T>(basePath, fetchOptions)
133
160
  }
134
161
  `;
135
- var generateIntegrationTypes = (adapter) => {
162
+ var generateIntegrationTypes = (adapter, astroMajorVersion = 5) => {
136
163
  let actionTypes = `
137
164
  // Generated by Hono Actions Integration
138
165
  declare module '@gnosticdev/hono-actions/actions' {
@@ -180,24 +207,51 @@ declare module '@gnosticdev/hono-actions/actions' {
180
207
  }
181
208
  export {}
182
209
  `;
183
- clientTypes += `
210
+ if (astroMajorVersion < 6) {
211
+ clientTypes += `
184
212
  type Runtime = import('@astrojs/cloudflare').Runtime<Env>
185
213
 
186
214
  declare namespace App {
187
215
  interface Locals extends Runtime {}
188
216
  }
189
217
  `;
218
+ }
190
219
  break;
191
220
  }
192
221
  return { actionTypes, clientTypes };
193
222
  };
194
-
195
- // src/lib/utils.ts
196
223
  var reservedRoutes = ["_astro", "_actions", "_server_islands"];
197
- var SUPPORTED_ADAPTERS = ["@astrojs/cloudflare", "@astrojs/node", "@astrojs/netlify", "@astrojs/vercel"];
224
+ var SUPPORTED_ADAPTERS = [
225
+ "@astrojs/cloudflare",
226
+ "@astrojs/node",
227
+ "@astrojs/netlify",
228
+ "@astrojs/vercel"
229
+ ];
198
230
  function isSupportedAdapter(adapter) {
199
231
  return SUPPORTED_ADAPTERS.includes(adapter);
200
232
  }
233
+ function parseAstroMajorVersion(version) {
234
+ const majorVersion = Number.parseInt(version, 10);
235
+ if (Number.isNaN(majorVersion)) {
236
+ if (process.env.PACKAGE_VERSION) {
237
+ return Number.parseInt(process.env.PACKAGE_VERSION, 10);
238
+ } else {
239
+ throw new Error(`Invalid Astro version: ${version}`);
240
+ }
241
+ }
242
+ return majorVersion;
243
+ }
244
+ function getInstalledAstroVersion() {
245
+ const require2 = createRequire(import.meta.url);
246
+ const astroPackage = require2("astro/package.json");
247
+ if (!astroPackage.version) {
248
+ throw new Error("Could not determine installed Astro version");
249
+ }
250
+ return astroPackage.version;
251
+ }
252
+ function getInstalledAstroMajorVersion() {
253
+ return parseAstroMajorVersion(getInstalledAstroVersion());
254
+ }
201
255
 
202
256
  // src/integration.ts
203
257
  var optionsSchema = z.object({
@@ -242,18 +296,27 @@ var integration_default = defineIntegration({
242
296
  }
243
297
  const { resolve } = createResolver(import.meta.url);
244
298
  const IS_DEBUG = process.env.__DEBUG__ === "true";
299
+ const astroMajorVersion = getInstalledAstroMajorVersion();
245
300
  return {
246
301
  name,
247
302
  hooks: {
248
303
  "astro:config:setup": async (params) => {
249
304
  const { logger, injectRoute, createCodegenDir, config } = params;
305
+ logger.info(
306
+ `using astro major version: ${astroMajorVersion}`
307
+ );
250
308
  const root = config.root.pathname;
251
- const absPatterns = ACTION_PATTERNS.map((p) => path.join(root, p));
309
+ const absPatterns = ACTION_PATTERNS.map(
310
+ (p) => path.join(root, p)
311
+ );
252
312
  const files = await glob(absPatterns, {
253
313
  expandDirectories: false,
254
314
  absolute: true
255
315
  });
256
316
  if (IS_DEBUG) {
317
+ logger.info(
318
+ `DEBUG: Detected Astro major version: ${astroMajorVersion}`
319
+ );
257
320
  logger.info(`DEBUG: Found actions: ${files.join("\n")}`);
258
321
  }
259
322
  const actionsPath = options.actionsPath ?? files[0];
@@ -270,7 +333,10 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
270
333
  `Found actions: ${path.relative(root, resolvedActionsPath)}`
271
334
  );
272
335
  const codeGenDir = createCodegenDir();
273
- const routerPathAbs = path.join(codeGenDir.pathname, "router.ts");
336
+ const routerPathAbs = path.join(
337
+ codeGenDir.pathname,
338
+ "router.ts"
339
+ );
274
340
  const relFromGenToActions = path.relative(codeGenDir.pathname, resolvedActionsPath).split(path.sep).join("/");
275
341
  const adapter = params.config.adapter?.name;
276
342
  if (!adapter) {
@@ -292,10 +358,23 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
292
358
  adapter
293
359
  });
294
360
  await fs.writeFile(routerPathAbs, routerContent, "utf-8");
295
- const astroHandlerPathAbs = path.join(codeGenDir.pathname, "api.ts");
296
- const astroHandlerContent = generateAstroHandler(adapter);
297
- await fs.writeFile(astroHandlerPathAbs, astroHandlerContent, "utf-8");
298
- const clientPathAbs = path.join(codeGenDir.pathname, "client.ts");
361
+ const astroHandlerPathAbs = path.join(
362
+ codeGenDir.pathname,
363
+ "api.ts"
364
+ );
365
+ const astroHandlerContent = generateAstroHandler(
366
+ adapter,
367
+ astroMajorVersion
368
+ );
369
+ await fs.writeFile(
370
+ astroHandlerPathAbs,
371
+ astroHandlerContent,
372
+ "utf-8"
373
+ );
374
+ const clientPathAbs = path.join(
375
+ codeGenDir.pathname,
376
+ "client.ts"
377
+ );
299
378
  if (!config.site) {
300
379
  logger.warn(
301
380
  "No site url found in astro config, add one if you want to use the hono client with SSR"
@@ -316,9 +395,15 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
316
395
  entrypoint: astroHandlerPathAbs,
317
396
  prerender: false
318
397
  });
319
- logger.info(`\u2705 Hono Actions route mounted at ${basePath}/[...slug]`);
398
+ logger.info(
399
+ `\u2705 Hono Actions route mounted at ${basePath}/[...slug]`
400
+ );
320
401
  },
321
- "astro:config:done": async ({ injectTypes, config, logger }) => {
402
+ "astro:config:done": async ({
403
+ injectTypes,
404
+ config,
405
+ logger
406
+ }) => {
322
407
  const adapter = config.adapter?.name;
323
408
  if (!adapter) {
324
409
  logger.warn("No adapter found...");
@@ -330,7 +415,7 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
330
415
  );
331
416
  return;
332
417
  }
333
- const { actionTypes, clientTypes } = generateIntegrationTypes(adapter);
418
+ const { actionTypes, clientTypes } = generateIntegrationTypes(adapter, astroMajorVersion);
334
419
  injectTypes({
335
420
  filename: "actions.d.ts",
336
421
  content: actionTypes
package/package.json CHANGED
@@ -5,8 +5,8 @@
5
5
  },
6
6
  "dependencies": {
7
7
  "@hono/standard-validator": "^0.2.0",
8
- "@hono/zod-validator": "^0.2.2",
9
- "astro-integration-kit": "^0.19.0",
8
+ "@hono/zod-validator": "^0.7.6",
9
+ "astro-integration-kit": "^0.20.0",
10
10
  "tinyglobby": "^0.2.15"
11
11
  },
12
12
  "description": "Define server actions with built-in validation, error handling, and a pre-built hono client for calling the routes.",
@@ -48,7 +48,7 @@
48
48
  "main": "./dist/index.js",
49
49
  "name": "@gnosticdev/hono-actions",
50
50
  "peerDependencies": {
51
- "astro": "^5.13.0",
51
+ "astro": "^5.13.0 || ^6.0.0",
52
52
  "hono": "^4.10.6"
53
53
  },
54
54
  "publishConfig": {
@@ -61,5 +61,5 @@
61
61
  },
62
62
  "type": "module",
63
63
  "types": "./dist/index.d.ts",
64
- "version": "2.0.10"
64
+ "version": "2.1.0"
65
65
  }