Skip to content

Conversation

@jonkoops
Copy link
Contributor

@jonkoops jonkoops commented May 2, 2025

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 XMLHttpRequest calls with the Fetch API, so that we can use promises instead, allowing for async/await everywhere 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

@jonkoops jonkoops requested review from a team and Copilot May 2, 2025 17:08

This comment was marked as outdated.

@jonkoops jonkoops force-pushed the use-fetch-api branch 2 times, most recently from 2254da3 to c01ff8c Compare May 4, 2025 11:37
@jonkoops jonkoops requested a review from Copilot May 4, 2025 13:42
Copy link

Copilot AI left a 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>
@jonkoops jonkoops enabled auto-merge (squash) May 5, 2025 09:44
Copy link
Contributor

@rmartinc rmartinc left a 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.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.')

@jonkoops jonkoops merged commit df9b272 into keycloak:main May 5, 2025
4 checks passed
@jonkoops
Copy link
Contributor Author

jonkoops commented May 5, 2025

Thanks @rmartinc, looks like the auto-merge already happened, but I will fix your comments in a follow up.

@jonkoops jonkoops deleted the use-fetch-api branch May 5, 2025 14:37
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

Successfully merging this pull request may close these issues.

Use Fetch API instead of legacy XMLHttpRequest API

2 participants