@finesoft/front 0.1.3 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,754 +1,8 @@
1
+ import { FrameworkConfig, Framework, BasePage, PrefetchedIntent } from './browser.cjs';
2
+ export { ACTION_KINDS, Action, ActionDispatcher, ActionHandler, ActionHandlerDependencies, AsyncMapper, BaseController, BaseItem, BaseLogger, BaseShelf, BrowserAppConfig, CompositeLogger, CompositeLoggerFactory, CompoundAction, ConsoleLogger, ConsoleLoggerFactory, Container, DEP_KEYS, ExternalUrlAction, ExternalUrlDependencies, FeatureFlags, FlowAction, FlowActionCallbacks, FlowActionDependencies, History, HttpClient, HttpClientConfig, HttpError, Intent, IntentController, IntentDispatcher, Locale, Logger, LoggerFactory, Logger as LoggerInterface, LruMap, Mapper, MetricsRecorder, Net, None, Optional, PrefetchedIntents, RouteDefinition, RouteMatch, Router, Storage, buildUrl, createPrefetchedIntentsFromDom, defineRoutes, deserializeServerData, generateUuid, getBaseUrl, isCompoundAction, isExternalUrlAction, isFlowAction, isNone, isSome, makeDependencies, makeExternalUrlAction, makeFlowAction, mapEach, pipe, pipeAsync, registerActionHandlers, registerExternalUrlHandler, registerFlowActionHandler, removeHost, removeQueryParams, removeScheme, resetFilterCache, shouldLog, stableStringify, startBrowserApp, tryScroll } from './browser.cjs';
1
3
  import { Hono } from 'hono';
2
4
  import { ViteDevServer } from 'vite';
3
5
 
