import { isAuthResponseMessage } from "./auth-messages"; interface FetchResponseLike { json(): Promise; ok: boolean; status: number; } type FetchLike = ( input: string, init?: RequestInit ) => Promise; type SendMessageLike = (message: unknown) => Promise; export function createProtectedApiClient(options: { baseUrl: string; fetchImpl?: FetchLike; sendMessage: SendMessageLike; }) { const fetchImpl = options.fetchImpl ?? fetch; return { async loadProtectedMockData() { const token = await readAccessToken(options.sendMessage); const response = await fetchImpl( new URL("/api/mock/protected", options.baseUrl).toString(), { headers: { Authorization: `Bearer ${token}` }, method: "GET" } ); if (response.status === 401 || response.status === 403) { throw new Error("protected api unauthorized"); } if (!response.ok) { throw new Error(`protected api request failed: ${response.status}`); } return response.json(); } }; } async function readAccessToken(sendMessage: SendMessageLike): Promise { const response = await sendMessage({ type: "auth:get-access-token" }); if ( !isAuthResponseMessage(response) || !response.ok || response.type !== "auth:token" || !response.value.accessToken.trim() ) { throw new Error("protected api token unavailable"); } return response.value.accessToken; }