Skip to content
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

Add middleware per route #128

Closed
wants to merge 3 commits into from
Closed

Conversation

kasongoyo
Copy link
Contributor

This pull request introduce a way to pass middleware instanc when defining route so that it can be utilized adapters when adding route to express or hapi context.

@coveralls
Copy link

coveralls commented Nov 16, 2017

Coverage Status

Coverage increased (+0.2%) to 91.729% when pulling b8e5cf0 on kasongoyo:master into fdbbd69 on senecajs:master.

@tswaters tswaters self-requested a review November 17, 2017 05:12
Copy link
Member

@tswaters tswaters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contribution.

Can you please add docs that describe the parameter?

lib/mapper.js Outdated
@@ -91,7 +97,7 @@ module.exports = function mapper (routePlan) {
return routes
}

function buildPath (route) {
function buildPath(route) {
Copy link
Member

@tswaters tswaters Nov 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix this (and the other linting errors?) Get the build passing....

@coveralls
Copy link

coveralls commented Nov 21, 2017

Coverage Status

Coverage decreased (-0.9%) to 90.672% when pulling 2a35ec0 on kasongoyo:master into fdbbd69 on senecajs:master.

@kasongoyo
Copy link
Contributor Author

Hey @tswaters, I wish to add my changes to the doc but I want to hear your opinions first about the changes I have made, remember you have not replied in the discussion we had in the issues section

Copy link
Member

@tswaters tswaters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turf .vscode directory. If need be, add it to .gitignore.

@@ -2,7 +2,7 @@

var _ = require('lodash')

module.exports = function log (options, context, auth, routes, done) {
module.exports = function log (options, context, auth, middleware, routes, done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change the api contract between seneca-web and adapters. Remove middleware parameter here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted

lib/mapper.js Outdated
@@ -66,6 +66,12 @@ module.exports = function mapper (routePlan) {
route.autoreply = false
}

// If there are middleware then set it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above this section, if routeSet.middleware has been provided, attach it to defaultRoute similar to how it is set below.

web.js Outdated
@@ -9,6 +9,7 @@ var opts = {
context: null,
adapter: LogAdapter,
auth: null,
middleware: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this option to the options key.

web.js Outdated
// they aren't stringify friendly
opts = extend(opts, _.omit(options, ['context', 'auth']))
// Avoid deepextending context, middleware and auth, they aren't stringify friendly
opts = extend(opts, _.omit(options, ['context', 'auth', 'middleware']))
Copy link
Member

@tswaters tswaters Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove middleware from here. extending functions should be fine. To be honest, I'm not sure why auth is here, I think that might be a HAPI thing that requires a complicated object.... Context definitely shouldn't be extended as this could be the entire web framework, and would be slow (also would likely blows up with circular references in koa1/koa2).

web.js Outdated
opts.context = options.context || null
opts.auth = options.auth || null
opts.middleware = options.middleware || null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to something like....

if (options.middleware) {
  opts.options.middleware = options.middleware
}

this would allow for shorthand providing options at the root object instead of inside options.

web.js Outdated
adapter.call(seneca, options, context, auth, routes, done)
// Call the adaptor with the mapped routes, context to apply them to and
// instance of seneca and the provided consumer callback.
adapter.call(seneca, options, context, auth, middleware, routes, done)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the middleware parameter.

web.js Outdated
locals = {
context: context,
adapter: adapter,
auth: auth,
middleware: middleware,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this.

web.js Outdated
function init (msg, done) {
var config = {
context: opts.context,
adapter: opts.adapter,
routes: opts.routes,
auth: opts.auth,
middleware: opts.middleware,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this.

Copy link
Member

@tswaters tswaters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to need a docs page... if you can try your best to at least get a start at writing this. I'll probably merge regardless of what is there and fix it up if it needs fixing.

@tswaters
Copy link
Member

Just noticed that web.test.js is completely devoid of actual tests. With some of the changes I've requested, there is another branch, and coverage will decrease... I'm not sure if it'll be enough to break the build, if it does don't worry about that -- I'll likely fix it after merge.

Also, don't be afraid to add yourself to contributers key in package.json.

Copy link
Member

@tswaters tswaters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just went through a few of these to add support - pretty straight forward for the most part. Only weird one was connect becuase it doesn't have a simple api for an array of middleware so I had to get creative.

Got up to hapi and I realized how we assign route.middleware might need some work.

In hapi, there is no concept of middleware. There is a similar concept of pre requests. (you might need to search for "route.options.pre", as their anchor linking seems broken atm).

This pre array looks different than middleware in that you can nested middleware differently. I think we should be OK passing middleware directly as pre, except for one problem -- right now we are using _.flatten([middleware]) to coerce it to an array. This has the side effect of flattening the array - and there's an expectation that hapi should support nested arrays.

It may be that we replace this with --

route.middleware = _.isArray(value.middleware) ? value.middleware : [value.middleware]

@coveralls
Copy link

coveralls commented Nov 28, 2017

Coverage Status

Coverage decreased (-0.6%) to 90.943% when pulling 0b8ad2e on kasongoyo:master into fdbbd69 on senecajs:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.6%) to 90.943% when pulling 0b8ad2e on kasongoyo:master into fdbbd69 on senecajs:master.

@tswaters
Copy link
Member

This looks pretty close.... I'm going to close this PR, but merge your branch into feature/middleware and fix up the readme with a few straggling issues before releasing this.

Once this is done, we need to get the web-adapters updated... as mentioned above I've already done a few of them and I see you started with the seneca-web adapter... if you want to start on that, feel free.

@tswaters
Copy link
Member

Thanks you @kasongoyo for all your hard work! I'm going to close this & move it to #130

Going to sit on this for a few days probably, come back and take a fresh look & hopefully get a new release out on the weekend sometime.

@tswaters tswaters closed this Nov 29, 2017
@kasongoyo
Copy link
Contributor Author

Thanks @tswaters for your assistance. Thanks more for improving docs regarding middleware feature. The doc is real nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants