feathersjs-ecosystem / feathers-swagger

Add documentation to your FeatherJS services and feed them to Swagger UI.
MIT License
225 stars 62 forks source link

discussion: introspect services for pre and post hooks, and other routes #81

Closed hdngr closed 5 years ago

hdngr commented 7 years ago
  1. Should we automate documentation of pre and post hooks? All hooks, or just often used hooks

  2. Should we introspect services (or even the underlying app) for routes not defined in the service itself?

    # For example
    app.use('/users', service);
    app.service('users')
    .hooks(hooks);
    app.post('/users/password', handlePasswordReset);

    @daffl - https://feathersjs.slack.com/archives/C52QSFK0A/p1507333034000102

I think the conversation is worth having. Maybe we decide that automation/automated introspection is not the answer, and maybe just more robust documentation on the best way to use swagger to document hook like functionality.

Thanks for all of the great work!!

daffl commented 7 years ago

I like the idea you mentioned on Slack of parsing JSDoc style comments to generate the documentation. Some questions to investigate are:

hdngr commented 7 years ago
hdngr commented 7 years ago

I would think that at the least, we would introspect for the following:

Other considerations?

One use case:

A package like feathers-mongoose is used for a messages service. feathers-mongoose could include es doc of the primary methods. All that would be left would be for the user's hooks, and filters to be documented. e.g. before create, restrict to owner, id field: '_id', owner field: 'from'

What other thoughts?

hdngr commented 7 years ago

@daffl did a little bit of research here... introspecting hooks could work. Changing one of the feathers conventions to have hooks return named functions as would be one way - e.g.

function someAwesomeHook () {
     return function() {...}
}

Would become

function someAwesomeHook () {
     return function someAwesomeNameForDocs(hook) {...}
}

This is may not be intuitive/obvious.

Another approach would be to wrap the hook

app.service('coolService').hooks({
  before: app.providers.esdoc(auth.hooks.authenticate('jwt'))
})

With this approach, maybe the common hooks give obvious documentation signals.

daffl commented 7 years ago

Custom hooks could still export their own docs I think. Something like

const document = require('feathers-api-doc');

/**
 * A hook method
 */
module.exports = function hookMethod(method) {

}

module.exports.docs = document(__dirname);

Basically anything that has a .docs property (services etc.) can be used by Swagger.

hdngr commented 7 years ago

totally - and what is the purpose of passing __dirname? What are we looking for in the module/service's directory?

daffl commented 7 years ago

Oops, that should've been __filename to parse the documentation from our file and export it with the function.

hdngr commented 7 years ago

oh - got it. So, document would need to be smart enough to know what was going on in every file where document(__dirname)? Hooks, filters, on (events), etc? Or, would it be configured?

Mairu commented 5 years ago

Any custom generation of docs should by handled by independet packages. The scope of this package is more to consume the docs.