@net-vert/core 0.5.1 → 1.0.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.ts CHANGED
@@ -1,17 +1,187 @@
1
1
  import { TaskQueue } from 'id-queue';
2
2
 
3
- declare type CachedData = {
4
- value: any;
5
- expiresAt: number;
3
+ declare type AnyRecord = Record<string | number | symbol, any>;
4
+
5
+ declare type AnyRecord_2 = Record<string, any>;
6
+
7
+ /**
8
+ * 缓存存储基类
9
+ * 提供针对缓存场景的快捷方法,自动处理 ExpirableValue 的封装和解封
10
+ */
11
+ declare abstract class BaseCacheStorage<K extends string | number | symbol, R> {
12
+ abstract getItem(key: K): ExpirableValue<R> | undefined;
13
+ abstract setItem(key: K, value: ExpirableValue<R>): ExpirableValue<R>;
14
+ abstract removeItem(key: K): void;
15
+ abstract clear(): void;
16
+ abstract length(): number;
17
+ abstract keys(): K[];
18
+ abstract iterate(iteratee: (value: ExpirableValue<R>, key: K, iterationNumber: number) => void): void;
19
+ /**
20
+ * 获取缓存值(自动处理过期检查和解包)
21
+ * @param key 缓存键
22
+ * @returns 缓存的原始值,如果不存在或已过期返回 undefined
23
+ */
24
+ get(key: K): R | undefined;
25
+ /**
26
+ * 设置缓存值(自动包装为 ExpirableValue)
27
+ * @param key 缓存键
28
+ * @param value 要缓存的值
29
+ * @param duration 有效期(毫秒),默认 24 小时
30
+ */
31
+ set(key: K, value: R, duration?: number): void;
32
+ /**
33
+ * 删除指定缓存(别名方法,等同于 removeItem)
34
+ * @param key 缓存键
35
+ */
36
+ delete(key: K): void;
37
+ /**
38
+ * 检查缓存是否存在且未过期
39
+ * @param key 缓存键
40
+ */
41
+ has(key: K): boolean;
42
+ /**
43
+ * 获取缓存数量(别名方法,等同于 length)
44
+ */
45
+ size(): number;
46
+ /**
47
+ * 遍历所有有效的缓存(自动跳过已过期的,并解包值)
48
+ * @param callback 回调函数,接收解包后的值、键和索引
49
+ */
50
+ forEach(callback: (value: R, key: K, index: number) => void): void;
51
+ /**
52
+ * 清理所有过期的缓存
53
+ * @returns 清理的缓存数量
54
+ */
55
+ clearExpired(): number;
56
+ /**
57
+ * 更新缓存的过期时间(不改变值)
58
+ * @param key 缓存键
59
+ * @param duration 新的有效期(毫秒)
60
+ * @returns 是否更新成功
61
+ */
62
+ touch(key: K, duration: number): boolean;
63
+ }
64
+
65
+ export declare type BaseRequestor = (config: RequestConfig<any>) => any;
66
+
67
+ /**
68
+ * 缓存中间件
69
+ * 支持:
70
+ * - 自定义缓存 key 生成
71
+ * - 自定义缓存有效期(固定时长或动态计算)
72
+ * - 自定义缓存有效性校验
73
+ * - 持久化存储(LocalStorage)或内存存储
74
+ */
75
+ export declare const cache: <D = any, R = any>(options?: Partial<CacheOptions<D, R>>) => CacheMiddleware<D, R>;
76
+
77
+ /** 请求前上下文:检查缓存是否有效 */
78
+ declare interface CacheCheckContext<D = any, R = any> {
79
+ key: CacheKey;
80
+ config: RequestConfig<D>;
81
+ cachedData?: ExpirableValue<R>;
82
+ }
83
+
84
+ /**
85
+ * 缓存 + 幂等组合配置(扁平化)
86
+ */
87
+ export declare type CachedIdempotentConfig<D = any, R = any> = FlattenWithInstanceKey<[
88
+ CacheOptions<D, R>,
89
+ IdempotencyOptions<D>
90
+ ]>;
91
+
92
+ /** 缓存 key 类型 */
93
+ declare type CacheKey = string | number | symbol;
94
+
95
+ /** 请求前上下文:生成缓存 key */
96
+ declare interface CacheKeyContext<D = any> {
97
+ config: RequestConfig<D>;
98
+ }
99
+
100
+ /**
101
+ * 基于 LocalStorage 的缓存存储
102
+ * 继承 LocalStorage 并扩展缓存快捷方法
103
+ */
104
+ declare class CacheLocalStorage<K extends string | number | symbol = string, R = any> extends LocalStorage<Record<K, ExpirableValue<R>>> implements BaseCacheStorage<K, R> {
105
+ get: (key: any) => any;
106
+ set: (key: any, value: any, duration?: number) => void;
107
+ delete: (key: any) => void;
108
+ has: (key: any) => boolean;
109
+ size: () => number;
110
+ forEach: (callback: (value: any, key: any, index: number) => void) => void;
111
+ clearExpired: () => number;
112
+ touch: (key: any, duration: number) => boolean;
113
+ }
114
+
115
+ /**
116
+ * 基于内存的缓存存储
117
+ * 继承 MemoryStorage 并扩展缓存快捷方法
118
+ */
119
+ declare class CacheMemoryStorage<K extends string | number | symbol = string, R = any> extends MemoryStorage<Record<K, ExpirableValue<R>>> implements BaseCacheStorage<K, R> {
120
+ get: (key: any) => any;
121
+ set: (key: any, value: any, duration?: number) => void;
122
+ delete: (key: any) => void;
123
+ has: (key: any) => boolean;
124
+ size: () => number;
125
+ forEach: (callback: (value: any, key: any, index: number) => void) => void;
126
+ clearExpired: () => number;
127
+ touch: (key: any, duration: number) => boolean;
128
+ }
129
+
130
+ /** 缓存中间件类型(带 storage 实例)*/
131
+ declare type CacheMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE_TYPE.CACHE, false, D, R> & {
132
+ storage: CacheStorage_2<CacheKey, R>;
133
+ };
134
+
135
+ /** 缓存模块配置 */
136
+ declare interface CacheOptions<D = any, R = any> {
137
+ /**
138
+ * 缓存 key 生成函数
139
+ * 默认使用 method + url 哈希
140
+ */
141
+ key: (ctx: CacheKeyContext<D>) => CacheKey;
142
+ /**
143
+ * 缓存有效期
144
+ * - number: 固定毫秒数
145
+ * - function: 可根据请求或响应动态计算
146
+ */
147
+ duration: number | ((ctx: CacheUpdateContext<D, R>) => number);
148
+ /**
149
+ * 判断缓存是否有效(请求前)
150
+ * - 可以根据现有缓存数据或其他条件动态判断
151
+ * - 返回 boolean 或 Promise<boolean>
152
+ */
153
+ isValid: (ctx: CacheCheckContext<D, R>) => boolean | Promise<boolean>;
154
+ /** 缓存介质, 待开发, 目前只支持内存和持久化 */
155
+ persist: boolean;
156
+ }
157
+
158
+ /**
159
+ * CacheStorage 类型别名
160
+ * 可以是 CacheMemoryStorage 或 CacheLocalStorage
161
+ */
162
+ declare type CacheStorage_2<K extends string | number | symbol = string, R = any> = CacheMemoryStorage<K, R> | CacheLocalStorage<K, R>;
163
+
164
+ /** 请求后上下文:更新缓存 */
165
+ declare interface CacheUpdateContext<D = any, R = any> {
166
+ key: CacheKey;
167
+ config: RequestConfig<D>;
168
+ cachedData?: ExpirableValue<R>;
169
+ response: R;
170
+ }
171
+
172
+ export declare const concurrent: <D = any, R = any>(options?: Partial<ConcurrentOptions<D>>) => ConcurrentMiddleware<D, R>;
173
+
174
+ declare type ConcurrentContext<D = any> = {
175
+ config: RequestConfig<D>;
176
+ };
177
+
178
+ declare type ConcurrentMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE_TYPE.CONCURRENT, false, D, R> & {
179
+ pool: ConcurrentPool;
6
180
  };
7
181
 
8
- declare type CacheRequestor<P extends boolean, S extends boolean> = {
9
- key?: (config: UnifiedConfig) => string;
10
- duration?: Duration;
11
- sync?: S;
12
- persist?: P;
13
- name?: [P, S] extends [true, false] ? string : undefined;
14
- isValid?: S extends true ? (params: IsValidParams) => boolean : (params: IsValidParams) => boolean | Promise<boolean>;
182
+ declare type ConcurrentOptions<D = any> = {
183
+ parallelCount: number;
184
+ createId: (params: ConcurrentContext<D>) => Id;
15
185
  };
16
186
 
17
187
  declare class ConcurrentPool<T extends any = any> {
@@ -19,141 +189,251 @@ declare class ConcurrentPool<T extends any = any> {
19
189
  tasks: TaskItemList<T>;
20
190
  runningCount: number;
21
191
  constructor(parallelCount?: number);
22
- add(id: string, task: Task<T>): Promise<unknown>;
23
- remove(id: string): void;
192
+ add(id: Id, task: Task<T>): Promise<T>;
193
+ remove(id: Id): void;
24
194
  execute(currentTask: TaskItem<T>): Promise<void>;
25
195
  _run(): void;
26
196
  }
27
197
 
28
- declare type Duration = number | (({ key, config, response }: {
29
- key: string;
30
- config: UnifiedConfig;
31
- response: any;
32
- }) => number);
198
+ /**
199
+ * 并发 + 重试组合配置(扁平化)
200
+ */
201
+ export declare type ConcurrentRetryConfig<D = any> = FlattenWithInstanceKey<[
202
+ ConcurrentOptions<D>,
203
+ RetryOptions<D>
204
+ ]>;
205
+
206
+ /**
207
+ * 创建带缓存和幂等的请求器
208
+ * 适用场景:数据查询接口,既需要缓存提升性能,又要避免重复请求
209
+ * @param config 组合配置对象
210
+ */
211
+ export declare function createCachedIdempotentRequestor<D = any, R = any>(config: CachedIdempotentConfig<D, R>): Requestor<false>;
212
+
213
+ /**
214
+ * 创建带并发控制和重试的请求器
215
+ * 适用场景:批量请求场景,需要控制并发数量,失败后自动重试
216
+ * @param config 组合配置对象
217
+ */
218
+ export declare function createConcurrentRetryRequestor<D = any>(config: ConcurrentRetryConfig<D>): Requestor<false>;
219
+
220
+ declare const createPromiseCache: () => {
221
+ getPromise: <T = unknown>(key: SafeKey) => Promise<T> | undefined;
222
+ setPromise: <T = unknown>(key: SafeKey, promise: Promise<T>) => void;
223
+ delPromise: (key: SafeKey) => boolean;
224
+ clearCache: () => void;
225
+ };
226
+
227
+ export declare function createRequestor<const Extensions extends readonly Middleware[]>(config: CreateRequestorConfig<Extensions> & {
228
+ extensions: Extensions;
229
+ }): Requestor<HasSyncMiddleware<Extensions>>;
230
+
231
+ export declare function createRequestor(config?: CreateRequestorConfig): Requestor<false>;
232
+
233
+ export declare interface CreateRequestorConfig<Extensions extends readonly Middleware[] = readonly Middleware[]> {
234
+ extensions?: Extensions;
235
+ instanceKey?: Key;
236
+ }
33
237
 
34
- declare type IdempotencyOptions = {
35
- key?: (config: UnifiedConfig) => string;
36
- duration?: number;
238
+ /**
239
+ * 带过期时间的值类型(通用型)
240
+ * 可用于缓存、幂等、同步等需要时间控制的场景
241
+ */
242
+ export declare type ExpirableValue<T = any> = {
243
+ value: T;
244
+ expireAt: number;
37
245
  };
38
246
 
39
- export declare const inject: (requestor: UnifiedRequestor, instanceKey?: string) => void;
247
+ /**
248
+ * 扁平化多个对象类型
249
+ * @template Types - 需要扁平化的对象类型元组
250
+ * @example
251
+ * ```ts
252
+ * type Result = Flatten<[{ a: string }, { b: number }, { c: boolean }]>
253
+ * // Result = { a: string } & { b: number } & { c: boolean }
254
+ * ```
255
+ */
256
+ export declare type Flatten<Types extends readonly any[]> = UnionToIntersection<Types[number]>;
40
257
 
41
- declare type IsValidParams = {
42
- key: string;
43
- config: UnifiedConfig;
44
- cachedData: CachedData;
258
+ /**
259
+ * 组合工具:扁平化多个对象类型并添加 instanceKey
260
+ * @template Types - 需要扁平化的对象类型元组
261
+ * @example
262
+ * ```ts
263
+ * type Result = FlattenWithInstanceKey<[TypeA, TypeB, TypeC]>
264
+ * // Result = TypeA & TypeB & TypeC & { instanceKey?: Key }
265
+ * ```
266
+ */
267
+ export declare type FlattenWithInstanceKey<Types extends readonly any[]> = WithInstanceKey<Flatten<Types>>;
268
+
269
+ export declare type HandlerParams<T extends keyof Requestor> = Parameters<Requestor[T]>;
270
+
271
+ export declare type HasSyncMiddleware<T extends readonly any[]> = T extends readonly [infer First, ...infer Rest] ? First extends TypedMiddleware<MIDDLEWARE_TYPE.SYNC, true, any, any> ? true : HasSyncMiddleware<Rest> : false;
272
+
273
+ declare type Id = string | number;
274
+
275
+ declare type IdempotencyContext<D = any> = {
276
+ config: RequestConfig<D>;
45
277
  };
46
278
 
47
- declare interface RequestConfig<D = any> {
48
- url?: string;
49
- method?: Exclude<keyof Requestor, "request">;
50
- baseURL?: string;
51
- headers?: Record<string, any>;
52
- params?: Record<string, any> | string;
53
- data?: D;
54
- timeout?: number;
55
- onUploadProgress?: <P extends ProgressEvent>(progressEvent: P) => void;
56
- onDownloadProgress?: <P extends ProgressEvent>(progressEvent: P) => void;
57
- validateStatus?: ((status: number) => boolean) | null;
58
- signal?: AbortSignal;
59
- }
60
-
61
- export declare const requestExtender: {
62
- cacheRequestor: <P extends boolean = false, S extends boolean = false>(config?: CacheRequestor<P, S>) => {
63
- requestor: Requestor;
64
- store: {
65
- has(key: string): boolean;
66
- get<T>(key: string): T | undefined;
67
- set<T>(key: string, value: T): T;
68
- remove(key: string): void;
69
- clear(): void;
70
- } | {
71
- has: (key: string) => Promise<boolean>;
72
- get: <T>(key: string) => Promise<T | null>;
73
- set: <T>(key: string, value: T) => Promise<T>;
74
- remove: (key: string) => Promise<void>;
75
- clear: () => Promise<void>;
76
- } | {
77
- has: (key: string) => boolean;
78
- get: (key: string) => any;
79
- set: <T>(key: string, value: T) => Map<string, any>;
80
- remove: (key: string) => boolean;
81
- clear: () => void;
82
- };
83
- };
84
- idempotencyRequestor: (config?: IdempotencyOptions) => {
85
- requestor: Requestor;
86
- store: {
87
- has(key: string): boolean;
88
- get<T>(key: string): T | undefined;
89
- set<T>(key: string, value: T): T;
90
- remove(key: string): void;
91
- clear(): void;
92
- } | {
93
- has: (key: string) => Promise<boolean>;
94
- get: <T>(key: string) => Promise<T | null>;
95
- set: <T>(key: string, value: T) => Promise<T>;
96
- remove: (key: string) => Promise<void>;
97
- clear: () => Promise<void>;
98
- } | {
99
- has: (key: string) => boolean;
100
- get: (key: string) => any;
101
- set: <T>(key: string, value: T) => Map<string, any>;
102
- remove: (key: string) => boolean;
103
- clear: () => void;
104
- };
105
- };
106
- retryRequestor: (config?: RetryOptions) => {
107
- requestor: Requestor;
108
- };
109
- concurrentPoolRequestor: (config?: {
110
- parallelCount?: number;
111
- createId?: (config: UnifiedConfig) => string;
112
- } & RetryOptions) => {
113
- requestor: Requestor;
114
- concurrentPool: ConcurrentPool<any>;
115
- };
116
- syncRequestor: (config?: {
117
- persist?: false;
118
- sync?: true;
119
- } & CacheRequestor<false, true>) => {
120
- requestor: Requestor;
121
- store: {
122
- has(key: string): boolean;
123
- get<T>(key: string): T | undefined;
124
- set<T>(key: string, value: T): T;
125
- remove(key: string): void;
126
- clear(): void;
127
- } | {
128
- has: (key: string) => Promise<boolean>;
129
- get: <T>(key: string) => Promise<T | null>;
130
- set: <T>(key: string, value: T) => Promise<T>;
131
- remove: (key: string) => Promise<void>;
132
- clear: () => Promise<void>;
133
- } | {
134
- has: (key: string) => boolean;
135
- get: (key: string) => any;
136
- set: <T>(key: string, value: T) => Map<string, any>;
137
- remove: (key: string) => boolean;
138
- clear: () => void;
139
- };
140
- };
279
+ declare type IdempotencyOptions<D = any> = {
280
+ key: (params: IdempotencyContext<D>) => string;
141
281
  };
142
282
 
143
- export declare interface Requestor {
144
- get<R = any, D = any>(url: string, config?: WithDynamicProps<RequestConfig<D>>): R;
145
- post<R = any, D = any>(url: string, data?: D, config?: RequestConfig<D>): R;
146
- delete<R = any, D = any>(url: string, config?: WithDynamicProps<RequestConfig<D>>): R;
147
- put<R = any, D = any>(url: string, data?: D, config?: WithDynamicProps<RequestConfig<D>>): R;
148
- request<R = any, D = any>(config: WithDynamicProps<UnifiedConfig<D>>): R;
283
+ export declare const idempotent: <D = any, R = any>(options?: Partial<IdempotencyOptions<D>>) => IdempotentMiddleware<D, R>;
284
+
285
+ declare type IdempotentMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE_TYPE.IDEMPOTENT, false, D, R> & {
286
+ promiseCache: PromiseCache;
287
+ };
288
+
289
+ export declare const inject: (requestor: BaseRequestor, instanceKey?: string) => void;
290
+
291
+ /**
292
+ * Storage 接口
293
+ * 定义存储器的基本操作
294
+ */
295
+ export declare interface IStorage<Schema extends Record<string, any> = Record<string, any>> {
296
+ /**
297
+ * 获取存储项
298
+ */
299
+ getItem<K extends keyof Schema>(key: K): Schema[K] | undefined;
300
+ /**
301
+ * 设置存储项
302
+ */
303
+ setItem<K extends keyof Schema>(key: K, value: Schema[K]): Schema[K];
304
+ /**
305
+ * 删除存储项
306
+ */
307
+ removeItem<K extends keyof Schema>(key: K): void;
308
+ /**
309
+ * 清空所有存储项
310
+ */
311
+ clear(): void;
312
+ /**
313
+ * 获取存储项数量
314
+ */
315
+ length(): number;
316
+ /**
317
+ * 获取所有 key
318
+ */
319
+ keys(): (keyof Schema)[];
320
+ /**
321
+ * 遍历所有存储项
322
+ */
323
+ iterate(iteratee: <K extends keyof Schema>(value: Schema[K], key: K, iterationNumber: number) => void): void;
324
+ }
325
+
326
+ export declare type Key = string | symbol | number;
327
+
328
+ /**
329
+ * LocalStorage 存储类
330
+ * 基于浏览器的 localStorage 实现
331
+ */
332
+ declare class LocalStorage<Schema extends AnyRecord_2 = AnyRecord_2> implements IStorage<Schema> {
333
+ constructor();
334
+ getItem<K extends keyof Schema>(key: K): Schema[K] | undefined;
335
+ setItem<K extends keyof Schema>(key: K, value: Schema[K]): Schema[K];
336
+ removeItem<K extends keyof Schema>(key: K): void;
337
+ clear(): void;
338
+ length(): number;
339
+ keys(): (keyof Schema)[];
340
+ iterate(iteratee: <K extends keyof Schema>(value: Schema[K], key: K, iterationNumber: number) => void): void;
341
+ }
342
+
343
+ export declare type MaybePromise<IsSync, R> = IsSync extends true ? R : IsSync extends false ? Promise<R> : R | Promise<R>;
344
+
345
+ declare class MemoryStorage<Schema extends AnyRecord = AnyRecord> implements IStorage<Schema> {
346
+ private store;
347
+ getItem<K extends keyof Schema>(key: K): Schema[K] | undefined;
348
+ setItem<K extends keyof Schema>(key: K, value: Schema[K]): Schema[K];
349
+ removeItem<K extends keyof Schema>(key: K): void;
350
+ clear(): void;
351
+ length(): number;
352
+ keys(): (keyof Schema)[];
353
+ iterate(iteratee: <K extends keyof Schema>(value: Schema[K], key: K, iterationNumber: number) => void): void;
354
+ }
355
+
356
+ export declare type Middleware<IsSync extends boolean = false, D = any, R = any> = (context: {
357
+ config: RequestConfig<D>;
358
+ next: () => MaybePromise<IsSync, R>;
359
+ ctx: MiddlewareContext;
360
+ }) => MaybePromise<IsSync, R>;
361
+
362
+ declare enum MIDDLEWARE_TYPE {
363
+ CACHE = "cache",
364
+ RETRY = "retry",
365
+ IDEMPOTENT = "idempotent",
366
+ CONCURRENT = "concurrent",
367
+ SYNC = "sync"
368
+ }
369
+
370
+ export declare interface MiddlewareContext extends Record<string, any> {
371
+ }
372
+
373
+ declare type MiddlewareType = MIDDLEWARE_TYPE;
374
+
375
+ declare type PromiseCache = ReturnType<typeof createPromiseCache>;
376
+
377
+ declare enum REQUEST_METHOD {
378
+ GET = "get",
379
+ POST = "post",
380
+ PUT = "put",
381
+ DELETE = "delete"
382
+ }
383
+
384
+ export declare interface RequestConfig<D = any> extends Record<string, any> {
385
+ url: string;
386
+ method: RequestMethod;
387
+ data?: D;
388
+ }
389
+
390
+ declare type RequestMethod = REQUEST_METHOD;
391
+
392
+ export declare interface Requestor<IsSync extends boolean = false> {
393
+ request: <R = any, D = any>(config: RequestConfig<D>) => MaybePromise<IsSync, R>;
394
+ get: <R = any, D = any>(url: string, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
395
+ post: <R = any, D = any>(url: string, data?: D, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
396
+ put: <R = any, D = any>(url: string, data?: D, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
397
+ delete: <R = any, D = any>(url: string, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
149
398
  }
150
399
 
151
- declare type RetryOptions = {
152
- retries?: number;
153
- delay?: number | ((attempt: number) => number);
154
- retryCondition?: (error: any) => boolean;
400
+ export declare const retry: <D = any, R = any>(options?: Partial<RetryOptions<D>>) => TypedMiddleware<MIDDLEWARE_TYPE.RETRY, false, D, R>;
401
+
402
+ declare type RetryContext<D = any> = {
403
+ config: RequestConfig<D>;
404
+ lastResponse: any;
405
+ attempt: number;
155
406
  };
156
407
 
408
+ declare type RetryOptions<D = any> = {
409
+ retries: number;
410
+ delay: number | ((params: RetryContext<D>) => number);
411
+ retryCondition: (params: RetryContext<D>) => boolean;
412
+ };
413
+
414
+ declare type SafeKey = string | number | symbol;
415
+
416
+ /**
417
+ * 缓存中有数据则直接返回。
418
+ * suspense 为 true 时,会抛出一个 Promise(而不是普通错误),Promise resolve 后得到数据。
419
+ * suspense 为 false 时,会返回一个 Promise,Promise resolve 后得到数据,调用者需要用 await 或 .then 捕获。
420
+ */
421
+ export declare function sync<D = any, R = any>(options: Partial<SyncOptions<D>> & {
422
+ suspense: true;
423
+ }): TypedMiddleware<MIDDLEWARE_TYPE.SYNC, true, D, R>;
424
+
425
+ export declare function sync<D = any, R = any>(options: Partial<SyncOptions<D>> & {
426
+ suspense: false;
427
+ }): TypedMiddleware<MIDDLEWARE_TYPE.SYNC, any, D, R>;
428
+
429
+ export declare function sync<D = any, R = any>(options?: Partial<SyncOptions<D>>): TypedMiddleware<MIDDLEWARE_TYPE.SYNC, true, D, R>;
430
+
431
+ declare interface SyncOptions<D = any, R = any> extends CacheOptions<D, R> {
432
+ isValid: (ctx: CacheCheckContext<D, R>) => boolean;
433
+ suspense: boolean;
434
+ wrapSuspense?: (params: WrapSuspense<D, R>) => any;
435
+ }
436
+
157
437
  declare type Task<T> = () => Promise<T>;
158
438
 
159
439
  declare type TaskItem<T> = {
@@ -164,15 +444,35 @@ declare type TaskItem<T> = {
164
444
 
165
445
  declare type TaskItemList<T> = TaskQueue<TaskItem<T>>;
166
446
 
167
- export declare type UnifiedConfig<D = any> = RequestConfig<D> & {
168
- url: string;
169
- method: Exclude<keyof Requestor, "request">;
447
+ export declare type TypedMiddleware<Type extends MiddlewareType, IsSync extends boolean = false, D = any, R = any> = Middleware<IsSync, D, R> & {
448
+ __middlewareType: Type;
170
449
  };
171
450
 
172
- export declare type UnifiedRequestor = <R = any, D = any>(config: UnifiedConfig<D>) => Promise<R>;
451
+ /* Excluded from this release type: UnionToIntersection */
452
+
453
+ export declare const useRequestor: (instanceKey?: Key) => BaseRequestor;
173
454
 
174
- export declare const useRequestor: (instanceKey?: string) => Requestor;
455
+ /**
456
+ * 给类型添加可选的 instanceKey 字段
457
+ * @template T - 源对象类型
458
+ * @example
459
+ * ```ts
460
+ * type Config = { timeout: number }
461
+ * type Result = WithInstanceKey<Config>
462
+ * // Result = { timeout: number; instanceKey?: Key }
463
+ * ```
464
+ */
465
+ export declare type WithInstanceKey<T> = T & {
466
+ /** 请求器实例 key(可选) */
467
+ instanceKey?: Key;
468
+ };
175
469
 
176
- declare type WithDynamicProps<T, V = any> = T & Record<string, V>;
470
+ export declare type WithoutMethod<D = any> = Omit<RequestConfig<D>, 'method' | 'url'>;
471
+
472
+ declare type WrapSuspense<D = any, R = any> = {
473
+ key: CacheKey;
474
+ config: RequestConfig<D>;
475
+ p: Promise<R>;
476
+ };
177
477
 
178
478
  export { }