diff --git a/package-lock.json b/package-lock.json index 31ccc1c..13ba8a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,8 @@ "@types/jsonwebtoken": "^9.0.5", "bcrypt-ts": "^5.0.1", "better-sqlite3": "^9.3.0", - "jsonwebtoken": "^9.0.2" + "jsonwebtoken": "^9.0.2", + "zod": "^3.22.4" }, "devDependencies": { "@sveltejs/adapter-auto": "^3.0.0", @@ -3621,6 +3622,14 @@ "engines": { "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" + } } } } diff --git a/package.json b/package.json index 9c25ca9..49bd70b 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "@types/jsonwebtoken": "^9.0.5", "bcrypt-ts": "^5.0.1", "better-sqlite3": "^9.3.0", - "jsonwebtoken": "^9.0.2" + "jsonwebtoken": "^9.0.2", + "zod": "^3.22.4" } } diff --git a/src/lib/server/users.ts b/src/lib/server/users.ts index 66c6649..fb703a7 100644 --- a/src/lib/server/users.ts +++ b/src/lib/server/users.ts @@ -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 } diff --git a/src/routes/api/users/[userId]/connections/+server.ts b/src/routes/api/users/[userId]/connections/+server.ts new file mode 100644 index 0000000..fa2f87f --- /dev/null +++ b/src/routes/api/users/[userId]/connections/+server.ts @@ -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 }) +}