UNPKG

@rsc-kit/mcp

Version:

An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.

204 lines (150 loc) • 6.8 kB
# Typed URLs > Checking and typing params, query strings and request bodies with a schema you already write. Export a schema beside a page, and its `params` and `searchParams` arrive checked and typed: ```tsx title="src/app/posts/[slug]/page.tsx" import { z } from 'zod' import type { PageProps } from '@rsc-kit/core/route-schema' export const params = z.object({ slug: z.string().min(1) }) export const searchParams = z.object({ page: z.coerce.number().int().min(1).default(1), tag: z.array(z.string()).default([]), }) // Renamed as they are destructured: `typeof params` must name the schema // exported above, not the prop. export default async function PostPage({ params: p, searchParams: q }: PageProps< typeof params, typeof searchParams >) { const { slug } = await p // string const { page, tag } = await q // number, string[] } ``` A URL is input from a stranger. Without a schema, `?page=banana` reaches your database as `NaN`. With one: - **`?page=3` arrives as `3`**, the number, not `"3"`. - **A missing value arrives as its default**, so there's no `undefined` branch to write. - **A bad value is refused once**, at the edge, instead of travelling on into whatever the page does next. Any [Standard Schema](https://standardschema.dev) works: Zod, Valibot or ArkType. The schema is asked to validate itself, and nothing here imports one. Both are ordinary exports, read by the build the same way `metadata` and `generateStaticParams` are. Export neither and nothing changes: `params` is a record of strings, and `searchParams` is a `URLSearchParams`. ## Validating a query string ```tsx title="src/app/products/page.tsx" export const searchParams = z.object({ q: z.string().default(''), sort: z.enum(['price', 'newest']).default('newest'), page: z.coerce.number().int().min(1).default(1), }) export default async function Products({ searchParams: q }: PageProps<never, typeof searchParams>) { const { q: query, sort, page } = await q return <ProductList query={query} sort={sort} page={page} /> } ``` `/products?sort=cheapest` is refused, and `/products` gets the defaults. Links to the page are typed from the same schema; see [Typed routes](/guides/typed-routes#typed-search-params). ### Repeated keys `?tag=red&tag=blue` arrives as an array, and `?q=shoes` as a string. The schema doesn't need to know which shape the URL happened to take, so `z.array(z.string())` and `z.string()` both work on the key you'd expect. ## What happens when a value is wrong A bad `params` and a bad `searchParams` fail differently: | What was wrong | Answer | Why | | --- | --- | --- | | `params` | **404**, `not-found.tsx` | the URL doesn't describe a page | | `searchParams` | nearest `error.tsx` | the page exists, the query was wrong | A slug your schema refuses isn't a broken page, it's an absent one. A `500` tells a crawler to come back later, while a `404` tells it the page is gone. Refusing a bad `?page=` as a 404, on the other hand, would let one bad link make a real page look deleted. ### Saying which field was wrong The error carries the fields, so a boundary can name them: ```tsx title="src/app/posts/[slug]/error.tsx" 'use client' import { isSearchParamsError } from '@rsc-kit/core/route-schema' export default function Error({ error }) { if (isSearchParamsError(error)) { return <p>That link is not quite right: {Object.keys(error.errors).join(', ')}</p> } return <p>Something went wrong.</p> } ``` ## Validating an API route A `route.ts` takes the same `params` and `searchParams`, plus `body`: ```ts title="src/app/api/posts/[id]/route.ts" import { z } from 'zod' import type { RouteContext } from '@rsc-kit/core/route-schema' export const params = z.object({ id: z.coerce.number().int() }) export const searchParams = z.object({ fields: z.string().optional() }) export const body = z.object({ title: z.string().min(1), draft: z.boolean().default(false) }) export async function POST( request: Request, { params, body }: RouteContext<typeof params, typeof searchParams, typeof body>, ) { const { id } = await params // number const { title } = await body // non-empty string return Response.json(await createPost(id, title), { status: 201 }) } ``` `RouteContext` is `PageProps` for a route: each schema's output behind a promise. With no schemas, `RouteContext<'/api/posts/[id]'>` types `params` from the segments. The handler still takes a real `Request` and returns a real `Response`. The schemas add a second argument and take nothing away. A route that exports none behaves exactly as before. | What was wrong | Answer | | --- | --- | | `params` | `404` | | `searchParams` | `400` with `{ message, errors }` | | `body` | `422` with `{ message, errors }` | `422` is what a [server action](/guides/validation/) already returns for a refused field, so a client has one shape to handle rather than two. ### JSON and forms Both encodings parse, since a URL is posted to by `fetch` and by `<form>` alike: ```ts await fetch('/api/posts/1', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'Hello' }), }) ``` ```html <form method="post" action="/api/posts/1"> <input name="title" /> </form> ``` A body that isn't valid JSON is a `422` about the body as a whole, not a crash. :::caution[Exporting `body` reads the request] The schema consumes the stream, so `await request.json()` inside the handler finds it already read. Use the parsed value instead. This happens only when you export a `body` schema, and only for `POST`, `PUT`, `PATCH` and `DELETE`. A `GET` handler is never touched. ::: ## A schema doesn't check who is asking A schema checks the **shape** of a request, not whether the person making it may. `params.id` being a number says nothing about whose row it is. The caller chooses the id, and trusting it is the whole of an IDOR. Check access on identity, in the handler, once the shape is known good: ```ts title="src/app/api/orders/[id]/route.ts" export async function GET(request: Request, { params }: RouteContext<'/api/orders/[id]'>) { const { id } = await params const order = await db.orders.find(id) if (order.userId !== (await currentUser()).id) { return new Response(null, { status: 404 }) } return Response.json(order) } ``` See [Authorization](/guides/authorization/). ## Why the props are promises A page or route awaits `params` and `searchParams` rather than receiving them resolved. That isn't only for symmetry. A route that never awaits `searchParams` provably doesn't vary by it, so the build can store one answer and serve it for `?utm_source=anything`. If they were resolved eagerly, the build couldn't know that, and every tracking link would miss the stored answer.