miragejs / miragejs

A client-side server to build, test and share your JavaScript app
https://miragejs.com/
MIT License
5.4k stars 168 forks source link

The `request` given to `passthrough` doesn't have query params #359

Open samselikoff opened 4 years ago

samselikoff commented 4 years ago

Believe the request object we pass to passthrough when its used as a function doesn't have queryParams defined, not sure if this is a Pretender thing or an us thing

bluenex commented 4 years ago

Not sure whether this is related. I have tried MirageJS on React Native and found that queryParams doesn't work. The route and request look like this:

this.get('/movies?a=:a', ...)

// request
fetch('/movies?a=1')..

and here is the error I have got:

image

samselikoff commented 4 years ago

The way pretender works is you access the query params via the request object. Does the route handler fire if you just put this.get(‘movies’)?

– Sam

On Jun 4, 2020, at 2:29 AM, Tulakan Ruangrong notifications@github.com wrote:

 Not sure whether this is related. I have tried MirageJS on React Native and found that queryParams doesn't work. The route and request looks like this:

this.get('/movies?a=:a', ...)

// request fetch('/movies?a=1').. and here is the error I have got:

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

bluenex commented 4 years ago

@samselikoff oh my bad, I was confusing on how to write this. Sorry about that and thank you very much!

samselikoff commented 4 years ago

No prob at all - let me know if you have any more questions!

IanVS commented 3 years ago

I've just hit this as well. It seems that the request doesn't have queryParams initially (can observe using console.log(JSON.stringify(request)), but it does get added eventually, as you can see if you just console.log(request).

IanVS commented 3 years ago

For now, I am working around this issue using:

const url = new URL(request.url);
const params = new URLSearchParams(url.searchParams);
return params.get('skipMirage') === 'true';

I think that is a decent approach. Maybe the docs could be updated to show that method instead of trying to access request.queryParams, which fails?