HTTP-интерцепторы (interceptors) — мощный механизм для перехвата и обработки HTTP-запросов и ответов. Рассмотрим их создание и применение.
Создадим простейший интерцептор, который логирует все запросы:
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class LoggingInterceptor implements HttpInterceptor {
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
console.log(`Запрос к URL: ${request.url}`);
return next.handle(request);
}
}
Интерцепторы регистрируются в основном модуле приложения:
import { HTTP_INTERCEPTORS } from '@angular/common/http';
@NgModule({
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: LoggingInterceptor,
multi: true // важно для нескольких интерцепторов
}
]
})
export class AppModule {}
intercept(request: HttpRequest<any>, next: HttpHandler) {
const authReq = request.clone({
headers: request.headers.set('Authorization', 'Bearer ' + this.auth.getToken())
});
return next.handle(authReq);
}
intercept(request: HttpRequest<any>, next: HttpHandler) {
return next.handle(request).pipe(
catchError(error => {
if (error.status === 401) {
this.auth.logout();
}
return throwError(error);
})
);
}
intercept(request: HttpRequest<any>, next: HttpHandler) {
const apiReq = request.clone({
url: `https://api.example.com/${request.url}`
});
return next.handle(apiReq);
}
private cache = new Map<string, any>();
intercept(request: HttpRequest<any>, next: HttpHandler) {
if (request.method !== 'GET') {
return next.handle(request);
}
const cachedResponse = this.cache.get(request.url);
if (cachedResponse) {
return of(cachedResponse);
}
return next.handle(request).pipe(
tap(event => {
if (event instanceof HttpResponse) {
this.cache.set(request.url, event);
}
})
);
}
request.clone()
)intercept(request: HttpRequest<any>, next: HttpHandler) {
return next.handle(request).pipe(
map(event => {
if (event instanceof HttpResponse) {
return event.clone({ body: this.modifyBody(event.body) });
}
return event;
})
);
}
intercept(request: HttpRequest<any>, next: HttpHandler) {
this.loadingService.show();
return next.handle(request).pipe(
finalize(() => this.loadingService.hide())
);
}
intercept(request: HttpRequest<any>, next: HttpHandler) {
return next.handle(request).pipe(
retryWhen(errors => errors.pipe(
delay(1000),
take(3)
)
);
}
HTTP-интерцепторы в Angular позволяют:
Пример комплексного интерцептора:
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private auth: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
// Пропускаем запросы авторизации
if (req.url.includes('/auth')) {
return next.handle(req);
}
const authReq = req.clone({
headers: req.headers.set('Authorization', `Bearer ${this.auth.getToken()}`),
url: environment.apiUrl + req.url
});
return next.handle(authReq).pipe(
catchError(err => {
if (err.status === 401) {
this.auth.logout();
}
return throwError(err);
}),
tap(event => {
if (event instanceof HttpResponse) {
console.log(`Ответ от ${req.url}`, event);
}
})
);
}
}