carbon-io / carbond

MIT License
2 stars 5 forks source link

Add a cleaner way to define multiple endpoints that do the same thing. #286

Closed DesignerDave closed 6 years ago

DesignerDave commented 6 years ago

Use-case: React apps typically do all the routing on the client, so you could end up with dozens of endpoints that render the same page in exactly the same way.

The cleanest way to do this at the moment seems to be something like this:

function createClientEndpoint(endpoints) {
  return o({
    _type: carbon.carbond.Endpoint,
    endpoints: endpoints || [],
    get: function(req, res) {
        res.render('client-page')
    },
  })
}

module.exports = o({
  endpoints: {
      '/': createClientEndpoint(),
      'systems': createClientEndpoint({
          ':id': createClientEndpoint({
              'overview': createClientEndpoint(),
              'history': createClientEndpoint(),
              'monitoring': createClientEndpoint(),
          }),
      }),
      'account': createClientEndpoint({
          'overview': createClientEndpoint(),
          'users': createClientEndpoint(),
          'access-control': createClientEndpoint(),
      }),
  }
})

It would be cool, and quite a bit cleaner to be able to do something along these lines instead:

module.exports = o({
  endpoints: {
    'client': o({
        _type: carbon.carbond.EndpointGroup,
        endpoints: [
            '/',
            { 'systems': [{ 
                ':id': [
                    'overview',
                    'history',
                    'monitoring' ],
                }] 
            },
            { 'account': [
                'overview',
                'users',
                'access-control' ],
            },
        ],
        get: function(req, res) {
            res.render('client-page')
        },
    })
  }
})