@lytjs/plugin-data-fetch 6.7.0 → 6.9.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 +196 -0
- package/dist/index.d.ts +196 -0
- package/package.json +3 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lytjs/plugin-data-fetch",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.9.0",
|
|
4
4
|
"description": "LytJS official data fetch plugin with caching, retries, and interceptors",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"clean": "rm -rf dist"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@lytjs/core": "^6.
|
|
33
|
-
"@lytjs/reactivity": "^6.
|
|
32
|
+
"@lytjs/core": "^6.9.0",
|
|
33
|
+
"@lytjs/reactivity": "^6.9.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"tsup": "^8.0.0",
|