A shorthand is a simple way to define a route handler for common API scenarios. Here’s a reference of each shorthand, along with the raw route handler that the shorthand represents.
In Mirage 0.1, shorthands responded with objects and arrays directly from the database. In 0.2, shorthands return Models and Collections, meaning you can customize the format of the response in the serializer layer.
Shorthands use default status codes, based on the HTTP verb:
GET, PUT and DEL are 200
POST is 201
GET shorthands
Collection
Shorthand
this.get('/contacts');// finds type by singularizing urlthis.get('/contacts','users');// optionally specify the collection as second param
Expanded
this.get('/contacts',({contacts})=>{returncontacts.all();// users in the second case});
Object
Shorthand
this.get('/contacts/:id');// finds type by singularizing urlthis.get('/contacts/:id','user');// optionally specify the type as second param
Expanded
this.get('/contacts/:id',({contacts},request)=>{letid=request.params.id;returncontacts.find(id);// users in the second case});
For this POST shorthand to work, Mirage needs to know the format of the JSON payload your Ember app sends along with the request, so that it can insert the appropriate data into the database. See the note on normalize in the Serializer docs for more information.
PUT shorthands
Update a resource
Shorthand
this.put('/contacts/:id');// finds type by singularizing urlthis.put('/contacts/:id','user');// optionally specify the type as second param
For this PUT shorthand to work, Mirage needs to know the format of the JSON payload your Ember app sends along with the request, so that it can insert the appropriate data into the database. See the note on normalize in the Serializer docs for more information.
DELETE shorthands
Remove a resource
Shorthand
this.del('/contacts/:id');// finds type by singularizing urlthis.del('/contacts/:id','user');// optionally specify the type as second param