Question presented to candidate: "Every request our app sends needs an auth header, and we want one log line per call. Show me how you would do that with a functional interceptor in a standalone Angular app, and where exactly you register it."
What a strong answer should cover:
- A functional interceptor is a plain function of type
HttpInterceptorFn:(req, next) => Observable<HttpEvent>. No class, noimplements. - Requests are immutable: change them with
req.clone({ ... })and pass the clone tonext(). - It runs in an injection context, so
inject(AuthStore)works at the top of the function body (but not later inside an RxJS callback). - Registration:
provideHttpClient(withInterceptors([a, b, c]))inappConfig(or a lazy route's providers); the array order is the run order for requests. - Returning without calling
next()short-circuits the chain (fakes, caches, mocks). - Modern facts: functional interceptors arrived with the standalone HTTP APIs in v15; in v22
HttpClientis injectable even withoutprovideHttpClient()and uses the fetch backend by default.
Clarifying questions expected:
- "Should every request get the header, or only calls to our own API origin?"
- "Is this a fully standalone app, or are there NgModule-era class interceptors we need to keep?"
Code / implementation expected: Yes β an auth interceptor that clones the request, a logging interceptor, and the appConfig that registers them.