question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calls with filter params (JSONAPI)

See original GitHub issue

I’m trying to stub a GET /supports?filter[proposal_id]=1&filter[author_id]=1 call, which uses JSONAPI filtering, with

this.get('/supports?filter[proposal_id]=1&filter[author_id]=1', function(db, request) {
...
});

but it doesn’t get recognized. this.get('/supports, function... does, but that means that all GET calls to /supports get intercepted as well.

Am I doing something wrong, or is this a limitation of the way uris are parsed? If so, are there any tricks to get around it?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
nselikoffcommented, May 23, 2019

Example that supports filtering by an array of values. You can pass a function to where instead of a POJO (see https://github.com/samselikoff/ember-cli-mirage/blob/master/addon/db-collection.js#L399).

  this.get('/trips', function (schema, request) {
    if (isEmpty(request.queryParams)) {
      return schema.trips.all();
    }

    let query = {};
    if (request.queryParams['filter[trip_tag]']) {
      filters.tripTag = request.queryParams['filter[trip_tag]'];
    }

    // custom query function to support array values for filters
    function customQueryFunction(record) {
      let keys = Object.keys(query);

      return keys.every(function(key) {
        if (Array.isArray(query[key])) {
          return query[key].includes(String(record[key]));
        } else {
          return String(record[key]) === String(query[key]);
        }
      });
    }
    return schema.trips.where(customQueryFunction);
  });

Another option here would be to do schema.trips.all().filter(...).

0reactions
nselikoffcommented, May 23, 2019

Latest issue tracking this feature request is https://github.com/samselikoff/ember-cli-mirage/issues/1419

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filtering
Resources can be filtered by attributes using the filter query string parameter. By default, all attributes are filterable. The filtering strategy we have ......
Read more >
Calls with filter params (JSONAPI) · Issue #278 - GitHub
I'm trying to stub a GET /supports?filter[proposal_id]=1&filter[author_id]=1 call, which uses JSONAPI filtering, ...
Read more >
Filtering | JSON:API module | Drupal Wiki guide on Drupal.org
This guide will teach you how to build filters like a pro. Quick Start. The simplest, most common filter is a key-value filter:...
Read more >
Share/Propose a Filtering Strategy - JSON API
The filter query parameter can be used as the basis for any number of filtering strategies.”. http://jsonapi.org/format/#fetching-filtering.
Read more >
JSON:API — Recommendations
Filtering. The base specification is agnostic about filtering strategies supported by a server. The filter query parameter family is reserved to be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found