hapijs / hapi

The Simple, Secure Framework Developers Trust
https://hapi.dev
Other
14.63k stars 1.34k forks source link

Pass original route option in server.rules() processor #4142

Open damusix opened 4 years ago

damusix commented 4 years ago

Support plan

Context

What problem are you trying to solve?

const processor = (rules, { method, path, vhosts, config }) => {

    const options = { tags: config.tags || [] }

    if (rules.audit) {
        options.tags = Hoek.merge(['audit'], options.tags)
    }

    if (rules.api) {
        options.tags = Hoek.merge(['api'], options.tags)
    }
}

Do you have a new or modified API suggestion to solve the problem?

https://github.com/hapijs/hapi/blob/master/lib/route.js#L68

const rulesConfig = internals.rules(rules, { method, path, vhost, config }, server);
devinivy commented 3 years ago

I started digging into this. One thing that makes this less straightforward than one might hope is that there are several levels of defaults that can be applied to the route config. That can be seen here:

https://github.com/hapijs/hapi/blob/c2107e9bc9c522c3778e90a3629a0c84f776f9fe/lib/route.js#L81-L83

So if we were to pass the route config into the rule, we'd need to consider which of the intermediate route configs ought to be passed. For example, server route defaults will be applied on top of the user's supplied route config prior to defaults from the rules processor. I believe this is why currently route.options.rules is isolated and passed on its own, to sidestep this awkward issue of working with un-finalized route configurations as various defaults are applied. This would also need to work consistently when a route has multiple rules processors. I am not sure I made this super clear— does that make sense? Any suggestions how it might be addressed?