diff --git a/examples/basic-app/app/api/directory.ts b/examples/basic-app/app/api/directory.ts index 59a9cf6d..dd5446cb 100644 --- a/examples/basic-app/app/api/directory.ts +++ b/examples/basic-app/app/api/directory.ts @@ -1,14 +1,27 @@ import { defineEndpoint } from "@wrnexus/core"; -import type { Context } from "@wrnexus/core"; +import { SearchDirectorySchema } from "../schemas/search-directory.ts"; -const ALL = [ +interface DirectoryUser { + name: string; + designation: string; +} + +const ALL: DirectoryUser[] = [ { name: "Ajay", designation: "UI" }, { name: "Asha", designation: "Backend" }, { name: "Chen", designation: "UI" }, ]; -export const POST = async (ctx: Context) => { - const body = (await ctx.req.json().catch(() => ({}))) as { name?: string }; - const needle = String(body.name ?? "").toLowerCase(); - return Response.json({ users: ALL.filter((user) => user.name.toLowerCase().includes(needle)) }); -}; +/** + * Schema-typed handler: input resolves to `{ name?: string }` from + * SearchDirectorySchema, so the block's request shape is checked for real + * (not against `unknown`, which the untyped-handler shape resolved to). + */ +export const POST = defineEndpoint<{ name?: string }, { users: DirectoryUser[] }>({ + input: SearchDirectorySchema, + description: "Case-insensitive substring search over the demo directory by name.", + handler(input) { + const needle = String(input.name ?? "").toLowerCase(); + return { users: ALL.filter((user) => user.name.toLowerCase().includes(needle)) }; + }, +}); diff --git a/examples/basic-app/app/pages/api-block-demo.wrn b/examples/basic-app/app/pages/api-block-demo.wrn index f2d999d3..36420dba 100644 --- a/examples/basic-app/app/pages/api-block-demo.wrn +++ b/examples/basic-app/app/pages/api-block-demo.wrn @@ -12,7 +12,7 @@ page ApiBlockDemo { } response { - return data.users + return data.data.users } error { diff --git a/examples/basic-app/app/schemas/search-directory.ts b/examples/basic-app/app/schemas/search-directory.ts new file mode 100644 index 00000000..92c3fea0 --- /dev/null +++ b/examples/basic-app/app/schemas/search-directory.ts @@ -0,0 +1,5 @@ +import { v } from "@wrnexus/validation"; + +export const SearchDirectorySchema = v.object({ + name: v.string().trim().optional(), +}); diff --git a/packages/core/src/endpoint.ts b/packages/core/src/endpoint.ts index 1d3e6b27..118f4367 100644 --- a/packages/core/src/endpoint.ts +++ b/packages/core/src/endpoint.ts @@ -96,7 +96,18 @@ export function defineEndpoint( if (definition.auth === "required" && !ctx.user) { throw new EndpointError(401, "UNAUTHENTICATED", "Authentication is required."); } - const input = definition.input ? schemaValue(definition.input, rawInput) : rawInput; + // The real HTTP router invokes route handlers as `handler(ctx)` — it never + // supplies a second argument. Callers that already have a parsed payload + // (unit tests, internal RPC-style calls) may still pass one explicitly, and + // that always wins. Otherwise, read the request ourselves: query params for + // GET/HEAD, JSON body for everything else. + const resolvedInput = + rawInput !== undefined + ? rawInput + : ctx.req.method.toUpperCase() === "GET" || ctx.req.method.toUpperCase() === "HEAD" + ? Object.fromEntries(ctx.url.searchParams) + : await ctx.req.json().catch(() => ({})); + const input = definition.input ? schemaValue(definition.input, resolvedInput) : resolvedInput; const rawOutput = await definition.handler(input, ctx); const output = definition.output ? schemaValue(definition.output, rawOutput) : rawOutput; return output instanceof Response ? output : json({ data: output });