Repository navigation
feat: add middleware field to App interface - #326
vkumar-sonata wants to merge 1 commit into
Conversation
|
@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
left a comment
There was a problem hiding this comment.
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.
| 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). |
There was a problem hiding this comment.
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.
Add an optional
middlewarefield toAppinterface, following the same per-app accumulation pattern established in ADR 0014 forexternalScripts.The shell collects
middlewareentries from every registeredAppand passes the accumulated array asauthMiddlewaretoinitialize(). No changes to the auth service are required.Why this is needed:
The
authMiddlewareoption ininitialize()is only reachable from the shell's bootstrap call. Individual apps have no path to contribute request interceptors they own - data they wrote inexternalScripts, values from their ownApp.config- without patching the shell directly. Themiddlewarefield 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 ADRtypes.ts- addmiddleware?: RequestMiddleware[]toAppruntime/scripts/index.ts- accumulate middlewareruntime/initialize.js- pass the accumulated middleware toinitialize()References
[docs/decisions/0006-remove-eslint-config-dependency.md](https://github.com/openedx/frontend-base/blob/main/docs/decisions/0006-remove-eslint-config-dependency.md)[docs/decisions/0014-per-app-external-scripts.rst](https://github.com/openedx/frontend-base/blob/main/docs/decisions/0014-per-app-external-scripts.rst)