swagger-node
swagger-node copied to clipboard
Document preflight configuration.
Is there any documentation regarding CORS preflight requests? Seems like these should automatically be configured based on the swagger definition.
I'm looking to return the verbs defined in swagger for my resources (paths) but I can't even find how to control "preFlightContinue" option.
Thanks in advance.
I just ended up creating a fitting to handling the verbs based off of swagger definition
const _ = require('lodash');
const verbWhitelist = ["get", "post"];
// Used for CORS preflight options by path.
module.exports = function create(fittingDef, bagpipes) {
return function corsVerbs(context, cb) {
// If not options, go to next controller
if (context.request.method !== 'OPTIONS') return cb(null, context);
let verbs = _.pick(context.request.swagger.path, verbWhitelist);
let responseVerbs = Object.keys(verbs).map(function (x) { return x.toUpperCase() });
if (responseVerbs.indexOf('OPTIONS') === -1) responseVerbs.push('OPTIONS');
context.response.set('Access-Control-Allow-Methods', responseVerbs.join(','))
context.response.status(204).send();
};
};