Query params in mirage

How to handle query params in ember-cli-mirage ??

Using the example from the mirage docs for using URL params:

this.get('/authors/:id', (schema, request) => {
  let id = request.params.id;
  return schema.authors.find(id);
})

Obviously this would match /authors/37 with id ‘37’. The queryParams key is also defined on the request object, so you just have to access it in a similar fashion. For example you could write an endpoint that does the same thing but in response to a query like /authors?id=37:

this.get('/authors', (schema, request) => {
  let id = request.queryParams.id;
  return schema.authors.find(id);
})

You can also get a little fancy and destructure if you prefer the look of this (I do, personally):

this.get('/authors', ({ authors }, { queryParams }) => {
  let id = queryParams.id;
  return authors.find(id);
})
3 Likes