feat(core): share API request assembly between both transports
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -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" };
|
||||
}
|
||||
Reference in New Issue
Block a user