Replies: 1 comment
-
|
Reuse the same schema in the handler and parse the request URL there: import {
createFileRoute,
defaultParseSearch,
} from '@tanstack/react-router'
import { z } from 'zod'
const searchSchema = z.object({
id: z.coerce.number().int(),
})
export const Route = createFileRoute('/test')({
validateSearch: searchSchema,
server: {
handlers: {
GET: async ({ request }) => {
const url = new URL(request.url)
const result = searchSchema.safeParse(
defaultParseSearch(url.search),
)
if (!result.success) {
return Response.json(
{
error: 'Invalid search parameters',
issues: result.error.issues,
},
{ status: 400 },
)
}
return Response.json({ id: result.data.id })
},
},
},
})
For a simple flat query, this is also valid: const raw = Object.fromEntries(new URL(request.url).searchParams)
const result = searchSchema.safeParse(raw)However, So the short answer is: there is currently no server-handler equivalent of Docs: TanStack Start server routes · TanStack Router search params |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I tried below, but not working.
search params are not mentioned in the server routes docs.
https://tanstack.com/start/latest/docs/framework/react/guide/server-routes
currently the only option is fallback to URLSearchParams:
Beta Was this translation helpful? Give feedback.
All reactions