Skip to content

feat: add middleware field to App interface - #326

Open
vkumar-sonata wants to merge 1 commit into
openedx:mainfrom
vkumar-sonata:add-middleware-field-to-app-interface
Open

vkumar-sonata wants to merge 1 commit into
openedx:mainfrom
vkumar-sonata:add-middleware-field-to-app-interface

Conversation

@vkumar-sonata

Copy link
Copy Markdown
Contributor

Add an optional middleware field to App interface, following the same per-app accumulation pattern established in ADR 0014 for externalScripts.

The shell collects middleware entries from every registered App and passes the accumulated array as authMiddleware to initialize(). No changes to the auth service are required.

Why this is needed:
The authMiddleware option in initialize() is only reachable from the shell's bootstrap call. Individual apps have no path to contribute request interceptors they own - data they wrote in externalScripts, values from their own App.config - without patching the shell directly. The middleware field closes this gap.

See [ADR 0018[(docs/decisions/0018-per-app-http-middleware.rst) for full rationale, rejected alternatives and consequences.

Changes

  • docs/decisions/0018-per-app-http-middleware.rst - new ADR
  • types.ts - add middleware?: RequestMiddleware[] to App
  • runtime/scripts/index.ts - accumulate middleware
  • runtime/initialize.js - pass the accumulated middleware to initialize()

References

  • ADR 0006: [docs/decisions/0006-remove-eslint-config-dependency.md](https://github.com/openedx/frontend-base/blob/main/docs/decisions/0006-remove-eslint-config-dependency.md)
  • ADR 0014: [docs/decisions/0014-per-app-external-scripts.rst](https://github.com/openedx/frontend-base/blob/main/docs/decisions/0014-per-app-external-scripts.rst)

@vkumar-sonata

Copy link
Copy Markdown
Contributor Author

@arbrandes Following the discussion here https://discuss.openedx.org/t/rfc-platform-level-utm-attribution-support-for-authenticated-mfe-users/19182/5, I raised this PR. Would you please kindly review the same and let me know if you have any feedback for me?

@arbrandes arbrandes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks: this is what I asked for in the forums. Having read the ADR against the code, though, I don't think we need the field yet: the UTM app can be built today on what's already there, and a new field on App is a contract we keep forever.

More details inline.

Comment on lines +31 to +37
HTTP request middleware has exactly the same shape: an ``App`` may need to
attach a request interceptor that reads data it owns — a cookie it wrote in
``externalScripts``, a value from its own ``App.config`` — and forwards it
as a request header on every authenticated call. Without a ``middleware``
field on ``App``, the only alternatives are to reach into auth service
internals directly (breaking encapsulation) or to require the shell to
special-case the app (coupling the shell to app-level concerns).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The ADR says the only alternatives are reaching into auth service internals or having the shell special-case the app. There is a third one. A loader in externalScripts runs at boot with the app's config, and from there it can fetch the HTTP clients through the public getters and add its own interceptor:

class UtmLoader {
  // ...
  loadScript() {
    captureUtmParams(this.cookieName);
    getAuthenticatedHttpClient().interceptors.request.use(addUtmHeader);
  }
}

What the field would add on top is a tidier spelling, and coverage of the handful of requests made during startup (user fetch, token refresh). Neither matters for UTM attribution.

So, what I'd like next: build a frontend-app-utm-attribution this way, and come back to this ADR with whatever it couldn't do. If the answer is "nothing", we've saved ourselves a contract. If there is something, the ADR's context section can say exactly what, which is a much stronger case than the one it makes now.

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.

2 participants