import { apiRequest } from "../../lib/api-client"; import type { PageResponse, Platform, PlatformCreateInput, PlatformUpdateInput } from "./types"; const basePath = "/api/v1/platforms"; type ApiPlatform = Omit & { regex_filters?: string[] | null; region_filters?: string[] | null; routable_node_count?: number | null; reverse_proxy_miss_action?: Platform["reverse_proxy_miss_action"] | null; reverse_proxy_empty_account_behavior?: Platform["reverse_proxy_empty_account_behavior"] | null; reverse_proxy_fixed_account_header?: string | null; passive_circuit_breaker_disabled?: boolean | null; }; function parseMissAction(raw: ApiPlatform["reverse_proxy_miss_action"]): Platform["reverse_proxy_miss_action"] { if (raw === "TREAT_AS_EMPTY" || raw === "REJECT") { return raw; } throw new Error(`invalid reverse_proxy_miss_action: ${String(raw)}`); } function normalizePlatform(raw: ApiPlatform): Platform { return { ...raw, reverse_proxy_miss_action: parseMissAction(raw.reverse_proxy_miss_action), regex_filters: Array.isArray(raw.regex_filters) ? raw.regex_filters : [], region_filters: Array.isArray(raw.region_filters) ? raw.region_filters : [], routable_node_count: typeof raw.routable_node_count === "number" ? raw.routable_node_count : 0, reverse_proxy_empty_account_behavior: raw.reverse_proxy_empty_account_behavior === "RANDOM" || raw.reverse_proxy_empty_account_behavior === "FIXED_HEADER" || raw.reverse_proxy_empty_account_behavior === "ACCOUNT_HEADER_RULE" ? raw.reverse_proxy_empty_account_behavior : "RANDOM", reverse_proxy_fixed_account_header: typeof raw.reverse_proxy_fixed_account_header === "string" ? raw.reverse_proxy_fixed_account_header : "", passive_circuit_breaker_disabled: typeof raw.passive_circuit_breaker_disabled === "boolean" ? raw.passive_circuit_breaker_disabled : false, }; } function normalizePlatformPage(raw: PageResponse): PageResponse { return { ...raw, items: raw.items.map(normalizePlatform), }; } export type ListPlatformsPageInput = { limit?: number; offset?: number; keyword?: string; }; export async function listPlatforms(input: ListPlatformsPageInput = {}): Promise> { const query = new URLSearchParams({ limit: String(input.limit ?? 50), offset: String(input.offset ?? 0), sort_by: "name", sort_order: "asc", }); const keyword = input.keyword?.trim(); if (keyword) { query.set("keyword", keyword); } const data = await apiRequest>(`${basePath}?${query.toString()}`); return normalizePlatformPage(data); } export async function getPlatform(id: string): Promise { const data = await apiRequest(`${basePath}/${id}`); return normalizePlatform(data); } export async function createPlatform(input: PlatformCreateInput): Promise { const data = await apiRequest(basePath, { method: "POST", body: input, }); return normalizePlatform(data); } export async function updatePlatform(id: string, input: PlatformUpdateInput): Promise { const data = await apiRequest(`${basePath}/${id}`, { method: "PATCH", body: input, }); return normalizePlatform(data); } export async function deletePlatform(id: string): Promise { await apiRequest(`${basePath}/${id}`, { method: "DELETE", }); } export async function resetPlatform(id: string): Promise { const data = await apiRequest(`${basePath}/${id}/actions/reset-to-default`, { method: "POST", }); return normalizePlatform(data); } export async function rebuildPlatform(id: string): Promise { await apiRequest<{ status: "ok" }>(`${basePath}/${id}/actions/rebuild-routable-view`, { method: "POST", }); } export async function clearAllPlatformLeases(id: string): Promise { await apiRequest(`${basePath}/${id}/leases`, { method: "DELETE", }); }