New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
addRoutes() adds unusable router and extend() will loose all prefixes #23
Comments
Hi! Thanks for opening that. What's your specific case and real world example? I'm a bit tired of this composition problems that are always reported. And i feel that in most cases they are just confusion with the docs and the understandings how they work in reality. Using |
Oh, and one more thing: |
My basic question is basically: how do i load Routes from files with prefixes to be sorted into other routes with prefixes. Like a basic router(prefix:'/') that gets added ones: [router(prefix:'/webapp')+routes and router(prefix:'/api')]. where api gets router(prefix:'/v1')+routes and router(prefix:'/latest') the same as v1. how would i do that to end up with: |
Hm, that clarify the things a bit. One more note that may worth: you don't need to merge all routers, but to call const router1 = new Router()
const router2 = new Router()
const router3 = new Router()
app.use(router1.middleware())
app.use(router2.middleware())
app.use(router3.middleware()) Try that, otherwise we'll try something other, i have some ideas, but probably this is the easiest. |
thought about this, but it would require either the each router to have a full prefix path or have each route to include the full path. what i am currently trying to do is, overwrite your router object and modify your addRoutes to resolve each router.routes[] with your utils.updatePrefix() to import/append them as routes to each "parent" / addRoutes caller. (which kills all possibilities to go with managing routes of the originating router) |
Can you share some examples/snippets? And, would you have some routes on the |
See that 'use strict'
const Router = require('./index')
const Koa = require('koa')
const R = new Router({ prefix: '/' }).loadMethods()
const routerApp = new Router({ prefix: '/webapp' }).loadMethods()
const routerApiV1 = new Router({ prefix: '/api/v1' }).loadMethods()
const routerApiLatest = new Router({ prefix: '/api/latest' }).loadMethods()
const app = new Koa()
R.get('/', (ctx, next) => {
ctx.body = `Hello world! Web`
return next()
})
R.get('/about', (ctx, next) => {
this.body = `About company`
return next()
})
console.log('root', R.getRoutes())
console.log('======')
routerApp.get('/', (ctx, next) => {
ctx.body = 'Home page of WebApp'
return next()
})
routerApp.get('/users', (ctx, next) => {
ctx.body = 'Users of WebApp'
return next()
})
console.log('webapp', routerApp.getRoutes())
console.log('======')
routerApiV1.get('/', (ctx, next) => {
ctx.body = 'API v1 home'
return next()
})
routerApiV1.get('/repos', (ctx, next) => {
ctx.body = 'User repos'
return next()
})
routerApiV1.get('/gists', (ctx, next) => {
ctx.body = 'User gists'
return next()
})
console.log('API Version 1', routerApiV1.getRoutes())
console.log('======')
routerApiLatest.get('/', (ctx, next) => {
ctx.body = 'Latest API endpoint, dont use in production'
return next()
})
routerApiLatest.get('/repositories', (ctx, next) => {
ctx.body = 'New version of old v1 /repos endpoint'
return next()
})
routerApiLatest.get('/issues', (ctx, next) => {
ctx.body = 'Repository issues'
return next()
})
console.log('API Latest', routerApiLatest.getRoutes())
console.log('======')
app.use(R.middleware())
app.use(routerApp.middleware())
app.use(routerApiV1.middleware())
app.use(routerApiLatest.middleware()) |
You probably didn't tried to set |
you might want to check this out: |
Don't use Is btw, // add routes - hack (keep prefixes, but does not merge them with local one, but local one would be '/' anyways) !!
app.routes.forEach( (e) =>
router.routes.push(e)
); that is exactly what app.addRoutes(router.routes)
api1.addRoutes(router.routes) It accepts any number of arguments and array of route objects too. (i believe) |
Hmmmm. Okey, start to getting the situation. Probably new |
Yep, easy. I just replcated your gist structure and everything, and implemented 'use strict'
const Router = require('koa-better-router')
const AAA = require('./a')
const BBB = require('./b')
const APIv1 = new Router({
prefix: '/api/v1',
}).loadMethods()
APIv1.get('/', (ctx, next) => {
ctx.body = router.routes.map(e => e.path)
return next()
})
APIv1.get('/x', (ctx, next) => {
ctx.body = 'list ...'
return next()
})
APIv1.extend(AAA, { preservePrefix: true })
APIv1.extend(BBB, { preservePrefix: true })
APIv1.getRoutes().forEach(route => {
console.log(route)
console.log('========')
})
module.exports = APIv1 the result is the following
is that looks okey? |
Basically, that's the updated code of KoaBetterRouter.prototype.extend = function extend(router, options) {
if (!(router instanceof KoaBetterRouter)) {
throw new TypeError(
'.extend: expect `router` to be instance of KoaBetterRouter'
)
}
const opts = Object.assign({ preservePrefix: false }, options)
const updateOnPreserve = (route, target) => {
route.prefix = target.prefix
route.route = route.path
route.path = route.prefix + route.path
// rebuild the matcher function
// to be against the new route path (which includes the target prefix)
route.match = router.route(route.path)
return route
}
router.routes.forEach(route => {
if (route.prefix !== this.options.prefix) {
route = opts.preservePrefix
? updateOnPreserve(route, this.options)
: utils.updatePrefix(this, this.options, route)
}
this.routes.push(route)
})
return this
} you can replace it and see if everything works as expected. There are more breaking changes that should be done, so i will update that too. |
Thanks for sorting this out 👍 great stuff btw! |
Thanks to you too! Open source is great thing and lovely place 🎉
I'll try as soon as possible. Probably on the weekend, at least. |
Adding a
let r2 = new Router({prefix: 'a'}).loadMethods().get('/', (ctx, next) => {ctx.body='supposed to be at /api/a/'; return next();})
withlet r1 = new Router({prefix: 'api'}).addRoutes(r2);
does not result in a callable route. Actually koa-better-router crashes with not having any routes.And using extend ->
let r1 = new Router({prefix: 'api'}).extend(r2);
will loose all prefix info from r2. Putting all '/' routes at the same base level.Any known workaround?
The text was updated successfully, but these errors were encountered: