2024-01-31 01:17:24 -05:00
|
|
|
import { Services, Connections } from '$lib/server/users'
|
2024-01-30 12:27:37 -05:00
|
|
|
import type { RequestHandler } from '@sveltejs/kit'
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
2024-01-31 01:17:24 -05:00
|
|
|
const isValidURL = (url: string): boolean => {
|
|
|
|
|
try {
|
|
|
|
|
new URL(url)
|
|
|
|
|
return true
|
|
|
|
|
} catch {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 12:27:37 -05:00
|
|
|
export const GET: RequestHandler = async ({ params }) => {
|
|
|
|
|
const userId = params.userId as string
|
|
|
|
|
|
|
|
|
|
const connections = Connections.getUserConnections(userId)
|
2024-01-31 01:17:24 -05:00
|
|
|
return new Response(JSON.stringify(connections))
|
2024-01-30 12:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const PATCH: RequestHandler = async ({ params, request }) => {
|
|
|
|
|
const userId = params.userId as string
|
|
|
|
|
|
|
|
|
|
const connectionSchema = z.object({
|
2024-01-31 01:17:24 -05:00
|
|
|
serviceType: z.enum(['jellyfin', 'youtube-music']),
|
|
|
|
|
serviceUserId: z.string(),
|
|
|
|
|
url: z.string().refine((val) => isValidURL(val)),
|
2024-01-30 12:27:37 -05:00
|
|
|
accessToken: z.string(),
|
2024-01-31 01:17:24 -05:00
|
|
|
refreshToken: z.string().nullable().optional(),
|
|
|
|
|
expiry: z.number().nullable().optional(),
|
2024-01-30 12:27:37 -05:00
|
|
|
})
|
|
|
|
|
|
2024-01-31 01:17:24 -05:00
|
|
|
const connection = await request.json()
|
|
|
|
|
|
|
|
|
|
const connectionValidation = connectionSchema.safeParse(connection)
|
|
|
|
|
if (!connectionValidation.success) return new Response(connectionValidation.error.message, { status: 400 })
|
|
|
|
|
|
|
|
|
|
const { serviceType, serviceUserId, url, accessToken, refreshToken, expiry } = connectionValidation.data
|
|
|
|
|
const newService = Services.addService(serviceType as ServiceType, serviceUserId, new URL(url))
|
|
|
|
|
const newConnection = Connections.addConnection(userId, newService.id, accessToken, refreshToken as string | null, expiry as number | null)
|
|
|
|
|
return new Response(JSON.stringify(newConnection))
|
|
|
|
|
}
|
2024-01-30 12:27:37 -05:00
|
|
|
|
2024-01-31 01:17:24 -05:00
|
|
|
export const DELETE: RequestHandler = async ({ request }) => {
|
|
|
|
|
const connectionId: string = await request.json()
|
|
|
|
|
try {
|
|
|
|
|
Connections.deleteConnection(connectionId)
|
|
|
|
|
return new Response('Connection Deleted')
|
|
|
|
|
} catch {
|
|
|
|
|
return new Response('Connection does not exist', { status: 400 })
|
|
|
|
|
}
|
2024-01-30 12:27:37 -05:00
|
|
|
}
|