To rewrite your interceptor using Angular's standard interceptor class (instead of the functional interceptor HttpInterceptorFn), you'll need to implement the HttpInterceptor interface from @angular/common/http.
Here's how you can rewrite your interceptor in a class-based style , which is more common and aligns with Angular's standard approach:
✅ Updated Interceptor Using Angular Standard Class Style
ts Copy code
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { UserStore } from '../store/user.store';
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
constructor(private userStore: UserStore) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const isTransaction = this.isTransactionRequest(req);
if (isTransaction) {
const loggedInUserName = this.userStore.loggedInUserName();
const clonedReq = req.clone({
headers: req.headers
.set('x-user-name', loggedInUserName)
});
return next.handle(clonedReq);
}
return next.handle(req);
}
private isTransactionRequest(req: HttpRequest<unknown>): boolean {
const { method, url } = req;
return !url.includes('/token.oauth') && ['POST', 'PUT', 'DELETE'].includes(method as string);
}
}
📦 Don't forget to provide it
In your app.module.ts, register the interceptor in the providers array:
ts Copy code
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { RequestInterceptor } from './interceptors/request.interceptor';
@NgModule({
...
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true
}
]
})
export class AppModule {}
✅ Summary
You're now using Angular's class-based interceptor (HttpInterceptor).
The logic remains the same:
Skip OAuth token requests.
For POST/PUT/DELETE requests, add an x-user-name header with the logged-in username.
This format is compatible with Angular versions that support both standalone APIs and module-based setups.
Let me know if you want to migrate this into a standalone setup using provideHttpClient!