Lazuli, now with openapi!

This commit is contained in:
Eclypsed
2024-02-18 00:01:54 -05:00
parent 85a17dcd89
commit 1608e03b97
14 changed files with 177 additions and 291 deletions

View File

@@ -1,28 +0,0 @@
import type { RequestHandler } from '@sveltejs/kit'
import { YouTubeMusic } from '$lib/service-managers/youtubeMusic'
import { Jellyfin } from '$lib/service-managers/jellyfin'
import { Connections } from '$lib/server/users'
export const GET: RequestHandler = async ({ params, url }) => {
const userId = params.userId as string
const requestedConnectionIds = url.searchParams.get('connectionIds')?.split(',')
const connectionInfo: ConnectionInfo[] = []
const userConnections: Connection[] = requestedConnectionIds ? Array.from(requestedConnectionIds, (id) => Connections.getConnection(id)) : Connections.getUserConnections(userId)
for (const connection of userConnections) {
let info: ConnectionInfo
switch (connection.service.type) {
case 'jellyfin':
info = await Jellyfin.connectionInfo(connection as Jellyfin.JFConnection)
break
case 'youtube-music':
info = await YouTubeMusic.connectionInfo(connection as YouTubeMusic.YTConnection)
break
}
connectionInfo.push(info)
}
return Response.json({ connectionInfo })
}

View File

@@ -1,48 +1,9 @@
import { Connections } from '$lib/server/users'
import { isValidURL } from '$lib/utils'
import type { RequestHandler } from '@sveltejs/kit'
import { z } from 'zod'
export const GET: RequestHandler = async ({ params }) => {
const userId = params.userId as string
const userId = params.userId!
const connections = Connections.getUserConnections(userId)
return Response.json(connections)
}
// This schema should be identical to the Connection Data Type but without the id and userId
const newConnectionSchema = z.object({
service: z.object({
type: z.enum(['jellyfin', 'youtube-music']),
userId: z.string(),
urlOrigin: z.string().refine((val) => isValidURL(val)),
}),
tokens: z.object({
accessToken: z.string(),
refreshToken: z.string().optional(),
expiry: z.number().optional(),
}),
})
export const POST: RequestHandler = async ({ params, request }) => {
const userId = params.userId as string
const connection: Connection = await request.json()
const connectionValidation = newConnectionSchema.safeParse(connection)
if (!connectionValidation.success) return new Response(connectionValidation.error.message, { status: 400 })
const { service, tokens } = connection
const newConnection = Connections.addConnection(userId, service, tokens)
return Response.json(newConnection)
}
export const DELETE: RequestHandler = async ({ request }) => {
const requestData = await request.json()
try {
Connections.deleteConnection(requestData.connectionId)
return new Response('Connection Deleted')
} catch (error) {
return new Response('Connection does not exist', { status: 400 })
}
return Response.json({ connections })
}

View File

@@ -5,15 +5,15 @@ import { Jellyfin } from '$lib/service-managers/jellyfin'
// This is temporary functionally for the sake of developing the app.
// In the future will implement more robust algorithm for offering recommendations
export const GET: RequestHandler = async ({ params, fetch }) => {
const userId = params.userId as string
const userId = params.userId!
const connectionsResponse = await fetch(`/api/users/${userId}/connections`, { headers: { apikey: SECRET_INTERNAL_API_KEY } })
const userConnections: Connection[] = await connectionsResponse.json()
const userConnections = await connectionsResponse.json()
const recommendations: Song[] = []
const recommendations: MediaItem[] = []
for (const connection of userConnections) {
const { service, tokens } = connection
for (const connection of userConnections.connections) {
const { service, accessToken } = connection as Connection
switch (service.type) {
case 'jellyfin':
@@ -26,12 +26,12 @@ export const GET: RequestHandler = async ({ params, fetch }) => {
})
const mostPlayedSongsURL = new URL(`/Users/${service.userId}/Items?${mostPlayedSongsSearchParams.toString()}`, service.urlOrigin).href
const requestHeaders = new Headers({ Authorization: `MediaBrowser Token="${tokens.accessToken}"` })
const requestHeaders = new Headers({ Authorization: `MediaBrowser Token="${accessToken}"` })
const mostPlayedResponse = await fetch(mostPlayedSongsURL, { headers: requestHeaders })
const mostPlayedData = await mostPlayedResponse.json()
mostPlayedData.Items.forEach((song: Jellyfin.Song) => recommendations.push(Jellyfin.songFactory(song, connection as Jellyfin.JFConnection)))
for (const song of mostPlayedData.Items) recommendations.push(Jellyfin.songFactory(song, connection))
break
}
}