4
- /**
5
- * Action 类型定义
6
- *
7
- * FlowAction — SPA 内部导航
8
- * ExternalUrlAction — 打开外部链接
9
- * CompoundAction — 组合多个 Action
10
- */
11
- /** Action Kind 常量 */
12
- declare const ACTION_KINDS: {
13
- FLOW: "flow";
14
- EXTERNAL_URL: "externalUrl";
15
- COMPOUND: "compound";
16
- };
17
- /** FlowAction — SPA 导航 */
18
- interface FlowAction {
19
- kind: typeof ACTION_KINDS.FLOW;
20
- url: string;
21
- /** 展示方式: 默认 push,modal 弹窗 */
22
- presentationContext?: "default" | "modal";
23
- }
24
- /** ExternalUrlAction — 外部链接 */
25
- interface ExternalUrlAction {
26
- kind: typeof ACTION_KINDS.EXTERNAL_URL;
27
- url: string;
28
- }
29
- /** CompoundAction — 组合 Action */
30
- interface CompoundAction {
31
- kind: typeof ACTION_KINDS.COMPOUND;
32
- actions: Action[];
33
- }
34
- /** 所有 Action 的联合类型 */
35
- type Action = FlowAction | ExternalUrlAction | CompoundAction;
36
- declare function isFlowAction(action: Action): action is FlowAction;
37
- declare function isExternalUrlAction(action: Action): action is ExternalUrlAction;
38
- declare function isCompoundAction(action: Action): action is CompoundAction;
39
- declare function makeFlowAction(url: string, presentationContext?: FlowAction["presentationContext"]): FlowAction;
40
- declare function makeExternalUrlAction(url: string): ExternalUrlAction;
41
-
42
- /**
43
- * ActionDispatcher — Action 分发器
44
- *
45
- * 注册不同 kind 的 handler,按类型分发。
46
- * CompoundAction 自动展开递归执行。
47
- */
48
-
49
- /** Action 处理器函数类型 */
50
- type ActionHandler<A extends Action = Action> = (action: A) => Promise<void> | void;
51
- declare class ActionDispatcher {
52
- private handlers;
53
- private wiredActions;
54
- /** 注册指定 kind 的 handler(防止重复注册) */
55
- onAction<A extends Action>(kind: string, handler: ActionHandler<A>): void;
56
- /** 执行一个 Action(CompoundAction 递归展开) */
57
- perform(action: Action): Promise<void>;
58
- }
59
-
60
- /**
61
- * Container — 通用的依赖注入容器
62
- */
63
- type Factory<T> = () => T;
64
- declare class Container {
65
- private registrations;
66
- /** 注册依赖(默认单例) */
67
- register<T>(key: string, factory: Factory<T>, singleton?: boolean): this;
68
- /** 解析依赖 */
69
- resolve<T>(key: string): T;
70
- /** 检查是否已注册 */
71
- has(key: string): boolean;
72
- /** 销毁容器,清除所有缓存 */
73
- dispose(): void;
74
- }
75
-
76
- /**
77
- * Intent 类型定义
78
- */
79
-
80
- /** Intent — 描述一个用户意图 */
81
- interface Intent<T = unknown> {
82
- /** Intent 标识符(用于匹配 Controller) */
83
- id: string;
84
- /** 意图参数 */
85
- params?: Record<string, string>;
86
- /** 预期返回的数据(仅用于类型推断) */
87
- _returnType?: T;
88
- }
89
- /** Intent Controller — 处理特定 intentId 的业务逻辑 */
90
- interface IntentController<T = unknown> {
91
- /** Controller 对应的 Intent ID */
92
- intentId: string;
93
- /** 执行意图,返回页面数据 */
94
- perform(intent: Intent<T>, container: Container): Promise<T> | T;
95
- }
96
-
97
- /**
98
- * IntentDispatcher — Intent 分发器
99
- *
100
- * 注册 IntentController,按 intentId 分发。
101
- */
102
-
103
- declare class IntentDispatcher {
104
- private controllers;
105
- /** 注册一个 IntentController */
106
- register(controller: IntentController): void;
107
- /** 分发 Intent 到对应 Controller */
108
- dispatch<T>(intent: Intent<T>, container: Container): Promise<T>;
109
- /** 检查是否已注册某个 Intent */
110
- has(intentId: string): boolean;
111
- }
112
-
113
- /** 日志级别 */
114
- type Level = "debug" | "info" | "warn" | "error";
115
- /**
116
- * Logger 接口
117
- *
118
- * 所有方法返回空字符串,允许在模板中内联使用而不渲染文本。
119
- */
120
- interface Logger {
121
- debug(...args: unknown[]): string;
122
- info(...args: unknown[]): string;
123
- warn(...args: unknown[]): string;
124
- error(...args: unknown[]): string;
125
- }
126
- interface LoggerFactory {
127
- loggerFor(category: string): Logger;
128
- }
129
-
130
- /**
131
- * 依赖工厂 — 创建所有基础依赖
132
- */
133
-
134
- /** 网络请求层 */
135
- interface Net {
136
- fetch(url: string, options?: RequestInit): Promise<Response>;
137
- }
138
- /** 多语言状态 */
139
- interface Locale {
140
- language: string;
141
- storefront: string;
142
- setActiveLocale(language: string, storefront: string): void;
143
- }
144
- /** 存储接口 */
145
- interface Storage {
146
- get(key: string): string | undefined;
147
- set(key: string, value: string): void;
148
- delete(key: string): void;
149
- }
150
- /** Feature Flags */
151
- interface FeatureFlags {
152
- isEnabled(key: string): boolean;
153
- getString(key: string): string | undefined;
154
- getNumber(key: string): number | undefined;
155
- }
156
- /** Metrics 记录器 */
157
- interface MetricsRecorder {
158
- recordPageView(page: string, fields?: Record<string, unknown>): void;
159
- recordEvent(name: string, fields?: Record<string, unknown>): void;
160
- }
161
- declare const DEP_KEYS: {
162
- readonly LOGGER: "logger";
163
- readonly LOGGER_FACTORY: "loggerFactory";
164
- readonly NET: "net";
165
- readonly LOCALE: "locale";
166
- readonly STORAGE: "storage";
167
- readonly FEATURE_FLAGS: "featureFlags";
168
- readonly METRICS: "metrics";
169
- readonly FETCH: "fetch";
170
- };
171
- interface MakeDependenciesOptions {
172
- fetch?: typeof globalThis.fetch;
173
- language?: string;
174
- storefront?: string;
175
- featureFlags?: Record<string, boolean | string | number>;
176
- }
177
- declare function makeDependencies(container: Container, options?: MakeDependenciesOptions): void;
178
-
179
- /**
180
- * URL 路由器 — URL pattern → Intent + FlowAction
181
- */
182
-
183
- /** 路由匹配结果 */
184
- interface RouteMatch {
185
- intent: Intent;
186
- action: FlowAction;
187
- }
188
- declare class Router {
189
- private routes;
190
- /** 添加路由规则 */
191
- add(pattern: string, intentId: string): this;
192
- /** 解析 URL → RouteMatch */
193
- resolve(urlOrPath: string): RouteMatch | null;
194
- /** 获取所有已注册的路由 */
195
- getRoutes(): string[];
196
- private extractPath;
197
- private extractQueryParams;
198
- }
199
-
200
- /**
201
- * BaseLogger — 抽象日志基类
202
- */
203
-
204
- declare abstract class BaseLogger implements Logger {
205
- protected category: string;
206
- constructor(category: string);
207
- abstract debug(...args: unknown[]): string;
208
- abstract info(...args: unknown[]): string;
209
- abstract warn(...args: unknown[]): string;
210
- abstract error(...args: unknown[]): string;
211
- }
212
-
213
- /**
214
- * Composite Logger — 组合日志,广播到多个后端
215
- */
216
-
217
- declare class CompositeLoggerFactory implements LoggerFactory {
218
- private readonly factories;
219
- constructor(factories: LoggerFactory[]);
220
- loggerFor(name: string): Logger;
221
- }
222
- declare class CompositeLogger implements Logger {
223
- private readonly loggers;
224
- constructor(loggers: Logger[]);
225
- debug(...args: unknown[]): string;
226
- info(...args: unknown[]): string;
227
- warn(...args: unknown[]): string;
228
- error(...args: unknown[]): string;
229
- private callAll;
230
- }
231
-
232
- /**
233
- * ConsoleLogger — 基于 console 的日志实现
234
- */
235
-
236
- declare class ConsoleLogger extends BaseLogger {
237
- debug(...args: unknown[]): string;
238
- info(...args: unknown[]): string;
239
- warn(...args: unknown[]): string;
240
- error(...args: unknown[]): string;
241
- }
242
- declare class ConsoleLoggerFactory implements LoggerFactory {
243
- loggerFor(category: string): Logger;
244
- }
245
-
246
- /**
247
- * localStorage 日志级别过滤
248
- *
249
- * 通过 localStorage.onyxLog 控制日志级别:
250
- * '*=info' — 全局 info 及以上
251
- * '*=info,Foo=off' — 全局 info,Foo 静默
252
- * 'Bar=error,Baz=warn' — Bar 只输出 error,Baz 输出 warn+
253
- */
254
-
255
- declare function shouldLog(name: string, level: Level): boolean;
256
- declare function resetFilterCache(): void;
257
-
258
- /**
259
- * BasePage — 所有页面共享的基础属性
260
- *
261
- * 具体页面类型由应用层定义并扩展此接口。
262
- */
263
- interface BasePage {
264
- id: string;
265
- pageType: string;
266
- title: string;
267
- description?: string;
268
- url?: string;
269
- }
270
-
271
- /**
272
- * PrefetchedIntents — SSR 数据缓存
273
- *
274
- * 服务端渲染时将 Intent→Data 映射序列化嵌入 HTML,
275
- * 客户端 hydrate 时提取缓存。Framework.dispatch() 优先查缓存,
276
- * 命中则直接返回,未命中则走 Controller 调度。
277
- */
278
-
279
- /** 预获取的 Intent-Data 对 */
280
- interface PrefetchedIntent {
281
- intent: Intent;
282
- data: unknown;
283
- }
284
- declare class PrefetchedIntents {
285
- private intents;
286
- private constructor();
287
- /** 从 PrefetchedIntent 数组创建缓存实例 */
288
- static fromArray(items: PrefetchedIntent[]): PrefetchedIntents;
289
- /** 创建空缓存实例 */
290
- static empty(): PrefetchedIntents;
291
- /**
292
- * 获取缓存的 Intent 结果(一次性使用)。
293
- * 命中后从缓存中删除。
294
- */
295
- get<T>(intent: Intent<T>): T | undefined;
296
- /** 检查缓存中是否有某个 Intent 的数据 */
297
- has(intent: Intent): boolean;
298
- /** 缓存中的条目数 */
299
- get size(): number;
300
- }
301
-
302
- /**
303
- * Framework — 框架核心类
304
- *
305
- * 对应原版 Jet 类,统一管理: DI 容器、Intent 分发、Action 分发、路由、Metrics。
306
- * 纯 TypeScript,不依赖任何 UI 框架。
307
- */
308
-
309
- /** Framework 初始化配置 */
310
- interface FrameworkConfig extends MakeDependenciesOptions {
311
- setupRoutes?: (router: Router) => void;
312
- prefetchedIntents?: PrefetchedIntents;
313
- }
314
- declare class Framework {
315
- readonly container: Container;
316
- readonly intentDispatcher: IntentDispatcher;
317
- readonly actionDispatcher: ActionDispatcher;
318
- readonly router: Router;
319
- readonly prefetchedIntents: PrefetchedIntents;
320
- private constructor();
321
- /** 创建并初始化 Framework 实例 */
322
- static create(config?: FrameworkConfig): Framework;
323
- /** 分发 Intent — 获取页面数据 */
324
- dispatch<T>(intent: Intent<T>): Promise<T>;
325
- /** 执行 Action — 处理用户交互 */
326
- perform(action: Action): Promise<void>;
327
- /** 路由 URL — 将 URL 解析为 Intent + Action */
328
- routeUrl(url: string): RouteMatch | null;
329
- /** 记录页面访问事件 */
330
- didEnterPage(page: BasePage): void;
331
- /** 注册 Action 处理器 */
332
- onAction<A extends Action>(kind: string, handler: ActionHandler<A>): void;
333
- /** 注册 Intent Controller */
334
- registerIntent(controller: IntentController): void;
335
- /** 销毁 Framework 实例 */
336
- dispose(): void;
337
- }
338
-
339
- /**
340
- * BaseShelf / BaseItem — 所有 Shelf 和 Item 共享的基础属性
341
- *
342
- * 具体 Shelf/Item 类型由应用层定义并扩展此接口。
343
- */
344
-
345
- interface BaseShelf {
346
- id: string;
347
- shelfType: string;
348
- title?: string;
349
- subtitle?: string;
350
- seeAllAction?: Action;
351
- isHorizontal?: boolean;
352
- }
353
- interface BaseItem {
354
- id: string;
355
- itemType: string;
356
- clickAction?: Action;
357
- }
358
-
359
- /**
360
- * stableStringify — 确定性 JSON 序列化(keys 按字母排序)
361
- *
362
- * 用作缓存 key:相同内容的对象始终产生相同字符串。
363
- */
364
- declare function stableStringify(obj: unknown): string;
365
-
366
- /**
367
- * HttpClient — 通用 HTTP 客户端基类
368
- *
369
- * 为 API Client 提供标准化的 HTTP 请求能力。
370
- * 子类继承后只需关注业务端点定义,不需要重复实现 fetch / JSON 解析 / 错误处理。
371
- */
372
- /** HTTP 请求错误 */
373
- declare class HttpError extends Error {
374
- readonly status: number;
375
- readonly statusText: string;
376
- readonly body?: string | undefined;
377
- constructor(status: number, statusText: string, body?: string | undefined);
378
- }
379
- /** HttpClient 构造配置 */
380
- interface HttpClientConfig {
381
- /** API base URL(如 "/api" 或 "https://example.com/api") */
382
- baseUrl: string;
383
- /** 默认请求头 */
384
- defaultHeaders?: Record<string, string>;
385
- /** 自定义 fetch 实现(便于测试或 SSR) */
386
- fetch?: typeof globalThis.fetch;
387
- }
388
- /**
389
- * 通用 HTTP 客户端基类
390
- *
391
- * 使用方式: 创建子类继承 HttpClient,定义业务方法调用 this.get() / this.post() 等。
392
- *
393
- * @example
394
- * ```ts
395
- * class MyApiClient extends HttpClient {
396
- * async getUser(id: string) {
397
- * return this.get<User>(`/users/${id}`);
398
- * }
399
- * }
400
- * ```
401
- */
402
- declare abstract class HttpClient {
403
- protected readonly baseUrl: string;
404
- protected readonly defaultHeaders: Record<string, string>;
405
- protected readonly fetchFn: typeof globalThis.fetch;
406
- constructor(config: HttpClientConfig);
407
- /** GET 请求,返回解析后的 JSON */
408
- protected get<T>(path: string, params?: Record<string, string>): Promise<T>;
409
- /** POST 请求,自动序列化 body 为 JSON */
410
- protected post<T>(path: string, body?: unknown, params?: Record<string, string>): Promise<T>;
411
- /** PUT 请求 */
412
- protected put<T>(path: string, body?: unknown, params?: Record<string, string>): Promise<T>;
413
- /** DELETE 请求 */
414
- protected del<T>(path: string, params?: Record<string, string>): Promise<T>;
415
- /**
416
- * 底层请求方法 — 子类可覆写以自定义行为
417
- *
418
- * 自动处理:
419
- * - URL 拼接 (baseUrl + path + params)
420
- * - 默认 headers 合并
421
- * - JSON body 序列化
422
- * - 响应 JSON 解析
423
- * - 非 2xx 状态码抛出 HttpError
424
- */
425
- protected request<T>(method: string, path: string, options?: {
426
- params?: Record<string, string>;
427
- body?: unknown;
428
- headers?: Record<string, string>;
429
- }): Promise<T>;
430
- /** 构建完整 URL — 子类可覆写以自定义 URL 拼接逻辑 */
431
- protected buildUrl(path: string, params?: Record<string, string>): string;
432
- }
433
-
434
- /**
435
- * BaseController — 抽象 Intent Controller 基类
436
- *
437
- * 提供标准化的 try/catch → fallback 模式。
438
- * 子类只需实现 execute() 和可选的 fallback()。
439
- */
440
-
441
- /**
442
- * 抽象 Controller 基类
443
- *
444
- * 统一处理:
445
- * - 类型安全的参数提取 (TParams)
446
- * - 返回类型约束 (TResult)
447
- * - try/catch 错误处理 + 可选 fallback
448
- *
449
- * @example
450
- * ```ts
451
- * class ProductController extends BaseController<{ productId: string }, ProductPage> {
452
- * readonly intentId = "product-page";
453
- *
454
- * async execute(params: { productId: string }, container: Container) {
455
- * const api = container.resolve<ApiClient>("api");
456
- * return api.getProduct(params.productId);
457
- * }
458
- *
459
- * fallback(params: { productId: string }, error: Error) {
460
- * return getMockProduct(params.productId);
461
- * }
462
- * }
463
- * ```
464
- */
465
- declare abstract class BaseController<TParams extends Record<string, string | undefined> = Record<string, string>, TResult = unknown> implements IntentController<TResult> {
466
- /** Controller 对应的 Intent ID */
467
- abstract readonly intentId: string;
468
- /**
469
- * 执行业务逻辑 — 子类必须实现
470
- *
471
- * @param params - Intent 参数(已类型化)
472
- * @param container - DI 容器
473
- * @returns 页面数据
474
- */
475
- abstract execute(params: TParams, container: Container): Promise<TResult> | TResult;
476
- /**
477
- * 错误回退 — 子类可选覆写
478
- *
479
- * 当 execute() 抛出异常时调用。
480
- * 默认行为: 重新抛出原始错误。
481
- *
482
- * @param params - Intent 参数
483
- * @param error - execute() 抛出的错误
484
- * @returns 回退数据
485
- */
486
- fallback(params: TParams, error: Error): Promise<TResult> | TResult;
487
- /**
488
- * IntentController.perform() 实现
489
- *
490
- * 自动 try/catch → fallback 模式。
491
- */
492
- perform(intent: Intent<TResult>, container: Container): Promise<TResult>;
493
- }
494
-
495
- /**
496
- * Mapper 类型工具 — 标准化数据转换管线
497
- *
498
- * 提供类型约定和组合函数,让 API 响应 → 页面模型 的转换有统一的签名模式。
499
- */
500
- /** 同步映射函数 */
501
- type Mapper<TInput, TOutput> = (input: TInput) => TOutput;
502
- /** 异步映射函数 */
503
- type AsyncMapper<TInput, TOutput> = (input: TInput) => TOutput | Promise<TOutput>;
504
- /**
505
- * 组合两个同步 Mapper: A → B → C
506
- */
507
- declare function pipe<A, B, C>(m1: Mapper<A, B>, m2: Mapper<B, C>): Mapper<A, C>;
508
- /**
509
- * 组合三个同步 Mapper: A → B → C → D
510
- */
511
- declare function pipe<A, B, C, D>(m1: Mapper<A, B>, m2: Mapper<B, C>, m3: Mapper<C, D>): Mapper<A, D>;
512
- /**
513
- * 组合四个同步 Mapper: A → B → C → D → E
514
- */
515
- declare function pipe<A, B, C, D, E>(m1: Mapper<A, B>, m2: Mapper<B, C>, m3: Mapper<C, D>, m4: Mapper<D, E>): Mapper<A, E>;
516
- /**
517
- * 组合任意数量的同步 Mapper
518
- */
519
- declare function pipe(...mappers: Mapper<unknown, unknown>[]): Mapper<unknown, unknown>;
520
- /**
521
- * 组合两个可能异步的 Mapper: A → B → C
522
- */
523
- declare function pipeAsync<A, B, C>(m1: AsyncMapper<A, B>, m2: AsyncMapper<B, C>): AsyncMapper<A, C>;
524
- /**
525
- * 组合三个可能异步的 Mapper
526
- */
527
- declare function pipeAsync<A, B, C, D>(m1: AsyncMapper<A, B>, m2: AsyncMapper<B, C>, m3: AsyncMapper<C, D>): AsyncMapper<A, D>;
528
- /**
529
- * 将一个 Mapper 应用到数组的每个元素
530
- */
531
- declare function mapEach<TInput, TOutput>(mapper: Mapper<TInput, TOutput>): Mapper<TInput[], TOutput[]>;
532
-
533
- /**
534
- * defineRoutes — 声明式路由 + Controller 注册
535
- *
536
- * 将命令式的路由注册 (20+ 行 framework.router.add / framework.registerIntent)
537
- * 简化为声明式配置数组。
538
- */
539
-
540
- /** 单条路由定义 */
541
- interface RouteDefinition {
542
- /** URL pattern (如 "/product/:productId") */
543
- path: string;
544
- /** Intent ID (如 "product-page") */
545
- intentId: string;
546
- /**
547
- * Controller 实例(可选)。
548
- * 同一个 intentId 的多条路由只需在第一条提供 controller。
549
- */
550
- controller?: IntentController;
551
- }
552
- /**
553
- * 声明式注册路由和 Controller
554
- *
555
- * - 自动去重: 同一 intentId 的 controller 只注册一次
556
- * - 路由和 controller 在同一个配置数组中,方便检查一致性
557
- *
558
- * @example
559
- * ```ts
560
- * defineRoutes(framework, [
561
- * { path: "/", intentId: "home", controller: new HomeController() },
562
- * { path: "/product/:id", intentId: "product", controller: new ProductController() },
563
- * { path: "/search", intentId: "search", controller: new SearchController() },
564
- * { path: "/charts/:type", intentId: "charts", controller: new ChartsController() },
565
- * { path: "/charts", intentId: "charts" }, // 同 intentId,不需要重复 controller
566
- * ]);
567
- * ```
568
- */
569
- declare function defineRoutes(framework: Framework, definitions: RouteDefinition[]): void;
570
-
571
- /**
572
- * LruMap — 固定容量的 LRU 缓存
573
- */
574
- declare class LruMap<K, V> {
575
- private map;
576
- private readonly capacity;
577
- constructor(capacity: number);
578
- get(key: K): V | undefined;
579
- set(key: K, value: V): void;
580
- has(key: K): boolean;
581
- delete(key: K): boolean;
582
- get size(): number;
583
- clear(): void;
584
- }
585
-
586
- /**
587
- * Optional 类型工具
588
- */
589
- type None = null | undefined;
590
- type Optional<T> = T | None;
591
- declare function isSome<T>(value: Optional<T>): value is T;
592
- declare function isNone<T>(value: Optional<T>): value is None;
593
-
594
- /**
595
- * URL 工具函数
596
- */
597
- /** 移除 URL scheme (https://, http://) */
598
- declare function removeScheme(url: string): string;
599
- /** 移除 URL host 部分,保留路径 */
600
- declare function removeHost(url: string): string;
601
- /** 移除 query 参数 */
602
- declare function removeQueryParams(url: string): string;
603
- /** 获取 URL 的基础路径(无 query、hash) */
604
- declare function getBaseUrl(url: string): string;
605
- /** 构建 URL(路径 + query 参数) */
606
- declare function buildUrl(path: string, params?: Record<string, string | undefined>): string;
607
-
608
- /**
609
- * UUID v4 生成器
610
- */
611
- declare function generateUuid(): string;
612
-
613
- /**
614
- * ExternalUrl Action Handler — 外部链接处理器
615
- */
616
-
617
- interface ExternalUrlDependencies {
618
- framework: Framework;
619
- log: Logger;
620
- }
621
- declare function registerExternalUrlHandler(deps: ExternalUrlDependencies): void;
622
-
623
- /**
624
- * FlowAction Handler — 核心 SPA 导航处理器
625
- *
626
- * 通过 FlowActionCallbacks 注入 UI 更新回调,
627
- * 不直接依赖任何 UI 框架的 store。
628
- */
629
-
630
- /** UI 框架回调 — 解耦 Svelte store 等依赖 */
631
- interface FlowActionCallbacks {
632
- /** 导航后更新当前路径(替代 currentPath.set()) */
633
- onNavigate(pathname: string): void;
634
- /** 模态页面展示(替代 openModal()) */
635
- onModal(page: BasePage): void;
636
- }
637
- /** 注册 FlowAction handler 所需的依赖 */
638
- interface FlowActionDependencies {
639
- framework: Framework;
640
- log: Logger;
641
- callbacks: FlowActionCallbacks;
642
- /** 更新应用 UI 的回调,page 可以是 Promise */
643
- updateApp: (props: {
644
- page: Promise<BasePage> | BasePage;
645
- isFirstPage?: boolean;
646
- }) => void;
647
- }
648
- declare function registerFlowActionHandler(deps: FlowActionDependencies): void;
649
-
650
- /**
651
- * Action Handler 统一注册入口
652
- */
653
-
654
- interface ActionHandlerDependencies {
655
- framework: Framework;
656
- log: Logger;
657
- callbacks: FlowActionCallbacks;
658
- updateApp: (props: {
659
- page: Promise<BasePage> | BasePage;
660
- isFirstPage?: boolean;
661
- }) => void;
662
- }
663
- declare function registerActionHandlers(deps: ActionHandlerDependencies): void;
664
-
665
- /**
666
- * startBrowserApp — 客户端 hydration 一站式启动
667
- *
668
- * 封装:
669
- * 1. PrefetchedIntents 从 DOM 提取
670
- * 2. Framework 创建 + 引导
671
- * 3. 应用层挂载(通过 mount 回调,框架无关)
672
- * 4. Action handlers 注册
673
- * 5. 初始页面触发
674
- */
675
-
676
- interface BrowserAppConfig {
677
- /** 注册 controllers 和路由的引导函数 */
678
- bootstrap: (framework: Framework) => void;
679
- /** 默认语言(回退值) */
680
- defaultLocale?: string;
681
- /**
682
- * 挂载应用到 DOM
683
- *
684
- * 框架无关 — Svelte / React / Vue 均可通过此回调实现。
685
- *
686
- * @param target - DOM 挂载点
687
- * @param context - Framework 实例 + 语言
688
- * @returns 更新函数,用于后续页面切换
689
- */
690
- mount: (target: HTMLElement, context: {
691
- framework: Framework;
692
- locale: string;
693
- }) => (props: {
694
- page: Promise<BasePage> | BasePage;
695
- isFirstPage?: boolean;
696
- }) => void;
697
- /** FlowAction / ExternalUrl 回调 */
698
- callbacks: FlowActionCallbacks;
699
- }
700
- /**
701
- * 启动客户端应用
702
- *
703
- * 自动执行 hydration 全流程。
704
- */
705
- declare function startBrowserApp(config: BrowserAppConfig): Promise<void>;
706
-
707
- /**
708
- * History 管理器 — 浏览器历史状态 + 滚动位置
709
- */
710
-
711
- interface HistoryOptions {
712
- getScrollablePageElement: () => HTMLElement | null;
713
- }
714
- declare class History<State> {
715
- private readonly entries;
716
- private readonly log;
717
- private readonly getScrollablePageElement;
718
- private currentStateId;
719
- constructor(log: Logger, options: HistoryOptions, sizeLimit?: number);
720
- replaceState(state: State, url: string): void;
721
- pushState(state: State, url: string): void;
722
- beforeTransition(): void;
723
- onPopState(listener: (url: string, state?: State) => void): void;
724
- updateState(update: (current?: State) => State): void;
725
- private get scrollTop();
726
- private set scrollTop(value);
727
- }
728
-
729
- /**
730
- * tryScroll — 渐进式滚动位置恢复
731
- *
732
- * 使用 rAF 循环等待页面高度足够后恢复滚动位置。
733
- */
734
-
735
- declare function tryScroll(log: Logger, getScrollableElement: () => HTMLElement | null, scrollY: number): void;
736
-
737
- /**
738
- * Server Data (browser side) — 从 DOM 反序列化服务端嵌入数据
739
- */
740
-
741
- /**
742
- * 从 DOM 反序列化服务端嵌入的数据。
743
- * 读取 `<script id="serialized-server-data">` 的内容并移除标签。
744
- */
745
- declare function deserializeServerData(): PrefetchedIntent[] | undefined;
746
- /**
747
- * 从 DOM 提取 SSR 数据并构建 PrefetchedIntents 实例。
748
- * 替代原来的 PrefetchedIntents.fromDom()。
749
- */
750
- declare function createPrefetchedIntentsFromDom(): PrefetchedIntents;
751
-
752
6
  /**
753
7
  * ssrRender — 通用 SSR 渲染管线
754
8
  *
@@ -817,6 +71,13 @@ declare function createSSRRender(config: SSRRenderConfig): (url: string, locale:
817
71
  /**
818
72
  * injectSSRContent — 将 SSR 渲染结果注入 HTML 模板
819
73
  */
