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