S
SolidJS3mo ago
kissa

Best way to get search params in an API route?

Since useSearchParams() doesn't work in API routes, I'm wondering if there's an easier way to get them than what I came up with:
export async function GET({ request }: APIEvent) {
let searchParams = Object.fromEntries(
new URL(request.url).searchParams.entries()
)
export async function GET({ request }: APIEvent) {
let searchParams = Object.fromEntries(
new URL(request.url).searchParams.entries()
)
which seems a bit cumbersome. I'm passing the params to zod so I'll need them in plain object format.
2 Replies
Rodrigo
Rodrigo3mo ago
I'm afraid this is the way, at least right now. The API routes engine is not yet wrapped with deeper and more thoughtful helpers as you'd expect in api- specific tools like Hono
Ximaz
Ximaz3w ago
Couldn't you use APIEvent["params"] ?
async function GET({ params, request }: APIEvent) {
// http://.../?param_one=blabla
const paramOne = params["param_one"] // equals to "blabla"
}
async function GET({ params, request }: APIEvent) {
// http://.../?param_one=blabla
const paramOne = params["param_one"] // equals to "blabla"
}
I believe ? I may be naive though, I am trying to learn API routing too... I was in fact too naive, params doesn't seem to have any value on my side.
Want results from more Discord servers?
Add your server