Experimenting with typescript API validation
This commit is contained in:
@@ -1,37 +1,52 @@
|
||||
import { Services, type DBServiceData, Connections, type DBConnectionData } from '$lib/server/users'
|
||||
import { Services, Connections } from '$lib/server/users'
|
||||
import type { RequestHandler } from '@sveltejs/kit'
|
||||
import { z } from 'zod'
|
||||
|
||||
const isValidURL = (url: string): boolean => {
|
||||
try {
|
||||
new URL(url)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
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 })
|
||||
return new Response(JSON.stringify(connections))
|
||||
}
|
||||
|
||||
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(),
|
||||
serviceType: z.enum(['jellyfin', 'youtube-music']),
|
||||
serviceUserId: z.string(),
|
||||
url: z.string().refine((val) => isValidURL(val)),
|
||||
accessToken: z.string(),
|
||||
refreshToken: z.string().nullable(),
|
||||
expiry: z.number().nullable(),
|
||||
refreshToken: z.string().nullable().optional(),
|
||||
expiry: z.number().nullable().optional(),
|
||||
})
|
||||
|
||||
const { service, connection } = await request.json()
|
||||
const connection = await request.json()
|
||||
|
||||
const serviceValidation = serviceSchema.safeParse(service)
|
||||
if (!serviceValidation.success) return new Response(serviceValidation.error.message, { status: 400 })
|
||||
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))
|
||||
}
|
||||
|
||||
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 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user