vs-rest-api
vs-rest-api copied to clipboard
Support for OPTIONS request!?
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?
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.