update add no path and random path
This commit is contained in:
28
demo/simple/src/nopath.ts
Normal file
28
demo/simple/src/nopath.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Route, App } from '@kevisual/router/src/index.ts';
|
||||
|
||||
|
||||
const app = new App();
|
||||
|
||||
app.route({
|
||||
description: 'sdf'
|
||||
}).define(async (ctx) => {
|
||||
ctx.body = 'this is no path fns';
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
|
||||
let id = ''
|
||||
console.log('routes', app.router.routes.map(item => {
|
||||
id = item.id;
|
||||
return {
|
||||
path: item.path,
|
||||
key: item.key,
|
||||
id: item.id,
|
||||
description: item.description
|
||||
}
|
||||
}))
|
||||
|
||||
app.call({id: id}).then(res => {
|
||||
console.log('id', id);
|
||||
console.log('res', res);
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.29",
|
||||
"version": "0.0.30",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"main": "./dist/router.js",
|
||||
|
||||
@@ -82,7 +82,7 @@ export class App<T = {}, U = AppReqRes> {
|
||||
}
|
||||
return new Route(path, key, opts);
|
||||
}
|
||||
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 }) {
|
||||
const router = this.router;
|
||||
return await router.call(message, ctx);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ export { Server, handleServer } from './server/index.ts';
|
||||
*/
|
||||
export { CustomError } from './result/error.ts';
|
||||
|
||||
export { Rule, Schema, createSchema } from './validator/index.ts';
|
||||
export { createSchema } from './validator/index.ts';
|
||||
|
||||
export type { Rule, Schema, } from './validator/index.ts';
|
||||
|
||||
export { App } from './app.ts';
|
||||
|
||||
|
||||
58
src/route.ts
58
src/route.ts
@@ -1,4 +1,4 @@
|
||||
import { nanoid } from 'nanoid';
|
||||
import { nanoid, random } from 'nanoid';
|
||||
import { CustomError } from './result/error.ts';
|
||||
import { Schema, Rule, createSchema } from './validator/index.ts';
|
||||
import { pick } from './utils/pick.ts';
|
||||
@@ -88,7 +88,7 @@ export type RouteOpts = {
|
||||
* }
|
||||
*/
|
||||
validator?: { [key: string]: Rule };
|
||||
schema?: { [key: string]: Schema<any> };
|
||||
schema?: { [key: string]: any };
|
||||
isVerify?: boolean;
|
||||
verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
|
||||
verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
|
||||
@@ -123,7 +123,7 @@ export class Route<U = { [key: string]: any }> {
|
||||
middleware?: RouteMiddleware[]; // middleware
|
||||
type? = 'route';
|
||||
private _validator?: { [key: string]: Rule };
|
||||
schema?: { [key: string]: Schema<any> };
|
||||
schema?: { [key: string]: any };
|
||||
data?: any;
|
||||
/**
|
||||
* 是否需要验证
|
||||
@@ -134,6 +134,9 @@ export class Route<U = { [key: string]: any }> {
|
||||
*/
|
||||
isDebug?: boolean;
|
||||
constructor(path: string, key: string = '', opts?: RouteOpts) {
|
||||
if (!path) {
|
||||
path = nanoid(8)
|
||||
}
|
||||
path = path.trim();
|
||||
key = key.trim();
|
||||
this.path = path;
|
||||
@@ -261,6 +264,17 @@ export class Route<U = { [key: string]: any }> {
|
||||
this.validator = validator;
|
||||
return this;
|
||||
}
|
||||
prompt(description: string): this;
|
||||
prompt(description: Function): this;
|
||||
prompt(...args: any[]) {
|
||||
const [description] = args;
|
||||
if (typeof description === 'string') {
|
||||
this.description = description;
|
||||
} else if (typeof description === 'function') {
|
||||
this.description = description() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
||||
}
|
||||
return this;
|
||||
}
|
||||
define<T extends { [key: string]: any } = RouterContextT>(opts: DefineRouteOpts): this;
|
||||
define<T extends { [key: string]: any } = RouterContextT>(fn: Run<T & U>): this;
|
||||
define<T extends { [key: string]: any } = RouterContextT>(key: string, fn: Run<T & U>): this;
|
||||
@@ -304,6 +318,27 @@ export class Route<U = { [key: string]: any }> {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
update(opts: DefineRouteOpts, checkList?: string[]): this {
|
||||
const keys = Object.keys(opts);
|
||||
const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
|
||||
checkList = checkList || defaultCheckList;
|
||||
for (let item of keys) {
|
||||
if (!checkList.includes(item)) {
|
||||
continue;
|
||||
}
|
||||
if (item === 'validator') {
|
||||
this.validator = opts[item];
|
||||
continue;
|
||||
}
|
||||
if (item === 'middleware') {
|
||||
this.middleware = this.middleware.concat(opts[item]);
|
||||
continue;
|
||||
}
|
||||
this[item] = opts[item];
|
||||
}
|
||||
return this;
|
||||
}
|
||||
addTo(router: QueryRouter | { add: (route: Route) => void;[key: string]: any }) {
|
||||
router.add(this);
|
||||
}
|
||||
@@ -583,8 +618,9 @@ export class QueryRouter {
|
||||
} else {
|
||||
return { code: 404, body: null, message: 'Not found route' };
|
||||
}
|
||||
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
||||
} else if (path) {
|
||||
return await this.parse({ ...message, path }, { ...this.context, ...ctx });
|
||||
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
||||
} else {
|
||||
return { code: 404, body: null, message: 'Not found path' };
|
||||
}
|
||||
@@ -596,7 +632,7 @@ export class QueryRouter {
|
||||
* @param ctx
|
||||
* @returns
|
||||
*/
|
||||
async queryRoute(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||
async queryRoute(message: { id?: string; path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||
const res = await this.parse(message, { ...this.context, ...ctx });
|
||||
return {
|
||||
code: res.code,
|
||||
@@ -621,9 +657,19 @@ export class QueryRouter {
|
||||
* 获取handle函数, 这里会去执行parse函数
|
||||
*/
|
||||
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
|
||||
return async (msg: { path: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
|
||||
return async (msg: { id?: string; path?: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
|
||||
try {
|
||||
const context = { ...ctx, ...handleContext };
|
||||
if (msg.id) {
|
||||
const route = router.routes.find((r) => r.id === msg.id);
|
||||
if (route) {
|
||||
msg.path = route.path;
|
||||
msg.key = route.key;
|
||||
} else {
|
||||
return { code: 404, message: 'Not found route' };
|
||||
}
|
||||
}
|
||||
// @ts-ignore
|
||||
const res = await router.parse(msg, context);
|
||||
if (wrapperFn) {
|
||||
res.data = res.body;
|
||||
|
||||
Reference in New Issue
Block a user