70 lines
2 KiB
TypeScript
70 lines
2 KiB
TypeScript
![]() |
import { parseSong, parseSuggestion, type FetchError, type Song, type Suggestion } from "./types"
|
||
|
|
||
|
export const joinRoom = async function (roomId: string): Promise<[FetchError | null, string]> {
|
||
|
let resp = await fetch("/api/join?room=" + roomId)
|
||
|
|
||
|
if (resp.status != 200) {
|
||
|
return [{ code: 400, message: "Cannot join the room" }, ""]
|
||
|
}
|
||
|
|
||
|
return [null, "test"]
|
||
|
}
|
||
|
|
||
|
export const getSuggestions = async function (roomId: string): Promise<[FetchError | null, Suggestion[]]> {
|
||
|
let resp = await fetch("/api/room/suggestions?room=" + roomId)
|
||
|
|
||
|
if (resp.status != 200) {
|
||
|
return [{ code: 400, message: "Failed to retrieve suggestions" }, []]
|
||
|
}
|
||
|
|
||
|
let json = await resp.json()
|
||
|
let suggestions: Suggestion[] = []
|
||
|
|
||
|
json["songs"].forEach(async (i: any) => {
|
||
|
suggestions.push(await parseSuggestion(i))
|
||
|
})
|
||
|
|
||
|
suggestions = suggestions.sort((a, b) => {
|
||
|
return a.upvote - b.upvote
|
||
|
})
|
||
|
|
||
|
return [null, suggestions]
|
||
|
}
|
||
|
|
||
|
export const getQueueSongs = async function (roomId: string): Promise<[FetchError | null, Song[], number]> {
|
||
|
let resp = await fetch("/api/queue?room=" + roomId)
|
||
|
if (resp.status != 200) {
|
||
|
return [{ code: 400, message: "Failed to load queue songs" }, [], 0]
|
||
|
}
|
||
|
|
||
|
let json = await resp.json()
|
||
|
let songs: Song[] = []
|
||
|
|
||
|
json["queue"].forEach(async (i: any) => {
|
||
|
songs.push(await parseSong(i))
|
||
|
})
|
||
|
|
||
|
let playingId = json["index"]
|
||
|
|
||
|
return [null, songs, playingId]
|
||
|
}
|
||
|
|
||
|
export const triggerPlayNext = async function (roomId: string): Promise<[FetchError | null, Song[], number]> {
|
||
|
let resp = await fetch("/api/queue/next?room=" + roomId, { method: "POST" })
|
||
|
|
||
|
if (resp.status != 200) {
|
||
|
return [{ code: 400, message: "Failed to trigger next song playback" }, [], 0]
|
||
|
}
|
||
|
|
||
|
let json = await resp.json()
|
||
|
|
||
|
let songs: Song[] = []
|
||
|
|
||
|
if (json["ended"]) {
|
||
|
json["queue"].forEach(async (i: any) => {
|
||
|
songs.push(await parseSong(i))
|
||
|
})
|
||
|
}
|
||
|
return [null, songs, json["index"]]
|
||
|
}
|