@gnosticdev/hono-actions 2.0.7 → 2.0.8
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 +13 -13
- package/dist/actions.js +20 -16
- package/dist/index.d.ts +5 -2
- package/dist/index.js +39 -11
- package/package.json +6 -7
package/dist/actions.d.ts
CHANGED
|
@@ -80,14 +80,14 @@ type HonoActionParams<TSchema extends HonoActionSchema, TReturn, TEnv extends Ho
|
|
|
80
80
|
declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActionSchema, TReturn, TContext extends Context<TEnv, any, any>>({ schema, handler }: HonoActionParams<TSchema, TReturn, TEnv, TContext>): hono_hono_base.HonoBase<TEnv, {
|
|
81
81
|
"/": {
|
|
82
82
|
$post: {
|
|
83
|
-
input: unknown extends ((undefined extends z.input<TSchema
|
|
84
|
-
json?: z.input<TSchema |
|
|
83
|
+
input: unknown extends ((undefined extends z.input<TSchema> ? true : false) extends true ? {
|
|
84
|
+
json?: z.input<TSchema> | undefined;
|
|
85
85
|
} : {
|
|
86
|
-
json: z.input<TSchema
|
|
87
|
-
}) ? {} : (undefined extends z.input<TSchema
|
|
88
|
-
json?: z.input<TSchema |
|
|
86
|
+
json: z.input<TSchema>;
|
|
87
|
+
}) ? {} : (undefined extends z.input<TSchema> ? true : false) extends true ? {
|
|
88
|
+
json?: z.input<TSchema> | undefined;
|
|
89
89
|
} : {
|
|
90
|
-
json: z.input<TSchema
|
|
90
|
+
json: z.input<TSchema>;
|
|
91
91
|
};
|
|
92
92
|
output: {
|
|
93
93
|
data: null;
|
|
@@ -99,14 +99,14 @@ declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActi
|
|
|
99
99
|
outputFormat: "json";
|
|
100
100
|
status: 500;
|
|
101
101
|
} | {
|
|
102
|
-
input: unknown extends ((undefined extends z.input<TSchema
|
|
103
|
-
json?: z.input<TSchema |
|
|
102
|
+
input: unknown extends ((undefined extends z.input<TSchema> ? true : false) extends true ? {
|
|
103
|
+
json?: z.input<TSchema> | undefined;
|
|
104
104
|
} : {
|
|
105
|
-
json: z.input<TSchema
|
|
106
|
-
}) ? {} : (undefined extends z.input<TSchema
|
|
107
|
-
json?: z.input<TSchema |
|
|
105
|
+
json: z.input<TSchema>;
|
|
106
|
+
}) ? {} : (undefined extends z.input<TSchema> ? true : false) extends true ? {
|
|
107
|
+
json?: z.input<TSchema> | undefined;
|
|
108
108
|
} : {
|
|
109
|
-
json: z.input<TSchema
|
|
109
|
+
json: z.input<TSchema>;
|
|
110
110
|
};
|
|
111
111
|
output: unknown extends (Awaited<TReturn> | null extends bigint | readonly bigint[] ? never : { [K in keyof {
|
|
112
112
|
data: Awaited<TReturn>;
|
|
@@ -155,6 +155,6 @@ declare function defineHonoAction<TEnv extends HonoEnv, TSchema extends HonoActi
|
|
|
155
155
|
status: 200;
|
|
156
156
|
};
|
|
157
157
|
};
|
|
158
|
-
}, "/">;
|
|
158
|
+
}, "/", "/">;
|
|
159
159
|
|
|
160
160
|
export { type Bindings, HonoActionError, type HonoEnv, type MergeActionKeyIntoPath, type Variables, defineHonoAction };
|
package/dist/actions.js
CHANGED
|
@@ -25,23 +25,27 @@ function defineHonoAction({ schema, handler }) {
|
|
|
25
25
|
const app = new Hono();
|
|
26
26
|
const route = app.post(
|
|
27
27
|
"/",
|
|
28
|
-
zValidator(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
zValidator(
|
|
29
|
+
"json",
|
|
30
|
+
schema ?? z.any(),
|
|
31
|
+
async (result, c) => {
|
|
32
|
+
if (!result.success) {
|
|
33
|
+
console.error(result.error.issues);
|
|
34
|
+
const firstIssue = result.error.issues[0];
|
|
35
|
+
return c.json(
|
|
36
|
+
{
|
|
37
|
+
data: null,
|
|
38
|
+
error: new HonoActionError({
|
|
39
|
+
message: firstIssue?.message || "Validation error",
|
|
40
|
+
code: "INPUT_VALIDATION_ERROR",
|
|
41
|
+
issue: firstIssue
|
|
42
|
+
})
|
|
43
|
+
},
|
|
44
|
+
400
|
|
45
|
+
);
|
|
46
|
+
}
|
|
43
47
|
}
|
|
44
|
-
|
|
48
|
+
),
|
|
45
49
|
async (c) => {
|
|
46
50
|
try {
|
|
47
51
|
const json = c.req.valid("json");
|
package/dist/index.d.ts
CHANGED
|
@@ -33,8 +33,11 @@ type IntegrationOptions = z.output<typeof optionsSchema>;
|
|
|
33
33
|
* generates type-safe client code, and sets up API routes.
|
|
34
34
|
*
|
|
35
35
|
* Supprted Adapters:
|
|
36
|
-
* -
|
|
37
|
-
* -
|
|
36
|
+
* - @astrojs/cloudflare
|
|
37
|
+
* - @astrojs/node
|
|
38
|
+
* - @astrojs/vercel
|
|
39
|
+
* - @astrojs/netlify
|
|
40
|
+
*
|
|
38
41
|
*
|
|
39
42
|
* @param options - Configuration options for the integration
|
|
40
43
|
* @param options.basePath - Base path for API routes (default: '/api')
|
package/dist/index.js
CHANGED
|
@@ -9,9 +9,9 @@ import { glob } from 'tinyglobby';
|
|
|
9
9
|
// src/integration-files.ts
|
|
10
10
|
function generateRouter(opts) {
|
|
11
11
|
const { basePath, relativeActionsPath, adapter } = opts;
|
|
12
|
-
let exportedApp =
|
|
12
|
+
let exportedApp = "export default app";
|
|
13
13
|
if (adapter === "@astrojs/netlify") {
|
|
14
|
-
exportedApp =
|
|
14
|
+
exportedApp = "export default handle(app)";
|
|
15
15
|
}
|
|
16
16
|
return `import type { HonoEnv, MergeActionKeyIntoPath } from '@gnosticdev/hono-actions/actions'
|
|
17
17
|
import { Hono } from 'hono'
|
|
@@ -103,26 +103,34 @@ export { handler as ALL }
|
|
|
103
103
|
var generateHonoClient = (port) => `
|
|
104
104
|
// Generated by Hono Actions Integration
|
|
105
105
|
import type { HonoRouter } from './router.js'
|
|
106
|
-
import {
|
|
107
|
-
import type { DetailedError } from 'hono/client'
|
|
106
|
+
import { parseResponse, hc } from 'hono/client'
|
|
107
|
+
import type { DetailedError, ClientRequestOptions } from 'hono/client'
|
|
108
108
|
|
|
109
109
|
function getBaseUrl() {
|
|
110
|
-
// client side can just use the
|
|
110
|
+
// client side can just use the origin
|
|
111
111
|
if (typeof window !== 'undefined') {
|
|
112
|
-
return
|
|
112
|
+
return window.location.origin
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
// dev server (dev server) needs to know the port
|
|
116
116
|
if (import.meta.env.DEV) {
|
|
117
|
-
return
|
|
117
|
+
return 'http://localhost:${port}'
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
// server side (production) needs full url
|
|
121
|
-
|
|
121
|
+
if (import.meta.env.SITE) {
|
|
122
|
+
return import.meta.env.SITE
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return '/'
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { parseResponse }
|
|
129
|
+
export type { DetailedError, ClientRequestOptions, HonoRouter }
|
|
130
|
+
export const honoClient = createHonoClient<HonoRouter>(getBaseUrl())
|
|
131
|
+
export function createHonoClient<T extends HonoRouter = HonoRouter>(basePath: string, fetchOptions?: ClientRequestOptions) {
|
|
132
|
+
return hc<T>(basePath, fetchOptions)
|
|
122
133
|
}
|
|
123
|
-
export { parseResponse, hc }
|
|
124
|
-
export type { DetailedError }
|
|
125
|
-
export const honoClient = hc<HonoRouter>(getBaseUrl())
|
|
126
134
|
`;
|
|
127
135
|
var generateIntegrationTypes = (adapter) => {
|
|
128
136
|
let actionTypes = `
|
|
@@ -137,9 +145,26 @@ var generateIntegrationTypes = (adapter) => {
|
|
|
137
145
|
let clientTypes = `
|
|
138
146
|
// Generated by Hono Actions Integration
|
|
139
147
|
declare module '@gnosticdev/hono-actions/client' {
|
|
148
|
+
/**
|
|
149
|
+
* Default hono client using the base url from the environment
|
|
150
|
+
* **Note** if running in production, the \`siteUrl\` option from the astro config will be used.
|
|
151
|
+
* If customization is needed, use the \`createHonoClient\` function instead.
|
|
152
|
+
*/
|
|
140
153
|
export const honoClient: typeof import('./client').honoClient
|
|
154
|
+
/**
|
|
155
|
+
* Helper function to parse the response from the Hono client
|
|
156
|
+
*/
|
|
141
157
|
export const parseResponse: typeof import('./client').parseResponse
|
|
158
|
+
/**
|
|
159
|
+
* Create a new hono client with custom base url + fetch options
|
|
160
|
+
*/
|
|
161
|
+
export const createHonoClient: typeof import('./client').createHonoClient
|
|
142
162
|
export type DetailedError = import('./client').DetailedError
|
|
163
|
+
export type ClientRequestOptions = import('./client').ClientRequestOptions
|
|
164
|
+
/**
|
|
165
|
+
* The hono actions routes. for use in the hono client
|
|
166
|
+
*/
|
|
167
|
+
export type HonoRouter = import('./client').HonoRouter
|
|
143
168
|
}
|
|
144
169
|
`;
|
|
145
170
|
switch (adapter) {
|
|
@@ -276,6 +301,9 @@ ${ACTION_PATTERNS.map((p) => ` - ${p}`).join("\n")}`
|
|
|
276
301
|
codeGenDir.pathname,
|
|
277
302
|
"client.ts"
|
|
278
303
|
);
|
|
304
|
+
if (!config.site) {
|
|
305
|
+
logger.warn("No site url found in astro config, add one if you want to use the hono client with SSR");
|
|
306
|
+
}
|
|
279
307
|
const clientContent = generateHonoClient(config.server.port);
|
|
280
308
|
await fs.writeFile(clientPathAbs, clientContent, "utf-8");
|
|
281
309
|
addVirtualImports(params, {
|
package/package.json
CHANGED
|
@@ -7,20 +7,18 @@
|
|
|
7
7
|
"@hono/standard-validator": "^0.2.0",
|
|
8
8
|
"@hono/zod-validator": "^0.2.2",
|
|
9
9
|
"astro-integration-kit": "^0.19.0",
|
|
10
|
-
"hono": "^4.10.6",
|
|
11
10
|
"tinyglobby": "^0.2.15"
|
|
12
11
|
},
|
|
13
12
|
"description": "Define server actions with built-in validation, error handling, and a pre-built hono client for calling the routes.",
|
|
14
13
|
"devDependencies": {
|
|
15
|
-
"tsup": "^8.5.0",
|
|
16
|
-
"typescript": "catalog:",
|
|
17
|
-
"vitest": "catalog:",
|
|
18
14
|
"@astrojs/cloudflare": "catalog:",
|
|
19
15
|
"@astrojs/netlify": "catalog:",
|
|
20
16
|
"@astrojs/node": "catalog:",
|
|
21
17
|
"@astrojs/vercel": "catalog:",
|
|
18
|
+
"@biomejs/biome": "catalog:",
|
|
22
19
|
"astro": "catalog:",
|
|
23
|
-
"
|
|
20
|
+
"tsup": "^8.5.0",
|
|
21
|
+
"typescript": "catalog:"
|
|
24
22
|
},
|
|
25
23
|
"exports": {
|
|
26
24
|
".": {
|
|
@@ -50,7 +48,8 @@
|
|
|
50
48
|
"main": "./dist/index.js",
|
|
51
49
|
"name": "@gnosticdev/hono-actions",
|
|
52
50
|
"peerDependencies": {
|
|
53
|
-
"astro": "^5.13.0"
|
|
51
|
+
"astro": "^5.13.0",
|
|
52
|
+
"hono": "^4.10.6"
|
|
54
53
|
},
|
|
55
54
|
"publishConfig": {
|
|
56
55
|
"access": "public"
|
|
@@ -62,5 +61,5 @@
|
|
|
62
61
|
},
|
|
63
62
|
"type": "module",
|
|
64
63
|
"types": "./dist/index.d.ts",
|
|
65
|
-
"version": "2.0.
|
|
64
|
+
"version": "2.0.8"
|
|
66
65
|
}
|