Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nest.JS: How to properly handle query param that contains url with multiple query params?

I am working with some old nest.js codebase and we have /login endpoint.

This endpoint is waiting for query param redirect_url.

redirect_url can contain multiple query params too like this http://localhost:3000/api/schemas/generate?appId=application&event=eventName&registry=registry.

And finally, we have something like this:

http://localhost:3000/api/login?redirect_url=http://localhost:3000/api/schemas/generate?appId=application&event=eventName&registry=registry

because it is not encoded expected that @Query() object is:

{
  "redirect_url": "http://localhost:3000/api/schemas/generate?appId=application",
  "event": "eventName",
  "registry": "registry"
}

What is the best way to achieve this in nest.js and not lost query params?

{
  "redirect_url": "http://localhost:3000/api/schemas/generate?appId=application&event=eventName&registry=registry"
}
like image 215
mykhailo.vz Avatar asked Jan 23 '26 13:01

mykhailo.vz


1 Answers

If you want to send the redirect_url with query params first you have to encode it using encodeURIComponent and decode it in nest with decodeURIComponent. Do something like this:

const redirect_url = "http://localhost:3000/api/schemas/generate?appId=application";
const encoded_url = encodeURIComponent(redirect_url);
const url = `http://localhost:3000/api/login?redirect_url=${redirect_url}&event=eventName&registry=registry`
like image 164
Carlosdp7 Avatar answered Jan 26 '26 03:01

Carlosdp7