fix(compiler): route the generated api object through the real buildApiRequest
Root-cause fix for the fix-round-1 review: the inlined query/body assembly in __wrnexusCallApi was a third, unguarded copy of buildApiRequest's rules. Restore the import of buildApiRequest from @wrnexus/core in the generated module and delete the inline copy. The four api-block-ssr.test.ts tests (and three in compiler.test.ts) that dynamically import a generated module from an OS tmpdir were failing against a stale globally-installed @wrnexus/core (v0.8.8, predates buildApiRequest) because that tmpdir has no node_modules of its own and bare-specifier resolution walked out of the workspace. Fixed at the source: symlink the workspace @wrnexus/core into each tmpdir root before the dynamic import, the same way every in-repo package already resolves it.
This commit is contained in:
@@ -1068,28 +1068,15 @@ async function __wrnexusCallApi(
|
||||
return await ctx.__wrnexusCallApi(path, method, input);
|
||||
}
|
||||
|
||||
const verb = String(method || "GET").toUpperCase();
|
||||
const values = (input ?? {}) as Record<string, unknown>;
|
||||
const built = __wrnexusBuildApiRequest(path, method, input as Record<string, unknown> | undefined);
|
||||
const url = new URL(built.url, ctx.req.url);
|
||||
const headers = new Headers(ctx.req.headers);
|
||||
let requestPath = path;
|
||||
let body: string | undefined;
|
||||
if (verb === "GET" || verb === "HEAD") {
|
||||
const query: string[] = [];
|
||||
for (const [key, value] of Object.entries(values)) {
|
||||
if (value === undefined || value === null || value === "") continue;
|
||||
query.push(\`\${encodeURIComponent(key)}=\${encodeURIComponent(String(value))}\`);
|
||||
}
|
||||
if (query.length) requestPath = \`\${path}?\${query.join("&")}\`;
|
||||
} else {
|
||||
body = JSON.stringify(values);
|
||||
headers.set("content-type", "application/json");
|
||||
}
|
||||
const url = new URL(requestPath, ctx.req.url);
|
||||
if (built.contentType) headers.set("content-type", built.contentType);
|
||||
const res = await fetch(
|
||||
new Request(url, {
|
||||
method,
|
||||
headers,
|
||||
...(body === undefined ? {} : { body }),
|
||||
...(built.body === undefined ? {} : { body: built.body }),
|
||||
}),
|
||||
);
|
||||
const type = res.headers.get("content-type") || "";
|
||||
@@ -1663,6 +1650,7 @@ function generateInner(ast: PageAst): string {
|
||||
const needsSsrRuntime = ssrBindings.length > 0 || loops.length > 0 || runtimeStateNames.size > 0;
|
||||
const needsRuntimeHelpers = needsSsrRuntime || hasServerApis;
|
||||
if (needsRuntimeHelpers) {
|
||||
out.push(`import { buildApiRequest as __wrnexusBuildApiRequest } from "@wrnexus/core";`);
|
||||
out.push(ssrRuntimeSource());
|
||||
out.push(
|
||||
`const __wrnexusSsrBindings: __WrnexusSsrBinding[] = ${JSON.stringify(ssrBindings, null, 2)};`,
|
||||
|
||||
Reference in New Issue
Block a user