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

@@ -10,6 +10,7 @@ import {
type PageResult,
type Station,
} from '@/apis';
import { unwrapResponse } from '@/utils';
export const pageKeyboardApi = async (pageQuery: PageParams<NdmKeyboardPageQuery>, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
const { stationCode, signal } = options ?? {};
@@ -17,9 +18,7 @@ export const pageKeyboardApi = async (pageQuery: PageParams<NdmKeyboardPageQuery
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmKeyboard/page`;
const resp = await client.post<PageResult<NdmKeyboardResultVO>>(endpoint, pageQuery, { signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -29,9 +28,7 @@ export const detailKeyboardApi = async (id: string, options?: { stationCode?: St
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmKeyboard/detail`;
const resp = await client.get<NdmKeyboardResultVO>(endpoint, { params: { id }, signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -41,9 +38,7 @@ export const saveKeyboardApi = async (saveVO: NdmKeyboardSaveVO, options?: { sta
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmKeyboard`;
const resp = await client.post<NdmKeyboardResultVO>(endpoint, saveVO, { signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -53,9 +48,7 @@ export const updateKeyboardApi = async (updateVO: NdmKeyboardUpdateVO, options?:
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmKeyboard`;
const resp = await client.put<NdmKeyboardResultVO>(endpoint, updateVO, { signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -65,9 +58,7 @@ export const deleteKeyboardApi = async (ids: string[], options?: { stationCode?:
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmKeyboard`;
const resp = await client.delete<boolean>(endpoint, ids, { signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -77,9 +68,7 @@ export const exportKeyboardApi = async (pageQuery: PageParams<NdmKeyboardPageQue
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmKeyboard/defaultExportByTemplate`;
const resp = await client.post<Blob>(endpoint, pageQuery, { responseType: 'blob', retRaw: true, signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -91,8 +80,6 @@ export const importKeyboardApi = async (file: File, options?: { stationCode?: St
const formData = new FormData();
formData.append('file', file);
const resp = await client.post<ImportMsg>(endpoint, formData, { signal, upload: true });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};