Perf: remove duplicate check of new version, refactor getLatestVersion func

This commit is contained in:
萌萌哒赫萝
2023-04-16 22:48:34 +08:00
parent 15d34ac90a
commit ef917ce26e
2 changed files with 18 additions and 21 deletions

View File

@@ -1,21 +1,23 @@
import axios from 'axios'
import { RELEASE_URL, RELEASE_URL_BACKUP } from './static'
import yaml from 'js-yaml'
import { RELEASE_URL, RELEASE_URL_BACKUP } from './static'
export const getLatestVersion = async () => {
let res: string = ''
export const getLatestVersion = async (): Promise<string> => {
try {
res = await axios.get(RELEASE_URL).then(r => {
const list = r.data as IStringKeyMap[]
const normalList = list.filter(item => !item.name.includes('beta'))
return normalList[0].name
}).catch(async () => {
const result = await axios.get(`${RELEASE_URL_BACKUP}/latest.yml`)
const r = yaml.load(result.data) as IStringKeyMap
return r.version
})
const { data } = await axios.get(RELEASE_URL)
const releases = data as IStringKeyMap[]
const normalList = releases.filter(item => !item.name.includes('beta'))
const latestRelease = normalList[0]
return latestRelease.name
} catch (err) {
console.log(err)
try {
const { data } = await axios.get(`${RELEASE_URL_BACKUP}/latest.yml`)
const r = yaml.load(data) as IStringKeyMap
return r.version
} catch (err) {
console.log(err)
return ''
}
}
return res
}