router 更新
This commit is contained in:
parent
bac3e5b393
commit
ba7e00bd7a
14
demo/simple/src/check-schema/index.ts
Normal file
14
demo/simple/src/check-schema/index.ts
Normal 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);
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.8-alpha.1",
|
||||
"version": "0.0.8-alpha.3",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
|
46
src/route.ts
46
src/route.ts
@ -31,7 +31,10 @@ export type RouteContext<T = { code?: number }, S = any> = {
|
||||
queryRouter?: QueryRouter;
|
||||
error?: any;
|
||||
/** 请求 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 */
|
||||
queryRoute?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
|
||||
index?: number;
|
||||
@ -130,15 +133,19 @@ export class Route<U = { [key: string]: any }> {
|
||||
this.isDebug = opts?.isDebug ?? false;
|
||||
}
|
||||
private createSchema() {
|
||||
const validator = this.validator;
|
||||
const keys = Object.keys(validator || {});
|
||||
const schemaList = keys.map((key) => {
|
||||
return { [key]: createSchema(validator[key]) };
|
||||
});
|
||||
const schema = schemaList.reduce((prev, current) => {
|
||||
return { ...prev, ...current };
|
||||
}, {});
|
||||
this.schema = schema;
|
||||
try {
|
||||
const validator = this.validator;
|
||||
const keys = Object.keys(validator || {});
|
||||
const schemaList = keys.map((key) => {
|
||||
return { [key]: createSchema(validator[key]) };
|
||||
});
|
||||
const schema = schemaList.reduce((prev, current) => {
|
||||
return { ...prev, ...current };
|
||||
}, {});
|
||||
this.schema = schema;
|
||||
} catch (e) {
|
||||
console.error('createSchema error:', e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -518,9 +525,24 @@ export class QueryRouter {
|
||||
* @param ctx
|
||||
* @returns
|
||||
*/
|
||||
async call(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||
return await this.parse(message, { ...this.context, ...ctx });
|
||||
async call(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||
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 的数据
|
||||
* @param message
|
||||
|
@ -68,9 +68,12 @@ export const schemaFormRule = (rule: Rule): z.ZodType<any, any, any> => {
|
||||
};
|
||||
export const createSchema = (rule: Rule): Schema => {
|
||||
try {
|
||||
rule.required = rule.required || false;
|
||||
rule.required = rule.required ?? false;
|
||||
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);
|
||||
} catch (e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user