diff --git a/src/app.d.ts b/src/app.d.ts index 38cf362..dc4548d 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -26,12 +26,21 @@ declare global { type serviceType = 'jellyfin' | 'youtube-music' - interface BaseConnection { + type Service = Jellyfin.Service | YouTubeMusic.Service + + type Tokens = T extends Jellyfin.Service ? Jellyfin.Tokens : T extends YouTubeMusic.Service ? YouTubeMusic.Tokens : {} + + // type ServiceTokenPair = [Jellyfin.Service, Jellyfin.Tokens] | [YouTubeMusic.Service, YouTubeMusic.Tokens] + + interface BaseConnection { id: string userId: string - type: serviceType + type: T extends Jellyfin.Service ? 'jellyfin' : T extends YouTubeMusic.Service ? 'youtube-music' : serviceType + service: T extends undefined ? Service : T } + type Connection = BaseConnection & Tokens + // These Schemas should only contain general info data that is necessary for data fetching purposes. // They are NOT meant to be stores for large amounts of data, i.e. Don't include the data for every single song the Playlist type. // Big data should be fetched as needed in the app, these exist to ensure that the info necessary to fetch that data is there. @@ -85,19 +94,17 @@ declare global { // The jellyfin API will not always return the data it says it will, for example /Users/AuthenticateByName says it will // retrun the ServerName, it wont. This must be fetched from /System/Info. // So, ONLY DEFINE THE INTERFACES FOR DATA THAT IS GARUNTEED TO BE RETURNED (unless the data value itself is inherently optional) - interface Connection extends BaseConnection { - type: 'jellyfin' - jellyfinUserId: string + interface Service { + userId: string urlOrigin: string - accessToken: string - info?: T - } - - interface ConnectionInfo { username?: string serverName?: string } + interface Tokens { + accessToken: string + } + interface User { Name: string Id: string @@ -164,17 +171,17 @@ declare global { } namespace YouTubeMusic { - interface Connection extends BaseConnection { - type: 'youtube-music' - youtubeUserId: string - accessToken: string - info?: T - } - - interface ConnectionInfo { + interface Service { + userId: string username?: string profilePicture?: string } + + interface Tokens { + accessToken: string + refreshToken: string + expiry: number + } } } diff --git a/src/lib/server/users.ts b/src/lib/server/users.ts index c3da22a..726254d 100644 --- a/src/lib/server/users.ts +++ b/src/lib/server/users.ts @@ -12,6 +12,7 @@ const initUsersTable = `CREATE TABLE IF NOT EXISTS Users( const initConnectionsTable = `CREATE TABLE IF NOT EXISTS Connections( id VARCHAR(36) PRIMARY KEY, userId VARCHAR(36) NOT NULL, + type VARCHAR(36) NOT NULL, service TEXT NOT NULL, accessToken TEXT NOT NULL, refreshToken TEXT, @@ -23,6 +24,7 @@ db.exec(initUsersTable), db.exec(initConnectionsTable) interface ConnectionsTableSchema { id: string userId: string + type: string service: string accessToken: string refreshToken?: string @@ -71,17 +73,19 @@ export class Connections { return connections } - static addConnection = (userId: string, service: Service, accessToken: string, refreshToken?: string, expiry?: number): Connection => { + static addConnection = (userId: string, service: Service, accessToken?: string, refreshToken?: string, expiry?: number): Connection => { const connectionId = generateUUID() - const ytConnection: YouTubeMusic.Connection = { + const test: Connection = { id: 'test', userId: 'test', - youtubeUserId: 'test', - type: 'youtube-music', + type: 'jellyfin', + service: { + userId: 'test', + urlOrigin: 'test', + }, accessToken: 'test', } - const test = this.insertConnection(ytConnection) - if (!isValidURL(service.urlOrigin)) throw new Error('Service does not have valid url') + // if (!isValidURL(service.urlOrigin)) throw new Error('Service does not have valid url') db.prepare('INSERT INTO Connections(id, userId, service, accessToken, refreshToken, expiry) VALUES(?, ?, ?, ?, ?, ?)').run(connectionId, userId, JSON.stringify(service), accessToken, refreshToken, expiry) return this.getConnection(connectionId) }