Mirage: Add custom host to adapter issue

I’m using Ember-cli-mirage to mock data. I want to slowly integrate parts of the real api which is located at http://localhost:8000. Ember docs tell me that I should be able to set an adapter so I can have a different host for each model. So I’ve setup adapters/customer.js with the following:

import DS from 'ember-data';

export default DS.RESTAdapter.extend( {
  host: 'http://localhost:8000',
  namespace: 'api/v1'
});

But when I make the call I’m getting an error

Mirage: Error: Your Ember app tried to GET 'http://localhost:8000/api/v1/customers',
         but there was no route defined to handle this request.
         Define a route that matches this path in your
         mirage/config.js file. Did you forget to add your namespace?

When I run the request without the custom host: adapter it works fine, so I know that mirage is serving the data correctly. But for the customer model I want to use a different host.

I suspect it’s something to do with my config/environment.js setup so I’m looking at a variation of Mirage is not working with ember-electron · Issue #497 · miragejs/ember-cli-mirage · GitHub as a potential workaround. But anyone who can shine light on the error or input would be appreciated.

You’re after what is known as a pass through. More details here: http://www.ember-cli-mirage.com/docs/v0.2.x/configuration/#passthrough

1 Like

Gah! That’ll teach me. Go back and read the Mirage docs :smiley:

Thanks so much - fixed.

For anyone who needs this, here’s the two files I updated as per the docs on Mirage:

// mirage/config.js
import Mirage from 'ember-cli-mirage';

export default function() {

  this.urlPrefix = 'http://localhost:8000';
  this.namespace = '/api/v1';

  // Requests for customers
  this.get('/customers');
  this.get('/customers/:id');
  this.post('/customers');
  this.del('/customers/:id');
  this.patch('/customers/:id');

  // Passthrough to Django API
  this.passthrough('/customers');

}
// app/adapters/application.js
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  host: 'http://localhost:8000',
  namespace: 'api/v1'
});

Hope this helps someone. :slight_smile:

2 Likes