@lytjs/plugin-data 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/README.md +82 -82
- package/dist/index.cjs +9 -7
- package/dist/index.mjs +9 -7
- package/package.json +66 -66
- package/dist/index.d.cts +0 -131
- package/dist/index.d.ts +0 -131
package/README.md
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
# @lytjs/plugin-data
|
|
2
|
-
|
|
3
|
-
LytJS 官方增强版数据获取插件,提供乐观更新、缓存策略、请求去重等功能。
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pnpm add @lytjs/plugin-data
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 快速开始
|
|
12
|
-
|
|
13
|
-
### 作为插件使用
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { createApp } from '@lytjs/core';
|
|
17
|
-
import pluginData from '@lytjs/plugin-data';
|
|
18
|
-
|
|
19
|
-
const app = createApp();
|
|
20
|
-
app.use(pluginData);
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
### 独立使用
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
import { createData } from '@lytjs/plugin-data';
|
|
27
|
-
|
|
28
|
-
// 创建数据实例
|
|
29
|
-
const data = createData('/api/users');
|
|
30
|
-
|
|
31
|
-
// 监听数据变化
|
|
32
|
-
data.on('data', (users) => {
|
|
33
|
-
console.log('数据已更新:', users);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// 手动刷新
|
|
37
|
-
await data.refresh();
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## 特性
|
|
41
|
-
|
|
42
|
-
- 乐观更新
|
|
43
|
-
- 请求去重
|
|
44
|
-
- 多种缓存策略(TTL、LRU)
|
|
45
|
-
- 自动重试
|
|
46
|
-
- 与 @lytjs/plugin-data-fetch 深度集成
|
|
47
|
-
- 零外部依赖
|
|
48
|
-
|
|
49
|
-
## API
|
|
50
|
-
|
|
51
|
-
### createData(url, options, globalOptions)
|
|
52
|
-
|
|
53
|
-
创建数据获取实例。
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
import { createData } from '@lytjs/plugin-data';
|
|
57
|
-
|
|
58
|
-
const data = createData('/api/users', {
|
|
59
|
-
method: 'GET',
|
|
60
|
-
cache: 'ttl',
|
|
61
|
-
ttl: 60000,
|
|
62
|
-
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### createDataManager(globalOptions)
|
|
66
|
-
|
|
67
|
-
创建全局数据管理器。
|
|
68
|
-
|
|
69
|
-
```typescript
|
|
70
|
-
import { createDataManager } from '@lytjs/plugin-data';
|
|
71
|
-
|
|
72
|
-
const manager = createDataManager({
|
|
73
|
-
defaultCache: 'ttl',
|
|
74
|
-
defaultTTL: 60000,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
const data = manager.create('/api/users');
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## 许可证
|
|
81
|
-
|
|
82
|
-
MIT
|
|
1
|
+
# @lytjs/plugin-data
|
|
2
|
+
|
|
3
|
+
LytJS 官方增强版数据获取插件,提供乐观更新、缓存策略、请求去重等功能。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @lytjs/plugin-data
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 快速开始
|
|
12
|
+
|
|
13
|
+
### 作为插件使用
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { createApp } from '@lytjs/core';
|
|
17
|
+
import pluginData from '@lytjs/plugin-data';
|
|
18
|
+
|
|
19
|
+
const app = createApp();
|
|
20
|
+
app.use(pluginData);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### 独立使用
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { createData } from '@lytjs/plugin-data';
|
|
27
|
+
|
|
28
|
+
// 创建数据实例
|
|
29
|
+
const data = createData('/api/users');
|
|
30
|
+
|
|
31
|
+
// 监听数据变化
|
|
32
|
+
data.on('data', (users) => {
|
|
33
|
+
console.log('数据已更新:', users);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// 手动刷新
|
|
37
|
+
await data.refresh();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 特性
|
|
41
|
+
|
|
42
|
+
- 乐观更新
|
|
43
|
+
- 请求去重
|
|
44
|
+
- 多种缓存策略(TTL、LRU)
|
|
45
|
+
- 自动重试
|
|
46
|
+
- 与 @lytjs/plugin-data-fetch 深度集成
|
|
47
|
+
- 零外部依赖
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### createData(url, options, globalOptions)
|
|
52
|
+
|
|
53
|
+
创建数据获取实例。
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { createData } from '@lytjs/plugin-data';
|
|
57
|
+
|
|
58
|
+
const data = createData('/api/users', {
|
|
59
|
+
method: 'GET',
|
|
60
|
+
cache: 'ttl',
|
|
61
|
+
ttl: 60000,
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### createDataManager(globalOptions)
|
|
66
|
+
|
|
67
|
+
创建全局数据管理器。
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { createDataManager } from '@lytjs/plugin-data';
|
|
71
|
+
|
|
72
|
+
const manager = createDataManager({
|
|
73
|
+
defaultCache: 'ttl',
|
|
74
|
+
defaultTTL: 60000,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const data = manager.create('/api/users');
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 许可证
|
|
81
|
+
|
|
82
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -162,14 +162,14 @@ function createData(url, options = {}, globalOptions = {}) {
|
|
|
162
162
|
}));
|
|
163
163
|
const {
|
|
164
164
|
baseUrl = globalOptions.baseUrl || "",
|
|
165
|
-
timeout = globalOptions.timeout || 3e4,
|
|
166
|
-
retries = globalOptions.retries || 0,
|
|
167
|
-
retryDelay = globalOptions.retryDelay || 1e3,
|
|
168
|
-
cacheStrategy = globalOptions.defaultCacheStrategy || "no-cache",
|
|
169
|
-
cacheTime = globalOptions.defaultCacheTime || 3e5,
|
|
165
|
+
timeout: _timeout = globalOptions.timeout || 3e4,
|
|
166
|
+
retries: _retries = globalOptions.retries || 0,
|
|
167
|
+
retryDelay: _retryDelay = globalOptions.retryDelay || 1e3,
|
|
168
|
+
cacheStrategy: _cacheStrategy = globalOptions.defaultCacheStrategy || "no-cache",
|
|
169
|
+
cacheTime: _cacheTime = globalOptions.defaultCacheTime || 3e5,
|
|
170
170
|
dedupe = globalOptions.defaultDedupe || false,
|
|
171
171
|
optimisticData,
|
|
172
|
-
prefetch = false
|
|
172
|
+
prefetch: _prefetch = false
|
|
173
173
|
} = options;
|
|
174
174
|
const cacheStorage = globalOptions.cacheStorage || new import_plugin_data_fetch.DefaultCacheStorage();
|
|
175
175
|
const fullUrl = baseUrl ? baseUrl.endsWith("/") ? baseUrl + url.slice(1) : baseUrl + url : url;
|
|
@@ -348,7 +348,9 @@ function createDataManager(globalOptions = {}) {
|
|
|
348
348
|
return instance.fetch();
|
|
349
349
|
},
|
|
350
350
|
addRequestInterceptor(interceptor) {
|
|
351
|
-
manager.addRequestInterceptor(
|
|
351
|
+
manager.addRequestInterceptor(
|
|
352
|
+
interceptor
|
|
353
|
+
);
|
|
352
354
|
},
|
|
353
355
|
addResponseInterceptor(interceptor) {
|
|
354
356
|
manager.addResponseInterceptor(interceptor);
|
package/dist/index.mjs
CHANGED
|
@@ -137,14 +137,14 @@ function createData(url, options = {}, globalOptions = {}) {
|
|
|
137
137
|
}));
|
|
138
138
|
const {
|
|
139
139
|
baseUrl = globalOptions.baseUrl || "",
|
|
140
|
-
timeout = globalOptions.timeout || 3e4,
|
|
141
|
-
retries = globalOptions.retries || 0,
|
|
142
|
-
retryDelay = globalOptions.retryDelay || 1e3,
|
|
143
|
-
cacheStrategy = globalOptions.defaultCacheStrategy || "no-cache",
|
|
144
|
-
cacheTime = globalOptions.defaultCacheTime || 3e5,
|
|
140
|
+
timeout: _timeout = globalOptions.timeout || 3e4,
|
|
141
|
+
retries: _retries = globalOptions.retries || 0,
|
|
142
|
+
retryDelay: _retryDelay = globalOptions.retryDelay || 1e3,
|
|
143
|
+
cacheStrategy: _cacheStrategy = globalOptions.defaultCacheStrategy || "no-cache",
|
|
144
|
+
cacheTime: _cacheTime = globalOptions.defaultCacheTime || 3e5,
|
|
145
145
|
dedupe = globalOptions.defaultDedupe || false,
|
|
146
146
|
optimisticData,
|
|
147
|
-
prefetch = false
|
|
147
|
+
prefetch: _prefetch = false
|
|
148
148
|
} = options;
|
|
149
149
|
const cacheStorage = globalOptions.cacheStorage || new DefaultCacheStorage();
|
|
150
150
|
const fullUrl = baseUrl ? baseUrl.endsWith("/") ? baseUrl + url.slice(1) : baseUrl + url : url;
|
|
@@ -323,7 +323,9 @@ function createDataManager(globalOptions = {}) {
|
|
|
323
323
|
return instance.fetch();
|
|
324
324
|
},
|
|
325
325
|
addRequestInterceptor(interceptor) {
|
|
326
|
-
manager.addRequestInterceptor(
|
|
326
|
+
manager.addRequestInterceptor(
|
|
327
|
+
interceptor
|
|
328
|
+
);
|
|
327
329
|
},
|
|
328
330
|
addResponseInterceptor(interceptor) {
|
|
329
331
|
manager.addResponseInterceptor(interceptor);
|
package/package.json
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
-
"name": "@lytjs/plugin-data",
|
|
4
|
-
"version": "6.
|
|
5
|
-
"description": "LytJS official enhanced data plugin with optimistic updates, deduplication, and store integration",
|
|
6
|
-
"author": "lytjs",
|
|
7
|
-
"license": "MIT",
|
|
8
|
-
"type": "module",
|
|
9
|
-
"main": "./dist/index.cjs",
|
|
10
|
-
"module": "./dist/index.mjs",
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"exports": {
|
|
13
|
-
".": {
|
|
14
|
-
"types": "./dist/index.d.ts",
|
|
15
|
-
"import": "./dist/index.mjs",
|
|
16
|
-
"require": "./dist/index.cjs"
|
|
17
|
-
},
|
|
18
|
-
"./package.json": "./package.json"
|
|
19
|
-
},
|
|
20
|
-
"files": [
|
|
21
|
-
"dist",
|
|
22
|
-
"README.md",
|
|
23
|
-
"LICENSE"
|
|
24
|
-
],
|
|
25
|
-
"sideEffects": false,
|
|
26
|
-
"scripts": {
|
|
27
|
-
"dev": "tsup --watch",
|
|
28
|
-
"build": "tsup",
|
|
29
|
-
"test": "vitest run",
|
|
30
|
-
"test:watch": "vitest",
|
|
31
|
-
"test:coverage": "vitest run --coverage",
|
|
32
|
-
"type-check": "tsc --noEmit",
|
|
33
|
-
"clean": "rm -rf dist node_modules .turbo"
|
|
34
|
-
},
|
|
35
|
-
"dependencies": {
|
|
36
|
-
"@lytjs/core": "
|
|
37
|
-
"@lytjs/reactivity": "
|
|
38
|
-
"@lytjs/plugin-data-fetch": "
|
|
39
|
-
},
|
|
40
|
-
"devDependencies": {
|
|
41
|
-
"tsup": "^8.3.6",
|
|
42
|
-
"typescript": "^5.7.3",
|
|
43
|
-
"vitest": "^3.0.0"
|
|
44
|
-
},
|
|
45
|
-
"peerDependencies": {},
|
|
46
|
-
"publishConfig": {
|
|
47
|
-
"access": "public",
|
|
48
|
-
"registry": "https://registry.npmjs.org/"
|
|
49
|
-
},
|
|
50
|
-
"repository": {
|
|
51
|
-
"type": "git",
|
|
52
|
-
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
53
|
-
"directory": "packages/plugins/packages/plugin-data"
|
|
54
|
-
},
|
|
55
|
-
"keywords": [
|
|
56
|
-
"lytjs",
|
|
57
|
-
"data",
|
|
58
|
-
"fetch",
|
|
59
|
-
"optimistic",
|
|
60
|
-
"store"
|
|
61
|
-
],
|
|
62
|
-
"homepage": "https://gitee.com/lytjs/lytjs/tree/develop/packages/plugins/packages/plugin-data#readme",
|
|
63
|
-
"bugs": {
|
|
64
|
-
"url": "https://gitee.com/lytjs/lytjs/issues"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@lytjs/plugin-data",
|
|
4
|
+
"version": "6.6.0",
|
|
5
|
+
"description": "LytJS official enhanced data plugin with optimistic updates, deduplication, and store integration",
|
|
6
|
+
"author": "lytjs",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"module": "./dist/index.mjs",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"scripts": {
|
|
27
|
+
"dev": "tsup --watch",
|
|
28
|
+
"build": "tsup",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"test:watch": "vitest",
|
|
31
|
+
"test:coverage": "vitest run --coverage",
|
|
32
|
+
"type-check": "tsc --noEmit",
|
|
33
|
+
"clean": "rm -rf dist node_modules .turbo"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@lytjs/core": "workspace:*",
|
|
37
|
+
"@lytjs/reactivity": "workspace:*",
|
|
38
|
+
"@lytjs/plugin-data-fetch": "workspace:*"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"tsup": "^8.3.6",
|
|
42
|
+
"typescript": "^5.7.3",
|
|
43
|
+
"vitest": "^3.0.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
53
|
+
"directory": "packages/plugins/packages/plugin-data"
|
|
54
|
+
},
|
|
55
|
+
"keywords": [
|
|
56
|
+
"lytjs",
|
|
57
|
+
"data",
|
|
58
|
+
"fetch",
|
|
59
|
+
"optimistic",
|
|
60
|
+
"store"
|
|
61
|
+
],
|
|
62
|
+
"homepage": "https://gitee.com/lytjs/lytjs/tree/develop/packages/plugins/packages/plugin-data#readme",
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://gitee.com/lytjs/lytjs/issues"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/index.d.cts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import * as _lytjs_core from '@lytjs/core';
|
|
2
|
-
import { FetchInstance, RequestOptions as RequestOptions$1, FetchError, CacheStorage, FetchPluginOptions, CacheEntry } from '@lytjs/plugin-data-fetch';
|
|
3
|
-
export { CacheEntry, CacheStorage, FetchError, generateCacheKey } from '@lytjs/plugin-data-fetch';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @lytjs/plugin-data - 类型定义
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
interface RequestOptions extends RequestOptions$1 {
|
|
10
|
-
/** 是否启用请求去重 */
|
|
11
|
-
dedupe?: boolean;
|
|
12
|
-
/** 乐观更新数据 */
|
|
13
|
-
optimisticData?: unknown;
|
|
14
|
-
/** 预取相关配置 */
|
|
15
|
-
prefetch?: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface DataPluginOptions extends FetchPluginOptions {
|
|
19
|
-
/** 默认去重设置 */
|
|
20
|
-
defaultDedupe?: boolean;
|
|
21
|
-
/** 离线模式 */
|
|
22
|
-
offlineMode?: boolean;
|
|
23
|
-
/** 与 store 的集成配置 */
|
|
24
|
-
storeIntegration?: {
|
|
25
|
-
/** 数据同步时的钩子 */
|
|
26
|
-
onSync?: (key: string, data: unknown) => void;
|
|
27
|
-
/** 数据更新时的钩子 */
|
|
28
|
-
onUpdate?: (key: string, data: unknown) => void;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
interface DataManager {
|
|
32
|
-
/** 创建数据实例 */
|
|
33
|
-
createData<T = unknown>(url: string, options?: RequestOptions): DataInstance<T>;
|
|
34
|
-
/** 执行简单 GET 请求 */
|
|
35
|
-
get<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
36
|
-
/** 执行简单 POST 请求 */
|
|
37
|
-
post<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
38
|
-
/** 执行简单 PUT 请求 */
|
|
39
|
-
put<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
40
|
-
/** 执行简单 DELETE 请求 */
|
|
41
|
-
delete<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
42
|
-
/** 添加请求拦截器 */
|
|
43
|
-
addRequestInterceptor(interceptor: (config: RequestOptions) => RequestOptions | Promise<RequestOptions>): void;
|
|
44
|
-
/** 添加响应拦截器 */
|
|
45
|
-
addResponseInterceptor(interceptor: <T = unknown>(response: T) => T | Promise<T>): void;
|
|
46
|
-
/** 添加错误拦截器 */
|
|
47
|
-
addErrorInterceptor(interceptor: (error: FetchError) => FetchError | Promise<FetchError>): void;
|
|
48
|
-
/** 获取缓存存储 */
|
|
49
|
-
getCacheStorage(): CacheStorage;
|
|
50
|
-
/** 清空缓存 */
|
|
51
|
-
clearCache(): void;
|
|
52
|
-
/** 清除特定缓存 */
|
|
53
|
-
invalidateCache(key: string): void;
|
|
54
|
-
/** 预取数据 */
|
|
55
|
-
prefetch<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
56
|
-
/** 获取当前的请求队列 */
|
|
57
|
-
getPendingRequests(): Set<string>;
|
|
58
|
-
/** 取消所有请求 */
|
|
59
|
-
cancelAllRequests(): void;
|
|
60
|
-
}
|
|
61
|
-
interface DataInstance<T = unknown> extends FetchInstance<T> {
|
|
62
|
-
/** 乐观更新数据 */
|
|
63
|
-
optimisticUpdate(data: T): void;
|
|
64
|
-
/** 回滚乐观更新 */
|
|
65
|
-
rollbackOptimistic(): void;
|
|
66
|
-
/** 获取预取状态 */
|
|
67
|
-
isPrefetching: boolean;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* TTL (Time To Live) 缓存策略实现
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
declare class TTLCache implements CacheStorage {
|
|
75
|
-
private cache;
|
|
76
|
-
get<T = unknown>(key: string): CacheEntry<T> | null;
|
|
77
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
78
|
-
delete(key: string): void;
|
|
79
|
-
clear(): void;
|
|
80
|
-
has(key: string): boolean;
|
|
81
|
-
/**
|
|
82
|
-
* 获取缓存大小
|
|
83
|
-
*/
|
|
84
|
-
size(): number;
|
|
85
|
-
/**
|
|
86
|
-
* 获取所有缓存键
|
|
87
|
-
*/
|
|
88
|
-
keys(): string[];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* LRU (Least Recently Used) 缓存策略实现
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
interface LRUEntry<T = unknown> extends CacheEntry<T> {
|
|
96
|
-
lastAccessed: number;
|
|
97
|
-
}
|
|
98
|
-
declare class LRUCache implements CacheStorage {
|
|
99
|
-
private cache;
|
|
100
|
-
private readonly maxSize;
|
|
101
|
-
constructor(maxSize?: number);
|
|
102
|
-
get<T = unknown>(key: string): LRUEntry<T> | null;
|
|
103
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
104
|
-
delete(key: string): void;
|
|
105
|
-
clear(): void;
|
|
106
|
-
has(key: string): boolean;
|
|
107
|
-
private evict;
|
|
108
|
-
/**
|
|
109
|
-
* 获取缓存大小
|
|
110
|
-
*/
|
|
111
|
-
size(): number;
|
|
112
|
-
/**
|
|
113
|
-
* 获取最大缓存大小
|
|
114
|
-
*/
|
|
115
|
-
getMaxSize(): number;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 创建数据实例
|
|
120
|
-
*/
|
|
121
|
-
declare function createData<T = unknown>(url: string, options?: RequestOptions, globalOptions?: DataPluginOptions): DataInstance<T>;
|
|
122
|
-
/**
|
|
123
|
-
* 创建数据管理器
|
|
124
|
-
*/
|
|
125
|
-
declare function createDataManager(globalOptions?: DataPluginOptions): DataManager;
|
|
126
|
-
/**
|
|
127
|
-
* 定义插件
|
|
128
|
-
*/
|
|
129
|
-
declare const pluginData: _lytjs_core.PluginDefinition<unknown>;
|
|
130
|
-
|
|
131
|
-
export { type DataInstance, type DataManager, type DataPluginOptions, LRUCache, type RequestOptions, TTLCache, createData, createDataManager, pluginData as default };
|
package/dist/index.d.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import * as _lytjs_core from '@lytjs/core';
|
|
2
|
-
import { FetchInstance, RequestOptions as RequestOptions$1, FetchError, CacheStorage, FetchPluginOptions, CacheEntry } from '@lytjs/plugin-data-fetch';
|
|
3
|
-
export { CacheEntry, CacheStorage, FetchError, generateCacheKey } from '@lytjs/plugin-data-fetch';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @lytjs/plugin-data - 类型定义
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
interface RequestOptions extends RequestOptions$1 {
|
|
10
|
-
/** 是否启用请求去重 */
|
|
11
|
-
dedupe?: boolean;
|
|
12
|
-
/** 乐观更新数据 */
|
|
13
|
-
optimisticData?: unknown;
|
|
14
|
-
/** 预取相关配置 */
|
|
15
|
-
prefetch?: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface DataPluginOptions extends FetchPluginOptions {
|
|
19
|
-
/** 默认去重设置 */
|
|
20
|
-
defaultDedupe?: boolean;
|
|
21
|
-
/** 离线模式 */
|
|
22
|
-
offlineMode?: boolean;
|
|
23
|
-
/** 与 store 的集成配置 */
|
|
24
|
-
storeIntegration?: {
|
|
25
|
-
/** 数据同步时的钩子 */
|
|
26
|
-
onSync?: (key: string, data: unknown) => void;
|
|
27
|
-
/** 数据更新时的钩子 */
|
|
28
|
-
onUpdate?: (key: string, data: unknown) => void;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
interface DataManager {
|
|
32
|
-
/** 创建数据实例 */
|
|
33
|
-
createData<T = unknown>(url: string, options?: RequestOptions): DataInstance<T>;
|
|
34
|
-
/** 执行简单 GET 请求 */
|
|
35
|
-
get<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
36
|
-
/** 执行简单 POST 请求 */
|
|
37
|
-
post<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
38
|
-
/** 执行简单 PUT 请求 */
|
|
39
|
-
put<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
40
|
-
/** 执行简单 DELETE 请求 */
|
|
41
|
-
delete<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
42
|
-
/** 添加请求拦截器 */
|
|
43
|
-
addRequestInterceptor(interceptor: (config: RequestOptions) => RequestOptions | Promise<RequestOptions>): void;
|
|
44
|
-
/** 添加响应拦截器 */
|
|
45
|
-
addResponseInterceptor(interceptor: <T = unknown>(response: T) => T | Promise<T>): void;
|
|
46
|
-
/** 添加错误拦截器 */
|
|
47
|
-
addErrorInterceptor(interceptor: (error: FetchError) => FetchError | Promise<FetchError>): void;
|
|
48
|
-
/** 获取缓存存储 */
|
|
49
|
-
getCacheStorage(): CacheStorage;
|
|
50
|
-
/** 清空缓存 */
|
|
51
|
-
clearCache(): void;
|
|
52
|
-
/** 清除特定缓存 */
|
|
53
|
-
invalidateCache(key: string): void;
|
|
54
|
-
/** 预取数据 */
|
|
55
|
-
prefetch<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
|
|
56
|
-
/** 获取当前的请求队列 */
|
|
57
|
-
getPendingRequests(): Set<string>;
|
|
58
|
-
/** 取消所有请求 */
|
|
59
|
-
cancelAllRequests(): void;
|
|
60
|
-
}
|
|
61
|
-
interface DataInstance<T = unknown> extends FetchInstance<T> {
|
|
62
|
-
/** 乐观更新数据 */
|
|
63
|
-
optimisticUpdate(data: T): void;
|
|
64
|
-
/** 回滚乐观更新 */
|
|
65
|
-
rollbackOptimistic(): void;
|
|
66
|
-
/** 获取预取状态 */
|
|
67
|
-
isPrefetching: boolean;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* TTL (Time To Live) 缓存策略实现
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
declare class TTLCache implements CacheStorage {
|
|
75
|
-
private cache;
|
|
76
|
-
get<T = unknown>(key: string): CacheEntry<T> | null;
|
|
77
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
78
|
-
delete(key: string): void;
|
|
79
|
-
clear(): void;
|
|
80
|
-
has(key: string): boolean;
|
|
81
|
-
/**
|
|
82
|
-
* 获取缓存大小
|
|
83
|
-
*/
|
|
84
|
-
size(): number;
|
|
85
|
-
/**
|
|
86
|
-
* 获取所有缓存键
|
|
87
|
-
*/
|
|
88
|
-
keys(): string[];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* LRU (Least Recently Used) 缓存策略实现
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
interface LRUEntry<T = unknown> extends CacheEntry<T> {
|
|
96
|
-
lastAccessed: number;
|
|
97
|
-
}
|
|
98
|
-
declare class LRUCache implements CacheStorage {
|
|
99
|
-
private cache;
|
|
100
|
-
private readonly maxSize;
|
|
101
|
-
constructor(maxSize?: number);
|
|
102
|
-
get<T = unknown>(key: string): LRUEntry<T> | null;
|
|
103
|
-
set<T = unknown>(key: string, value: CacheEntry<T>): void;
|
|
104
|
-
delete(key: string): void;
|
|
105
|
-
clear(): void;
|
|
106
|
-
has(key: string): boolean;
|
|
107
|
-
private evict;
|
|
108
|
-
/**
|
|
109
|
-
* 获取缓存大小
|
|
110
|
-
*/
|
|
111
|
-
size(): number;
|
|
112
|
-
/**
|
|
113
|
-
* 获取最大缓存大小
|
|
114
|
-
*/
|
|
115
|
-
getMaxSize(): number;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 创建数据实例
|
|
120
|
-
*/
|
|
121
|
-
declare function createData<T = unknown>(url: string, options?: RequestOptions, globalOptions?: DataPluginOptions): DataInstance<T>;
|
|
122
|
-
/**
|
|
123
|
-
* 创建数据管理器
|
|
124
|
-
*/
|
|
125
|
-
declare function createDataManager(globalOptions?: DataPluginOptions): DataManager;
|
|
126
|
-
/**
|
|
127
|
-
* 定义插件
|
|
128
|
-
*/
|
|
129
|
-
declare const pluginData: _lytjs_core.PluginDefinition<unknown>;
|
|
130
|
-
|
|
131
|
-
export { type DataInstance, type DataManager, type DataPluginOptions, LRUCache, type RequestOptions, TTLCache, createData, createDataManager, pluginData as default };
|