Skip to content

Commit d2e3ce2

Browse files
author
pompurin404
committed
fix mihomo api
1 parent cdb3b8a commit d2e3ce2

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@mihomo-party/sysproxy": "^1.0.1",
2525
"@nextui-org/react": "^2.4.6",
2626
"axios": "^1.7.2",
27+
"dayjs": "^1.11.12",
2728
"electron-updater": "^6.2.1",
2829
"framer-motion": "^11.3.19",
2930
"next-themes": "^0.3.0",

pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/core/mihomoApi.ts

+36-26
Original file line numberDiff line numberDiff line change
@@ -27,72 +27,82 @@ export const getAxios = async (force: boolean = false): Promise<AxiosInstance> =
2727

2828
export async function mihomoVersion(): Promise<IMihomoVersion> {
2929
const instance = await getAxios()
30-
return instance.get('/version').catch((e) => {
31-
return e.response.data
32-
})
30+
return (await instance.get('/version').catch(() => {
31+
return { version: '-' }
32+
})) as IMihomoVersion
3333
}
3434

3535
export const mihomoConfig = async (): Promise<IMihomoConfig> => {
3636
const instance = await getAxios()
37-
return instance.get('/configs').catch((e) => {
38-
return e.response.data
39-
})
37+
return (await instance.get('/configs').catch(() => {
38+
return {}
39+
})) as IMihomoConfig
4040
}
4141

4242
export const patchMihomoConfig = async (patch: Partial<IMihomoConfig>): Promise<void> => {
4343
const instance = await getAxios()
44-
return instance.patch('/configs', patch).catch((e) => {
44+
return (await instance.patch('/configs', patch).catch((e) => {
4545
return e.response.data
46-
})
46+
})) as Promise<void>
4747
}
4848

4949
export const mihomoConnections = async (): Promise<IMihomoConnectionsInfo> => {
5050
const instance = await getAxios()
51-
return instance.get('/connections').catch((e) => {
52-
return e.response.data
53-
})
51+
return (await instance.get('/connections').catch(() => {
52+
return { downloadTotal: 0, uploadTotal: 0, connections: [], memory: 0 }
53+
})) as IMihomoConnectionsInfo
5454
}
5555

5656
export const mihomoCloseConnection = async (id: string): Promise<void> => {
5757
const instance = await getAxios()
58-
return instance.delete(`/connections/${encodeURIComponent(id)}`).catch((e) => {
58+
return (await instance.delete(`/connections/${encodeURIComponent(id)}`).catch((e) => {
5959
return e.response.data
60-
})
60+
})) as Promise<void>
6161
}
6262

6363
export const mihomoCloseAllConnections = async (): Promise<void> => {
6464
const instance = await getAxios()
65-
return instance.delete('/connections').catch((e) => {
65+
return (await instance.delete('/connections').catch((e) => {
6666
return e.response.data
67-
})
67+
})) as Promise<void>
6868
}
6969

7070
export const mihomoRules = async (): Promise<IMihomoRulesInfo> => {
7171
const instance = await getAxios()
72-
return instance.get('/rules').catch((e) => {
73-
return e.response.data
74-
})
72+
return (await instance.get('/rules').catch(() => {
73+
return { rules: [] }
74+
})) as IMihomoRulesInfo
7575
}
7676

7777
export const mihomoProxies = async (): Promise<IMihomoProxies> => {
7878
const instance = await getAxios()
79-
return instance.get('/proxies').catch((e) => {
80-
return e.response.data
81-
})
79+
return (await instance.get('/proxies').catch(() => {
80+
return { proxies: {} }
81+
})) as IMihomoProxies
8282
}
8383

8484
export const mihomoChangeProxy = async (group: string, proxy: string): Promise<IMihomoProxy> => {
8585
const instance = await getAxios()
86-
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch((e) => {
87-
return e.response.data
88-
})
86+
return (await instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch(() => {
87+
return {
88+
alive: false,
89+
extra: {},
90+
history: [],
91+
id: '',
92+
name: '',
93+
tfo: false,
94+
type: 'Shadowsocks',
95+
udp: false,
96+
xudp: false
97+
}
98+
})) as IMihomoProxy
8999
}
90100

91101
export const mihomoProxyDelay = async (proxy: string, url?: string): Promise<IMihomoDelay> => {
92102
const appConfig = getAppConfig()
93103
const { delayTestUrl, delayTestTimeout } = appConfig
94104
const instance = await getAxios()
95-
return instance
105+
return (await instance
96106
.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
97107
params: {
98108
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
@@ -101,7 +111,7 @@ export const mihomoProxyDelay = async (proxy: string, url?: string): Promise<IMi
101111
})
102112
.catch((e) => {
103113
return e.response.data
104-
})
114+
})) as IMihomoDelay
105115
}
106116

107117
export const startMihomoTraffic = (): void => {

0 commit comments

Comments
 (0)