koajs / router

Router middleware for Koa. Maintained by @forwardemail and @ladjs.
MIT License
849 stars 174 forks source link

[feat] add support for path-group style routing #166

Closed sombriks closed 1 year ago

sombriks commented 1 year ago

Describe the feature

i am looking for a path-building idiom using koa-router similar to what javalin does.

something like this would be nice:

const group = new Router.Group().path("/", group => {
    group.post("login", loginRequest)
    group.post("signup", signupRequest)
    group.path("user/:userId/accounts", ifAuthenticated, group => {
        group.get(listAccountsRequest)
        group.post(ifAllowed, insertAccountRequest)
        group.path("/:accountId", group => {
            group.get(findAccountRequest)
            group.put(updateAccountRequest)
            group.del(ifAllowed, delAccountRequest)
            group.get("/transactions",listTransactionsRequest)
        })
    })
})
// then we build our api definition into a router
const router = group.build()

Checklist

sombriks commented 1 year ago

I've created a pull request implementing it but after some consideration it makes little sense to add this to the router itself; instead, a supporting library brings the very same benefit without growing the current router api surface. https://github.com/sombriks/koa-api-builder