vigetlabs / gangway

A client-side API abstraction layer
MIT License
13 stars 1 forks source link

Nh namespacing #24

Closed nhunzaker closed 8 years ago

nhunzaker commented 8 years ago

This PR should allow for the deep namespacing of routes. For example, the following are now possible:

var API = Gangway()

API.resource('users', {}, function(users) {
    users.resource('posts', {}, function (posts) {
        posts.route({
            wordCount: {
                path: 'word-count'
            }
        })
    })
})

console.log(API) // => { users: { create, read, update, delete, posts: { create, read, update, delete, wordCount } } }

Additionally:

var API = Gangway()

var users = API.namespace('users').route({
    posts: {
        read() {
            path: 'posts/:id'
        }
    }
})

console.log(API) // => { users: { posts: { read } }

Nested parameter names for nested routes are singularized, following the convention of Rails. For example:

var API = Gangway()

API.resource('users', {}, function(users) {
    users.resource('posts')
})

This will produce the following routes:

and

mackermedia commented 8 years ago

Should

var users = API.namespace('users').route({
    posts: {
        read() {
            path: 'users/:id'
        }
    }
})

be

var users = API.namespace('users').route({
    posts: {
        read() {
            path: 'users/:user_id/posts'
        }
    }
})

? That path thing reads funny to me as is.

nhunzaker commented 8 years ago

@mackermedia I had that path wrong with way. It should have been /posts/:id.

Edit: More in a minute

mackermedia commented 8 years ago

I'm not sure I understand all of the code updates, but the tests look good :+1:

nhunzaker commented 8 years ago

@mackermedia path is awkward. The best alternative that I can think of would be a breaking update (now that we have namespaces) to convert the following:

API.route({
    posts: {
        read() {
            path: 'posts/:id'
        }
    }
})

into

API.namespace('posts').route({
    read() {
        path: ':id'
    }
})

Additionally, we could allow absolute paths to operate against the base URL like:

API.namespace('posts').route({
    read() {
        path: '/posts/:id'
    }
})

I think this feels more intuitive to how relative paths work in the browser. We'd definitely have to ship a 2.0 for this though, and it would break every project using Gangway. Still, the upgrade path would be pretty clear.

nhunzaker commented 8 years ago

Lastly, is namespace the best name for this?

cwmanning commented 8 years ago

:+1:

greypants commented 8 years ago

:+1: this is perfect!