refactor: do not hardcode valid types

This commit is contained in:
etiennecollin
2025-08-14 12:45:48 -04:00
parent 83a39b3a37
commit 34f9ebd098

View File

@@ -1,6 +1,8 @@
import { getRuntimeConfig } from "@/utils/runtimeConfig"; import { getRuntimeConfig } from "@/utils/runtimeConfig";
type WifiType = "WPA" | "WEP" | "nopass"; // Derive the type from the array
const validWifiTypes = ["WPA", "WEP", "nopass"] as const;
type WifiType = (typeof validWifiTypes)[number];
export interface WifiConfig { export interface WifiConfig {
ssid: string; ssid: string;
@@ -50,15 +52,15 @@ export function generateWifiConfig(): WifiConfig {
type_parsed = "nopass"; type_parsed = "nopass";
break; break;
default: default:
// TODO: Find how to print a type throw `Invalid WiFi type provided: ${type}. Valid types: ${validWifiTypes.join(
throw `Invalid WiFi type provided: ${type}. Valid types: "WPA" | "WEP" | "nopass"`; " | ",
)}`;
} }
} }
if (password && type_parsed === "nopass") { if (password && type_parsed === "nopass") {
throw "Incoherent WiFi configuration: password provided, but type set to 'nopass'"; throw "Incoherent WiFi configuration: password provided, but type set to 'nopass'";
} else if (!password && type_parsed !== "nopass") { } else if (!password && type_parsed !== "nopass") {
// TODO: This is true for WPA, but check for WEP
throw "Incoherent WiFi configuration: password not provided, but type implies a password"; throw "Incoherent WiFi configuration: password not provided, but type implies a password";
} }