@hyperspan/framework 1.0.8 → 1.0.10
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 +1 -1
- package/src/client/_hs/hyperspan-actions.client.ts +1 -1
- package/src/middleware.ts +8 -2
- package/src/server.ts +12 -2
- package/src/types.ts +8 -1
package/package.json
CHANGED
|
@@ -91,7 +91,7 @@ function formSubmitToRoute(e: Event, form: HTMLFormElement, opts: TFormSubmitOpt
|
|
|
91
91
|
|
|
92
92
|
const target = content.includes('<html') ? window.document.body : hsActionTag || form;
|
|
93
93
|
|
|
94
|
-
Idiomorph.morph(target, content);
|
|
94
|
+
Idiomorph.morph(target, content, { morphStyle: 'innerHTML' });
|
|
95
95
|
opts.afterResponse && opts.afterResponse();
|
|
96
96
|
lazyLoadScripts();
|
|
97
97
|
});
|
package/src/middleware.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { formDataToJSON } from './utils';
|
|
2
2
|
import { z, flattenError, prettifyError } from 'zod/v4';
|
|
3
|
+
import { HTTPResponseException } from './server';
|
|
3
4
|
|
|
4
5
|
import type { ZodAny, ZodObject, ZodError } from 'zod/v4';
|
|
5
6
|
import type { Hyperspan as HS } from './types';
|
|
6
|
-
import { HTTPResponseException } from './server';
|
|
7
7
|
|
|
8
8
|
export type TValidationType = 'json' | 'form' | 'urlencoded';
|
|
9
9
|
|
|
@@ -25,7 +25,7 @@ function inferValidationType(headers: Headers): TValidationType {
|
|
|
25
25
|
return 'json';
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export class ZodValidationError extends Error {
|
|
28
|
+
export class ZodValidationError extends Error implements HS.ZodValidationError {
|
|
29
29
|
public fieldErrors;
|
|
30
30
|
public formErrors: unknown[];
|
|
31
31
|
constructor(error: ZodError, schema: ZodObject | ZodAny) {
|
|
@@ -38,6 +38,9 @@ export class ZodValidationError extends Error {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Validate the query parameters of the request using a Zod schema.
|
|
43
|
+
*/
|
|
41
44
|
export function validateQuery(schema: ZodObject | ZodAny): HS.MiddlewareFunction {
|
|
42
45
|
return async (context: HS.Context, next: HS.NextFunction) => {
|
|
43
46
|
const query = formDataToJSON(context.req.query);
|
|
@@ -55,6 +58,9 @@ export function validateQuery(schema: ZodObject | ZodAny): HS.MiddlewareFunction
|
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Validate the body of the request using a Zod schema. Type can be inferred from the Content-Type header or provided explicitly.
|
|
63
|
+
*/
|
|
58
64
|
export function validateBody(schema: ZodObject | ZodAny, type?: TValidationType): HS.MiddlewareFunction {
|
|
59
65
|
return async (context: HS.Context, next: HS.NextFunction) => {
|
|
60
66
|
// Infer type from Content-Type header if not provided
|
package/src/server.ts
CHANGED
|
@@ -253,8 +253,18 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
253
253
|
return context.res.error(new Error('Method not allowed'), { status: 405 });
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
//
|
|
257
|
-
|
|
256
|
+
// Run the route handler
|
|
257
|
+
let routeContent: unknown;
|
|
258
|
+
try {
|
|
259
|
+
routeContent = await handler(context);
|
|
260
|
+
} catch (e) {
|
|
261
|
+
// Return the response from the HTTPResponseException if it exists
|
|
262
|
+
if (e instanceof HTTPResponseException) {
|
|
263
|
+
routeContent = e._response;
|
|
264
|
+
} else {
|
|
265
|
+
throw e;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
258
268
|
|
|
259
269
|
// Return Response if returned from route handler
|
|
260
270
|
if (routeContent instanceof Response) {
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { HSHtml } from '@hyperspan/html';
|
|
2
|
-
import { ZodValidationError } from './middleware';
|
|
3
2
|
import * as z from 'zod/v4';
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -198,4 +197,12 @@ export namespace Hyperspan {
|
|
|
198
197
|
*/
|
|
199
198
|
renderScriptTag: (loadScript?: ((module: unknown) => HSHtml | string | void) | string) => HSHtml;
|
|
200
199
|
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Zod validation error type. Used in actions and validation middleware.
|
|
203
|
+
*/
|
|
204
|
+
export interface ZodValidationError extends Error {
|
|
205
|
+
fieldErrors: Record<string, string[] | undefined>;
|
|
206
|
+
formErrors: unknown[];
|
|
207
|
+
}
|
|
201
208
|
}
|