feat: add options and helper function to enable cors#19
Open
jneums wants to merge 2 commits intokrpeacock:mainfrom
Open
feat: add options and helper function to enable cors#19jneums wants to merge 2 commits intokrpeacock:mainfrom
jneums wants to merge 2 commits intokrpeacock:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request introduces Cross-Origin Resource Sharing (CORS) support. It also includes documentation and testing for the new functionality.
According to this thread on the developer forum, options headers should now be forwarded correctly to the canister, enabling the ability to support CORS.
Currently, browsers block frontend applications served from a different origin (e.g.,
localhost:5173) from makingfetchrequests to a canister API (*.localhost:4943). This PR resolves the issue by implementing the standard CORS preflight (OPTIONS) and response header mechanism.Changes Made
Server.moLibrary Updates:optionsRequestsmap and a publicserver.options()method to allow for the registration ofOPTIONSroute handlers.server.enableCors(origin, methods, headers)helper function. This function configures the server to automatically handle preflightOPTIONSrequests and add the necessaryAccess-Control-Allow-Originheader to all API responses.Access-Control-Allow-Originheaders from being sent in preflight responses.Documentation (
README.md):server.enableCors()helper function with a clear usage example.server.options()method.Testing (
test/test.spec.js):OPTIONSpreflight response, ensuring theAccess-Control-Allow-Originheader is present on subsequent requests, and confirming that the duplicate header bug is fixed.Test Canister (
test/main.mo):server.enableCors()./post-testendpoint to facilitate testing of preflight requests forPOSTmethods.How to Test
npm test.Checklist
.options()and.enableCors()methods.