@hyperspan/framework 1.0.21 → 1.0.23
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/package.json +2 -2
- package/src/actions.test.ts +29 -1
- package/src/actions.ts +15 -7
- package/src/middleware.ts +2 -2
- package/src/types.ts +28 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperspan/framework",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.23",
|
|
4
4
|
"description": "Hyperspan Web Framework",
|
|
5
5
|
"main": "src/server.ts",
|
|
6
6
|
"types": "src/server.ts",
|
|
@@ -84,6 +84,6 @@
|
|
|
84
84
|
"@hyperspan/html": "^1.0.1",
|
|
85
85
|
"debug": "^4.4.3",
|
|
86
86
|
"isbot": "^5.1.32",
|
|
87
|
-
"zod": "^4.
|
|
87
|
+
"zod": "^4.4.3"
|
|
88
88
|
}
|
|
89
89
|
}
|
package/src/actions.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { test, expect, describe } from 'bun:test';
|
|
|
2
2
|
import { createAction } from './actions';
|
|
3
3
|
import { html, render, type HSHtml } from '@hyperspan/html';
|
|
4
4
|
import { createContext } from './server';
|
|
5
|
-
import * as z from 'zod
|
|
5
|
+
import * as z from 'zod';
|
|
6
6
|
|
|
7
7
|
describe('createAction', () => {
|
|
8
8
|
test('creates an action with a simple form and no schema', async () => {
|
|
@@ -71,6 +71,34 @@ describe('createAction', () => {
|
|
|
71
71
|
expect(responseText).toContain('<p>Hello, John Doe!</p>');
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
+
test('returns HSHtml directly from POST handler', async () => {
|
|
75
|
+
const action = createAction({
|
|
76
|
+
name: 'test',
|
|
77
|
+
}).form((c) => {
|
|
78
|
+
return html`
|
|
79
|
+
<form>
|
|
80
|
+
<input type="text" name="name" />
|
|
81
|
+
<button type="submit">Submit</button>
|
|
82
|
+
</form>
|
|
83
|
+
`;
|
|
84
|
+
}).post(async (c, { data }) => {
|
|
85
|
+
return html`<p>Hello, ${data?.name}!</p>`;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const formData = new FormData();
|
|
89
|
+
formData.append('name', 'Jane Doe');
|
|
90
|
+
|
|
91
|
+
const request = new Request(`http://localhost:3000${action._path()}`, {
|
|
92
|
+
method: 'POST',
|
|
93
|
+
body: formData,
|
|
94
|
+
});
|
|
95
|
+
const response = await action.fetch(request);
|
|
96
|
+
expect(response).toBeInstanceOf(Response);
|
|
97
|
+
expect(response.status).toBe(200);
|
|
98
|
+
const responseText = await response.text();
|
|
99
|
+
expect(responseText).toContain('<p>Hello, Jane Doe!</p>');
|
|
100
|
+
});
|
|
101
|
+
|
|
74
102
|
test('errors thrown on POST handler provided by user are caught and rendered', async () => {
|
|
75
103
|
const action = createAction({
|
|
76
104
|
name: 'test',
|
package/src/actions.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { html } from '@hyperspan/html';
|
|
2
2
|
import { createRoute, HTTPResponseException, returnHTMLResponse } from './server';
|
|
3
|
-
import * as z from 'zod
|
|
3
|
+
import * as z from 'zod';
|
|
4
4
|
import type { Hyperspan as HS } from './types';
|
|
5
5
|
import { assetHash, formDataToJSON } from './utils';
|
|
6
6
|
import { buildClientJS } from './client/js';
|
|
@@ -23,12 +23,20 @@ const actionsClientJS = await buildClientJS(import.meta.resolve('./client/_hs/hy
|
|
|
23
23
|
* 5. Replaces form content in place with HTML response content from server via the Idiomorph library
|
|
24
24
|
* 6. Handles any Exception thrown on server as error displayed back to user on the page
|
|
25
25
|
*/
|
|
26
|
-
export function createAction<
|
|
26
|
+
export function createAction<S extends z.ZodType>(
|
|
27
|
+
params: { name: string; schema: S }
|
|
28
|
+
): HS.Action<S>;
|
|
29
|
+
export function createAction(
|
|
30
|
+
params: { name: string; schema?: undefined }
|
|
31
|
+
): HS.Action<undefined>;
|
|
32
|
+
export function createAction<S extends z.ZodType>(
|
|
33
|
+
params: { name: string; schema?: S }
|
|
34
|
+
): HS.Action<S | undefined> {
|
|
27
35
|
const { name, schema } = params;
|
|
28
36
|
const path = `/__actions/${assetHash(name)}`;
|
|
29
37
|
|
|
30
|
-
let _handler: Parameters<HS.Action<
|
|
31
|
-
let _errorHandler: Parameters<HS.Action<
|
|
38
|
+
let _handler: Parameters<HS.Action<S | undefined>['post']>[0] | null = null;
|
|
39
|
+
let _errorHandler: Parameters<HS.Action<S | undefined>['errorHandler']>[0] | null = null;
|
|
32
40
|
|
|
33
41
|
const route = createRoute({ path, name })
|
|
34
42
|
.get((c: HS.Context) => api.render(c))
|
|
@@ -37,7 +45,7 @@ export function createAction<T extends z.ZodObject<any, any>>(params: { name: st
|
|
|
37
45
|
throw new Error('Action POST handler not set! Every action must have a POST handler.');
|
|
38
46
|
}
|
|
39
47
|
|
|
40
|
-
const data = c.vars.body as
|
|
48
|
+
const data = c.vars.body as HS.InferActionData<S> || formDataToJSON(await c.req.formData()) || {};
|
|
41
49
|
log('POST handler', { data });
|
|
42
50
|
const response = await _handler(c, { data });
|
|
43
51
|
log('POST handler response', { response });
|
|
@@ -57,7 +65,7 @@ export function createAction<T extends z.ZodObject<any, any>>(params: { name: st
|
|
|
57
65
|
* Custom error handler for the action since validateBody() throws a HTTPResponseException
|
|
58
66
|
*/
|
|
59
67
|
.errorHandler(async (c: HS.Context, err: HTTPResponseException) => {
|
|
60
|
-
const data = c.vars.body as
|
|
68
|
+
const data = c.vars.body as HS.InferActionData<S> || formDataToJSON(await c.req.formData()) || {};
|
|
61
69
|
const error = err._error as ZodValidationError || err;
|
|
62
70
|
|
|
63
71
|
// Set the status to 400 if it's a ZodValidationError, otherwise 500 (Error thrown by user POST handler)
|
|
@@ -73,7 +81,7 @@ export function createAction<T extends z.ZodObject<any, any>>(params: { name: st
|
|
|
73
81
|
// Set the name of the action for the route
|
|
74
82
|
route._config.name = name;
|
|
75
83
|
|
|
76
|
-
const api: HS.Action<
|
|
84
|
+
const api: HS.Action<S | undefined> = {
|
|
77
85
|
_kind: 'hsAction',
|
|
78
86
|
_config: route._config,
|
|
79
87
|
_path() {
|
package/src/middleware.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { formDataToJSON } from './utils';
|
|
2
|
-
import { z, flattenError, prettifyError } from 'zod
|
|
2
|
+
import { z, flattenError, prettifyError } from 'zod';
|
|
3
3
|
import { HTTPResponseException } from './server';
|
|
4
4
|
|
|
5
|
-
import type { ZodAny, ZodObject, ZodError } from 'zod
|
|
5
|
+
import type { ZodAny, ZodObject, ZodError } from 'zod';
|
|
6
6
|
import type { Hyperspan as HS } from './types';
|
|
7
7
|
|
|
8
8
|
export type TValidationType = 'json' | 'form' | 'urlencoded';
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HSHtml } from '@hyperspan/html';
|
|
2
|
-
import * as z from 'zod
|
|
2
|
+
import * as z from 'zod';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Hyperspan Types
|
|
@@ -175,30 +175,41 @@ export namespace Hyperspan {
|
|
|
175
175
|
/**
|
|
176
176
|
* Action = Form + route handler
|
|
177
177
|
*/
|
|
178
|
+
/** Raw form field values when an action has no schema */
|
|
179
|
+
export type ActionFormValues = Record<string, string | string[]>;
|
|
180
|
+
/** Infer validated action data from an optional schema */
|
|
181
|
+
export type InferActionData<S extends z.ZodType | undefined> =
|
|
182
|
+
S extends z.ZodType ? z.output<S> : ActionFormValues;
|
|
178
183
|
// Form renderer
|
|
179
184
|
export type ActionFormResponse = HSHtml | void | null | Promise<HSHtml | void | null>;
|
|
180
|
-
export type ActionFormProps<
|
|
181
|
-
|
|
182
|
-
|
|
185
|
+
export type ActionFormProps<S extends z.ZodType | undefined = undefined> = {
|
|
186
|
+
data?: Partial<InferActionData<S>>;
|
|
187
|
+
error?: ZodValidationError;
|
|
188
|
+
};
|
|
189
|
+
export type ActionForm<S extends z.ZodType | undefined = undefined> = (
|
|
190
|
+
c: Context, props: ActionFormProps<S>
|
|
183
191
|
) => ActionFormResponse;
|
|
184
192
|
// Form handler
|
|
185
|
-
export type
|
|
186
|
-
export type ActionFormHandlerProps<
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
193
|
+
export type ActionFormHandlerReturn = RouteHandlerReturn;
|
|
194
|
+
export type ActionFormHandlerProps<S extends z.ZodType | undefined = undefined> = {
|
|
195
|
+
data: InferActionData<S>;
|
|
196
|
+
error?: ZodValidationError | Error;
|
|
197
|
+
};
|
|
198
|
+
export type ActionFormHandler<S extends z.ZodType | undefined = undefined> = (
|
|
199
|
+
c: Context, props: ActionFormHandlerProps<S>
|
|
200
|
+
) => ActionFormHandlerReturn | Promise<ActionFormHandlerReturn>;
|
|
190
201
|
// Action API
|
|
191
|
-
export interface Action<
|
|
202
|
+
export interface Action<S extends z.ZodType | undefined = undefined> {
|
|
192
203
|
_kind: 'hsAction';
|
|
193
204
|
_config: Partial<Hyperspan.RouteConfig>;
|
|
194
205
|
_path(): string;
|
|
195
|
-
_form: null | ActionForm<
|
|
196
|
-
form(form: ActionForm<
|
|
197
|
-
render: (c: Context, props?: ActionFormProps<
|
|
198
|
-
post: (handler: ActionFormHandler<
|
|
199
|
-
errorHandler: (handler: ActionFormHandler<
|
|
200
|
-
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Action<
|
|
201
|
-
middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Action<
|
|
206
|
+
_form: null | ActionForm<S>;
|
|
207
|
+
form(form: ActionForm<S>): Action<S>;
|
|
208
|
+
render: (c: Context, props?: ActionFormProps<S>) => ActionFormResponse;
|
|
209
|
+
post: (handler: ActionFormHandler<S>) => Action<S>;
|
|
210
|
+
errorHandler: (handler: ActionFormHandler<S>) => Action<S>;
|
|
211
|
+
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Action<S>;
|
|
212
|
+
middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Action<S>;
|
|
202
213
|
fetch: (request: Request) => Promise<Response>;
|
|
203
214
|
}
|
|
204
215
|
|