/** * Assemble an API request from a block's declared input. * * Shared by both transports on purpose. The browser and the in-process server * caller must send the same thing for the same call; two copies of these rules * would drift, and the drift would be invisible because each side is tested * separately. */ export interface BuiltApiRequest { url: string; body?: string; contentType?: string; } export function buildApiRequest( path: string, method: string, input: Record | undefined, ): BuiltApiRequest { const verb = String(method || "GET").toUpperCase(); const values = input ?? {}; if (verb === "GET" || verb === "HEAD") { const query: string[] = []; for (const [key, value] of Object.entries(values)) { // 0 and false are legitimate values and must survive. if (value === undefined || value === null || value === "") continue; query.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`); } return { url: query.length ? `${path}?${query.join("&")}` : path }; } return { url: path, body: JSON.stringify(values), contentType: "application/json" }; }