fix: add app-barowser for browser

This commit is contained in:
熊潇 2025-06-12 15:02:16 +08:00
parent 324c4e9862
commit d2ebb5f488
4 changed files with 30 additions and 5 deletions

View File

@ -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',

5
src/app-browser.ts Normal file
View File

@ -0,0 +1,5 @@
import { QueryRouterServer } from './browser.ts';
export const App = QueryRouterServer;
export * from './browser.ts';

View File

@ -54,7 +54,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
queryRoute?: (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;
/** 是否需要序列化 */
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
needSerialize?: boolean;
} & T;
export type SimpleObject = Record<string, any>;
@ -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);
}
}

15
src/utils/is-engine.ts Normal file
View File

@ -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';
};