refactor: 优化请求封装

- 优化Result接口定义
- 新增响应数据解析逻辑
- 优化错误解析逻辑
This commit is contained in:
yangsy
2025-12-17 13:26:25 +08:00
parent 52ba3add3f
commit 495dc001a1
25 changed files with 179 additions and 320 deletions

View File

@@ -1,4 +1,5 @@
import { ndmClient, userClient, type IcmpEntity, type Station } from '@/apis';
import { unwrapResponse } from '@/utils';
export const exportIcmpApi = async (status?: string, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
const { stationCode, signal } = options ?? {};
@@ -13,9 +14,7 @@ export const exportIcmpApi = async (status?: string, options?: { stationCode?: S
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
signal,
});
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -36,9 +35,7 @@ export const exportIcmpByStationApi = async (stationCodes: Station['code'][], st
signal,
},
);
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -48,8 +45,6 @@ export const icmpEntityByDeviceId = async (deviceId: string, options?: { station
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmIcmpExport/icmpEntityByDeviceId`;
const resp = await client.get<IcmpEntity[]>(endpoint, { params: { deviceId }, signal, retRaw: true });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};