swagger-node icon indicating copy to clipboard operation
swagger-node copied to clipboard

Document preflight configuration.

Open amaurer opened this issue 7 years ago • 1 comments

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.

amaurer avatar Jun 11 '18 15:06 amaurer

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();

  };


};

amaurer avatar Jun 11 '18 16:06 amaurer