@route-forge/core 1.4.0 → 2.1.0

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,5 +1,19 @@
1
- import { I as InterceptorManager, a as InterceptorHandler, C as CacheStorage, R as RouteMeta, L as LevelRoutesResponse, b as RouteForgeOptions, c as RouteForge } from './types-BFlrOTrN.cjs';
2
- export { A as AdapterOption, d as ApiCallParams, B as BoundForge, F as Fetcher, e as ForgeApiParams, f as ForgeApiResponse, g as ForgeRequest, h as ForgeRouteMap, i as ForgeRouteName, j as LoadingChangeCallback, k as LoadingChangeEvent, l as LoadingTracker, m as RequestConfig, n as ResponseData, S as SummaryResponse, U as UseForgeApiBoundCall, o as UseForgeApiBoundReturn, p as UseForgeApiCall, q as UseForgeApiReturn, r as UseForgeByPrefixReturn } from './types-BFlrOTrN.cjs';
1
+ import { a as RouteForgeOptions, b as RouteForge, I as InterceptorManager, c as InterceptorHandler, C as CacheStorage, R as RouteMeta, L as LevelRoutesResponse } from './types-C51rXaiY.cjs';
2
+ export { A as AdapterOption, d as ApiCallParams, B as BoundForge, F as Fetcher, e as ForgeApiParams, f as ForgeApiResponse, g as ForgeRequest, h as ForgeRouteMap, i as ForgeRouteName, j as LoadingChangeCallback, k as LoadingChangeEvent, l as LoadingTracker, m as RequestConfig, n as ResponseData, S as SummaryResponse, U as UseForgeApiBoundCall, o as UseForgeApiBoundReturn, p as UseForgeApiCall, q as UseForgeApiReturn } from './types-C51rXaiY.cjs';
3
+
4
+ /**
5
+ * Route Forge 主入口:createRouteForge
6
+ * @see .docs/SPEC.md §4.1.1 ~ §4.1.4
7
+ *
8
+ * 能力清单(v1.0 MVP):
9
+ * - 按层级懒加载与隔离缓存(§4.1.2)
10
+ * - 并发去重(§4.1.4)
11
+ * - 通过路由名调用 API(§4.1.3)
12
+ * - 拦截器(§4.1.3a)
13
+ * - Adapter auto 检测 / builtin / axios / 自定义 Fetcher(§4.3)
14
+ */
15
+
16
+ declare function createRouteForge(options: RouteForgeOptions): RouteForge;
3
17
 
4
18
  /**
5
19
  * 拦截器管理器实现
@@ -45,6 +59,10 @@ declare function createInterceptorManager<TIn, TOut = TIn>(): InterceptorManager
45
59
  * - 每层级独立条目,互不污染(cache key = `route-forge:${level}`)
46
60
  * - TTL 优先用后端响应里的 cache 字段,本地 cache.ttl 仅作兜底
47
61
  * - storage: 'memory' | 'sessionStorage' | 'localStorage'
62
+ * - storage 模式下维护内存镜像:首次读盘解析后驻留内存,后续 get 直接命中
63
+ * (读盘 + JSON.parse 整层路由表是同步阻塞操作,热路径重复执行代价高);
64
+ * 写操作(set/del/clear)同步更新镜像,并通过 storage 事件感知其他
65
+ * tab 的写入/失效,保证跨 tab 新鲜度与无镜像时一致
48
66
  */
49
67
 
