From 495dc001a1f724bec414455b617d2e6b6a6f02ec Mon Sep 17 00:00:00 2001 From: yangsy Date: Wed, 17 Dec 2025 13:26:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化Result接口定义 - 新增响应数据解析逻辑 - 优化错误解析逻辑 --- src/apis/request/biz/alarm/ndm-alarm-host.ts | 29 +++----- src/apis/request/biz/all/ndm-devices.ts | 5 +- .../biz/constant/reset-monitor-shedule.ts | 4 +- src/apis/request/biz/icmp/batch-verify.ts | 5 +- src/apis/request/biz/icmp/ndm-icmp-export.ts | 13 ++-- src/apis/request/biz/icmp/verify.ts | 6 +- src/apis/request/biz/log/ndm-call-log.ts | 9 +-- .../request/biz/log/ndm-device-alarm-log.ts | 13 ++-- src/apis/request/biz/log/ndm-icmp-log.ts | 5 +- src/apis/request/biz/log/ndm-record-check.ts | 13 ++-- src/apis/request/biz/log/ndm-snmp-log.ts | 5 +- src/apis/request/biz/log/ndm-vimp-log.ts | 9 +-- .../request/biz/other/ndm-security-box.ts | 40 +++-------- .../biz/other/ndm-service-available.ts | 9 +-- src/apis/request/biz/other/ndm-switch.ts | 32 +++------ src/apis/request/biz/storage/ndm-nvr.ts | 35 +++------- .../request/biz/video/ndm-camera-ignore.ts | 21 ++---- src/apis/request/biz/video/ndm-camera.ts | 37 +++------- src/apis/request/biz/video/ndm-decoder.ts | 32 +++------ src/apis/request/biz/video/ndm-keyboard.ts | 29 +++----- .../request/biz/video/ndm-media-server.ts | 32 +++------ .../request/biz/video/ndm-video-server.ts | 32 +++------ src/apis/request/system/def-parameter.ts | 9 +-- src/types/axios.ts | 8 +-- src/utils/request-client.ts | 67 ++++++++++++++----- 25 files changed, 179 insertions(+), 320 deletions(-) diff --git a/src/apis/request/biz/alarm/ndm-alarm-host.ts b/src/apis/request/biz/alarm/ndm-alarm-host.ts index 6135fd9..194113f 100644 --- a/src/apis/request/biz/alarm/ndm-alarm-host.ts +++ b/src/apis/request/biz/alarm/ndm-alarm-host.ts @@ -10,6 +10,7 @@ import { type PageResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageAlarmHostApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -17,9 +18,7 @@ export const pageAlarmHostApi = async (pageQuery: PageParams>(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 detailAlarmHostApi = async (id: string, options?: { stationCode?: S const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmAlarmHost/detail`; const resp = await client.get(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 saveAlarmHostApi = async (saveVO: NdmAlarmHostSaveVO, options?: { s const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmAlarmHost`; const resp = await client.post(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 updateAlarmHostApi = async (updateVO: NdmAlarmHostUpdateVO, options const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmAlarmHost`; const resp = await client.put(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 deleteAlarmHostApi = async (ids: string[], options?: { stationCode? const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmAlarmHost`; const resp = await client.delete(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 exportAlarmHostApi = async (pageQuery: PageParams(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 importAlarmHostApi = async (file: File, options?: { stationCode?: S const formData = new FormData(); formData.append('file', file); const resp = await client.post(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; }; diff --git a/src/apis/request/biz/all/ndm-devices.ts b/src/apis/request/biz/all/ndm-devices.ts index 039d573..5d3d735 100644 --- a/src/apis/request/biz/all/ndm-devices.ts +++ b/src/apis/request/biz/all/ndm-devices.ts @@ -1,4 +1,5 @@ import { initStationDevices, ndmClient, userClient, type StationDevices } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const getAllDevicesApi = async (options?: { stationCode?: string; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,9 +7,7 @@ export const getAllDevicesApi = async (options?: { stationCode?: string; signal? const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDevices/all`; const resp = await client.get(endpoint, { retRaw: true, signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); // 由于各线路后端版本不一致,接口返回的设备类型可能不够完整,需要做一次合并 return { ...initStationDevices(), diff --git a/src/apis/request/biz/constant/reset-monitor-shedule.ts b/src/apis/request/biz/constant/reset-monitor-shedule.ts index c9eaeda..981b6fe 100644 --- a/src/apis/request/biz/constant/reset-monitor-shedule.ts +++ b/src/apis/request/biz/constant/reset-monitor-shedule.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const resetMonitorScheduleApi = async (options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,6 +7,5 @@ export const resetMonitorScheduleApi = async (options?: { stationCode?: Station[ const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmConstant/anyTenant/resetMonitorSchedule`; const resp = await client.get(endpoint, { signal }); - const [err] = resp; - if (err) throw err; + unwrapResponse(resp); }; diff --git a/src/apis/request/biz/icmp/batch-verify.ts b/src/apis/request/biz/icmp/batch-verify.ts index 872e545..157678b 100644 --- a/src/apis/request/biz/icmp/batch-verify.ts +++ b/src/apis/request/biz/icmp/batch-verify.ts @@ -1,11 +1,10 @@ import { userClient, type VerifyServer } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const batchVerifyApi = async (options?: { signal?: AbortSignal }) => { const { signal } = options ?? {}; const endpoint = `/api/ndm/ndmKeepAlive/batchVerify`; const resp = await userClient.post(endpoint, {}, { retRaw: true, timeout: 5000, signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/apis/request/biz/icmp/ndm-icmp-export.ts b/src/apis/request/biz/icmp/ndm-icmp-export.ts index 51e44c7..aaa3211 100644 --- a/src/apis/request/biz/icmp/ndm-icmp-export.ts +++ b/src/apis/request/biz/icmp/ndm-icmp-export.ts @@ -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(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; }; diff --git a/src/apis/request/biz/icmp/verify.ts b/src/apis/request/biz/icmp/verify.ts index f897a46..30aa3d1 100644 --- a/src/apis/request/biz/icmp/verify.ts +++ b/src/apis/request/biz/icmp/verify.ts @@ -1,11 +1,11 @@ import { ndmClient, userClient, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const verifyApi = async (options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; const client = stationCode ? ndmClient : userClient; const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmKeepAlive/verify`; - const resp = await client.post(endpoint, {}, { timeout: 5000, signal }); - const [err] = resp; - if (err) throw err; + const resp = await client.post(endpoint, {}, { retRaw: true, timeout: 5000, signal }); + unwrapResponse(resp); }; diff --git a/src/apis/request/biz/log/ndm-call-log.ts b/src/apis/request/biz/log/ndm-call-log.ts index ed24f05..c62d1e4 100644 --- a/src/apis/request/biz/log/ndm-call-log.ts +++ b/src/apis/request/biz/log/ndm-call-log.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type NdmCallLogPageQuery, type NdmCallLogResultVO, type PageParams, type PageResult, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageCallLogApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,9 +7,7 @@ export const pageCallLogApi = async (pageQuery: PageParams, const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCallLog/page`; const resp = await client.post>(endpoint, pageQuery, { 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 exportCallLogApi = async (pageQuery: PageParams(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; }; diff --git a/src/apis/request/biz/log/ndm-device-alarm-log.ts b/src/apis/request/biz/log/ndm-device-alarm-log.ts index 129d726..80db272 100644 --- a/src/apis/request/biz/log/ndm-device-alarm-log.ts +++ b/src/apis/request/biz/log/ndm-device-alarm-log.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type NdmDeviceAlarmLogPageQuery, type NdmDeviceAlarmLogResultVO, type NdmDeviceAlarmLogUpdateVO, type PageParams, type PageResult, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageDeviceAlarmLogApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,9 +7,7 @@ export const pageDeviceAlarmLogApi = async (pageQuery: PageParams>(endpoint, pageQuery, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -18,9 +17,7 @@ export const updateDeviceAlarmLogApi = async (updateVO: NdmDeviceAlarmLogUpdateV const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDeviceAlarmLog`; const resp = await client.put(endpoint, updateVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -30,8 +27,6 @@ export const exportDeviceAlarmLogApi = async (pageQuery: PageParams(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; }; diff --git a/src/apis/request/biz/log/ndm-icmp-log.ts b/src/apis/request/biz/log/ndm-icmp-log.ts index 47ab04e..853602e 100644 --- a/src/apis/request/biz/log/ndm-icmp-log.ts +++ b/src/apis/request/biz/log/ndm-icmp-log.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type NdmIcmpLogPageQuery, type NdmIcmpLogResultVO, type PageParams, type PageResult, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageIcmpLogApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,8 +7,6 @@ export const pageIcmpLogApi = async (pageQuery: PageParams, const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmIcmpLog/page`; const resp = await client.post>(endpoint, pageQuery, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/apis/request/biz/log/ndm-record-check.ts b/src/apis/request/biz/log/ndm-record-check.ts index de7ea8e..b2cf6c6 100644 --- a/src/apis/request/biz/log/ndm-record-check.ts +++ b/src/apis/request/biz/log/ndm-record-check.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type ClientChannel, type NdmNvrResultVO, type NdmRecordCheck } from '@/apis'; +import { unwrapResponse } from '@/utils'; import dayjs from 'dayjs'; export const getChannelListApi = async (ndmNvr: NdmNvrResultVO, options?: { stationCode?: string; signal?: AbortSignal }) => { @@ -7,9 +8,7 @@ export const getChannelListApi = async (ndmNvr: NdmNvrResultVO, options?: { stat const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmRecordCheck/getChannelList`; const resp = await client.post(endpoint, { code: ndmNvr.gbCode, time: '' }, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -24,9 +23,7 @@ export const getRecordCheckApi = async (ndmNvr: NdmNvrResultVO, lastDays: number const end = endDateTime.format('YYYY-MM-DD'); const parentId = ndmNvr.gbCode; const resp = await client.post(endpoint, { start, end, parentId, gbCodeList }, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -36,9 +33,7 @@ export const reloadRecordCheckApi = async (channel: ClientChannel, dayOffset: nu const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmRecordCheck/reloadRecordCheckByGbId`; const resp = await client.post(endpoint, { ...channel, dayOffset }, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/apis/request/biz/log/ndm-snmp-log.ts b/src/apis/request/biz/log/ndm-snmp-log.ts index 78c7cb6..f943aa7 100644 --- a/src/apis/request/biz/log/ndm-snmp-log.ts +++ b/src/apis/request/biz/log/ndm-snmp-log.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type NdmSnmpLogPageQuery, type NdmSnmpLogResultVO, type PageParams, type PageResult, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageSnmpLogApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,8 +7,6 @@ export const pageSnmpLogApi = async (pageQuery: PageParams, const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmSnmpLog/page`; const resp = await client.post>(endpoint, pageQuery, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/apis/request/biz/log/ndm-vimp-log.ts b/src/apis/request/biz/log/ndm-vimp-log.ts index 016f781..d291ae9 100644 --- a/src/apis/request/biz/log/ndm-vimp-log.ts +++ b/src/apis/request/biz/log/ndm-vimp-log.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type NdmVimpLogPageQuery, type NdmVimpLogResultVO, type PageParams, type PageResult, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageVimpLogApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,9 +7,7 @@ export const pageVimpLogApi = async (pageQuery: PageParams, const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmVimpLog/page`; const resp = await client.post>(endpoint, pageQuery, { 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 exportVimpLogApi = async (pageQuery: PageParams(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; }; diff --git a/src/apis/request/biz/other/ndm-security-box.ts b/src/apis/request/biz/other/ndm-security-box.ts index 89c2b76..896c665 100644 --- a/src/apis/request/biz/other/ndm-security-box.ts +++ b/src/apis/request/biz/other/ndm-security-box.ts @@ -10,6 +10,7 @@ import { type PageResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageSecurityBoxApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -17,9 +18,7 @@ export const pageSecurityBoxApi = async (pageQuery: PageParams>(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(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(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(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(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(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(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(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(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(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; }; diff --git a/src/apis/request/biz/other/ndm-service-available.ts b/src/apis/request/biz/other/ndm-service-available.ts index 75191d1..4b5d33c 100644 --- a/src/apis/request/biz/other/ndm-service-available.ts +++ b/src/apis/request/biz/other/ndm-service-available.ts @@ -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(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(endpoint, { signal }); - const [err, data] = resp; - if (err) throw err; - if (data === null) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/apis/request/biz/other/ndm-switch.ts b/src/apis/request/biz/other/ndm-switch.ts index dc01d5e..0ea79ba 100644 --- a/src/apis/request/biz/other/ndm-switch.ts +++ b/src/apis/request/biz/other/ndm-switch.ts @@ -10,6 +10,7 @@ import { type PageResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageSwitchApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -17,9 +18,7 @@ export const pageSwitchApi = async (pageQuery: PageParams, o const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmSwitch/page`; const resp = await client.post>(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(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(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(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(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, const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmSwitch/defaultExportByTemplate`; const resp = await client.post(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(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(endpoint, ids, { signal }); - const [err] = resp; - if (err) throw err; + unwrapResponse(resp); }; diff --git a/src/apis/request/biz/storage/ndm-nvr.ts b/src/apis/request/biz/storage/ndm-nvr.ts index ea68e2b..e7b36f2 100644 --- a/src/apis/request/biz/storage/ndm-nvr.ts +++ b/src/apis/request/biz/storage/ndm-nvr.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type ImportMsg, type NdmNvrPageQuery, type NdmNvrResultVO, type NdmNvrSaveVO, type NdmNvrUpdateVO, type PageParams, type PageResult, type Station } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageNvrPageApi = async (pageQuery: PageParams, options?: { stationCode: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,9 +7,7 @@ export const pageNvrPageApi = async (pageQuery: PageParams, opt const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr/page`; const resp = await client.post>(endpoint, pageQuery, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -18,9 +17,7 @@ export const detailNvrApi = async (id: string, options?: { stationCode?: Station const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr/detail`; const resp = await client.get(endpoint, { params: { id }, signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -30,9 +27,7 @@ export const saveNvrApi = async (saveVO: NdmNvrSaveVO, options?: { stationCode?: const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr`; const resp = await client.post(endpoint, saveVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -42,9 +37,7 @@ export const updateNvrApi = async (updateVO: NdmNvrUpdateVO, options?: { station const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr`; const resp = await client.put(endpoint, updateVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -54,9 +47,7 @@ export const deleteNvrApi = async (ids: string[], options?: { stationCode?: Stat const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr`; const resp = await client.delete(endpoint, ids, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -66,9 +57,7 @@ export const exportNvrApi = async (pageQuery: PageParams, optio const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr/defaultExportByTemplate`; const resp = await client.post(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; }; @@ -80,9 +69,7 @@ export const importNvrApi = async (file: File, options?: { stationCode?: Station const formData = new FormData(); formData.append('file', file); const resp = await client.post(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; }; @@ -92,8 +79,7 @@ export const probeNvrApi = async (ids: string[], options?: { stationCode?: Stati const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr/probeByIds`; const resp = await client.post(endpoint, ids, { signal }); - const [err] = resp; - if (err) throw err; + unwrapResponse(resp); }; export const syncNvrChannelsApi = async (options?: { stationCode?: string; signal?: AbortSignal }) => { @@ -102,6 +88,5 @@ export const syncNvrChannelsApi = async (options?: { stationCode?: string; signa const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmNvr/syncNvrChannels`; const resp = await client.get(endpoint, { signal }); - const [err] = resp; - if (err) throw err; + unwrapResponse(resp); }; diff --git a/src/apis/request/biz/video/ndm-camera-ignore.ts b/src/apis/request/biz/video/ndm-camera-ignore.ts index 49e956e..719284f 100644 --- a/src/apis/request/biz/video/ndm-camera-ignore.ts +++ b/src/apis/request/biz/video/ndm-camera-ignore.ts @@ -1,4 +1,5 @@ import { ndmClient, userClient, type NdmCameraIgnorePageQuery, type NdmCameraIgnoreResultVO, type NdmCameraIgnoreSaveVO, type NdmCameraIgnoreUpdateVO, type PageParams, type PageResult } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageCameraIgnoreApi = async (pageQuery: PageParams, options?: { stationCode?: string; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -6,9 +7,7 @@ export const pageCameraIgnoreApi = async (pageQuery: PageParams>(endpoint, pageQuery, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -18,9 +17,7 @@ export const detailCameraIgnoreApi = async (id: string, options?: { stationCode? const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCameraIgnore/detail`; const resp = await client.get(endpoint, { params: { id }, signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -30,9 +27,7 @@ export const saveCameraIgnoreApi = async (saveVO: NdmCameraIgnoreSaveVO, options const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCameraIgnore`; const resp = await client.post(endpoint, saveVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -42,9 +37,7 @@ export const updateCameraIgnoreApi = async (updateVO: NdmCameraIgnoreUpdateVO, o const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCameraIgnore`; const resp = await client.put(endpoint, updateVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -54,8 +47,6 @@ export const deleteCameraIgnoreApi = async (ids: string[], options?: { stationCo const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCameraIgnore`; const resp = await client.delete(endpoint, ids, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/apis/request/biz/video/ndm-camera.ts b/src/apis/request/biz/video/ndm-camera.ts index 96bb10b..926786f 100644 --- a/src/apis/request/biz/video/ndm-camera.ts +++ b/src/apis/request/biz/video/ndm-camera.ts @@ -11,6 +11,7 @@ import { type SnapResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageCameraApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -18,9 +19,7 @@ export const pageCameraApi = async (pageQuery: PageParams, o const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCamera/page`; const resp = await client.post>(endpoint, pageQuery, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -30,9 +29,7 @@ export const detailCameraApi = async (id: string, options?: { stationCode?: Stat const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCamera/detail`; const resp = await client.get(endpoint, { params: { id }, signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -42,9 +39,7 @@ export const saveCameraApi = async (saveVO: NdmCameraSaveVO, options?: { station const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCamera`; const resp = await client.post(endpoint, saveVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -54,9 +49,7 @@ export const updateCameraApi = async (updateVO: NdmCameraUpdateVO, options?: { s const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCamera`; const resp = await client.put(endpoint, updateVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -66,9 +59,7 @@ export const deleteCameraApi = async (ids: string[], options?: { stationCode?: S const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCamera`; const resp = await client.delete(endpoint, ids, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -78,9 +69,7 @@ export const exportCameraApi = async (pageQuery: PageParams, const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCamera/defaultExportByTemplate`; const resp = await client.post(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; }; @@ -92,9 +81,7 @@ export const importCameraApi = async (file: File, options?: { stationCode?: Stat const formData = new FormData(); formData.append('file', file); const resp = await client.post(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; }; @@ -102,9 +89,7 @@ export const getCameraSnapApi = async (deviceId: string, options?: { signal?: Ab const { signal } = options ?? {}; const endpoint = `/api/ndm/ndmCamera/getSnapByDeviceId`; const resp = await ndmClient.get(endpoint, { params: { deviceId }, signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -114,8 +99,6 @@ export const syncCameraApi = async (options?: { stationCode?: string; signal?: A const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmCamera/syncCamera`; const resp = await client.get(endpoint, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/apis/request/biz/video/ndm-decoder.ts b/src/apis/request/biz/video/ndm-decoder.ts index 060f47f..dfdab55 100644 --- a/src/apis/request/biz/video/ndm-decoder.ts +++ b/src/apis/request/biz/video/ndm-decoder.ts @@ -10,6 +10,7 @@ import { type PageResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageDecoderApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -17,9 +18,7 @@ export const pageDecoderApi = async (pageQuery: PageParams, const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDecoder/page`; const resp = await client.post>(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 detailDecoderApi = async (id: string, options?: { stationCode?: Sta const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDecoder/detail`; const resp = await client.get(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 saveDecoderApi = async (saveVO: NdmDecoderSaveVO, options?: { stati const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDecoder`; const resp = await client.post(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 updateDecoderApi = async (updateVO: NdmDecoderUpdateVO, options?: { const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDecoder`; const resp = await client.put(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 deleteDecoderApi = async (ids: string[], options?: { stationCode?: const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDecoder`; const resp = await client.delete(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 exportDecoderApi = async (pageQuery: PageParams(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 importDecoderApi = async (file: File, options?: { stationCode?: Sta const formData = new FormData(); formData.append('file', file); const resp = await client.post(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 probeDecoderApi = async (ids: string[], options?: { stationCode?: S const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmDecoder/probeByIds`; const resp = await client.post(endpoint, ids, { signal }); - const [err] = resp; - if (err) throw err; + unwrapResponse(resp); }; diff --git a/src/apis/request/biz/video/ndm-keyboard.ts b/src/apis/request/biz/video/ndm-keyboard.ts index 62978da..9599f75 100644 --- a/src/apis/request/biz/video/ndm-keyboard.ts +++ b/src/apis/request/biz/video/ndm-keyboard.ts @@ -10,6 +10,7 @@ import { type PageResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageKeyboardApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -17,9 +18,7 @@ export const pageKeyboardApi = async (pageQuery: PageParams>(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(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(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(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(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(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(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; }; diff --git a/src/apis/request/biz/video/ndm-media-server.ts b/src/apis/request/biz/video/ndm-media-server.ts index 09f5a37..7329589 100644 --- a/src/apis/request/biz/video/ndm-media-server.ts +++ b/src/apis/request/biz/video/ndm-media-server.ts @@ -10,6 +10,7 @@ import { type PageResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const postNdmMediaServerPage = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -17,9 +18,7 @@ export const postNdmMediaServerPage = async (pageQuery: PageParams>(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 detailMediaServerApi = async (id: string, options?: { stationCode?: const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmMediaServer/detail`; const resp = await client.get(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 saveMediaServerApi = async (saveVO: NdmMediaServerSaveVO, options?: const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmMediaServer`; const resp = await client.post(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 updateMediaServerApi = async (updateVO: NdmMediaServerUpdateVO, opt const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmMediaServer`; const resp = await client.put(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 deleteMediaServerApi = async (ids: string[], options?: { stationCod const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmMediaServer`; const resp = await client.delete(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 exportMediaServerApi = async (pageQuery: PageParams(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 importMediaServerApi = async (file: File, options?: { stationCode?: const formData = new FormData(); formData.append('file', file); const resp = await client.post(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 probeMediaServerApi = async (ids: string[], options?: { stationCode const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmMediaServer/probeByIds`; const resp = await client.post(endpoint, ids, { signal }); - const [err] = resp; - if (err) throw err; + unwrapResponse(resp); }; diff --git a/src/apis/request/biz/video/ndm-video-server.ts b/src/apis/request/biz/video/ndm-video-server.ts index 811df47..d4aa70c 100644 --- a/src/apis/request/biz/video/ndm-video-server.ts +++ b/src/apis/request/biz/video/ndm-video-server.ts @@ -10,6 +10,7 @@ import { type PageResult, type Station, } from '@/apis'; +import { unwrapResponse } from '@/utils'; export const pageVideoServerApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -17,9 +18,7 @@ export const pageVideoServerApi = async (pageQuery: PageParams>(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 detailVideoServerApi = async (id: string, options?: { stationCode?: const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmVideoServer/detail`; const resp = await client.get(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 saveVideoServerApi = async (saveVO: NdmVideoServerSaveVO, options?: const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmVideoServer`; const resp = await client.post(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 updateVideoServerApi = async (id: string, updateVO: NdmVideoServerU const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmVideoServer`; const resp = await client.put(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 deleteVideoServerApi = async (ids: string[], options?: { stationCod const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmVideoServer`; const resp = await client.delete(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 exportVideoServerApi = async (pageQuery: PageParams(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 importVideoServerApi = async (file: File, options?: { stationCode?: const formData = new FormData(); formData.append('file', file); const resp = await client.post(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 probeVideoServerApi = async (ids: string[], options?: { stationCode const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/ndm/ndmVideoServer/probeByIds`; const resp = await client.post(endpoint, ids, { signal }); - const [err] = resp; - if (err) throw err; + unwrapResponse(resp); }; diff --git a/src/apis/request/system/def-parameter.ts b/src/apis/request/system/def-parameter.ts index 9c3814e..52125e8 100644 --- a/src/apis/request/system/def-parameter.ts +++ b/src/apis/request/system/def-parameter.ts @@ -1,5 +1,6 @@ import { ndmClient, userClient, type DefParameterPageQuery, type DefParameterResultVO, type DefParameterUpdateVO, type PageParams, type PageResult, type Station } from '@/apis'; import type { Result } from '@/types'; +import { unwrapResponse } from '@/utils'; export const pageDefParameterApi = async (pageQuery: PageParams, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => { const { stationCode, signal } = options ?? {}; @@ -7,9 +8,7 @@ export const pageDefParameterApi = async (pageQuery: PageParams>(endpoint, pageQuery, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; @@ -19,8 +18,6 @@ export const updateDefParameterApi = async (updateVO: DefParameterUpdateVO, opti const prefix = stationCode ? `/${stationCode}` : ''; const endpoint = `${prefix}/api/system/defParameter`; const resp = await client.put>(endpoint, updateVO, { signal }); - const [err, data] = resp; - if (err) throw err; - if (!data) throw new Error(`${data}`); + const data = unwrapResponse(resp); return data; }; diff --git a/src/types/axios.ts b/src/types/axios.ts index 110cbb4..017a7b0 100644 --- a/src/types/axios.ts +++ b/src/types/axios.ts @@ -1,10 +1,10 @@ export interface Result { + isSuccess: boolean; code: number; data: T; - errorMsg: string; - extra: unknown; - isSuccess: boolean; msg: string; - path?: string; + errorMsg: string; timestamp: string; + path: string | null; + extra: Record | null; } diff --git a/src/utils/request-client.ts b/src/utils/request-client.ts index a788494..b8f197a 100644 --- a/src/utils/request-client.ts +++ b/src/utils/request-client.ts @@ -114,23 +114,54 @@ export class RequestClient { } } -export const parseErrorFeedback = (error: Error) => { - let errorFeedback: string; - if (!isAxiosError(error)) { - errorFeedback = error.message; - } else { - const axiosError = error as AxiosError; - const respData = axiosError.response?.data as Record | string | undefined; - if (!respData) { - const { code, message } = axiosError; - errorFeedback = `${code} : ${message}`; - } else { - if (typeof respData === 'string') { - errorFeedback = respData; - } else { - errorFeedback = `${respData['path']}: ${respData['msg'] ?? respData['errorMsg'] ?? respData['error']}`; - } - } +// 从响应中解析出数据 +export const unwrapResponse = (resp: Response) => { + const [err, data, result] = resp; + + // 如果 err 存在说明有 http 错误,那么直接抛出错误, + // 如果没有 err,那么接下来判断是否存在业务错误。 + + // 如果 result 中 isSuccess 为 false,说明有业务错误, + // 如果 result 不存在,说明 retRaw 为 true,是直接返回 data 的情况。 + + if (err) throw err; + + // 不能判断 !result 从而提前结束分支, + // 因为当 retRaw 为 true 时,result 总是为 null。 + if (result) { + const { isSuccess, path, msg, errorMsg } = result; + if (!isSuccess) throw new Error(`${path ? `${path}: ` : ''}${msg || errorMsg || '请求失败'}`); } - return errorFeedback; + + // 不做严格判空,null == undefined -> true + if (data == null) throw new Error('响应数据为空'); + + return data; +}; + +// 从错误中解析出错误信息 +export const parseErrorFeedback = (error: Error) => { + // 当发生 http 错误时,unwrapResponse 会直接抛出错误, + // 所以如果不是 AxiosError,说明是业务错误,直接返回错误信息。 + if (!isAxiosError(error)) { + return error.message; + } + + // 如果是 AxiosError,说明是 http 错误,尝试获取 result, + const result = error.response?.data; + + // 如果 result 不存在,返回 error 中原生的 code + message + if (!result) { + const { code, message } = error; + return `${code ? `code: ${code}, ` : ''}${message ? `message: ${message}` : ''}`; + } + + // 如果 result 存在,判断是否是字符串, + // 如果是字符串,说明是一些奇怪的、直接返回的错误信息,直接返回; + // 如果不是字符串,说明是 Result 类型,返回其中的 path + msg + errorMsg。 + if (typeof result === 'string') { + return result; + } + const { path, msg, errorMsg } = result as Result; + return `${path ? `${path}: ` : ''}${msg || errorMsg || '请求失败'}`; };