1
0
mirror of https://github.com/kremalicious/blowfish.git synced 2024-11-22 01:36:58 +01:00

site: move asset filtering to api path

This commit is contained in:
Matthias Kretschmann 2020-03-11 04:08:32 +01:00
parent 35f577dcc8
commit 5978e2d085
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 27 additions and 28 deletions

View File

@ -1,7 +1,7 @@
const axios = require('axios')
require('dotenv').config
import axios from 'axios'
import 'dotenv'
module.exports = async (req, res) => {
export default async (req, res) => {
try {
const response = await axios(
'https://api.github.com/repos/kremalicious/blowfish/releases/latest',
@ -11,8 +11,30 @@ module.exports = async (req, res) => {
}
}
)
res.status(200).send(response.data)
const { assets, published_at, tag_name } = response.data
const downloads = assets
.filter(
asset =>
asset.name.includes('mac.zip') |
(asset.name.includes('.exe') &&
!asset.name.includes('.exe.blockmap')) |
asset.name.includes('.deb')
)
.map(asset => {
const isMac = asset.name.includes('mac.zip')
const isWin = asset.name.includes('.exe')
return {
name: isMac ? 'macOS' : isWin ? 'Windows' : 'Linux, deb',
url: asset.browser_download_url
}
})
res.status(200).json({ downloads, published_at, tag_name })
} catch (error) {
console.error(error.message)
res.status(500).send(error.message)
}
}

View File

@ -3,30 +3,8 @@ async function init() {
const release = await response.json()
replaceDom(release)
}
window.addEventListener('load', () => init())
function getDownloads(release) {
const downloads = release.assets
.filter(
asset =>
asset.name.includes('mac.zip') |
(asset.name.includes('.exe') && !asset.name.includes('.exe.blockmap')) |
asset.name.includes('.deb')
)
.map(asset => {
const isMac = asset.name.includes('mac.zip')
const isWin = asset.name.includes('.exe')
return {
name: isMac ? 'macOS' : isWin ? 'Windows' : 'Linux, deb',
url: asset.browser_download_url
}
})
return downloads
}
function replaceDom(release) {
if (!release) return
@ -38,14 +16,13 @@ function replaceDom(release) {
releaseTagElement.innerHTML = release.tag_name
releaseDateElement.innerHTML = `on ${dateFormatted}`
const downloads = getDownloads(release)
downloadsElement.innerHTML = ''
const isMac = navigator.userAgent.includes('Mac OS')
const isWin = navigator.userAgent.includes('Windows')
const isLinux = navigator.userAgent.includes('Linux')
const downloadAll = downloads.map(download => {
const downloadAll = release.downloads.map(download => {
const isTargetOs =
isMac & download.name.includes('mac') ||
isWin & download.name.includes('Windows') ||