@net-vert/core 0.5.1 → 1.2.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/README.md +1044 -67
- package/dist/index.d.ts +322 -134
- package/dist/index.js +659 -299
- package/dist/index.umd.cjs +1 -1
- package/package.json +9 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,99 @@
|
|
|
1
|
+
import { AnyRecord } from 'store-vert';
|
|
2
|
+
import { EnhancedStore } from 'store-vert';
|
|
3
|
+
import { Key as Key_2 } from 'store-vert';
|
|
4
|
+
import { Store } from 'store-vert';
|
|
5
|
+
import { StoreFactory } from 'store-vert';
|
|
6
|
+
import { StoreKey } from 'store-vert';
|
|
1
7
|
import { TaskQueue } from 'id-queue';
|
|
2
8
|
|
|
3
|
-
declare type
|
|
4
|
-
|
|
5
|
-
|
|
9
|
+
export declare type BaseRequestor = (config: RequestConfig<any>) => any;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 缓存中间件
|
|
13
|
+
* 支持:
|
|
14
|
+
* - 自定义缓存 key 生成
|
|
15
|
+
* - 自定义缓存有效期(固定时长或动态计算)
|
|
16
|
+
* - 自定义缓存有效性校验
|
|
17
|
+
* - 自定义存储介质
|
|
18
|
+
*/
|
|
19
|
+
export declare const cache: <D = any, R = any>(options?: Partial<CacheOptions<D, R>>) => CacheMiddleware<D, R>;
|
|
20
|
+
|
|
21
|
+
/** 请求前上下文:检查缓存是否有效 */
|
|
22
|
+
declare interface CacheCheckContext<D = any, R = any> {
|
|
23
|
+
key: CacheKey;
|
|
24
|
+
config: RequestConfig<D>;
|
|
25
|
+
cachedData?: R;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 缓存 + 幂等组合配置(扁平化)
|
|
30
|
+
*/
|
|
31
|
+
export declare type CachedIdempotentConfig<D = any, R = any> = FlattenWithInstanceKey<[
|
|
32
|
+
CacheOptions<D, R>,
|
|
33
|
+
IdempotencyOptions<D>
|
|
34
|
+
]>;
|
|
35
|
+
|
|
36
|
+
/** 缓存 key 类型 */
|
|
37
|
+
export declare type CacheKey = string | number | symbol;
|
|
38
|
+
|
|
39
|
+
/** 请求前上下文:生成缓存 key */
|
|
40
|
+
declare interface CacheKeyContext<D = any> {
|
|
41
|
+
config: RequestConfig<D>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 缓存中间件类型(带 storage 实例)*/
|
|
45
|
+
declare type CacheMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE_TYPE.CACHE, false, D, R> & {
|
|
46
|
+
storage: CacheStorageInstance<R>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** 缓存模块配置 */
|
|
50
|
+
declare interface CacheOptions<D = any, R = any> {
|
|
51
|
+
/**
|
|
52
|
+
* 缓存 key 生成函数
|
|
53
|
+
* 默认使用 method + url 哈希
|
|
54
|
+
*/
|
|
55
|
+
key: (ctx: CacheKeyContext<D>) => CacheKey;
|
|
56
|
+
/**
|
|
57
|
+
* 缓存有效期
|
|
58
|
+
* - number: 固定毫秒数
|
|
59
|
+
* - function: 可根据请求或响应动态计算
|
|
60
|
+
*/
|
|
61
|
+
duration: number | ((ctx: CacheUpdateContext<D, R>) => number);
|
|
62
|
+
/**
|
|
63
|
+
* 判断缓存是否有效(请求前)
|
|
64
|
+
* - 可以根据现有缓存数据或其他条件动态判断
|
|
65
|
+
* - 返回 boolean 或 Promise<boolean>
|
|
66
|
+
*/
|
|
67
|
+
isValid: (ctx: CacheCheckContext<D, R>) => boolean | Promise<boolean>;
|
|
68
|
+
/** 缓存介质 */
|
|
69
|
+
store: StoreDescriptor;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare type CacheSchema<R> = Record<CacheKey, R>;
|
|
73
|
+
|
|
74
|
+
declare type CacheStorageInstance<R = any> = ExpirableCacheStorage<R> & EnhancedStore<ExpirableSchema<CacheSchema<R>>>;
|
|
75
|
+
|
|
76
|
+
/** 请求后上下文:更新缓存 */
|
|
77
|
+
declare interface CacheUpdateContext<D = any, R = any> {
|
|
78
|
+
key: CacheKey;
|
|
79
|
+
config: RequestConfig<D>;
|
|
80
|
+
cachedData?: R;
|
|
81
|
+
response: R;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export declare const concurrent: <D = any, R = any>(options?: Partial<ConcurrentOptions<D>>) => ConcurrentMiddleware<D, R>;
|
|
85
|
+
|
|
86
|
+
declare type ConcurrentContext<D = any> = {
|
|
87
|
+
config: RequestConfig<D>;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
declare type ConcurrentMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE_TYPE.CONCURRENT, false, D, R> & {
|
|
91
|
+
pool: ConcurrentPool;
|
|
6
92
|
};
|
|
7
93
|
|
|
8
|
-
declare type
|
|
9
|
-
|
|
10
|
-
|
|
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>;
|
|
94
|
+
declare type ConcurrentOptions<D = any> = {
|
|
95
|
+
parallelCount: number;
|
|
96
|
+
createId: (params: ConcurrentContext<D>) => Id;
|
|
15
97
|
};
|
|
16
98
|
|
|
17
99
|
declare class ConcurrentPool<T extends any = any> {
|
|
@@ -19,141 +101,227 @@ declare class ConcurrentPool<T extends any = any> {
|
|
|
19
101
|
tasks: TaskItemList<T>;
|
|
20
102
|
runningCount: number;
|
|
21
103
|
constructor(parallelCount?: number);
|
|
22
|
-
add(id:
|
|
23
|
-
remove(id:
|
|
104
|
+
add(id: Id, task: Task<T>): Promise<T>;
|
|
105
|
+
remove(id: Id): void;
|
|
24
106
|
execute(currentTask: TaskItem<T>): Promise<void>;
|
|
25
107
|
_run(): void;
|
|
26
108
|
}
|
|
27
109
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
110
|
+
/**
|
|
111
|
+
* 并发 + 重试组合配置(扁平化)
|
|
112
|
+
*/
|
|
113
|
+
export declare type ConcurrentRetryConfig<D = any> = FlattenWithInstanceKey<[
|
|
114
|
+
ConcurrentOptions<D>,
|
|
115
|
+
RetryOptions<D>
|
|
116
|
+
]>;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 创建带缓存和幂等的请求器
|
|
120
|
+
* 适用场景:数据查询接口,既需要缓存提升性能,又要避免重复请求
|
|
121
|
+
* @param config 组合配置对象
|
|
122
|
+
*/
|
|
123
|
+
export declare function createCachedIdempotentRequestor<D = any, R = any>(config: CachedIdempotentConfig<D, R>): Requestor<false>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 创建带并发控制和重试的请求器
|
|
127
|
+
* 适用场景:批量请求场景,需要控制并发数量,失败后自动重试
|
|
128
|
+
* @param config 组合配置对象
|
|
129
|
+
*/
|
|
130
|
+
export declare function createConcurrentRetryRequestor<D = any>(config: ConcurrentRetryConfig<D>): Requestor<false>;
|
|
131
|
+
|
|
132
|
+
declare const createPromiseCache: () => {
|
|
133
|
+
getPromise: <T = unknown>(key: SafeKey) => Promise<T> | undefined;
|
|
134
|
+
setPromise: <T = unknown>(key: SafeKey, promise: Promise<T>) => void;
|
|
135
|
+
delPromise: (key: SafeKey) => boolean;
|
|
136
|
+
clearCache: () => void;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export declare function createRequestor<const Extensions extends readonly Middleware[]>(config: CreateRequestorConfig<Extensions> & {
|
|
140
|
+
extensions: Extensions;
|
|
141
|
+
}): Requestor<HasSyncMiddleware<Extensions>>;
|
|
142
|
+
|
|
143
|
+
export declare function createRequestor(config?: CreateRequestorConfig): Requestor<false>;
|
|
144
|
+
|
|
145
|
+
export declare interface CreateRequestorConfig<Extensions extends readonly Middleware[] = readonly Middleware[]> {
|
|
146
|
+
extensions?: Extensions;
|
|
147
|
+
instanceKey?: Key;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
declare class ExpirableCacheStorage<R = any> {
|
|
151
|
+
store: EnhancedStore<ExpirableSchema<CacheSchema<R>>>;
|
|
152
|
+
constructor(store: StoreDescriptor);
|
|
153
|
+
/**
|
|
154
|
+
* 设置缓存(自动包装成 ExpirableValue)
|
|
155
|
+
* @param key 缓存 key
|
|
156
|
+
* @param value 要缓存的值
|
|
157
|
+
* @param duration 过期时长(毫秒),默认 24 小时
|
|
158
|
+
*/
|
|
159
|
+
setCache(key: CacheKey, value: R, duration?: number): void;
|
|
160
|
+
/**
|
|
161
|
+
* 获取缓存值(检查过期,如果过期返回 undefined)
|
|
162
|
+
* @param key 缓存 key
|
|
163
|
+
* @returns 未过期返回值,已过期返回 undefined
|
|
164
|
+
*/
|
|
165
|
+
getCache(key: CacheKey): Promise<R | undefined>;
|
|
166
|
+
/**
|
|
167
|
+
* 检查是否有有效的缓存(未过期)
|
|
168
|
+
* @param key 缓存 key
|
|
169
|
+
* @returns true 表示有有效缓存,false 表示无缓存或已过期
|
|
170
|
+
*/
|
|
171
|
+
hasValidCache(key: CacheKey): Promise<boolean>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 将 Schema 的所有值类型映射为 ExpirableValue 包装的版本
|
|
176
|
+
* 用于缓存存储的类型转换
|
|
177
|
+
*/
|
|
178
|
+
export declare type ExpirableSchema<Schema extends AnyRecord> = {
|
|
179
|
+
[K in keyof Schema]: ExpirableValue<Schema[K]>;
|
|
180
|
+
};
|
|
33
181
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
182
|
+
/**
|
|
183
|
+
* 带过期时间的值类型(通用型)
|
|
184
|
+
* 可用于缓存、幂等、同步等需要时间控制的场景
|
|
185
|
+
*/
|
|
186
|
+
export declare type ExpirableValue<T = any> = {
|
|
187
|
+
value: T;
|
|
188
|
+
expireAt: number;
|
|
37
189
|
};
|
|
38
190
|
|
|
39
|
-
|
|
191
|
+
/**
|
|
192
|
+
* 扁平化多个对象类型
|
|
193
|
+
* @template Types - 需要扁平化的对象类型元组
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* type Result = Flatten<[{ a: string }, { b: number }, { c: boolean }]>
|
|
197
|
+
* // Result = { a: string } & { b: number } & { c: boolean }
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export declare type Flatten<Types extends readonly any[]> = UnionToIntersection<Types[number]>;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 组合工具:扁平化多个对象类型并添加 instanceKey
|
|
204
|
+
* @template Types - 需要扁平化的对象类型元组
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* type Result = FlattenWithInstanceKey<[TypeA, TypeB, TypeC]>
|
|
208
|
+
* // Result = TypeA & TypeB & TypeC & { instanceKey?: Key }
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
export declare type FlattenWithInstanceKey<Types extends readonly any[]> = WithInstanceKey<Flatten<Types>>;
|
|
212
|
+
|
|
213
|
+
export declare type HandlerParams<T extends keyof Requestor> = Parameters<Requestor[T]>;
|
|
214
|
+
|
|
215
|
+
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;
|
|
216
|
+
|
|
217
|
+
declare type Id = string | number;
|
|
40
218
|
|
|
41
|
-
declare type
|
|
42
|
-
|
|
43
|
-
config: UnifiedConfig;
|
|
44
|
-
cachedData: CachedData;
|
|
219
|
+
declare type IdempotencyContext<D = any> = {
|
|
220
|
+
config: RequestConfig<D>;
|
|
45
221
|
};
|
|
46
222
|
|
|
47
|
-
declare
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
223
|
+
declare type IdempotencyOptions<D = any> = {
|
|
224
|
+
key: (params: IdempotencyContext<D>) => string;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export declare const idempotent: <D = any, R = any>(options?: Partial<IdempotencyOptions<D>>) => IdempotentMiddleware<D, R>;
|
|
228
|
+
|
|
229
|
+
declare type IdempotentMiddleware<D = any, R = any> = TypedMiddleware<MIDDLEWARE_TYPE.IDEMPOTENT, false, D, R> & {
|
|
230
|
+
promiseCache: PromiseCache;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export declare const inject: (requestor: BaseRequestor, instanceKey?: string) => void;
|
|
234
|
+
|
|
235
|
+
export declare type Key = string | symbol | number;
|
|
236
|
+
|
|
237
|
+
export declare type MaybePromise<IsSync, R> = IsSync extends true ? R : IsSync extends false ? Promise<R> : R | Promise<R>;
|
|
238
|
+
|
|
239
|
+
export declare type Middleware<IsSync extends boolean = false, D = any, R = any> = (context: {
|
|
240
|
+
config: RequestConfig<D>;
|
|
241
|
+
next: () => MaybePromise<IsSync, R>;
|
|
242
|
+
ctx: MiddlewareContext;
|
|
243
|
+
}) => MaybePromise<IsSync, R>;
|
|
244
|
+
|
|
245
|
+
declare enum MIDDLEWARE_TYPE {
|
|
246
|
+
CACHE = "cache",
|
|
247
|
+
RETRY = "retry",
|
|
248
|
+
IDEMPOTENT = "idempotent",
|
|
249
|
+
CONCURRENT = "concurrent",
|
|
250
|
+
SYNC = "sync"
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export declare interface MiddlewareContext extends Record<string, any> {
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
declare type MiddlewareType = MIDDLEWARE_TYPE;
|
|
257
|
+
|
|
258
|
+
declare type PromiseCache = ReturnType<typeof createPromiseCache>;
|
|
259
|
+
|
|
260
|
+
declare enum REQUEST_METHOD {
|
|
261
|
+
GET = "get",
|
|
262
|
+
POST = "post",
|
|
263
|
+
PUT = "put",
|
|
264
|
+
DELETE = "delete"
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export declare interface RequestConfig<D = any> extends Record<string, any> {
|
|
268
|
+
url: string;
|
|
269
|
+
method: RequestMethod;
|
|
53
270
|
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
271
|
}
|
|
60
272
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
};
|
|
141
|
-
};
|
|
142
|
-
|
|
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;
|
|
273
|
+
declare type RequestMethod = REQUEST_METHOD;
|
|
274
|
+
|
|
275
|
+
export declare interface Requestor<IsSync extends boolean = false> {
|
|
276
|
+
request: <R = any, D = any>(config: RequestConfig<D>) => MaybePromise<IsSync, R>;
|
|
277
|
+
get: <R = any, D = any>(url: string, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
|
|
278
|
+
post: <R = any, D = any>(url: string, data?: D, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
|
|
279
|
+
put: <R = any, D = any>(url: string, data?: D, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
|
|
280
|
+
delete: <R = any, D = any>(url: string, config?: WithoutMethod<D>) => MaybePromise<IsSync, R>;
|
|
149
281
|
}
|
|
150
282
|
|
|
151
|
-
declare
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
283
|
+
export declare const retry: <D = any, R = any>(options?: Partial<RetryOptions<D>>) => TypedMiddleware<MIDDLEWARE_TYPE.RETRY, false, D, R>;
|
|
284
|
+
|
|
285
|
+
declare type RetryContext<D = any> = {
|
|
286
|
+
config: RequestConfig<D>;
|
|
287
|
+
lastResponse: any;
|
|
288
|
+
attempt: number;
|
|
155
289
|
};
|
|
156
290
|
|
|
291
|
+
declare type RetryOptions<D = any> = {
|
|
292
|
+
retries: number;
|
|
293
|
+
delay: number | ((params: RetryContext<D>) => number);
|
|
294
|
+
retryCondition: (params: RetryContext<D>) => boolean;
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
declare type SafeKey = string | number | symbol;
|
|
298
|
+
|
|
299
|
+
export declare type StoreDescriptor = StoreKey | {
|
|
300
|
+
key: Key_2;
|
|
301
|
+
factory: StoreFactory<Store<AnyRecord>, any[]>;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 缓存中有数据则直接返回。
|
|
306
|
+
* suspense 为 true 时,会抛出一个 Promise(而不是普通错误),Promise resolve 后得到数据。
|
|
307
|
+
* suspense 为 false 时,会返回一个 Promise,Promise resolve 后得到数据,调用者需要用 await 或 .then 捕获。
|
|
308
|
+
*/
|
|
309
|
+
export declare function sync<D = any, R = any>(options: Partial<SyncOptions<D>> & {
|
|
310
|
+
suspense: true;
|
|
311
|
+
}): TypedMiddleware<MIDDLEWARE_TYPE.SYNC, true, D, R>;
|
|
312
|
+
|
|
313
|
+
export declare function sync<D = any, R = any>(options: Partial<SyncOptions<D>> & {
|
|
314
|
+
suspense: false;
|
|
315
|
+
}): TypedMiddleware<MIDDLEWARE_TYPE.SYNC, any, D, R>;
|
|
316
|
+
|
|
317
|
+
export declare function sync<D = any, R = any>(options?: Partial<SyncOptions<D>>): TypedMiddleware<MIDDLEWARE_TYPE.SYNC, true, D, R>;
|
|
318
|
+
|
|
319
|
+
declare interface SyncOptions<D = any, R = any> extends CacheOptions<D, R> {
|
|
320
|
+
isValid: (ctx: CacheCheckContext<D, R>) => boolean;
|
|
321
|
+
suspense: boolean;
|
|
322
|
+
wrapSuspense?: (params: WrapSuspense<D, R>) => any;
|
|
323
|
+
}
|
|
324
|
+
|
|
157
325
|
declare type Task<T> = () => Promise<T>;
|
|
158
326
|
|
|
159
327
|
declare type TaskItem<T> = {
|
|
@@ -164,15 +332,35 @@ declare type TaskItem<T> = {
|
|
|
164
332
|
|
|
165
333
|
declare type TaskItemList<T> = TaskQueue<TaskItem<T>>;
|
|
166
334
|
|
|
167
|
-
export declare type
|
|
168
|
-
|
|
169
|
-
method: Exclude<keyof Requestor, "request">;
|
|
335
|
+
export declare type TypedMiddleware<Type extends MiddlewareType, IsSync extends boolean = false, D = any, R = any> = Middleware<IsSync, D, R> & {
|
|
336
|
+
__middlewareType: Type;
|
|
170
337
|
};
|
|
171
338
|
|
|
172
|
-
|
|
339
|
+
/* Excluded from this release type: UnionToIntersection */
|
|
173
340
|
|
|
174
|
-
export declare const useRequestor: (instanceKey?:
|
|
341
|
+
export declare const useRequestor: (instanceKey?: Key) => BaseRequestor;
|
|
175
342
|
|
|
176
|
-
|
|
343
|
+
/**
|
|
344
|
+
* 给类型添加可选的 instanceKey 字段
|
|
345
|
+
* @template T - 源对象类型
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* type Config = { timeout: number }
|
|
349
|
+
* type Result = WithInstanceKey<Config>
|
|
350
|
+
* // Result = { timeout: number; instanceKey?: Key }
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
353
|
+
export declare type WithInstanceKey<T> = T & {
|
|
354
|
+
/** 请求器实例 key(可选) */
|
|
355
|
+
instanceKey?: Key;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
export declare type WithoutMethod<D = any> = Omit<RequestConfig<D>, 'method' | 'url'>;
|
|
359
|
+
|
|
360
|
+
declare type WrapSuspense<D = any, R = any> = {
|
|
361
|
+
key: CacheKey;
|
|
362
|
+
config: RequestConfig<D>;
|
|
363
|
+
p: Promise<R>;
|
|
364
|
+
};
|
|
177
365
|
|
|
178
366
|
export { }
|