refactor: 优化请求封装

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

View File

@@ -10,6 +10,7 @@ import {
type PageResult,
type Station,
} from '@/apis';
import { unwrapResponse } from '@/utils';
export const pageSecurityBoxApi = async (pageQuery: PageParams<NdmSecurityBoxPageQuery>, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
const { stationCode, signal } = options ?? {};
@@ -17,9 +18,7 @@ export const pageSecurityBoxApi = async (pageQuery: PageParams<NdmSecurityBoxPag
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox/page`;
const resp = await client.post<PageResult<NdmSecurityBoxResultVO>>(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 detailSecurityBoxApi = async (id: string, options?: { stationCode?:
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox/detail`;
const resp = await client.get<NdmSecurityBoxResultVO>(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 saveSecurityBoxApi = async (saveVO: NdmSecurityBoxSaveVO, options?:
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox`;
const resp = await client.post<NdmSecurityBoxResultVO>(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 updateSecurityBoxApi = async (updateVO: NdmSecurityBoxUpdateVO, opt
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox`;
const resp = await client.put<NdmSecurityBoxResultVO>(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 deleteSecurityBoxApi = async (ids: string[], options?: { stationCod
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox`;
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 exportSecurityBoxApi = async (pageQuery: PageParams<NdmSecurityBoxP
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox/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,9 +80,7 @@ export const importSecurityBoxApi = async (file: File, options?: { stationCode?:
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;
};
@@ -103,8 +90,7 @@ export const probeSecurityBoxApi = async (ids: string[], options?: { stationCode
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox/probeByIds`;
const resp = await client.post<void>(endpoint, ids, { signal });
const [err] = resp;
if (err) throw err;
unwrapResponse(resp);
};
export const turnCitcuitStatusApi = async (ipAddress: string, circuitIndex: number, status: number, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
@@ -113,9 +99,7 @@ export const turnCitcuitStatusApi = async (ipAddress: string, circuitIndex: numb
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox/turnStatus`;
const resp = await client.post<boolean>(endpoint, { community: 'public', ipAddress, circuit: `${circuitIndex}`, status }, { signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -125,8 +109,6 @@ export const rebootSecurityBoxApi = async (ipAddress: string, options?: { statio
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSecurityBox/reboot`;
const resp = await client.post<boolean>(endpoint, { community: 'public', ipAddress }, { signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};

View File

@@ -1,4 +1,5 @@
import { ndmClient, userClient, type MediaServerStatus, type Station } from '@/apis';
import { unwrapResponse } from '@/utils';
export const isMediaServerAliveApi = async (options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
const { stationCode, signal } = options ?? {};
@@ -6,9 +7,7 @@ export const isMediaServerAliveApi = async (options?: { stationCode?: Station['c
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmServiceAvailable/mediaServer/isAlive`;
const resp = await client.get<MediaServerStatus[]>(endpoint, { signal });
const [err, data] = resp;
if (err) throw err;
if (!data) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};
@@ -18,8 +17,6 @@ export const isSipServerAliveApi = async (options?: { stationCode?: Station['cod
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmServiceAvailable/sipServer/isAlive`;
const resp = await client.get<boolean>(endpoint, { signal });
const [err, data] = resp;
if (err) throw err;
if (data === null) throw new Error(`${data}`);
const data = unwrapResponse(resp);
return data;
};

View File

@@ -10,6 +10,7 @@ import {
type PageResult,
type Station,
} from '@/apis';
import { unwrapResponse } from '@/utils';
export const pageSwitchApi = async (pageQuery: PageParams<NdmSwitchPageQuery>, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
const { stationCode, signal } = options ?? {};
@@ -17,9 +18,7 @@ export const pageSwitchApi = async (pageQuery: PageParams<NdmSwitchPageQuery>, o
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSwitch/page`;
const resp = await client.post<PageResult<NdmSwitchResultVO>>(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 detailSwitchApi = async (id: string, options?: { stationCode?: Stat
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSwitch/detail`;
const resp = await client.get<NdmSwitchResultVO>(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 saveSwitchApi = async (saveVO: NdmSwitchSaveVO, options?: { station
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSwitch`;
const resp = await client.post<NdmSwitchResultVO>(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 updateSwitchApi = async (updateVO: NdmSwitchUpdateVO, options?: { s
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSwitch`;
const resp = await client.put<NdmSwitchResultVO>(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 deleteSwitchApi = async (ids: string[], options?: { stationCode?: S
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSwitch`;
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 exportSwitchApi = async (pageQuery: PageParams<NdmSwitchPageQuery>,
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSwitch/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,9 +80,7 @@ export const importSwitchApi = async (file: File, options?: { stationCode?: Stat
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;
};
@@ -103,6 +90,5 @@ export const probeSwitchApi = async (ids: string[], options?: { stationCode?: St
const prefix = stationCode ? `/${stationCode}` : '';
const endpoint = `${prefix}/api/ndm/ndmSwitch/probeByIds`;
const resp = await client.post<void>(endpoint, ids, { signal });
const [err] = resp;
if (err) throw err;
unwrapResponse(resp);
};