74
+ /** SSR HTML 模板占位符常量 */
75
+ declare const SSR_PLACEHOLDERS: {
76
+ readonly LANG: "<!--ssr-lang-->";
77
+ readonly HEAD: "<!--ssr-head-->";
78
+ readonly BODY: "<!--ssr-body-->";
79
+ readonly DATA: "<!--ssr-data-->";
80
+ };
820
81
  interface InjectSSROptions {
821
82
  template: string;
822
83
  locale: string;
@@ -952,9 +213,15 @@ interface StartServerOptions {
952
213
  vite?: ViteDevServer;
953
214
  /** 运行时信息(可选,不传时自动检测) */
954
215
  runtime?: RuntimeInfo;
216
+ /** 已注册的路由列表(用于启动日志) */
217
+ routes?: string[];
218
+ /** 支持的语言列表(用于启动日志) */
219
+ locales?: string[];
220
+ /** SSR 入口路径(用于启动日志) */
221
+ ssrEntryPath?: string;
955
222
  }
956
223
  declare function startServer(options: StartServerOptions): Promise<{
957
224
  vite?: ViteDevServer;
958
225
  }>;
959
226
 
960
- export { ACTION_KINDS, type Action, ActionDispatcher, type ActionHandler, type ActionHandlerDependencies, type AsyncMapper, BaseController, type BaseItem, BaseLogger, type BasePage, type BaseShelf, type BrowserAppConfig, CompositeLogger, CompositeLoggerFactory, type CompoundAction, ConsoleLogger, ConsoleLoggerFactory, Container, DEP_KEYS, type ExternalUrlAction, type ExternalUrlDependencies, type FeatureFlags, type FlowAction, type FlowActionCallbacks, type FlowActionDependencies, Framework, type FrameworkConfig, History, HttpClient, type HttpClientConfig, HttpError, type InjectSSROptions, type Intent, type IntentController, IntentDispatcher, type Locale, type Logger, type LoggerFactory, type Logger as LoggerInterface, LruMap, type Mapper, type MetricsRecorder, type Net, type None, type Optional, type PrefetchedIntent, PrefetchedIntents, type RouteDefinition, type RouteMatch, Router, type RuntimeInfo, type SSRAppOptions, type SSRModule, type SSRRenderConfig, type SSRRenderOptions, type SSRRenderResult, type ServerConfig, type ServerInstance, type StartServerOptions, type Storage, buildUrl, createPrefetchedIntentsFromDom, createSSRApp, createSSRRender, createServer, defineRoutes, deserializeServerData, detectRuntime, generateUuid, getBaseUrl, injectSSRContent, isCompoundAction, isExternalUrlAction, isFlowAction, isNone, isSome, makeDependencies, makeExternalUrlAction, makeFlowAction, mapEach, parseAcceptLanguage, pipe, pipeAsync, registerActionHandlers, registerExternalUrlHandler, registerFlowActionHandler, removeHost, removeQueryParams, removeScheme, resetFilterCache, resolveRoot, serializeServerData, shouldLog, ssrRender, stableStringify, startBrowserApp, startServer, tryScroll };
227
+ export { BasePage, Framework, FrameworkConfig, type InjectSSROptions, PrefetchedIntent, type RuntimeInfo, type SSRAppOptions, type SSRModule, type SSRRenderConfig, type SSRRenderOptions, type SSRRenderResult, SSR_PLACEHOLDERS, type ServerConfig, type ServerInstance, type StartServerOptions, createSSRApp, createSSRRender, createServer, detectRuntime, injectSSRContent, parseAcceptLanguage, resolveRoot, serializeServerData, ssrRender, startServer };