response-handler.ts
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import type { RequestConfig, ResponseData } from "../../types";
import { dispatchEvent } from "../../monitoring/events";
import { IResponseConfig } from "../types";
// 重新导出事件相关类型和函数,保持向后兼容
export type { EventType, EventHandler } from "../../monitoring/events";
export { addEventListener, removeEventListener } from "../../monitoring/events";
/**
* 查找有效的状态码键
*/
const findValidCodeKey = (codeKey: string | string[], data: any): string | undefined =>
Array.isArray(codeKey) ? codeKey.find(key => key in data) : codeKey;
/**
* 检查是否需要登出
*/
const shouldLogout = (code: any, errValue: any, logoutValue: any): boolean =>
code == logoutValue || errValue == logoutValue;
/**
* 状态处理策略
*/
interface StatusHandler {
condition: (context: StatusContext) => boolean;
handle: (context: StatusContext) => ResponseData<any>;
}
interface StatusContext {
code: string;
errValue: any;
successValue: string;
logoutValue: string;
isLogoutRequired: boolean;
res: any;
responseData: ResponseData<any>;
config: RequestConfig;
dataKey: string;
}
/**
* 创建状态处理策略
*/
const createStatusHandlers = (): StatusHandler[] => [
{
condition: ({ code, successValue }) => code === successValue,
handle: ({ res, config, dataKey }) => {
dispatchEvent("success", res, config);
return res[dataKey];
}
},
{
condition: ({ code, logoutValue, isLogoutRequired }) =>
code === logoutValue || isLogoutRequired,
handle: ({ res, config, responseData, dataKey }) => {
// dispatchEvent("logout", res, config);
return { ...responseData, data: res[dataKey] };
}
},
{
condition: () => true, // 默认处理器
handle: ({ res, config, responseData, dataKey }) => {
dispatchEvent("error", res, config);
return { ...responseData, data: res[dataKey] };
}
}
];
/**
* 处理响应数据,集成用户提供的响应拦截器逻辑
*/
export function processResponse({ responseData, config}: IResponseConfig): ResponseData<any> {
// 触发响应事件
dispatchEvent("response", responseData, config);
const { data: res } = responseData;
const { returnAll, response } = config;
// 早期返回条件检查
if (!response?.code_key) {
return responseData;
}
const {
code_key,
errId = "errId",
code_success_value = "0",
data_key = "data",
errId_logout_value,
} = response;
// 查找有效的状态码键
const codeKey = findValidCodeKey(code_key, res);
if (!codeKey) {
return responseData;
}
const code = res[codeKey];
const errValue = res[errId];
const isLogoutRequired = shouldLogout(code, errValue, errId_logout_value);
if (isLogoutRequired) {
dispatchEvent("logout", res, config);
}
// 处理完整响应返回
if (returnAll) {
return { ...responseData, data: res };
}
// 使用策略模式处理不同状态
const context: StatusContext = {
code: String(code),
errValue,
successValue: String(code_success_value),
logoutValue: String(errId_logout_value),
isLogoutRequired,
res,
responseData,
config,
dataKey: data_key
};
const handlers = createStatusHandlers();
const handler = handlers.find(h => h.condition(context));
return handler?.handle(context) ?? responseData;
}