From d2ebb5f4882cb88f5044c406150d3b5303e92122 Mon Sep 17 00:00:00 2001 From: xion Date: Thu, 12 Jun 2025 15:02:16 +0800 Subject: [PATCH] fix: add app-barowser for browser --- rollup.config.js | 4 ++-- src/app-browser.ts | 5 +++++ src/route.ts | 11 ++++++++--- src/utils/is-engine.ts | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 src/app-browser.ts create mode 100644 src/utils/is-engine.ts diff --git a/rollup.config.js b/rollup.config.js index 0f6502e..f96da71 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -60,7 +60,7 @@ export default [ plugins: [dts()], }, { - input: 'src/browser.ts', + input: 'src/app-browser.ts', output: { file: 'dist/router-browser.js', format: 'es', @@ -74,7 +74,7 @@ export default [ ], }, { - input: 'src/browser.ts', + input: 'src/app-browser.ts', output: { file: 'dist/router-browser.d.ts', format: 'es', diff --git a/src/app-browser.ts b/src/app-browser.ts new file mode 100644 index 0000000..2c5d17c --- /dev/null +++ b/src/app-browser.ts @@ -0,0 +1,5 @@ +import { QueryRouterServer } from './browser.ts'; + +export const App = QueryRouterServer; + +export * from './browser.ts'; diff --git a/src/route.ts b/src/route.ts index d3063c7..007a656 100644 --- a/src/route.ts +++ b/src/route.ts @@ -54,7 +54,7 @@ export type RouteContext = { queryRoute?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise; index?: number; throw?: (code?: number | string, message?: string, tips?: string) => void; - /** 是否需要序列化 */ + /** 是否需要序列化, 使用JSON.stringify和JSON.parse */ needSerialize?: boolean; } & T; export type SimpleObject = Record; @@ -603,7 +603,12 @@ export class QueryRouter { message: res.message, }; } - async setContext(ctx: RouteContext) { + /** + * 设置上下文 + * @description 这里的上下文是为了在handle函数中使用 + * @param ctx + */ + setContext(ctx: RouteContext) { this.context = ctx; } getList(): RouteInfo[] { @@ -647,7 +652,7 @@ export class QueryRouter { throw(...args: any[]) { throw new CustomError(...args); } - hasRoute(path: string, key?: string) { + hasRoute(path: string, key: string = '') { return this.routes.find((r) => r.path === path && r.key === key); } } diff --git a/src/utils/is-engine.ts b/src/utils/is-engine.ts new file mode 100644 index 0000000..a524171 --- /dev/null +++ b/src/utils/is-engine.ts @@ -0,0 +1,15 @@ +export const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null; +export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && typeof document.createElement === 'function'; +// @ts-ignore +export const isDeno = typeof Deno !== 'undefined' && typeof Deno.version === 'object' && typeof Deno.version.deno === 'string'; + +export const getEngine = () => { + if (isNode) { + return 'node'; + } else if (isBrowser) { + return 'browser'; + } else if (isDeno) { + return 'deno'; + } + return 'unknown'; +};