mulesoft-labs / osprey-resources

Automatically support RAML resources
Other
2 stars 7 forks source link

Possibility to add middleware(s) before the function that will generate the route for that path? #5

Closed jy95 closed 6 years ago

jy95 commented 7 years ago

Hello,

I just wonder if there is not a possibility to add middleware(s) just before the handler function ...

I tried to mix the 2 osprey middlewares : https://github.com/mulesoft-labs/osprey-resources and https://github.com/mulesoft-labs/osprey-method-handler . It doesn't seem to work together ..

I was thinking about something like that :

var express = require('express');
var resources = require('osprey-resources');
var app = express();
var handler = require('osprey-method-handler')
var connect = require('connect');

var router = resources(
  [{
    relativeUri: '/users',
    methods: [{
      method: 'post',
      body: {
        'application/json': {
          schema: '...'
        }
      }
    }]
  }],
function (method, path) {
      // the middleware(s)
      var chain = connect();
      var methodType = method.method.toUpperCase();
      chain.use(handler(method, path, methodType, {}))
      return chain;
 },
  function (method, path) {
    return function (req, res, next) {
      // the route
      res.end('hello, world!')
    }
  }
);

app.use('/v1' , router);

So, any idea ?