-
Notifications
You must be signed in to change notification settings - Fork 38
Use Fetch API instead of XMLHttpRequest #91
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
Conversation
2254da3 to
c01ff8c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the adapter’s network interactions by replacing all XMLHttpRequest calls with the Fetch API, improving maintainability and aligning with modern asynchronous patterns. It also enhances test coverage and supplements the documentation for these changes.
- Replaces XHR calls with promise-based Fetch API calls in key adapter functions.
- Updates and adds tests to validate proper error messaging and functionality for user profile, user info, and login flows.
- Adjusts configuration instantiation and adds support for dynamic adapter configuration.
Reviewed Changes
Copilot reviewed 9 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/tests/*.spec.ts | Updates tests with improved error messages and additional profile checks. |
| test/support/test-executor.ts | Adds new executor methods for handling profile and user info. |
| test/app/app.ts | Introduces an Express-based test server and removes legacy JS server. |
| lib/keycloak.js | Refactors network-related code from XHR to Fetch API, with promise handling. |
| .github/workflows/main.yml | Updates Node.js version for enhanced TypeScript support. |
Files not reviewed (2)
- docs/guides/securing-apps/javascript-adapter.adoc: Language not supported
- test/package.json: Language not supported
Comments suppressed due to low confidence (2)
lib/keycloak.js:652
- The reference to 'req.status' in the token refresh catch block is invalid since the code now uses fetch. Consider removing this check or replacing it with a validation based on the fetch response.
if (req.status === 400) {
lib/keycloak.js:568
- [nitpick] The error message could be revised for improved clarity (e.g., 'make sure the adapter is not configured using a generic OIDC provider') provided that tests are updated accordingly.
throw new Error('Unable to load user profile, make sure the adapter not is configured using a generic OIDC provider.');
Closes keycloak#87 Signed-off-by: Jon Koops <jonkoops@gmail.com>
rmartinc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jonkoops!
Just a little typo, but it can be merged if there are no more requests for changes.
| } | ||
| } | ||
| if (!realmUrl) { | ||
| throw new Error('Unable to load user profile, make sure the adapter not is configured using a generic OIDC provider.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| throw new Error('Unable to load user profile, make sure the adapter not is configured using a generic OIDC provider.'); | |
| throw new Error('Unable to load user profile, make sure the adapter is not configured using a generic OIDC provider.'); |
| await executor.submitLoginForm() | ||
| await executor.instantiateAdapter(configOptions) | ||
| await executor.initializeAdapter(initOptions) | ||
| await expect(executor.loadUserProfile()).rejects.toThrow('Unable to load user profile, make sure the adapter not is configured using a generic OIDC provider.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| await expect(executor.loadUserProfile()).rejects.toThrow('Unable to load user profile, make sure the adapter not is configured using a generic OIDC provider.') | |
| await expect(executor.loadUserProfile()).rejects.toThrow('Unable to load user profile, make sure the adapter is not configured using a generic OIDC provider.') |
|
Thanks @rmartinc, looks like the auto-merge already happened, but I will fix your comments in a follow up. |
This PR is the first step in refactoring the internals so that we can get rid of the callback nightmare that is preventing the library from being maintainable. It replaces all the
XMLHttpRequestcalls with the Fetch API, so that we can use promises instead, allowing forasync/awaiteverywhere in later refactors.It also improves test coverage for the code that was touched, as well as supplementing some documentation related to these features.
Closes #87