Appyre / driven

An unassuming but opinionated framework for building realtime APIs
6 stars 0 forks source link

[Proposal] Route Registration API #2

Open rtablada opened 8 years ago

rtablada commented 8 years ago

Ideally route registration would be something like either:

Ember Inspired

// app/routes.js
import Router from 'driven-api/router';

const router = new Router();

router.register(function() {
  this.group('posts', function() {

    this.get('posts', {action: 'posts.all'});
  });

  this.get('version', {action: 'version.info'});
});

export default router;

Or more Laravel inspired (less magical this keyword):

// app/routes.js
import Router from 'driven-api/router';

const router = new Router();

router.group('posts', function(resource) {
  resource.get('posts', {action: 'posts.all'});
});

router.get('version', {action: 'version.info'});

export default router;
rtablada commented 8 years ago

Technically, I think this is more of a RouteManager but want to keep the end user :grinning: so...

The module that takes this map and reduces it into middleware to run through express can be named anything really.

runspired commented 8 years ago

This is the es5 version of this I had working in the app built with Driven.

const Router = require('../driven/services/router');

const AppRouter = Router.compose({

    map() {
        this.namespace('v1', { path: '/api/v1' }, function() {
            this.endpoint('auth', function() {
                this.action('sign-in');
                this.action('sign-out');
                this.action('me');
            });
            this.endpoint('set', function() {
                this.action('list');
                this.action('single', { path: '/:id' });
                this.action('update', { path: '/:id', method: 'PUT' });
                this.action('create', { path: '/', method: 'POST' });
            });
        });
    }

});

module.exports = AppRouter;