Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for OPTIONS request!? #3

Open
schlundus opened this issue Apr 29, 2019 · 1 comment
Open

Support for OPTIONS request!? #3

schlundus opened this issue Apr 29, 2019 · 1 comment

Comments

@schlundus
Copy link

During requests from browser (chrome) i've got som CORS related errors, because of OPTIONS requests issued by the browser, so some methods cannot be used (like workspace PATCH)....
Are browsers supported / tested?

@elisherer
Copy link

elisherer commented May 27, 2020

I would also like to see this implemented

edit:

Workaround

/.vscode/settings.json

{
    "rest.api": {
        // ...
        "guest": {
            "canAnything": true // to pass OPTIONS calls
        },
        "customOnly": true,
        "endpoints": {
            "workspace": {
                "script": "./vs-code/rest-api-cors.js"
            }
        }
    }
}

/.vscode/rest-api-cors.js

const cors = args => {
  Object.assign(args.headers, {
    ["Access-Control-Allow-Origin"]: args.request.request.headers.origin,
    ["Access-Control-Allow-Headers"]: "authorization, Content-Type",
    ["Access-Control-Allow-Methods"]: "GET,POST,PATCH,PUT,DELETE,OPTIONS",
    ["Access-Control-Allow-Credentials"]: "true"
  });
};

const workspace = (args, method) => args.require('./api/workspace')[method](args).then(err => {
  if (err) {
    args.setContent(JSON.stringify(err), "application/json");
  }
  return {};
});

exports.OPTIONS = cors;
exports.GET = args => cors(args) || workspace(args, 'GET');
exports.PUT = args => cors(args) || workspace(args, 'PUT');

You can change the workspace function to detect which module to load based on the URL and make it generic.

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

No branches or pull requests

2 participants