-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
38 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.