How am I intended to use handler and dataProvider in swaggerize-express? How am I intended to use handler and dataProvider in swaggerize-express? express express

How am I intended to use handler and dataProvider in swaggerize-express?


I'm confused as well. Did you find the solution? I found out that I can use the provided callback to send a response in the positive case (i.e. when the data base item is found).

var id = req.params.id;// item = ... get your data base item via id ...callback(null, { responses: item });

I'm still wondering though which parameters to pass to callback in the negative case. I tried to call callback(404) indicating that I want the server to give a "Resource Not Found" response. This, however, results in a HTTP-500 Error.


I wanted to share how I decided to structure my application. However, I'd love to hear if you found a more effective way to do so. Here are the basic concepts I had in mind:

  • handler - Should manage all route related logic, meaning parameter and attribute validation and routing to the correct dataProvider
  • dataProvider - Should manage interactions with the model layer / database. Passing control back to the handler if it is ultimately not the correct dataProvider.

handler/user/{id}.js

get: function getUser(req, res, next) {  var status = 200;  var provider = dataProvider['get']['200'];  // Check if params.id is a valid integer  // If not, get response from dataProvider['get']['400']  if (isNaN(req.params.id)) {    status = 400;    provider = dataProvider['get']['400'];    provider(req, res, function(err, data) {      if (err) {        return next(err);      }      res.status(status).send(data && data.responses)    });    return;  }  provider(req, res, function(err, data) {    if (err) {      return next(err);    }    // Check if user with id exists    // If not, get response from dataProvider['get']['404']    if (!data) {      status = 404;      provider = dataProvider['get']['404'];      provider(req, res, function(err, data) {        if (err) {          return next(err);        }        res.status(status).send(data && data.responses)      });      return;    }    res.status(status).send(data && data.responses);  });}

data/user/{id}.js

get: {  200: function(req, res, callback) {    User.find({      where: {        id: req.params.id      }    }).then(function(object) {      if (!object) {        return callback(null, null);      }      callback(null, {        responses: object.toJSON()      });    });  },  400: function(req, res, callback) {    callback(null, {      responses: ERR.USER.INVALIDID    });  },  404: function(req, res, callback) {    callback(null, {      responses: ERR.USER.NOTFOUND    });  }},

I think it looks like how it was designed to separate the responsibilities nicely. I'm not sure if this pattern is taken from another framework/language, but I couldn't find much online to support this. What do you think?