router添加 throw

This commit is contained in:
xion 2024-11-10 19:03:56 +08:00
parent df737e5f27
commit 303c579e92
4 changed files with 22 additions and 2 deletions

5
.gitignore vendored
View File

@ -1,3 +1,6 @@
node_modules
src/app.config.json5
dist
dist
.npmrc
.turbo

View File

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

View File

@ -1,6 +1,8 @@
import { QueryRouter, Route, RouteContext, RouteOpts } from './route.ts';
import { Server, Cors } from './server/server.ts';
import { WsServer } from './server/ws-server.ts';
import { CustomError } from './result/error.ts';
type RouterHandle = (msg: { path: string; [key: string]: any }) => { code: string; data?: any; message?: string; [key: string]: any };
type AppOptions<T = {}> = {
router?: QueryRouter;
@ -90,4 +92,9 @@ export class App<T = {}> {
importApp(app: App) {
this.importRoutes(app.exportRoutes());
}
throw(code?: number | string, message?: string, tips?: string): void;
throw(...args: any[]) {
throw new CustomError(...args);
}
}

View File

@ -32,6 +32,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
error?: any;
call?: (message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
index?: number;
throw?: (code?: number | string, message?: string, tips?: string) => void;
} & T;
export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
@ -252,6 +253,10 @@ export class Route {
this.data = data;
return this;
}
throw(code?: number | string, message?: string, tips?: string): void;
throw(...args: any[]) {
throw new CustomError(...args);
}
}
export class QueryRouter {
@ -465,6 +470,7 @@ export class QueryRouter {
ctx = ctx || {};
ctx.query = { ...ctx.query, ...query, ...payload };
ctx.state = {};
ctx.throw = this.throw;
// put queryRouter to ctx
// TODO: 是否需要queryRouter函数内部处理router路由执行这应该是避免去内部去包含的功能过
ctx.queryRouter = this;
@ -505,6 +511,10 @@ export class QueryRouter {
importRouter(router: QueryRouter) {
this.importRoutes(router.routes);
}
throw(code?: number | string, message?: string, tips?: string): void;
throw(...args: any[]) {
throw new CustomError(...args);
}
}
type QueryRouterServerOpts = {