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
+14 -3
View File
@@ -10,6 +10,7 @@ import { brotliCompressSync, constants as zlibConstants } from "node:zlib";
import {
bridgeRealtime,
buildApiRequest,
createContext,
createCorsPreflightResponse,
createRealtimeRegistry,
@@ -1454,7 +1455,12 @@ export function createHandlers(deps: RuntimeDeps): Handlers {
}
}
async function callApiFromContext(ctx: Context, path: string, method = "GET"): Promise<unknown> {
async function callApiFromContext(
ctx: Context,
path: string,
method = "GET",
input?: Record<string, unknown>,
): Promise<unknown> {
if (!isSafeApiPath(path)) {
throw new Error("Unsafe framework API path");
}
@@ -1464,10 +1470,15 @@ export function createHandlers(deps: RuntimeDeps): Handlers {
throw new Error(`Unsupported framework API method: ${normalizedMethod}`);
}
const apiUrl = new URL(path, ctx.req.url);
const built = buildApiRequest(path, normalizedMethod, input);
const apiUrl = new URL(built.url, ctx.req.url);
const headers = new Headers(ctx.req.headers);
if (built.contentType) headers.set("content-type", built.contentType);
const apiReq = new Request(apiUrl, {
method: normalizedMethod,
headers: ctx.req.headers,
headers,
...(built.body === undefined ? {} : { body: built.body }),
});
const apiCtx = createContext(apiReq, apiUrl);
apiCtx.locals = ctx.locals;