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

11
package-lock.json generated
View File

@@ -13,7 +13,8 @@
"@types/jsonwebtoken": "^9.0.5", "@types/jsonwebtoken": "^9.0.5",
"bcrypt-ts": "^5.0.1", "bcrypt-ts": "^5.0.1",
"better-sqlite3": "^9.3.0", "better-sqlite3": "^9.3.0",
"jsonwebtoken": "^9.0.2" "jsonwebtoken": "^9.0.2",
"zod": "^3.22.4"
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "^3.0.0", "@sveltejs/adapter-auto": "^3.0.0",
@@ -3621,6 +3622,14 @@
"engines": { "engines": {
"node": ">= 14" "node": ">= 14"
} }
},
"node_modules/zod": {
"version": "3.22.4",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.22.4.tgz",
"integrity": "sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
} }
} }
} }

View File

@@ -32,6 +32,7 @@
"@types/jsonwebtoken": "^9.0.5", "@types/jsonwebtoken": "^9.0.5",
"bcrypt-ts": "^5.0.1", "bcrypt-ts": "^5.0.1",
"better-sqlite3": "^9.3.0", "better-sqlite3": "^9.3.0",
"jsonwebtoken": "^9.0.2" "jsonwebtoken": "^9.0.2",
"zod": "^3.22.4"
} }
} }

View File

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