Major improvements to how connections are managed, also interfaces, interfaces EVERYWHERE
This commit is contained in:
@@ -10,38 +10,35 @@ export const GET: RequestHandler = async ({ params }) => {
|
||||
return new Response(JSON.stringify(connections))
|
||||
}
|
||||
|
||||
const connectionSchema = z.object({
|
||||
serviceType: z.enum(['jellyfin', 'youtube-music']),
|
||||
serviceUserId: z.string(),
|
||||
urlOrigin: z.string().refine((val) => isValidURL(val)),
|
||||
// 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)),
|
||||
}),
|
||||
accessToken: z.string(),
|
||||
})
|
||||
export type NewConnection = z.infer<typeof connectionSchema>
|
||||
|
||||
export const POST: RequestHandler = async ({ params, request }) => {
|
||||
const userId = params.userId as string
|
||||
|
||||
const connection = await request.json()
|
||||
const connection: Connection = await request.json()
|
||||
|
||||
const connectionValidation = connectionSchema.safeParse(connection)
|
||||
const connectionValidation = newConnectionSchema.safeParse(connection)
|
||||
if (!connectionValidation.success) return new Response(connectionValidation.error.message, { status: 400 })
|
||||
|
||||
const { serviceType, serviceUserId, urlOrigin, accessToken } = connectionValidation.data
|
||||
const service: Service = {
|
||||
type: serviceType,
|
||||
userId: serviceUserId,
|
||||
urlOrigin: new URL(urlOrigin).origin,
|
||||
}
|
||||
const { service, accessToken } = connection
|
||||
const newConnection = Connections.addConnection(userId, service, accessToken)
|
||||
return new Response(JSON.stringify(newConnection))
|
||||
}
|
||||
|
||||
export const DELETE: RequestHandler = async ({ request }) => {
|
||||
const connectionId: string = await request.json()
|
||||
const requestData = await request.json()
|
||||
try {
|
||||
Connections.deleteConnection(connectionId)
|
||||
Connections.deleteConnection(requestData.connectionId)
|
||||
return new Response('Connection Deleted')
|
||||
} catch {
|
||||
} catch (error) {
|
||||
return new Response('Connection does not exist', { status: 400 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user