50
68
  interface CacheEntry {
@@ -58,6 +76,8 @@ declare class RouteCache {
58
76
  private readonly fallbackTtl;
59
77
  private readonly backend;
60
78
  private readonly memory;
79
+ /** 其他 tab 修改 storage 时失效对应镜像(storage 事件:自己的写不触发,自己的写经 set/del 已同步) */
80
+ private readonly onStorageEvent;
61
81
  constructor(opts: {
62
82
  storage: CacheStorage;
63
83
  ttl: number;
@@ -71,6 +91,37 @@ declare class RouteCache {
71
91
  private isExpired;
72
92
  }
73
93
 
94
+ /**
95
+ * 智能路由名前缀解析
96
+ *
97
+ * 当前缀与后缀拼接产生歧义时(后缀本身已以前缀开头),
98
+ * 通过查询已加载路由表来判定正确名称:
99
+ * - 优先尝试 prefix + sep + suffix(完整拼接)
100
+ * - 若不存在则回退到 suffix(视为已含前缀)
101
+ * - 均不存在则抛 UnknownRouteError
102
+ *
103
+ * @see .docs/SPEC.md §4.1.7
104
+ */
105
+ /**
106
+ * 路由名解析所需的最小 forge 接口。
107
+ * 仅依赖 load() 和 hasRoute(),不依赖拦截器等完整能力。
108
+ * 兼容 RouteForge、BoundForgeTyped、ForgeInstanceTyped 等所有形态。
109
+ */
110
+ interface RouteResolver {
111
+ load(level: string | string[]): Promise<void>;
112
+ hasRoute(level: string, name: string): boolean;
113
+ }
114
+ /**
115
+ * 异步版本:先确保层级已加载,再做歧义消解。
116
+ * 适用于 api() 调用路径。
117
+ */
118
+ declare function resolveRouteName(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): Promise<string>;
119
+ /**
120
+ * 同步版本:基于已加载缓存做歧义消解。
121
+ * 适用于 route() / url() 调用路径(要求层级已加载)。
122
+ */
123
+ declare function resolveRouteNameSync(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): string;
124
+
74
125
  /**
75
126
  * Route Forge 错误基类与具体错误类
76
127
  * @see .docs/SPEC.md §6
@@ -78,14 +129,19 @@ declare class RouteCache {
78
129
  interface ForgeErrorContext {
79
130
  [key: string]: unknown;
80
131
  }
132
+ /**
133
+ * Route Forge 错误码字面量联合。
134
+ * 用户侧 `switch (e.code)` 可获得穷尽检查(漏分支编译报错)。
135
+ */
136
+ type ForgeErrorCode = 'RF_FE_001' | 'RF_FE_002' | 'RF_FE_003' | 'RF_FE_005' | 'RF_FE_006' | 'RF_FE_007' | 'RF_FE_008' | 'RF_FE_009' | 'RF_FE_010';
81
137
  declare class ForgeError extends Error {
82
- readonly code: string;
138
+ readonly code: ForgeErrorCode;
83
139
  readonly route?: string;
84
140
  readonly level?: string;
85
141
  readonly context?: ForgeErrorContext;
86
142
  readonly cause?: unknown;
87
143
  constructor(message: string, opts: {
88
- code: string;
144
+ code: ForgeErrorCode;
89
145
  route?: string;
90
146
  level?: string;
91
147
  context?: ForgeErrorContext;
@@ -132,49 +188,4 @@ declare class RequestAbortedError extends ForgeError {
132
188
  constructor(route?: string, level?: string, cause?: unknown);
133
189
  }
134
190
 
135
- /**
136
- * Route Forge 主入口:createRouteForge
137
- * @see .docs/SPEC.md §4.1.1 ~ §4.1.4
138
- *
139
- * 能力清单(v1.0 MVP):
140
- * - 按层级懒加载与隔离缓存(§4.1.2)
141
- * - 并发去重(§4.1.4)
142
- * - 通过路由名调用 API(§4.1.3)
143
- * - 拦截器(§4.1.3a)
144
- * - Adapter auto 检测 / builtin / axios / 自定义 Fetcher(§4.3)
145
- */
146
-
147
- declare function createRouteForge(options: RouteForgeOptions): RouteForge;
148
-
149
- /**
150
- * 智能路由名前缀解析
151
- *
152
- * 当前缀与后缀拼接产生歧义时(后缀本身已以前缀开头),
153
- * 通过查询已加载路由表来判定正确名称:
154
- * - 优先尝试 prefix + sep + suffix(完整拼接)
155
- * - 若不存在则回退到 suffix(视为已含前缀)
156
- * - 均不存在则抛 UnknownRouteError
157
- *
158
- * @see .docs/SPEC.md §4.1.7
159
- */
160
- /**
161
- * 路由名解析所需的最小 forge 接口。
162
- * 仅依赖 load() 和 hasRoute(),不依赖拦截器等完整能力。
163
- * 兼容 RouteForge、BoundForgeTyped、ForgeInstanceTyped 等所有形态。
164
- */
165
- interface RouteResolver {
166
- load(level: string | string[]): Promise<void>;
167
- hasRoute(level: string, name: string): boolean;
168
- }
169
- /**
170
- * 异步版本:先确保层级已加载,再做歧义消解。
171
- * 适用于 api() 调用路径。
172
- */
173
- declare function resolveRouteName(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): Promise<string>;
174
- /**
175
- * 同步版本:基于已加载缓存做歧义消解。
176
- * 适用于 route() / url() 调用路径(要求层级已加载)。
177
- */
178
- declare function resolveRouteNameSync(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): string;
179
-
180
- export { AdapterNotFoundError, CacheStorage, ForgeError, HTTPError, InterceptorHandler, InterceptorManager, InterceptorManagerImpl, InvalidInterceptorReturnError, LevelRoutesResponse, MissingRouteParamError, NetworkError, RequestAbortedError, RouteCache, RouteForge, RouteForgeOptions, RouteMeta, type RouteResolver, UnknownLevelError, UnknownRouteError, createInterceptorManager, createRouteForge, resolveRouteName, resolveRouteNameSync };
191
+ export { AdapterNotFoundError, CacheStorage, ForgeError, type ForgeErrorCode, HTTPError, InterceptorHandler, InterceptorManager, InterceptorManagerImpl, InvalidInterceptorReturnError, LevelRoutesResponse, MissingRouteParamError, NetworkError, RequestAbortedError, RouteCache, RouteForge, RouteForgeOptions, RouteMeta, type RouteResolver, UnknownLevelError, UnknownRouteError, createInterceptorManager, createRouteForge, resolveRouteName, resolveRouteNameSync };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,19 @@
1
- import { I as InterceptorManager, a as InterceptorHandler, C as CacheStorage, R as RouteMeta, L as LevelRoutesResponse, b as RouteForgeOptions, c as RouteForge } from './types-BFlrOTrN.js';
2
- export { A as AdapterOption, d as ApiCallParams, B as BoundForge, F as Fetcher, e as ForgeApiParams, f as ForgeApiResponse, g as ForgeRequest, h as ForgeRouteMap, i as ForgeRouteName, j as LoadingChangeCallback, k as LoadingChangeEvent, l as LoadingTracker, m as RequestConfig, n as ResponseData, S as SummaryResponse, U as UseForgeApiBoundCall, o as UseForgeApiBoundReturn, p as UseForgeApiCall, q as UseForgeApiReturn, r as UseForgeByPrefixReturn } from './types-BFlrOTrN.js';
1
+ import { a as RouteForgeOptions, b as RouteForge, I as InterceptorManager, c as InterceptorHandler, C as CacheStorage, R as RouteMeta, L as LevelRoutesResponse } from './types-C51rXaiY.js';
2
+ export { A as AdapterOption, d as ApiCallParams, B as BoundForge, F as Fetcher, e as ForgeApiParams, f as ForgeApiResponse, g as ForgeRequest, h as ForgeRouteMap, i as ForgeRouteName, j as LoadingChangeCallback, k as LoadingChangeEvent, l as LoadingTracker, m as RequestConfig, n as ResponseData, S as SummaryResponse, U as UseForgeApiBoundCall, o as UseForgeApiBoundReturn, p as UseForgeApiCall, q as UseForgeApiReturn } from './types-C51rXaiY.js';
3
+
4
+ /**
5
+ * Route Forge 主入口:createRouteForge
6
+ * @see .docs/SPEC.md §4.1.1 ~ §4.1.4
7
+ *
8
+ * 能力清单(v1.0 MVP):
9
+ * - 按层级懒加载与隔离缓存(§4.1.2)
10
+ * - 并发去重(§4.1.4)
11
+ * - 通过路由名调用 API(§4.1.3)
12
+ * - 拦截器(§4.1.3a)
13
+ * - Adapter auto 检测 / builtin / axios / 自定义 Fetcher(§4.3)
14
+ */
15
+
16
+ declare function createRouteForge(options: RouteForgeOptions): RouteForge;
3
17
 
4
18
  /**
5
19
  * 拦截器管理器实现
@@ -45,6 +59,10 @@ declare function createInterceptorManager<TIn, TOut = TIn>(): InterceptorManager
45
59
  * - 每层级独立条目,互不污染(cache key = `route-forge:${level}`)
46
60
  * - TTL 优先用后端响应里的 cache 字段,本地 cache.ttl 仅作兜底
47
61
  * - storage: 'memory' | 'sessionStorage' | 'localStorage'
62
+ * - storage 模式下维护内存镜像:首次读盘解析后驻留内存,后续 get 直接命中
63
+ * (读盘 + JSON.parse 整层路由表是同步阻塞操作,热路径重复执行代价高);
64
+ * 写操作(set/del/clear)同步更新镜像,并通过 storage 事件感知其他
65
+ * tab 的写入/失效,保证跨 tab 新鲜度与无镜像时一致
48
66
  */
49
67
 
50
68
  interface CacheEntry {
@@ -58,6 +76,8 @@ declare class RouteCache {
58
76
  private readonly fallbackTtl;
59
77
  private readonly backend;
60
78
  private readonly memory;
79
+ /** 其他 tab 修改 storage 时失效对应镜像(storage 事件:自己的写不触发,自己的写经 set/del 已同步) */
80
+ private readonly onStorageEvent;
61
81
  constructor(opts: {
62
82
  storage: CacheStorage;
63
83
  ttl: number;
@@ -71,6 +91,37 @@ declare class RouteCache {
71
91
  private isExpired;
72
92
  }
73
93
 
94
+ /**
95
+ * 智能路由名前缀解析
96
+ *
97
+ * 当前缀与后缀拼接产生歧义时(后缀本身已以前缀开头),
98
+ * 通过查询已加载路由表来判定正确名称:
99
+ * - 优先尝试 prefix + sep + suffix(完整拼接)
100
+ * - 若不存在则回退到 suffix(视为已含前缀)
101
+ * - 均不存在则抛 UnknownRouteError
102
+ *
103
+ * @see .docs/SPEC.md §4.1.7
104
+ */
105
+ /**
106
+ * 路由名解析所需的最小 forge 接口。
107
+ * 仅依赖 load() 和 hasRoute(),不依赖拦截器等完整能力。
108
+ * 兼容 RouteForge、BoundForgeTyped、ForgeInstanceTyped 等所有形态。
109
+ */
110
+ interface RouteResolver {
111
+ load(level: string | string[]): Promise<void>;
112
+ hasRoute(level: string, name: string): boolean;
113
+ }
114
+ /**
115
+ * 异步版本:先确保层级已加载,再做歧义消解。
116
+ * 适用于 api() 调用路径。
117
+ */
118
+ declare function resolveRouteName(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): Promise<string>;
119
+ /**
120
+ * 同步版本:基于已加载缓存做歧义消解。
121
+ * 适用于 route() / url() 调用路径(要求层级已加载)。
122
+ */
123
+ declare function resolveRouteNameSync(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): string;
124
+
74
125
  /**
75
126
  * Route Forge 错误基类与具体错误类
76
127
  * @see .docs/SPEC.md §6
@@ -78,14 +129,19 @@ declare class RouteCache {
78
129
  interface ForgeErrorContext {
79
130
  [key: string]: unknown;
80
131
  }
132
+ /**
133
+ * Route Forge 错误码字面量联合。
134
+ * 用户侧 `switch (e.code)` 可获得穷尽检查(漏分支编译报错)。
135
+ */
136
+ type ForgeErrorCode = 'RF_FE_001' | 'RF_FE_002' | 'RF_FE_003' | 'RF_FE_005' | 'RF_FE_006' | 'RF_FE_007' | 'RF_FE_008' | 'RF_FE_009' | 'RF_FE_010';
81
137
  declare class ForgeError extends Error {
82
- readonly code: string;
138
+ readonly code: ForgeErrorCode;
83
139
  readonly route?: string;
84
140
  readonly level?: string;
85
141
  readonly context?: ForgeErrorContext;
86
142
  readonly cause?: unknown;
87
143
  constructor(message: string, opts: {
88
- code: string;
144
+ code: ForgeErrorCode;
89
145
  route?: string;
90
146
  level?: string;
91
147
  context?: ForgeErrorContext;
@@ -132,49 +188,4 @@ declare class RequestAbortedError extends ForgeError {
132
188
  constructor(route?: string, level?: string, cause?: unknown);
133
189
  }
134
190
 
135
- /**
136
- * Route Forge 主入口:createRouteForge
137
- * @see .docs/SPEC.md §4.1.1 ~ §4.1.4
138
- *
139
- * 能力清单(v1.0 MVP):
140
- * - 按层级懒加载与隔离缓存(§4.1.2)
141
- * - 并发去重(§4.1.4)
142
- * - 通过路由名调用 API(§4.1.3)
143
- * - 拦截器(§4.1.3a)
144
- * - Adapter auto 检测 / builtin / axios / 自定义 Fetcher(§4.3)
145
- */
146
-
147
- declare function createRouteForge(options: RouteForgeOptions): RouteForge;
148
-
149
- /**
150
- * 智能路由名前缀解析
151
- *
152
- * 当前缀与后缀拼接产生歧义时(后缀本身已以前缀开头),
153
- * 通过查询已加载路由表来判定正确名称:
154
- * - 优先尝试 prefix + sep + suffix(完整拼接)
155
- * - 若不存在则回退到 suffix(视为已含前缀)
156
- * - 均不存在则抛 UnknownRouteError
157
- *
158
- * @see .docs/SPEC.md §4.1.7
159
- */
160
- /**
161
- * 路由名解析所需的最小 forge 接口。
162
- * 仅依赖 load() 和 hasRoute(),不依赖拦截器等完整能力。
163
- * 兼容 RouteForge、BoundForgeTyped、ForgeInstanceTyped 等所有形态。
164
- */
165
- interface RouteResolver {
166
- load(level: string | string[]): Promise<void>;
167
- hasRoute(level: string, name: string): boolean;
168
- }
169
- /**
170
- * 异步版本:先确保层级已加载,再做歧义消解。
171
- * 适用于 api() 调用路径。
172
- */
173
- declare function resolveRouteName(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): Promise<string>;
174
- /**
175
- * 同步版本:基于已加载缓存做歧义消解。
176
- * 适用于 route() / url() 调用路径(要求层级已加载)。
177
- */
178
- declare function resolveRouteNameSync(forge: RouteResolver, level: string, prefix: string, suffix: string, separator?: string): string;
179
-
180
- export { AdapterNotFoundError, CacheStorage, ForgeError, HTTPError, InterceptorHandler, InterceptorManager, InterceptorManagerImpl, InvalidInterceptorReturnError, LevelRoutesResponse, MissingRouteParamError, NetworkError, RequestAbortedError, RouteCache, RouteForge, RouteForgeOptions, RouteMeta, type RouteResolver, UnknownLevelError, UnknownRouteError, createInterceptorManager, createRouteForge, resolveRouteName, resolveRouteNameSync };
191
+ export { AdapterNotFoundError, CacheStorage, ForgeError, type ForgeErrorCode, HTTPError, InterceptorHandler, InterceptorManager, InterceptorManagerImpl, InvalidInterceptorReturnError, LevelRoutesResponse, MissingRouteParamError, NetworkError, RequestAbortedError, RouteCache, RouteForge, RouteForgeOptions, RouteMeta, type RouteResolver, UnknownLevelError, UnknownRouteError, createInterceptorManager, createRouteForge, resolveRouteName, resolveRouteNameSync };