@lytjs/plugin-data-fetch 6.5.0 → 6.7.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 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("Request cancelled", currentConfig, void 0, error);
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(url, { ...options, method: "GET" }, {
288
- ...globalOptions,
289
- cacheStorage,
290
- requestInterceptors,
291
- responseInterceptors,
292
- errorInterceptors
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(url, {
301
- ...options,
302
- method: "POST",
303
- body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
304
- headers: {
305
- "Content-Type": "application/json",
306
- ...options.headers
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(url, {
322
- ...options,
323
- method: "PUT",
324
- body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
325
- headers: {
326
- "Content-Type": "application/json",
327
- ...options.headers
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(url, { ...options, method: "DELETE" }, {
343
- ...globalOptions,
344
- cacheStorage,
345
- requestInterceptors,
346
- responseInterceptors,
347
- errorInterceptors
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("Request cancelled", currentConfig, void 0, error);
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(url, { ...options, method: "GET" }, {
260
- ...globalOptions,
261
- cacheStorage,
262
- requestInterceptors,
263
- responseInterceptors,
264
- errorInterceptors
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(url, {
273
- ...options,
274
- method: "POST",
275
- body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
276
- headers: {
277
- "Content-Type": "application/json",
278
- ...options.headers
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(url, {
294
- ...options,
295
- method: "PUT",
296
- body: body ? typeof body === "string" ? body : JSON.stringify(body) : void 0,
297
- headers: {
298
- "Content-Type": "application/json",
299
- ...options.headers
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(url, { ...options, method: "DELETE" }, {
315
- ...globalOptions,
316
- cacheStorage,
317
- requestInterceptors,
318
- responseInterceptors,
319
- errorInterceptors
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@lytjs/plugin-data-fetch",
3
- "version": "6.5.0",
3
+ "version": "6.7.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.5.0",
33
- "@lytjs/reactivity": "^6.5.0"
32
+ "@lytjs/core": "^6.7.0",
33
+ "@lytjs/reactivity": "^6.7.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "tsup": "^8.0.0",
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 };