xxf_react 0.7.6 → 0.7.7

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.
@@ -2,14 +2,19 @@
2
2
  * 全局缓存拦截器
3
3
  * 用于 xxf_react/http 的 ApiBuilder
4
4
  */
5
- import { CacheInterceptor } from "./CacheInterceptor";
5
+ import { CacheInterceptor } from './CacheInterceptor';
6
6
  /**
7
7
  * 默认缓存拦截器
8
+ *
8
9
  * 只有满足以下条件才缓存:
9
10
  * - HTTP 状态码 = 200
10
- * - Content-Type 包含 application/json
11
- * - data 是有效对象
12
- * - ApiResponse.code = 0 或 200
11
+ * - Content-Type 包含 application/json(不区分大小写)
12
+ * - data 是有效对象或数组
13
+ * - ApiResponse.code 存在且为成功值(0 或 200
14
+ *
15
+ * 特殊处理:
16
+ * - code 字段支持 number 或 string 类型
17
+ * - 所有检查都有异常保护,异常时默认不缓存
13
18
  *
14
19
  * @example
15
20
  * ```ts
@@ -22,17 +27,26 @@ import { CacheInterceptor } from "./CacheInterceptor";
22
27
  export declare const defaultCacheInterceptor: CacheInterceptor;
23
28
  /**
24
29
  * 创建自定义缓存拦截器
25
- * 在默认检查基础上添加自定义逻辑
30
+ *
31
+ * 在默认检查基础上添加自定义逻辑。
32
+ * 默认检查通过后才会执行自定义检查。
26
33
  *
27
34
  * @param customCheck 自定义检查函数,在默认检查通过后执行
35
+ * @returns 组合后的缓存拦截器
28
36
  *
29
37
  * @example
30
38
  * ```ts
39
+ * // 额外检查:只缓存有 data 字段的响应
31
40
  * const myInterceptor = createCacheInterceptor((ctx) => {
32
- * // 额外检查:只缓存有 data 字段的响应
33
41
  * const res = ctx.data as { data?: unknown }
34
42
  * return res.data !== undefined
35
43
  * })
44
+ *
45
+ * // 额外检查:只缓存列表数据
46
+ * const listInterceptor = createCacheInterceptor((ctx) => {
47
+ * const res = ctx.data as { data?: unknown[] }
48
+ * return Array.isArray(res.data) && res.data.length > 0
49
+ * })
36
50
  * ```
37
51
  */
38
52
  export declare function createCacheInterceptor(customCheck?: (ctx: Parameters<CacheInterceptor>[0]) => boolean): CacheInterceptor;
