@lytjs/plugin-data-fetch 6.5.0 → 6.6.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.cjs +64 -43
- package/dist/index.mjs +64 -43
- package/package.json +53 -53
- package/dist/index.d.cts +0 -196
- package/dist/index.d.ts +0 -196
package/dist/index.cjs
CHANGED
|
@@ -186,7 +186,12 @@ function createFetch(url, options = {}, globalOptions = {}) {
|
|
|
186
186
|
return result;
|
|
187
187
|
} catch (error) {
|
|
188
188
|
if (abortSignal.aborted) {
|
|
189
|
-
lastError = createFetchError(
|
|
189
|
+
lastError = createFetchError(
|
|
190
|
+
"Request cancelled",
|
|
191
|
+
currentConfig,
|
|
192
|
+
void 0,
|
|
193
|
+
error
|
|
194
|
+
);
|
|
190
195
|
break;
|
|
191
196
|
}
|
|
192
197
|
if (attempt < retries) {
|
|
@@ -284,68 +289,84 @@ function createFetchManager(globalOptions = {}) {
|
|
|
284
289
|
* 简单 GET 请求
|
|
285
290
|
*/
|
|
286
291
|
async get(url, options = {}) {
|
|
287
|
-
const instance = createFetch(
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
292
|
+
const instance = createFetch(
|
|
293
|
+
url,
|
|
294
|
+
{ ...options, method: "GET" },
|
|
295
|
+
{
|
|
296
|
+
...globalOptions,
|
|
297
|
+
cacheStorage,
|
|
298
|
+
requestInterceptors,
|
|
299
|
+
responseInterceptors,
|
|
300
|
+
errorInterceptors
|
|
301
|
+
}
|
|
302
|
+
);
|
|
294
303
|
return instance.fetch();
|
|
295
304
|
},
|
|
296
305
|
/**
|
|
297
306
|
* 简单 POST 请求
|
|
298
307
|
*/
|
|
299
308
|
async post(url, body, options = {}) {
|
|
300
|
-
const instance = createFetch(
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
"
|
|
306
|
-
|
|
309
|
+
const instance = createFetch(
|
|
310
|
+
url,
|
|
311
|
+
{
|
|
312
|
+
...options,
|
|
313
|
+
method: "POST",
|
|
314
|
+
body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
|
|
315
|
+
headers: {
|
|
316
|
+
"Content-Type": "application/json",
|
|
317
|
+
...options.headers
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
...globalOptions,
|
|
322
|
+
cacheStorage,
|
|
323
|
+
requestInterceptors,
|
|
324
|
+
responseInterceptors,
|
|
325
|
+
errorInterceptors
|
|
307
326
|
}
|
|
308
|
-
|
|
309
|
-
...globalOptions,
|
|
310
|
-
cacheStorage,
|
|
311
|
-
requestInterceptors,
|
|
312
|
-
responseInterceptors,
|
|
313
|
-
errorInterceptors
|
|
314
|
-
});
|
|
327
|
+
);
|
|
315
328
|
return instance.fetch();
|
|
316
329
|
},
|
|
317
330
|
/**
|
|
318
331
|
* 简单 PUT 请求
|
|
319
332
|
*/
|
|
320
333
|
async put(url, body, options = {}) {
|
|
321
|
-
const instance = createFetch(
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
"
|
|
327
|
-
|
|
334
|
+
const instance = createFetch(
|
|
335
|
+
url,
|
|
336
|
+
{
|
|
337
|
+
...options,
|
|
338
|
+
method: "PUT",
|
|
339
|
+
body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
|
|
340
|
+
headers: {
|
|
341
|
+
"Content-Type": "application/json",
|
|
342
|
+
...options.headers
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
...globalOptions,
|
|
347
|
+
cacheStorage,
|
|
348
|
+
requestInterceptors,
|
|
349
|
+
responseInterceptors,
|
|
350
|
+
errorInterceptors
|
|
328
351
|
}
|
|
329
|
-
|
|
330
|
-
...globalOptions,
|
|
331
|
-
cacheStorage,
|
|
332
|
-
requestInterceptors,
|
|
333
|
-
responseInterceptors,
|
|
334
|
-
errorInterceptors
|
|
335
|
-
});
|
|
352
|
+
);
|
|
336
353
|
return instance.fetch();
|
|
337
354
|
},
|
|
338
355
|
/**
|
|
339
356
|
* 简单 DELETE 请求
|
|
340
357
|
*/
|
|
341
358
|
async delete(url, options = {}) {
|
|
342
|
-
const instance = createFetch(
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
359
|
+
const instance = createFetch(
|
|
360
|
+
url,
|
|
361
|
+
{ ...options, method: "DELETE" },
|
|
362
|
+
{
|
|
363
|
+
...globalOptions,
|
|
364
|
+
cacheStorage,
|
|
365
|
+
requestInterceptors,
|
|
366
|
+
responseInterceptors,
|
|
367
|
+
errorInterceptors
|
|
368
|
+
}
|
|
369
|
+
);
|
|
349
370
|
return instance.fetch();
|
|
350
371
|
},
|
|
351
372
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -158,7 +158,12 @@ function createFetch(url, options = {}, globalOptions = {}) {
|
|
|
158
158
|
return result;
|
|
159
159
|
} catch (error) {
|
|
160
160
|
if (abortSignal.aborted) {
|
|
161
|
-
lastError = createFetchError(
|
|
161
|
+
lastError = createFetchError(
|
|
162
|
+
"Request cancelled",
|
|
163
|
+
currentConfig,
|
|
164
|
+
void 0,
|
|
165
|
+
error
|
|
166
|
+
);
|
|
162
167
|
break;
|
|
163
168
|
}
|
|
164
169
|
if (attempt < retries) {
|
|
@@ -256,68 +261,84 @@ function createFetchManager(globalOptions = {}) {
|
|
|
256
261
|
* 简单 GET 请求
|
|
257
262
|
*/
|
|
258
263
|
async get(url, options = {}) {
|
|
259
|
-
const instance = createFetch(
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
264
|
+
const instance = createFetch(
|
|
265
|
+
url,
|
|
266
|
+
{ ...options, method: "GET" },
|
|
267
|
+
{
|
|
268
|
+
...globalOptions,
|
|
269
|
+
cacheStorage,
|
|
270
|
+
requestInterceptors,
|
|
271
|
+
responseInterceptors,
|
|
272
|
+
errorInterceptors
|
|
273
|
+
}
|
|
274
|
+
);
|
|
266
275
|
return instance.fetch();
|
|
267
276
|
},
|
|
268
277
|
/**
|
|
269
278
|
* 简单 POST 请求
|
|
270
279
|
*/
|
|
271
280
|
async post(url, body, options = {}) {
|
|
272
|
-
const instance = createFetch(
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
"
|
|
278
|
-
|
|
281
|
+
const instance = createFetch(
|
|
282
|
+
url,
|
|
283
|
+
{
|
|
284
|
+
...options,
|
|
285
|
+
method: "POST",
|
|
286
|
+
body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
|
|
287
|
+
headers: {
|
|
288
|
+
"Content-Type": "application/json",
|
|
289
|
+
...options.headers
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
...globalOptions,
|
|
294
|
+
cacheStorage,
|
|
295
|
+
requestInterceptors,
|
|
296
|
+
responseInterceptors,
|
|
297
|
+
errorInterceptors
|
|
279
298
|
}
|
|
280
|
-
|
|
281
|
-
...globalOptions,
|
|
282
|
-
cacheStorage,
|
|
283
|
-
requestInterceptors,
|
|
284
|
-
responseInterceptors,
|
|
285
|
-
errorInterceptors
|
|
286
|
-
});
|
|
299
|
+
);
|
|
287
300
|
return instance.fetch();
|
|
288
301
|
},
|
|
289
302
|
/**
|
|
290
303
|
* 简单 PUT 请求
|
|
291
304
|
*/
|
|
292
305
|
async put(url, body, options = {}) {
|
|
293
|
-
const instance = createFetch(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
"
|
|
299
|
-
|
|
306
|
+
const instance = createFetch(
|
|
307
|
+
url,
|
|
308
|
+
{
|
|
309
|
+
...options,
|
|
310
|
+
method: "PUT",
|
|
311
|
+
body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
|
|
312
|
+
headers: {
|
|
313
|
+
"Content-Type": "application/json",
|
|
314
|
+
...options.headers
|
|
315
|
+
}
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
...globalOptions,
|
|
319
|
+
cacheStorage,
|
|
320
|
+
requestInterceptors,
|
|
321
|
+
responseInterceptors,
|
|
322
|
+
errorInterceptors
|
|
300
323
|
}
|
|
301
|
-
|
|
302
|
-
...globalOptions,
|
|
303
|
-
cacheStorage,
|
|
304
|
-
requestInterceptors,
|
|
305
|
-
responseInterceptors,
|
|
306
|
-
errorInterceptors
|
|
307
|
-
});
|
|
324
|
+
);
|
|
308
325
|
return instance.fetch();
|
|
309
326
|
},
|
|
310
327
|
/**
|
|
311
328
|
* 简单 DELETE 请求
|
|
312
329
|
*/
|
|
313
330
|
async delete(url, options = {}) {
|
|
314
|
-
const instance = createFetch(
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
331
|
+
const instance = createFetch(
|
|
332
|
+
url,
|
|
333
|
+
{ ...options, method: "DELETE" },
|
|
334
|
+
{
|
|
335
|
+
...globalOptions,
|
|
336
|
+
cacheStorage,
|
|
337
|
+
requestInterceptors,
|
|
338
|
+
responseInterceptors,
|
|
339
|
+
errorInterceptors
|
|
340
|
+
}
|
|
341
|
+
);
|
|
321
342
|
return instance.fetch();
|
|
322
343
|
},
|
|
323
344
|
/**
|
package/package.json
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@lytjs/plugin-data-fetch",
|
|
3
|
-
"version": "6.
|
|
4
|
-
"description": "LytJS official data fetch plugin with caching, retries, and interceptors",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.mjs",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.mjs",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
},
|
|
15
|
-
"./package.json": "./package.json"
|
|
16
|
-
},
|
|
17
|
-
"files": [
|
|
18
|
-
"dist"
|
|
19
|
-
],
|
|
20
|
-
"sideEffects": false,
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "tsup",
|
|
23
|
-
"dev": "tsup --watch",
|
|
24
|
-
"test": "vitest run",
|
|
25
|
-
"test:watch": "vitest",
|
|
26
|
-
"test:coverage": "vitest run --coverage",
|
|
27
|
-
"type-check": "tsc --noEmit",
|
|
28
|
-
"lint": "eslint \"src/**/*.ts\"",
|
|
29
|
-
"clean": "rm -rf dist"
|
|
30
|
-
},
|
|
31
|
-
"dependencies": {
|
|
32
|
-
"@lytjs/core": "
|
|
33
|
-
"@lytjs/reactivity": "
|
|
34
|
-
},
|
|
35
|
-
"devDependencies": {
|
|
36
|
-
"tsup": "^8.0.0",
|
|
37
|
-
"typescript": "^5.4.0",
|
|
38
|
-
"vitest": "^3.0.0"
|
|
39
|
-
},
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
44
|
-
"directory": "packages/plugins/packages/plugin-data-fetch"
|
|
45
|
-
},
|
|
46
|
-
"keywords": [
|
|
47
|
-
"lytjs",
|
|
48
|
-
"fetch",
|
|
49
|
-
"ajax",
|
|
50
|
-
"cache",
|
|
51
|
-
"interceptor"
|
|
52
|
-
]
|
|
53
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@lytjs/plugin-data-fetch",
|
|
3
|
+
"version": "6.6.0",
|
|
4
|
+
"description": "LytJS official data fetch plugin with caching, retries, and interceptors",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"test:coverage": "vitest run --coverage",
|
|
27
|
+
"type-check": "tsc --noEmit",
|
|
28
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
29
|
+
"clean": "rm -rf dist"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@lytjs/core": "workspace:*",
|
|
33
|
+
"@lytjs/reactivity": "workspace:*"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"tsup": "^8.0.0",
|
|
37
|
+
"typescript": "^5.4.0",
|
|
38
|
+
"vitest": "^3.0.0"
|
|
39
|
+
},
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
44
|
+
"directory": "packages/plugins/packages/plugin-data-fetch"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"lytjs",
|
|
48
|
+
"fetch",
|
|
49
|
+
"ajax",
|
|
50
|
+
"cache",
|
|
51
|
+
"interceptor"
|
|
52
|
+
]
|
|
53
|
+
}
|
package/dist/index.d.cts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import * as _lytjs_core from '@lytjs/core';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @lytjs/plugin-data-fetch - 类型定义
|
|
5
|
-
*/
|
|
6
|
-
interface RequestOptions extends RequestInit {
|
|
7
|
-
/** 基础 URL */
|
|
8
|
-
baseUrl?: string;
|
|
9
|
-
/** 超时时间(毫秒) */
|
|
10
|
-
timeout?: number;
|
|
11
|
-
/** 重试次数 */
|
|
12
|
-
retries?: number;
|
|
13
|
-
/** 重试延迟(毫秒) */
|
|
14
|
-
retryDelay?: number;
|
|
15
|
-
/** 缓存策略 */
|
|
16
|
-
cacheStrategy?: 'no-cache' | 'cache-first' | 'network-first' | 'cache-only';
|
|
17
|
-
/** 缓存时间(毫秒) */
|
|
18
|
-
cacheTime?: number;
|
|
19
|
-
/** 请求标识,用于缓存键生成 */
|
|
20
|
-
requestKey?: string;
|
|
21
|
-
/** 是否取消重复请求 */
|
|
22
|
-
cancelDuplicate?: boolean;
|
|
23
|
-
/** 自定义错误处理 */
|
|
24
|
-
onError?: (error: FetchError) => void | Promise<void>;
|
|
25
|
-
}
|
|
26
|
-
interface FetchError extends Error {
|
|
27
|
-
/** HTTP 状态码 */
|
|
28
|
-
status?: number;
|
|
29
|
-
/** 原始响应 */
|
|
30
|
-
response?: Response;
|
|
31
|
-
/** 原始错误 */
|
|
32
|
-
originalError?: Error;
|
|
33
|
-
/** 请求配置 */
|
|
34
|
-
config?: RequestOptions;
|
|
35
|
-
}
|
|
36
|
-
interface CacheEntry<T = unknown> {
|
|
37
|
-
/** 缓存数据 */
|
|
38
|
-
data: T;
|
|
39
|
-
/** 过期时间 */
|
|
40
|
-
expiresAt: number;
|
|
41
|
-
/** 创建时间 */
|
|
42
|
-
createdAt: number;
|
|
43
|
-
}
|
|
44
|
-
interface CacheStorage {
|
|
45
|
-
/** 获取缓存 */
|
|
46
|
-
get<T = unknown>(key: string): CacheEntry<T> | null;
|
|
47
|
-
/** 设置缓存 */
|
|
48
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
49
|
-
/** 删除缓存 */
|
|
50
|
-
delete(key: string): void;
|
|
51
|
-
/** 清空缓存 */
|
|
52
|
-
clear(): void;
|
|
53
|
-
/** 检查缓存是否存在且有效 */
|
|
54
|
-
has(key: string): boolean;
|
|
55
|
-
}
|
|
56
|
-
interface Interceptor<T = unknown> {
|
|
57
|
-
/** 请求拦截器 */
|
|
58
|
-
request?: (config: RequestOptions) => RequestOptions | Promise<RequestOptions>;
|
|
59
|
-
/** 响应拦截器 */
|
|
60
|
-
response?: (response: T) => T | Promise<T>;
|
|
61
|
-
/** 错误拦截器 */
|
|
62
|
-
error?: (error: FetchError) => FetchError | Promise<FetchError>;
|
|
63
|
-
}
|
|
64
|
-
interface FetchState<T = unknown> {
|
|
65
|
-
/** 请求数据 */
|
|
66
|
-
data: T | null;
|
|
67
|
-
/** 加载状态 */
|
|
68
|
-
isLoading: boolean;
|
|
69
|
-
/** 错误状态 */
|
|
70
|
-
error: FetchError | null;
|
|
71
|
-
/** 是否已完成 */
|
|
72
|
-
isSuccess: boolean;
|
|
73
|
-
/** 是否失败 */
|
|
74
|
-
isError: boolean;
|
|
75
|
-
/** 请求次数 */
|
|
76
|
-
refetchCount: number;
|
|
77
|
-
}
|
|
78
|
-
interface FetchInstance<T = unknown> {
|
|
79
|
-
/** 当前状态 */
|
|
80
|
-
state: Readonly<FetchState<T>>;
|
|
81
|
-
/** 发起请求 */
|
|
82
|
-
fetch(): Promise<T>;
|
|
83
|
-
/** 重新请求 */
|
|
84
|
-
refetch(): Promise<T>;
|
|
85
|
-
/** 取消请求 */
|
|
86
|
-
cancel(): void;
|
|
87
|
-
/** 手动更新数据 */
|
|
88
|
-
setData(data: T | ((prev: T | null) => T)): void;
|
|
89
|
-
/** 手动更新错误 */
|
|
90
|
-
setError(error: FetchError | null): void;
|
|
91
|
-
/** 清空状态 */
|
|
92
|
-
reset(): void;
|
|
93
|
-
}
|
|
94
|
-
interface FetchPluginOptions {
|
|
95
|
-
/** 基础 URL */
|
|
96
|
-
baseUrl?: string;
|
|
97
|
-
/** 默认超时时间 */
|
|
98
|
-
timeout?: number;
|
|
99
|
-
/** 默认重试次数 */
|
|
100
|
-
retries?: number;
|
|
101
|
-
/** 默认重试延迟 */
|
|
102
|
-
retryDelay?: number;
|
|
103
|
-
/** 默认缓存策略 */
|
|
104
|
-
defaultCacheStrategy?: RequestOptions['cacheStrategy'];
|
|
105
|
-
/** 默认缓存时间 */
|
|
106
|
-
defaultCacheTime?: number;
|
|
107
|
-
/** 请求拦截器 */
|
|
108
|
-
requestInterceptors?: Interceptor['request'][];
|
|
109
|
-
/** 响应拦截器 */
|
|
110
|
-
responseInterceptors?: Interceptor['response'][];
|
|
111
|
-
/** 错误拦截器 */
|
|
112
|
-
errorInterceptors?: Interceptor['error'][];
|
|
113
|
-
/** 自定义缓存存储 */
|
|
114
|
-
cacheStorage?: CacheStorage;
|
|
115
|
-
}
|
|
116
|
-
interface RequestInterceptor {
|
|
117
|
-
(config: RequestOptions): RequestOptions | Promise<RequestOptions>;
|
|
118
|
-
}
|
|
119
|
-
interface ResponseInterceptor {
|
|
120
|
-
<T = unknown>(response: T): T | Promise<T>;
|
|
121
|
-
}
|
|
122
|
-
interface ErrorInterceptor {
|
|
123
|
-
(error: FetchError): FetchError | Promise<FetchError>;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 默认内存缓存实现
|
|
128
|
-
*/
|
|
129
|
-
declare class DefaultCacheStorage implements CacheStorage {
|
|
130
|
-
private cache;
|
|
131
|
-
get<T = unknown>(key: string): CacheEntry<T> | null;
|
|
132
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
133
|
-
delete(key: string): void;
|
|
134
|
-
clear(): void;
|
|
135
|
-
has(key: string): boolean;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* 生成缓存键
|
|
139
|
-
*/
|
|
140
|
-
declare function generateCacheKey(url: string, options?: RequestOptions): string;
|
|
141
|
-
/**
|
|
142
|
-
* 创建数据获取实例
|
|
143
|
-
*/
|
|
144
|
-
declare function createFetch<T = unknown>(url: string, options?: RequestOptions, globalOptions?: FetchPluginOptions): FetchInstance<T>;
|
|
145
|
-
/**
|
|
146
|
-
* 创建 Fetch 管理器
|
|
147
|
-
*/
|
|
148
|
-
declare function createFetchManager(globalOptions?: FetchPluginOptions): {
|
|
149
|
-
/**
|
|
150
|
-
* 创建 Fetch 实例
|
|
151
|
-
*/
|
|
152
|
-
createFetch<T = unknown>(url: string, options?: RequestOptions): FetchInstance<T>;
|
|
153
|
-
/**
|
|
154
|
-
* 简单 GET 请求
|
|
155
|
-
*/
|
|
156
|
-
get<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
157
|
-
/**
|
|
158
|
-
* 简单 POST 请求
|
|
159
|
-
*/
|
|
160
|
-
post<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
161
|
-
/**
|
|
162
|
-
* 简单 PUT 请求
|
|
163
|
-
*/
|
|
164
|
-
put<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
165
|
-
/**
|
|
166
|
-
* 简单 DELETE 请求
|
|
167
|
-
*/
|
|
168
|
-
delete<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
169
|
-
/**
|
|
170
|
-
* 添加请求拦截器
|
|
171
|
-
*/
|
|
172
|
-
addRequestInterceptor(interceptor: RequestInterceptor): void;
|
|
173
|
-
/**
|
|
174
|
-
* 添加响应拦截器
|
|
175
|
-
*/
|
|
176
|
-
addResponseInterceptor(interceptor: ResponseInterceptor): void;
|
|
177
|
-
/**
|
|
178
|
-
* 添加错误拦截器
|
|
179
|
-
*/
|
|
180
|
-
addErrorInterceptor(interceptor: ErrorInterceptor): void;
|
|
181
|
-
/**
|
|
182
|
-
* 获取缓存存储
|
|
183
|
-
*/
|
|
184
|
-
getCacheStorage(): CacheStorage;
|
|
185
|
-
/**
|
|
186
|
-
* 清除缓存
|
|
187
|
-
*/
|
|
188
|
-
clearCache(): void;
|
|
189
|
-
/**
|
|
190
|
-
* 删除特定缓存
|
|
191
|
-
*/
|
|
192
|
-
invalidateCache(key: string): void;
|
|
193
|
-
};
|
|
194
|
-
declare const pluginDataFetch: _lytjs_core.PluginDefinition<unknown>;
|
|
195
|
-
|
|
196
|
-
export { type CacheEntry, type CacheStorage, DefaultCacheStorage, type ErrorInterceptor, type FetchError, type FetchInstance, type FetchPluginOptions, type FetchState, type Interceptor, type RequestInterceptor, type RequestOptions, type ResponseInterceptor, createFetch, createFetchManager, pluginDataFetch as default, generateCacheKey };
|
package/dist/index.d.ts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import * as _lytjs_core from '@lytjs/core';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @lytjs/plugin-data-fetch - 类型定义
|
|
5
|
-
*/
|
|
6
|
-
interface RequestOptions extends RequestInit {
|
|
7
|
-
/** 基础 URL */
|
|
8
|
-
baseUrl?: string;
|
|
9
|
-
/** 超时时间(毫秒) */
|
|
10
|
-
timeout?: number;
|
|
11
|
-
/** 重试次数 */
|
|
12
|
-
retries?: number;
|
|
13
|
-
/** 重试延迟(毫秒) */
|
|
14
|
-
retryDelay?: number;
|
|
15
|
-
/** 缓存策略 */
|
|
16
|
-
cacheStrategy?: 'no-cache' | 'cache-first' | 'network-first' | 'cache-only';
|
|
17
|
-
/** 缓存时间(毫秒) */
|
|
18
|
-
cacheTime?: number;
|
|
19
|
-
/** 请求标识,用于缓存键生成 */
|
|
20
|
-
requestKey?: string;
|
|
21
|
-
/** 是否取消重复请求 */
|
|
22
|
-
cancelDuplicate?: boolean;
|
|
23
|
-
/** 自定义错误处理 */
|
|
24
|
-
onError?: (error: FetchError) => void | Promise<void>;
|
|
25
|
-
}
|
|
26
|
-
interface FetchError extends Error {
|
|
27
|
-
/** HTTP 状态码 */
|
|
28
|
-
status?: number;
|
|
29
|
-
/** 原始响应 */
|
|
30
|
-
response?: Response;
|
|
31
|
-
/** 原始错误 */
|
|
32
|
-
originalError?: Error;
|
|
33
|
-
/** 请求配置 */
|
|
34
|
-
config?: RequestOptions;
|
|
35
|
-
}
|
|
36
|
-
interface CacheEntry<T = unknown> {
|
|
37
|
-
/** 缓存数据 */
|
|
38
|
-
data: T;
|
|
39
|
-
/** 过期时间 */
|
|
40
|
-
expiresAt: number;
|
|
41
|
-
/** 创建时间 */
|
|
42
|
-
createdAt: number;
|
|
43
|
-
}
|
|
44
|
-
interface CacheStorage {
|
|
45
|
-
/** 获取缓存 */
|
|
46
|
-
get<T = unknown>(key: string): CacheEntry<T> | null;
|
|
47
|
-
/** 设置缓存 */
|
|
48
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
49
|
-
/** 删除缓存 */
|
|
50
|
-
delete(key: string): void;
|
|
51
|
-
/** 清空缓存 */
|
|
52
|
-
clear(): void;
|
|
53
|
-
/** 检查缓存是否存在且有效 */
|
|
54
|
-
has(key: string): boolean;
|
|
55
|
-
}
|
|
56
|
-
interface Interceptor<T = unknown> {
|
|
57
|
-
/** 请求拦截器 */
|
|
58
|
-
request?: (config: RequestOptions) => RequestOptions | Promise<RequestOptions>;
|
|
59
|
-
/** 响应拦截器 */
|
|
60
|
-
response?: (response: T) => T | Promise<T>;
|
|
61
|
-
/** 错误拦截器 */
|
|
62
|
-
error?: (error: FetchError) => FetchError | Promise<FetchError>;
|
|
63
|
-
}
|
|
64
|
-
interface FetchState<T = unknown> {
|
|
65
|
-
/** 请求数据 */
|
|
66
|
-
data: T | null;
|
|
67
|
-
/** 加载状态 */
|
|
68
|
-
isLoading: boolean;
|
|
69
|
-
/** 错误状态 */
|
|
70
|
-
error: FetchError | null;
|
|
71
|
-
/** 是否已完成 */
|
|
72
|
-
isSuccess: boolean;
|
|
73
|
-
/** 是否失败 */
|
|
74
|
-
isError: boolean;
|
|
75
|
-
/** 请求次数 */
|
|
76
|
-
refetchCount: number;
|
|
77
|
-
}
|
|
78
|
-
interface FetchInstance<T = unknown> {
|
|
79
|
-
/** 当前状态 */
|
|
80
|
-
state: Readonly<FetchState<T>>;
|
|
81
|
-
/** 发起请求 */
|
|
82
|
-
fetch(): Promise<T>;
|
|
83
|
-
/** 重新请求 */
|
|
84
|
-
refetch(): Promise<T>;
|
|
85
|
-
/** 取消请求 */
|
|
86
|
-
cancel(): void;
|
|
87
|
-
/** 手动更新数据 */
|
|
88
|
-
setData(data: T | ((prev: T | null) => T)): void;
|
|
89
|
-
/** 手动更新错误 */
|
|
90
|
-
setError(error: FetchError | null): void;
|
|
91
|
-
/** 清空状态 */
|
|
92
|
-
reset(): void;
|
|
93
|
-
}
|
|
94
|
-
interface FetchPluginOptions {
|
|
95
|
-
/** 基础 URL */
|
|
96
|
-
baseUrl?: string;
|
|
97
|
-
/** 默认超时时间 */
|
|
98
|
-
timeout?: number;
|
|
99
|
-
/** 默认重试次数 */
|
|
100
|
-
retries?: number;
|
|
101
|
-
/** 默认重试延迟 */
|
|
102
|
-
retryDelay?: number;
|
|
103
|
-
/** 默认缓存策略 */
|
|
104
|
-
defaultCacheStrategy?: RequestOptions['cacheStrategy'];
|
|
105
|
-
/** 默认缓存时间 */
|
|
106
|
-
defaultCacheTime?: number;
|
|
107
|
-
/** 请求拦截器 */
|
|
108
|
-
requestInterceptors?: Interceptor['request'][];
|
|
109
|
-
/** 响应拦截器 */
|
|
110
|
-
responseInterceptors?: Interceptor['response'][];
|
|
111
|
-
/** 错误拦截器 */
|
|
112
|
-
errorInterceptors?: Interceptor['error'][];
|
|
113
|
-
/** 自定义缓存存储 */
|
|
114
|
-
cacheStorage?: CacheStorage;
|
|
115
|
-
}
|
|
116
|
-
interface RequestInterceptor {
|
|
117
|
-
(config: RequestOptions): RequestOptions | Promise<RequestOptions>;
|
|
118
|
-
}
|
|
119
|
-
interface ResponseInterceptor {
|
|
120
|
-
<T = unknown>(response: T): T | Promise<T>;
|
|
121
|
-
}
|
|
122
|
-
interface ErrorInterceptor {
|
|
123
|
-
(error: FetchError): FetchError | Promise<FetchError>;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 默认内存缓存实现
|
|
128
|
-
*/
|
|
129
|
-
declare class DefaultCacheStorage implements CacheStorage {
|
|
130
|
-
private cache;
|
|
131
|
-
get<T = unknown>(key: string): CacheEntry<T> | null;
|
|
132
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
133
|
-
delete(key: string): void;
|
|
134
|
-
clear(): void;
|
|
135
|
-
has(key: string): boolean;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* 生成缓存键
|
|
139
|
-
*/
|
|
140
|
-
declare function generateCacheKey(url: string, options?: RequestOptions): string;
|
|
141
|
-
/**
|
|
142
|
-
* 创建数据获取实例
|
|
143
|
-
*/
|
|
144
|
-
declare function createFetch<T = unknown>(url: string, options?: RequestOptions, globalOptions?: FetchPluginOptions): FetchInstance<T>;
|
|
145
|
-
/**
|
|
146
|
-
* 创建 Fetch 管理器
|
|
147
|
-
*/
|
|
148
|
-
declare function createFetchManager(globalOptions?: FetchPluginOptions): {
|
|
149
|
-
/**
|
|
150
|
-
* 创建 Fetch 实例
|
|
151
|
-
*/
|
|
152
|
-
createFetch<T = unknown>(url: string, options?: RequestOptions): FetchInstance<T>;
|
|
153
|
-
/**
|
|
154
|
-
* 简单 GET 请求
|
|
155
|
-
*/
|
|
156
|
-
get<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
157
|
-
/**
|
|
158
|
-
* 简单 POST 请求
|
|
159
|
-
*/
|
|
160
|
-
post<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
161
|
-
/**
|
|
162
|
-
* 简单 PUT 请求
|
|
163
|
-
*/
|
|
164
|
-
put<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
165
|
-
/**
|
|
166
|
-
* 简单 DELETE 请求
|
|
167
|
-
*/
|
|
168
|
-
delete<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
169
|
-
/**
|
|
170
|
-
* 添加请求拦截器
|
|
171
|
-
*/
|
|
172
|
-
addRequestInterceptor(interceptor: RequestInterceptor): void;
|
|
173
|
-
/**
|
|
174
|
-
* 添加响应拦截器
|
|
175
|
-
*/
|
|
176
|
-
addResponseInterceptor(interceptor: ResponseInterceptor): void;
|
|
177
|
-
/**
|
|
178
|
-
* 添加错误拦截器
|
|
179
|
-
*/
|
|
180
|
-
addErrorInterceptor(interceptor: ErrorInterceptor): void;
|
|
181
|
-
/**
|
|
182
|
-
* 获取缓存存储
|
|
183
|
-
*/
|
|
184
|
-
getCacheStorage(): CacheStorage;
|
|
185
|
-
/**
|
|
186
|
-
* 清除缓存
|
|
187
|
-
*/
|
|
188
|
-
clearCache(): void;
|
|
189
|
-
/**
|
|
190
|
-
* 删除特定缓存
|
|
191
|
-
*/
|
|
192
|
-
invalidateCache(key: string): void;
|
|
193
|
-
};
|
|
194
|
-
declare const pluginDataFetch: _lytjs_core.PluginDefinition<unknown>;
|
|
195
|
-
|
|
196
|
-
export { type CacheEntry, type CacheStorage, DefaultCacheStorage, type ErrorInterceptor, type FetchError, type FetchInstance, type FetchPluginOptions, type FetchState, type Interceptor, type RequestInterceptor, type RequestOptions, type ResponseInterceptor, createFetch, createFetchManager, pluginDataFetch as default, generateCacheKey };
|