angular-http-client
Installation
SKILL.md
HTTP Client
Priority: P1 (HIGH)
Principles
- Functional Interceptors: Use HttpInterceptorFn (e.g.,
(req, next) => next(req.clone({ setHeaders: { Authorization: token } }))). Clone requests withreq.clone(— class-based interceptors deprecated. Register via withInterceptors([...]) in provideHttpClient. - Typed Responses: Always type
http.post<T>(),http.get<T>(). Useinject(HttpClient)in services (not constructor injection). Add provideHttpClient(withInterceptors([...]), withFetch()) toapp.config.ts. - Services: Encapsulate all HTTP calls in Services. Never call
httpin Components.
Signal-Based HTTP (Angular 17+)
Prefer httpResource() over manual subscribe for reactive data loading — it auto-refetches when its signal inputs change:
// Reactive: refetches automatically when userId() changes
userResource = httpResource<User>(() => `/api/users/${this.userId()}`);
// States: .isLoading() | .hasValue() | .error() | .value() | .reload()