@gnosticdev/hono-actions 2.0.11 → 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/README.md +3 -1
- package/dist/actions.d.ts +1237 -26
- package/dist/index.d.ts +1 -24
- package/dist/index.js +73 -9
- package/package.json +4 -4
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
|
-
},
|
|
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
|
-
|
|
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
|
-
|
|
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 = [
|
|
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,11 +296,15 @@ 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
309
|
const absPatterns = ACTION_PATTERNS.map(
|
|
252
310
|
(p) => path.join(root, p)
|
|
@@ -256,6 +314,9 @@ var integration_default = defineIntegration({
|
|
|
256
314
|
absolute: true
|
|
257
315
|
});
|
|
258
316
|
if (IS_DEBUG) {
|
|
317
|
+
logger.info(
|
|
318
|
+
`DEBUG: Detected Astro major version: ${astroMajorVersion}`
|
|
319
|
+
);
|
|
259
320
|
logger.info(`DEBUG: Found actions: ${files.join("\n")}`);
|
|
260
321
|
}
|
|
261
322
|
const actionsPath = options.actionsPath ?? files[0];
|
|
@@ -301,7 +362,10 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
|
|
|
301
362
|
codeGenDir.pathname,
|
|
302
363
|
"api.ts"
|
|
303
364
|
);
|
|
304
|
-
const astroHandlerContent = generateAstroHandler(
|
|
365
|
+
const astroHandlerContent = generateAstroHandler(
|
|
366
|
+
adapter,
|
|
367
|
+
astroMajorVersion
|
|
368
|
+
);
|
|
305
369
|
await fs.writeFile(
|
|
306
370
|
astroHandlerPathAbs,
|
|
307
371
|
astroHandlerContent,
|
|
@@ -351,7 +415,7 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
|
|
|
351
415
|
);
|
|
352
416
|
return;
|
|
353
417
|
}
|
|
354
|
-
const { actionTypes, clientTypes } = generateIntegrationTypes(adapter);
|
|
418
|
+
const { actionTypes, clientTypes } = generateIntegrationTypes(adapter, astroMajorVersion);
|
|
355
419
|
injectTypes({
|
|
356
420
|
filename: "actions.d.ts",
|
|
357
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.
|
|
9
|
-
"astro-integration-kit": "^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
|
|
64
|
+
"version": "2.1.0"
|
|
65
65
|
}
|