81 lines
2.7 KiB
Swift
81 lines
2.7 KiB
Swift
import Foundation
|
|
|
|
/// MongoDB references are populated on some API routes and bare ObjectIds on others.
|
|
nonisolated public enum APIReference<Value: Decodable & Sendable>: Decodable, Sendable {
|
|
case id(String)
|
|
case value(Value)
|
|
|
|
public init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
if let id = try? container.decode(String.self) {
|
|
self = .id(id)
|
|
} else {
|
|
self = .value(try container.decode(Value.self))
|
|
}
|
|
}
|
|
}
|
|
|
|
nonisolated public struct Artist: Decodable, Identifiable, Sendable {
|
|
public let id: String
|
|
public let name: String
|
|
public let file: String?
|
|
enum CodingKeys: String, CodingKey { case id = "_id", name, file }
|
|
}
|
|
|
|
nonisolated public struct Genre: Decodable, Identifiable, Sendable {
|
|
public let id: String
|
|
public let name: String
|
|
enum CodingKeys: String, CodingKey { case id = "_id", name }
|
|
}
|
|
|
|
nonisolated public struct Album: Decodable, Identifiable, Sendable {
|
|
public let id: String
|
|
public let name: String
|
|
public let artists: [APIReference<Artist>]
|
|
public let file: String?
|
|
public let genre: APIReference<Genre>?
|
|
enum CodingKeys: String, CodingKey { case id = "_id", name, artists, file, genre }
|
|
}
|
|
|
|
nonisolated public struct Lyrics: Decodable, Sendable {
|
|
public let synced: Bool
|
|
public let text: String
|
|
}
|
|
|
|
nonisolated public struct Track: Decodable, Identifiable, Sendable {
|
|
public let id: String
|
|
public let name: String
|
|
public let album: APIReference<Album>
|
|
public let artists: [APIReference<Artist>]
|
|
public let fileDir: String
|
|
public let durationInSeconds: Double
|
|
public let lyrics: Lyrics?
|
|
enum CodingKeys: String, CodingKey {
|
|
case id = "_id", name, album, artists, fileDir, durationInSeconds, lyrics
|
|
}
|
|
}
|
|
|
|
nonisolated public struct User: Decodable, Identifiable, Sendable {
|
|
public let id: String
|
|
public let name: String
|
|
public let email: String
|
|
public let role: String
|
|
public let savedTracks: [String]?
|
|
enum CodingKeys: String, CodingKey { case id = "_id", name, email, role, savedTracks }
|
|
}
|
|
|
|
nonisolated public struct SessionStatus: Decodable, Sendable { public let authenticated: Bool }
|
|
nonisolated public struct Session: Decodable, Sendable {
|
|
public let expireAt: Date
|
|
/// Returned for administrative bearer auth; ordinary user auth uses the HttpOnly cookie.
|
|
public let sessionToken: String
|
|
}
|
|
nonisolated public struct SearchResults: Decodable, Sendable {
|
|
public let tracks: [Track]
|
|
public let albums: [Album]
|
|
public let artists: [Artist]
|
|
}
|
|
nonisolated public struct TrackMembership: Decodable, Sendable { public let contains: Bool }
|
|
|
|
nonisolated public enum SearchType: String, Sendable { case track, album, artist, `default` }
|