router 更新

This commit is contained in:
熊潇 2025-03-02 16:51:07 +08:00
parent bac3e5b393
commit ba7e00bd7a
4 changed files with 54 additions and 15 deletions

View File

@ -0,0 +1,14 @@
import { createSchema } from '@kevisual/router';
const a = createSchema({
type: 'string',
minLength: 1,
maxLength: 10,
regex: '^[a-zA-Z0-9_]+$',
required: false,
});
console.log(a.safeParse('1234567890'));
console.log(a.safeParse('').error);
console.log(a.safeParse(undefined));
console.log(a.safeParse(null).error);

View File

@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/package", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.8-alpha.1", "version": "0.0.8-alpha.3",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.js", "module": "dist/index.js",

View File

@ -31,7 +31,10 @@ export type RouteContext<T = { code?: number }, S = any> = {
queryRouter?: QueryRouter; queryRouter?: QueryRouter;
error?: any; error?: any;
/** 请求 route的返回结果包函ctx */ /** 请求 route的返回结果包函ctx */
call?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>; call?: (
message: { path: string; key?: string; payload?: any; [key: string]: any } | { id: string; apyload?: any; [key: string]: any },
ctx?: RouteContext & { [key: string]: any },
) => Promise<any>;
/** 请求 route的返回结果不包函ctx */ /** 请求 route的返回结果不包函ctx */
queryRoute?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>; queryRoute?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
index?: number; index?: number;
@ -130,6 +133,7 @@ export class Route<U = { [key: string]: any }> {
this.isDebug = opts?.isDebug ?? false; this.isDebug = opts?.isDebug ?? false;
} }
private createSchema() { private createSchema() {
try {
const validator = this.validator; const validator = this.validator;
const keys = Object.keys(validator || {}); const keys = Object.keys(validator || {});
const schemaList = keys.map((key) => { const schemaList = keys.map((key) => {
@ -139,6 +143,9 @@ export class Route<U = { [key: string]: any }> {
return { ...prev, ...current }; return { ...prev, ...current };
}, {}); }, {});
this.schema = schema; this.schema = schema;
} catch (e) {
console.error('createSchema error:', e);
}
} }
/** /**
@ -518,9 +525,24 @@ export class QueryRouter {
* @param ctx * @param ctx
* @returns * @returns
*/ */
async call(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) { async call(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
return await this.parse(message, { ...this.context, ...ctx }); let path = message.path;
let key = message.key;
if (message.id) {
const route = this.routes.find((r) => r.id === message.id);
if (route) {
path = route.path;
key = route.key;
} else {
return { code: 404, body: null, message: 'Not found route' };
} }
} else if (path) {
return await this.parse({ ...message, path }, { ...this.context, ...ctx });
} else {
return { code: 404, body: null, message: 'Not found path' };
}
}
/** /**
* result * result
* @param message * @param message

View File

@ -68,9 +68,12 @@ export const schemaFormRule = (rule: Rule): z.ZodType<any, any, any> => {
}; };
export const createSchema = (rule: Rule): Schema => { export const createSchema = (rule: Rule): Schema => {
try { try {
rule.required = rule.required || false; rule.required = rule.required ?? false;
if (!rule.required) { if (!rule.required) {
return schemaFormRule(rule).nullable(); // nullable is null
// nullish is null or undefined
// optional is undefined
return schemaFormRule(rule).optional();
} }
return schemaFormRule(rule); return schemaFormRule(rule);
} catch (e) { } catch (e) {