Better management of REST API endpoints (DS.RESTRoutes)

Its quite often to require different API endpoints, other than the DS.RESTAdapter defaults. For example you might have nested resources:

POST /posts/1/comments
DELETE /groups/9/members

Some may be namespaced, some not:

GET /api/products
GET /users

Instead of customizing each Model’s adapter (overriding find, createRecord, buildUrl, namespace, etc.), would it be beneficial to have a single place to define the API routing scheme, similar to Rails routes.rb? Something along the lines:

DS.RESTRoutes.map(function() {
  this.namespace('api', function() {
    
    this.resource('posts', { for: 'all' }, function() {
      this.resource('comments', { for: ['create', 'update', 'index'] }
    });

    this.resource('comments', { for: ['show', 'update', 'delete'] });
  });

  this.resource('users', { for: ['all'] }); // Default
});

With the above routing scheme, if I do:

store.find('comment', { post: somePost });

the RESTAdapter will know it needs to request /api/posts/19/comments instead of the default /api/comments?post_id=19.

4 Likes

Looks like a good idea! I have had an issue with nested routes and stuff recently and haven’t resolved the issue yet.

This looks great, lots of flexibility!

Useful. Please don’t forget collection methods like /movies/published, /movies/drafts, etc…