Skip to content

Commit

Permalink
fix: runtimeConfig 获取方式问题
Browse files Browse the repository at this point in the history
  • Loading branch information
imba97 committed Sep 30, 2024
1 parent 58869e9 commit 9445bea
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
44 changes: 30 additions & 14 deletions composables/navidrome/shared.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import axios, { type AxiosRequestConfig } from 'axios'
import axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios'
import defu from 'defu'

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

export const commonOptions = {
params: {
u: runtimeConfig.env.NAVIDROME_USERNAME,
p: runtimeConfig.env.NAVIDROME_PASSWORD,
v: '1.16.1',
c: 'my-client',
f: 'json'
export const axiosInfo = {
get commonOptions() {
const runtimeConfig = useRuntimeConfig()

return {
params: {
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
})

return axiosInstance
}
}

export const axiosInstance = axios.create({
baseURL: runtimeConfig.env.NAVIDROME_API_URL
})

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

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

export default async function (id: string) {
const cover = await axiosInstance.post('/getCoverArt.view', null, mergeOptions({
const cover = await axiosInfo.instance.post('/getCoverArt.view', null, mergeOptions({
params: {
id
},
Expand Down
8 changes: 4 additions & 4 deletions composables/navidrome/useNavidromeData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { axiosInstance, commonOptions, mergeOptions } from './shared'
import { axiosInfo, mergeOptions } from './shared'

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

async function getHistory() {
const recent = await axiosInstance.post('/getAlbumList.view', null, mergeOptions({
const recent = await axiosInfo.instance.post('/getAlbumList.view', null, mergeOptions({
params: {
type: 'recent',
size: 1
Expand Down Expand Up @@ -74,12 +74,12 @@ async function getHistory() {
}

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

async function getAlbum(id: string) {
return await axiosInstance.post('/getAlbum.view', null, mergeOptions({
return await axiosInfo.instance.post('/getAlbum.view', null, mergeOptions({
params: {
id
}
Expand Down

0 comments on commit 9445bea

Please sign in to comment.