Route requests through local proxy with CORS #95
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Because VSCode has no intention of changing their stance on required CORS headers to work in webviews microsoft/vscode#72900 -- the only workaround I see is using a proxy service like http://www.whateverorigin.org/ which just proxies requests and adds CORS headers.
Fortunately this app is in a unique place in that it's already running a webserver. So we can use that same server to proxy requests and add CORS. This is pretty easy with SwaggerUIBundle's
requestInterceptor
config argument which takes a function for altering requests.Thus this PR does two things:
/proxy
endpoint to the server which takes a query paramurl
with the actual URL. userequest
library to forward the entire request object to that url. Add CORS to the response and return it.requestInterceptor
argument toSwaggerUIBundle
to change the url to use the backend's/proxy?url=...
The result -- no need to worry about CORS, the UI works the same.
Fixes #30
Fixes #23
Edit: Since this has been here for a while; if anyone is interested in using this now it can be done with the release on my fork where I have included the compiled vsix file: https://github.com/MaayanLab/vs-swagger-viewer/releases if you do use it, let me know if you find any issues.
Edit: Updated a bit to fix an issue with POST requests
request
as a dependency, using insteadhttp
andhttps
url
which may also cause conflicts, we alwaysPOST
to/proxy
serializing the url, method, headers, and body with jsonEdit: A bug in the last fix broke querystrings 🤦 this was fixed.