Use opensubsonic types in module and code

This commit is contained in:
Neshura 2024-04-29 07:12:43 +02:00
parent b0ec1c7d56
commit 7488b7cfeb
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 54 additions and 35 deletions

View file

@ -10,17 +10,28 @@ export module OpenSubsonic {
const apiVer = "1.16.1"; // Version supported by Navidrome. Variable for easier updating
const clientName = "Lydstyrke";
export async function get(path: string, parameters: {parameter: string, value: string}[] = []) {
export async function get(path: string, parameters: Array<Parameter> = []) {
const apiPath = getApiUrl(path, parameters);
const res = (await fetch(apiPath));
if (res.ok) {
switch (res.headers.get("content-type")) {
const contentType =res.headers.get("content-type");
switch (contentType) {
case("application/json"): {
return (await res.json())["subsonic-response"];
}
case("audio/mp4"): {
return res;
}
case("image/jpeg"): {
return res;
}
case("image/png"): {
return res;
}
default: {
console.warn(`Content Type: '${contentType}' is not supported`)
}
}
}
else {
@ -28,10 +39,10 @@ export module OpenSubsonic {
}
}
export function getApiUrl(path: string, parameters: {parameter: string, value: string}[] = []) {
export function getApiUrl(path: string, parameters: Array<Parameter> = []) {
let apiPath = generateBasePath(path);
parameters.forEach(({parameter, value}) => {
apiPath = apiPath + `&${parameter}=${value}`;
parameters.forEach((parameter) => {
apiPath = apiPath + `&${parameter.key}=${parameter.value}`;
})
return apiPath;

View file

@ -1,7 +1,10 @@
<svelte:options runes={true} />
<script lang="ts">
import {OpenSubsonic} from "$lib/opensubsonic";
import {
type Parameter, type Song, type NowPlayingResponse, type GetPlayQueueResponse,
OpenSubsonic
} from "$lib/opensubsonic";
import {onMount} from "svelte";
import QueueFrame from "$lib/components/custom/QueueFrame.svelte";
import {Button} from "$lib/components/ui/button";
@ -31,18 +34,18 @@
let progress = $state(0);
let duration = $state(0);
let queue: Array<unknown> = $state([]);
let queue: Array<Song> = $state([]);
async function fetchQueue() {
const data = await OpenSubsonic.get("getPlayQueue");
if (data) {
const data: GetPlayQueueResponse = await OpenSubsonic.get("getPlayQueue");
if (data && data.playQueue.entry) {
queue = [];
queue = queue.concat(data.playQueue.entry);
}
}
async function fetchNowPlaying() {
const data = await OpenSubsonic.get("getNowPlaying");
const data: NowPlayingResponse = await OpenSubsonic.get("getNowPlaying");
let foundInNowPlaying = false;
if (data && data.nowPlaying.entry) {
data.nowPlaying.entry.forEach((entry) => {
@ -59,18 +62,23 @@
}
async function saveQueue() {
let songs = [];
let songs: Array<Parameter> = [];
queue.forEach((song, idx) => {
if (idx === 0) {
songs.push({parameter: "current", value: song.id})
songs.push({parameter: "id", value: song.id})
// Add Progress within current song
}
else {
songs.push({parameter: "id", value: song.id})
}
})
if (queue.length === 0) {
songs.push({key: "id", value: ""})
}
else {
queue.forEach((song, idx) => {
if (idx === 0) {
songs.push({key: "current", value: song.id})
songs.push({key: "id", value: song.id})
// Add Progress within current song
}
else {
songs.push({key: "id", value: song.id})
}
})
}
const data = await OpenSubsonic.get("savePlayQueue", songs);
if (data) {
await fetchQueue();
@ -83,22 +91,22 @@
}
}
function playSong(song: unknown, songIndex: number) {
function playSong(song: Song, songIndex: number) {
pause();
newSong(song);
currentSong.queueIndex = songIndex;
play();
}
function newSong(song: unknown) {
let parameters = [
{ parameter: "id", value: song.id },
//{ parameter: "maxBitRate", value: } // TODO
//{ parameter: "format", value: } // TODO
//{ parameter: "timeOffset", value: } // TODO? Only Video related
//{ parameter: "size", value: } // TODO? Only Video related
{ parameter: "estimateContentLength", value: "true" },
//{ parameter: "converted", value: } // TODO? Only Video related
function newSong(song: Song) {
let parameters: Array<Parameter> = [
{ key: "id", value: song.id },
//{ key: "maxBitRate", value: } // TODO
//{ key: "format", value: } // TODO
//{ key: "timeOffset", value: } // TODO? Only Video related
//{ key: "size", value: } // TODO? Only Video related
{ key: "estimateContentLength", value: "true" },
//{ key: "converted", value: } // TODO? Only Video related
];
let url = OpenSubsonic.getApiUrl("stream", parameters);
currentSong.source = new Audio(url); // Assign new URL
@ -113,10 +121,10 @@
currentSong.source.volume = volume;
isPaused = currentSong.source.paused;
let time: number = Date.now();
let parameters = [
{ parameter: "id", value: song.id },
{ parameter: "time", value: time},
{ parameter: "submission", value: false}
let parameters: Array<Parameter> = [
{ key: "id", value: song.id },
{ key: "time", value: time},
{ key: "submission", value: false}
];
OpenSubsonic.get("scrobble", parameters);
}