Files
WRNexusJS/packages/compiler/test/apis-server-emit.test.ts
T
Clintchiz 847b7010d1 feat(compiler): emit the server-side api object
Server module now declares `const api = { ... }` for apis {} blocks in
mode "any", dispatching in-process via requireRequestContext + the
existing __wrnexusCallApi transport helper. The try wraps only the
transport call; the response body runs after it, outside the try, so
a bug in the author's response code surfaces rather than being
mistaken for a request failure. A block with no error {} section
rethrows instead of resolving undefined.

Also closes the pageCtx.__wrnexusCallApi wiring gap in
dev-server/runtime.ts: it now forwards input through to
callApiFromContext instead of dropping it.
2026-08-20 07:03:11 +05:30

47 lines
1.1 KiB
TypeScript

import { expect, test } from "bun:test";
import { parse } from "@wrnexus/syntax";
import { generate } from "../src/codegen.ts";
const SOURCE = `page Probe {
apis {
searchUsers POST /api/users {
request { body { name?: string } }
response { return data.users }
error { return [] }
}
}
load server directory {
return await api.searchUsers({ name: "a" })
}
view { <main>x</main> }
}
`;
test("the server module declares an api object with the block's path and method", () => {
const generated = generate(parse(SOURCE));
expect(generated).toContain("const api =");
expect(generated).toContain("searchUsers");
expect(generated).toContain('"/api/users"');
expect(generated).toContain('"POST"');
});
test("the response body is spliced in", () => {
expect(generate(parse(SOURCE))).toContain("data.users");
});
test("a block with no error section rethrows rather than resolving undefined", () => {
const generated = generate(
parse(`page P {
apis { a GET /api/a { response { return data } } }
load server x { return await api.a() }
view { <main>x</main> }
}
`),
);
expect(generated).toContain("throw");
});