Typed searchParams in API routes?
Unanswered
Southern rough shrimp posted this in #help-forum
Southern rough shrimpOP
I asked this previously but cannot find my post anymore. Maybe it was deleted!? I tried
type GetArgs = {
searchParams: {
input?: string,
},
};
export async function GET(req: Request, { searchParams: { input } }: GetArgs) {
to get the input value from the url as in /api?input=something
but this generates a 500 server error
Edit: I am aware I can use https://nextjs.org/docs/app/building-your-application/routing/route-handlers#url-query-parameters but this ain't typed
type GetArgs = {
searchParams: {
input?: string,
},
};
export async function GET(req: Request, { searchParams: { input } }: GetArgs) {
to get the input value from the url as in /api?input=something
but this generates a 500 server error
Edit: I am aware I can use https://nextjs.org/docs/app/building-your-application/routing/route-handlers#url-query-parameters but this ain't typed
3 Replies
American Crow
This is not really adressing the issue but i don't think you want to type the
The user can input whatever he/she wants into the url, so the correct type for your codebase is
A more general type like this ensures that you are aware of the fact, that you have to check the actual value at runtime
Which can be done using zod or any other validator library
searchParams with the value you expect.The user can input whatever he/she wants into the url, so the correct type for your codebase is
searchParams: { [key: string]: string | string[] | undefined }A more general type like this ensures that you are aware of the fact, that you have to check the actual value at runtime
Which can be done using zod or any other validator library
That is why server actions are so powerful.
🙂