@@ -1 +1 @@
1
- {"version":3,"file":"DefaultCacheInterceptor.d.ts","sourceRoot":"","sources":["../../../src/http/interceptor/DefaultCacheInterceptor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AAEpD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,uBAAuB,EAAE,gBAkBrC,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAClC,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,UAAU,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,GAChE,gBAAgB,CASlB"}
1
+ {"version":3,"file":"DefaultCacheInterceptor.d.ts","sourceRoot":"","sources":["../../../src/http/interceptor/DefaultCacheInterceptor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAGrD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,uBAAuB,EAAE,gBAuCrC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,sBAAsB,CAClC,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,UAAU,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,GAChE,gBAAgB,CAiBlB"}
@@ -2,13 +2,19 @@
2
2
  * 全局缓存拦截器
3
3
  * 用于 xxf_react/http 的 ApiBuilder
4
4
  */
5
+ import { isApiSuccess } from '../../models';
5
6
  /**
6
7
  * 默认缓存拦截器
8
+ *
7
9
  * 只有满足以下条件才缓存:
8
10
  * - HTTP 状态码 = 200
9
- * - Content-Type 包含 application/json
10
- * - data 是有效对象
11
- * - ApiResponse.code = 0 或 200
11
+ * - Content-Type 包含 application/json(不区分大小写)
12
+ * - data 是有效对象或数组
13
+ * - ApiResponse.code 存在且为成功值(0 或 200
14
+ *
15
+ * 特殊处理:
16
+ * - code 字段支持 number 或 string 类型
17
+ * - 所有检查都有异常保护,异常时默认不缓存
12
18
  *
13
19
  * @example
14
20
  * ```ts
@@ -19,45 +25,82 @@
19
25
  * ```
20
26
  */
21
27
  export const defaultCacheInterceptor = (ctx) => {
22
- // 检查 HTTP 状态码
23
- if (ctx.status !== 200) {
24
- return false;
25
- }
26
- // 检查 Content-Type 是否为 JSON
27
- const contentType = ctx.headers.get('Content-Type') || '';
28
- if (!contentType.includes('application/json')) {
29
- return false;
28
+ var _a, _b;
29
+ try {
30
+ // 1. 检查 HTTP 状态码
31
+ if (ctx.status !== 200) {
32
+ return false;
33
+ }
34
+ // 2. 检查 Content-Type 是否为 JSON(不区分大小写)
35
+ const contentType = (_b = (_a = ctx.headers) === null || _a === void 0 ? void 0 : _a.get('Content-Type')) !== null && _b !== void 0 ? _b : '';
36
+ if (!contentType.toLowerCase().includes('application/json')) {
37
+ return false;
38
+ }
39
+ // 3. 检查 data 是否为有效对象或数组
40
+ if (ctx.data === null || ctx.data === undefined) {
41
+ return false;
42
+ }
43
+ if (typeof ctx.data !== 'object') {
44
+ return false;
45
+ }
46
+ // 4. 检查业务码(支持 number 或 string 类型)
47
+ const res = ctx.data;
48
+ if (res.code === undefined || res.code === null) {
49
+ // 没有 code 字段,不符合 ApiResponse 规范,不缓存
50
+ return false;
51
+ }
52
+ // 将 code 转换为 number 进行判断
53
+ const codeNum = typeof res.code === 'string' ? parseInt(res.code, 10) : res.code;
54
+ if (isNaN(codeNum)) {
55
+ return false;
56
+ }
57
+ return isApiSuccess(codeNum);
30
58
  }
31
- // 检查 data 是否为有效对象
32
- if (!ctx.data || typeof ctx.data !== 'object') {
59
+ catch {
60
+ // 任何异常都不缓存,保守策略
33
61
  return false;
34
62
  }
35
- // 检查业务码:code 必须是 0 或 200
36
- const res = ctx.data;
37
- return !(res.code !== 0 && res.code !== 200);
38
63
  };
39
64
  /**
40
65
  * 创建自定义缓存拦截器
41
- * 在默认检查基础上添加自定义逻辑
66
+ *
67
+ * 在默认检查基础上添加自定义逻辑。
68
+ * 默认检查通过后才会执行自定义检查。
42
69
  *
43
70
  * @param customCheck 自定义检查函数,在默认检查通过后执行
71
+ * @returns 组合后的缓存拦截器
44
72
  *
45
73
  * @example
46
74
  * ```ts
75
+ * // 额外检查:只缓存有 data 字段的响应
47
76
  * const myInterceptor = createCacheInterceptor((ctx) => {
48
- * // 额外检查:只缓存有 data 字段的响应
49
77
  * const res = ctx.data as { data?: unknown }
50
78
  * return res.data !== undefined
51
79
  * })
80
+ *
81
+ * // 额外检查:只缓存列表数据
82
+ * const listInterceptor = createCacheInterceptor((ctx) => {
83
+ * const res = ctx.data as { data?: unknown[] }
84
+ * return Array.isArray(res.data) && res.data.length > 0
85
+ * })
52
86
  * ```
53
87
  */
54
88
  export function createCacheInterceptor(customCheck) {
55
89
  return (ctx) => {
56
- // 先执行默认检查
57
- if (!defaultCacheInterceptor(ctx)) {
90
+ try {
91
+ // 先执行默认检查
92
+ if (!defaultCacheInterceptor(ctx)) {
93
+ return false;
94
+ }
95
+ // 执行自定义检查(如果有)
96
+ if (customCheck) {
97
+ return customCheck(ctx);
98
+ }
99
+ return true;
100
+ }
101
+ catch {
102
+ // 自定义检查异常时不缓存
58
103
  return false;
59
104
  }
60
- // 执行自定义检查
61
- return !(customCheck && !customCheck(ctx));
62
105
  };
63
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xxf_react",
3
- "version": "0.7.6",
3
+ "version": "0.7.7",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",