Started playing around with typescript generic types

This commit is contained in:
Eclypsed
2024-02-19 15:03:39 -05:00
parent 00fd41ae69
commit 46e55f10c5
7 changed files with 21 additions and 69 deletions

View File

@@ -1,41 +0,0 @@
import type { RequestHandler } from '@sveltejs/kit'
import { isValidURL } from '$lib/utils'
import { z } from 'zod'
export const POST: RequestHandler = async ({ request, fetch }) => {
const jellyfinAuthSchema = z.object({
serverUrl: z.string().refine((val) => isValidURL(val)),
username: z.string(),
password: z.string(),
deviceId: z.string(),
})
const jellyfinAuthData = await request.json()
const jellyfinAuthValidation = jellyfinAuthSchema.safeParse(jellyfinAuthData)
if (!jellyfinAuthValidation.success) return new Response('Invalid data in request body', { status: 400 })
const { serverUrl, username, password, deviceId } = jellyfinAuthValidation.data
const authUrl = new URL('/Users/AuthenticateByName', serverUrl).href
try {
const authResponse = await fetch(authUrl, {
method: 'POST',
body: JSON.stringify({
Username: username,
Pw: password,
}),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Emby-Authorization': `MediaBrowser Client="Lazuli", Device="Chrome", DeviceId="${deviceId}", Version="1.0.0.0"`,
},
})
if (!authResponse.ok) return new Response('Failed to authenticate', { status: 401 })
const authData = await authResponse.json()
return Response.json({
userId: authData.User.Id,
accessToken: authData.AccessToken,
})
} catch {
return new Response('Fetch request failed', { status: 404 })
}
}