Files
otunes/apps/mobile/lib/server-config.ts
T
joshuafhiggins 73c7283d4d Add Expo mobile app and extract shared Convex package
- Replace mobile placeholder with Expo Router app (tabs, playback, auth, library)
- Move Convex backend to @otunes/convex for web and mobile reuse
- Update web app imports and API routes to use shared package
2026-08-02 04:14:01 +00:00

85 lines
2.4 KiB
TypeScript

export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
export type ServerConfig = {
/** Raw hostname the user entered, e.g. `music.example.com` or `localhost:3000` */
hostname: string
/** Base URL for the Next.js server, e.g. `https://music.example.com` */
serverUrl: string
/** Convex deployment URL, e.g. `https://convex.music.example.com` */
convexUrl: string
}
const SERVER_CONFIG_KEY = "otunes:server-config"
function isLocalHost(hostname: string): boolean {
const host = hostname.split(":")[0]?.toLowerCase() ?? ""
return (
host === "localhost" ||
host === "127.0.0.1" ||
host.endsWith(".local") ||
/^\d{1,3}(\.\d{1,3}){3}$/.test(host)
)
}
function normalizeHostnameInput(input: string): string {
return input.trim().replace(/^https?:\/\//i, "").replace(/\/+$/, "")
}
function buildServerUrl(hostname: string): string {
const protocol = isLocalHost(hostname) ? "http" : "https"
return `${protocol}://${hostname}`
}
function buildConvexUrl(hostname: string): string {
const [host, port] = hostname.split(":")
const convexHost = `convex.${host}`
const protocol = isLocalHost(hostname) ? "http" : "https"
return port ? `${protocol}://${convexHost}:${port}` : `${protocol}://${convexHost}`
}
export function parseServerHostname(input: string): ServerConfig | null {
const hostname = normalizeHostnameInput(input)
if (!hostname) return null
try {
const serverUrl = buildServerUrl(hostname)
const convexUrl = buildConvexUrl(hostname)
new URL(serverUrl)
new URL(convexUrl)
return { hostname, serverUrl, convexUrl }
} catch {
return null
}
}
export async function loadServerConfig(
getItem: (key: string) => Promise<string | null>
): Promise<ServerConfig | null> {
const raw = await getItem(SERVER_CONFIG_KEY)
if (!raw) return null
try {
const parsed = JSON.parse(raw) as ServerConfig
if (parsed.hostname && parsed.serverUrl && parsed.convexUrl) {
return parsed
}
} catch {
return null
}
return null
}
export async function saveServerConfig(
setItem: (key: string, value: string) => Promise<void>,
config: ServerConfig
): Promise<void> {
await setItem(SERVER_CONFIG_KEY, JSON.stringify(config))
}
export async function clearServerConfig(
removeItem: (key: string) => Promise<void>
): Promise<void> {
await removeItem(SERVER_CONFIG_KEY)
}
export { SERVER_CONFIG_KEY }