feat(core): share API request assembly between both transports

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-20 06:43:42 +05:30
co-authored by Claude Opus 5
parent 768074ac0a
commit f32b33e3b6
4 changed files with 107 additions and 3 deletions
+36
View File
@@ -0,0 +1,36 @@
/**
* 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<string, unknown> | 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" };
}