working on the api now

This commit is contained in:
Eclypsed
2024-01-30 12:27:37 -05:00
parent 675e2b2d68
commit fcbcf6780d
4 changed files with 53 additions and 6 deletions

View File

@@ -15,11 +15,11 @@ type UserQueryParams = {
includePassword?: boolean
}
interface DBServiceData {
export interface DBServiceData {
id: string
type: ServiceType
userId: string
url: URL
url: string
}
interface DBServiceRow {
@@ -29,7 +29,7 @@ interface DBServiceRow {
url: string
}
interface DBConnectionData {
export interface DBConnectionData {
id: string
user: User
service: DBServiceData
@@ -81,7 +81,7 @@ export class Users {
export class Services {
static getService = (id: string): DBServiceData => {
const { type, userId, url } = db.prepare('SELECT * FROM Users WHERE id = ?').get(id) as DBServiceRow
const service: DBServiceData = { id, type: type as ServiceType, userId, url: new URL(url) }
const service: DBServiceData = { id, type: type as ServiceType, userId, url }
return service
}

View File

@@ -0,0 +1,37 @@
import { Services, type DBServiceData, Connections, type DBConnectionData } from '$lib/server/users'
import type { RequestHandler } from '@sveltejs/kit'
import { z } from 'zod'
export const GET: RequestHandler = async ({ params }) => {
const userId = params.userId as string
const repsonseHeaders = new Headers({
'Content-Type': 'application/json',
})
const connections = Connections.getUserConnections(userId)
return new Response(JSON.stringify(connections), { headers: repsonseHeaders })
}
export const PATCH: RequestHandler = async ({ params, request }) => {
const userId = params.userId as string
const serviceSchema = z.object({
serviceType: z.enum(['jellyfin', 'youtube-music']),
userId: z.string(),
url: z.string(),
})
const connectionSchema = z.object({
userId: z.string(),
serviceId: z.string(),
accessToken: z.string(),
refreshToken: z.string().nullable(),
expiry: z.number().nullable(),
})
const { service, connection } = await request.json()
const serviceValidation = serviceSchema.safeParse(service)
if (!serviceValidation.success) return new Response(serviceValidation.error.message, { status: 400 })
}