From af7d809270c8b6ee4d92889ac35feb06bf029226 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 21 Feb 2026 00:26:21 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=E8=87=B30.0.83=EF=BC=8C=E5=B9=B6=E5=9C=A8=20CustomErr?= =?UTF-8?q?or=20=E7=B1=BB=E4=B8=AD=E6=B7=BB=E5=8A=A0=E9=9D=99=E6=80=81=20t?= =?UTF-8?q?hrow=20=E6=96=B9=E6=B3=95=E4=BB=A5=E5=A2=9E=E5=BC=BA=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/result/error.ts | 22 ++++++++++++++++++++++ src/route.ts | 26 ++++++-------------------- src/test/chat.ts | 2 +- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 5297a58..fb0b98e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "@kevisual/router", - "version": "0.0.82", + "version": "0.0.83", "description": "", "type": "module", "main": "./dist/router.js", diff --git a/src/result/error.ts b/src/result/error.ts index 975c636..c821904 100644 --- a/src/result/error.ts +++ b/src/result/error.ts @@ -47,6 +47,22 @@ export class CustomError extends Error { static isError(error: unknown): error is CustomError { return error instanceof CustomError || (typeof error === 'object' && error !== null && 'code' in error); } + static throw(code?: number | string, message?: string): void; + static throw(code?: number | string, opts?: CustomErrorOptions): void; + static throw(opts?: CustomErrorOptions): void; + static throw(...args: any[]) { + const [args0, args1] = args; + if (args0 && typeof args0 === 'object') { + throw new CustomError(args0); + } + if (args1 && typeof args1 === 'object') { + throw new CustomError(args0, args1); + } else if (args1) { + throw new CustomError(args0, { message: args1 }); + } + // args1 不存在; + throw new CustomError(args0); + } parse(e?: CustomError) { if (e) { return CustomError.parseError(e); @@ -61,6 +77,12 @@ export class CustomError extends Error { } } +export interface throwError { + throw(code?: number | string, message?: string): void; + throw(code?: number | string, opts?: CustomErrorOptions): void; + throw(opts?: CustomErrorOptions): void; +} + /* try { // diff --git a/src/route.ts b/src/route.ts index b58074b..6907a73 100644 --- a/src/route.ts +++ b/src/route.ts @@ -1,4 +1,4 @@ -import { CustomError, CustomErrorOptions } from './result/error.ts'; +import { CustomError, throwError, CustomErrorOptions } from './result/error.ts'; import { pick } from './utils/pick.ts'; import { listenProcess, MockProcess } from './utils/listen-process.ts'; import { z } from 'zod'; @@ -56,7 +56,7 @@ export type RouteContext = { /** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/ run?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise; index?: number; - throw?: (code?: number | string, message?: string, tips?: string) => void; + throw?: throwError['throw']; /** 是否需要序列化, 使用JSON.stringify和JSON.parse */ needSerialize?: boolean; } & T; @@ -123,7 +123,7 @@ export const createSkill = (skill: Skill): Skill => { export type RouteInfo = Pick; -export class Route { +export class Route implements throwError { /** * 一级路径 */ @@ -242,9 +242,8 @@ export class Route void;[key: string]: any }, opts?: AddOpts) { router.add(this, opts); } - throw(code?: number | string, message?: string, tips?: string): void; throw(...args: any[]) { - throw new CustomError(...args); + CustomError.throw(...args); } } @@ -263,7 +262,7 @@ export const fromJSONSchema = schema.fromJSONSchema; * @parmas overwrite 是否覆盖已存在的route,默认true */ export type AddOpts = { overwrite?: boolean }; -export class QueryRouter { +export class QueryRouter implements throwError { appId: string = ''; routes: Route[]; maxNextRoute = 40; @@ -610,21 +609,8 @@ export class QueryRouter { importRouter(router: QueryRouter) { this.importRoutes(router.routes); } - throw(code?: number | string, message?: string): void; - throw(code?: number | string, opts?: CustomErrorOptions): void; - throw(opts?: CustomErrorOptions): void; throw(...args: any[]) { - const [args0, args1] = args; - if (args0 && typeof args0 === 'object') { - throw new CustomError(args0); - } - if (args1 && typeof args1 === 'object') { - throw new CustomError(args0, args1); - } else if (args1) { - throw new CustomError(args0, { message: args1 }); - } - // args1 不存在; - throw new CustomError(args0); + CustomError.throw(...args); } hasRoute(path: string, key: string = '') { return this.routes.find((r) => r.path === path && r.key === key); diff --git a/src/test/chat.ts b/src/test/chat.ts index db5773b..45da1b2 100644 --- a/src/test/chat.ts +++ b/src/test/chat.ts @@ -14,4 +14,4 @@ app.prompt('获取天气的工具。\n参数是 city 为对应的城市').define export const chat = new RouterChat({ router: app.router }); -console.log(chat.chat()); \ No newline at end of file +console.log(chat.getChatPrompt()); \ No newline at end of file