Skip to content
This repository was archived by the owner on Sep 14, 2022. It is now read-only.

Document preflight configuration. #563

Open
amaurer opened this issue Jun 11, 2018 · 1 comment
Open

Document preflight configuration. #563

amaurer opened this issue Jun 11, 2018 · 1 comment

Comments

@amaurer
Copy link

amaurer commented Jun 11, 2018

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
Copy link
Author

amaurer commented Jun 11, 2018

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

  };


};

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant