Skip to content

Commit

Permalink
refactor: 请求 Navidrome 逻辑重构
Browse files Browse the repository at this point in the history
  • Loading branch information
imba97 committed Oct 1, 2024
1 parent a46a5ab commit 0e35aec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 140 deletions.
56 changes: 20 additions & 36 deletions composables/navidrome/shared.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
import axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios'
import fetchAdapter from '@haverstack/axios-fetch-adapter'
import defu from 'defu'

let axiosInstance: AxiosInstance | null = null
const runtimeConfig = useRuntimeConfig()

export const axiosInfo = {
get commonOptions() {
const runtimeConfig = useRuntimeConfig()
export const navidromeRequest = {
baseURL: runtimeConfig.env.NAVIDROME_API_URL,

return {
params: {
u: runtimeConfig.env.NAVIDROME_USERNAME,
p: runtimeConfig.env.NAVIDROME_PASSWORD,
v: '1.16.1',
c: 'my-client',
f: 'json'
}
}
commonOptions: {
u: runtimeConfig.env.NAVIDROME_USERNAME,
p: runtimeConfig.env.NAVIDROME_PASSWORD,
v: '1.16.1',
c: 'my-client',
f: 'json'
},

get instance() {
if (axiosInstance) {
return axiosInstance
}

const runtimeConfig = useRuntimeConfig()

axiosInstance = axios.create({
baseURL: runtimeConfig.env.NAVIDROME_API_URL,
adapter: fetchAdapter
async get(url: string, params: Record<string, any> = {}) {
const queryString = new URLSearchParams(defu(this.commonOptions, params)).toString()
const response = await fetch(`${this.baseURL}${url}?${queryString}`, {
method: 'GET'
})

return axiosInstance
return response.json()
},

get testEnv() {
const runtimeConfig = useRuntimeConfig()

return {
...runtimeConfig.env
}
async arrayBuffer(url: string, params: Record<string, any> = {}) {
const queryString = new URLSearchParams(defu(this.commonOptions, params)).toString()
const response = await fetch(`${this.baseURL}${url}?${queryString}`, {
method: 'GET'
})
return response.arrayBuffer()
}
}

export function mergeOptions(...options: AxiosRequestConfig[]) {
return defu(axiosInfo.commonOptions, ...options)
}
15 changes: 6 additions & 9 deletions composables/navidrome/useNavidromeCover.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { Buffer } from 'node:buffer'

import { axiosInfo, mergeOptions } from './shared'
import { navidromeRequest } from './shared'

export default async function (id: string) {
const cover = await axiosInfo.instance.post('/getCoverArt.view', null, mergeOptions({
params: {
id
},
responseType: 'arraybuffer'
}))
const cover = await navidromeRequest.arrayBuffer('/getCoverArt.view', {
id
})

if (!(cover.data instanceof Buffer)) {
if (!(cover instanceof ArrayBuffer)) {
return ''
}

return cover.data
return Buffer.from(cover)
}
28 changes: 12 additions & 16 deletions composables/navidrome/useNavidromeData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { axiosInfo, mergeOptions } from './shared'
import { navidromeRequest } from './shared'

export default async function () {
let currentMusic
Expand Down Expand Up @@ -28,13 +28,11 @@ export default async function () {
}

async function getHistory() {
const recent = await axiosInfo.instance.post('/getAlbumList.view', null, mergeOptions({
params: {
type: 'recent',
size: 1
}
}))
.then(res => _get(res.data, 'subsonic-response.albumList.album.0'))
const recent = await navidromeRequest.get('/getAlbumList.view', {
type: 'recent',
size: 1
})
.then(res => _get(res, 'subsonic-response.albumList.album.0'))

if (!recent) {
return
Expand Down Expand Up @@ -74,15 +72,13 @@ async function getHistory() {
}

async function getPlaying() {
return await axiosInfo.instance.post('/getNowPlaying.view', null, axiosInfo.commonOptions)
.then(res => _get(res.data, 'subsonic-response.nowPlaying.entry.0', null))
return await navidromeRequest.get('/getNowPlaying.view')
.then(res => _get(res, 'subsonic-response.nowPlaying.entry.0', null))
}

async function getAlbum(id: string) {
return await axiosInfo.instance.post('/getAlbum.view', null, mergeOptions({
params: {
id
}
}))
.then(res => _get(res.data, 'subsonic-response.album.song', []))
return await navidromeRequest.get('/getAlbum.view', {
id
})
.then(res => _get(res, 'subsonic-response.album.song', []))
}
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"@unocss/nuxt": "^0.63.1",
"@unocss/reset": "^0.63.1",
"@vueuse/nuxt": "^11.1.0",
"axios": "^1.7.4",
"defu": "^6.1.4",
"floating-vue": "^5.2.2",
"nuxt": "^3.12.4",
Expand All @@ -27,7 +26,6 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240925.0",
"@haverstack/axios-fetch-adapter": "^0.12.0",
"@iconify/json": "^2.2.237",
"@imba97/eslint-config": "^0.0.2",
"@types/fs-extra": "^11.0.4",
Expand Down
71 changes: 0 additions & 71 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions server/api/playing/test.ts

This file was deleted.

0 comments on commit 0e35aec

Please sign in to comment.