@hyperspan/framework 1.0.0-alpha.0 → 1.0.0-alpha.1
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/server.ts +10 -0
- package/src/middleware/zod.ts +0 -46
package/package.json
CHANGED
package/src/server.ts
CHANGED
|
@@ -356,6 +356,16 @@ export function isRunnableRoute(route: unknown): boolean {
|
|
|
356
356
|
return 'hsRoute' === obj?._kind && 'fetch' in obj;
|
|
357
357
|
}
|
|
358
358
|
|
|
359
|
+
/**
|
|
360
|
+
* Is valid route path to add to server?
|
|
361
|
+
*/
|
|
362
|
+
export function isValidRoutePath(path: string): boolean {
|
|
363
|
+
const isHiddenRoute = path.includes('/__');
|
|
364
|
+
const isTestFile = path.includes('.test') || path.includes('.spec');
|
|
365
|
+
|
|
366
|
+
return !isHiddenRoute && !isTestFile;
|
|
367
|
+
}
|
|
368
|
+
|
|
359
369
|
/**
|
|
360
370
|
* Basic error handling
|
|
361
371
|
* @TODO: Should check for and load user-customizeable template with special name (app/__error.ts ?)
|
package/src/middleware/zod.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { Hyperspan as HS } from '../types';
|
|
2
|
-
import type { ZodAny, ZodObject, ZodError } from 'zod/v4';
|
|
3
|
-
import { flattenError } from 'zod/v4';
|
|
4
|
-
|
|
5
|
-
export class ZodValidationError extends Error {
|
|
6
|
-
constructor(flattened: ReturnType<typeof flattenError>) {
|
|
7
|
-
super('Input validation error(s)');
|
|
8
|
-
this.name = 'ZodValidationError';
|
|
9
|
-
|
|
10
|
-
// Copy all properties from flattened error
|
|
11
|
-
Object.assign(this, flattened);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function validateQuery(schema: ZodObject): HS.MiddlewareFunction {
|
|
16
|
-
return async (context: HS.Context, next: HS.NextFunction) => {
|
|
17
|
-
const query: Record<string, string> = Object.fromEntries(context.req.query.entries());
|
|
18
|
-
const validated = schema.safeParse(query);
|
|
19
|
-
|
|
20
|
-
if (!validated.success) {
|
|
21
|
-
const err = formatZodError(validated.error);
|
|
22
|
-
return context.res.error(err, { status: 400 });
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return next();
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function validateBody(schema: ZodObject | ZodAny): HS.MiddlewareFunction {
|
|
30
|
-
return async (context: HS.Context, next: HS.NextFunction) => {
|
|
31
|
-
const body = await context.req.body;
|
|
32
|
-
const validated = schema.safeParse(body);
|
|
33
|
-
|
|
34
|
-
if (!validated.success) {
|
|
35
|
-
const err = formatZodError(validated.error);
|
|
36
|
-
return context.res.error(err, { status: 400 });
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return next();
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function formatZodError(error: ZodError): ZodValidationError {
|
|
44
|
-
const zodError = flattenError(error);
|
|
45
|
-
return new ZodValidationError(zodError);
|
|
46
|
-
}
|