Handle internet connection lost
#angular #interceptor
β Link: https://medium.com/@ivandztitan/handle-internet-connection-lost-and-server-issues-with-angular-interceptor-d5d00fa78ce6
#angular #interceptor
β Link: https://medium.com/@ivandztitan/handle-internet-connection-lost-and-server-issues-with-angular-interceptor-d5d00fa78ce6
π2
Cache API calls in Angular using Interceptor
#angular #interceptor
β Link: https://medium.com/@emadhassankhan/cache-api-calls-in-angular-using-interceptor-cc62e5d0d2c4
#angular #interceptor
β Link: https://medium.com/@emadhassankhan/cache-api-calls-in-angular-using-interceptor-cc62e5d0d2c4
Interceptors in A15
#angular #interceptor
π© > Angular 15
β Link: https://blog.herodevs.com/angular-15-introduces-functional-http-interceptors-59299cce60bf
#angular #interceptor
π© > Angular 15
β Link: https://blog.herodevs.com/angular-15-introduces-functional-http-interceptors-59299cce60bf
β³οΈ Angular function based interceptor
#angular #interceptor
π©> Angular 16
β Article Link: https://itnext.io/migrate-angular-interceptors-to-function-based-interceptors-90bd433e0c2a
π Code Link: https://stackblitz.com/edit/angular-gd6vru?file=src%2Fretry.interceptor.ts
#angular #interceptor
π©> Angular 16
β Article Link: https://itnext.io/migrate-angular-interceptors-to-function-based-interceptors-90bd433e0c2a
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
π2
π Managing Concurrent HTTP Requests with Interceptors
#angular #interceptor
β Article link: https://medium.com/@gurunadhpukkalla/optimizing-angular-apps-managing-concurrent-http-requests-with-interceptors-queue-based-b4a9e0319409
#angular #interceptor
β Article link: https://medium.com/@gurunadhpukkalla/optimizing-angular-apps-managing-concurrent-http-requests-with-interceptors-queue-based-b4a9e0319409
π 8 Problems Solved by Interceptors in Angular
#angular #interceptor
β Article link: https://itnext.io/8-daily-problems-solved-by-interceptors-in-angular-59c8ce1e1074
#angular #interceptor
β Article link: https://itnext.io/8-daily-problems-solved-by-interceptors-in-angular-59c8ce1e1074
π How to Rewrite a Interceptor into a Functional Interceptor in Angular
#angular #interceptor
β Article link: https://medium.com/@seanhaddock_60973/how-to-create-a-functional-interceptor-in-angular-c2302cc67a90
#angular #interceptor
β Article link: https://medium.com/@seanhaddock_60973/how-to-create-a-functional-interceptor-in-angular-c2302cc67a90
π2
π Angular Interceptors: Authentication Interceptor
#angular #interceptor
#angular #interceptor
An authentication interceptor is used to add authentication tokens to outgoing requests and handle authentication-related errors. This is essential for securing your applicationβs API requests.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler) {
const authToken = 'your-auth-token';
const authRequest = request.clone({
headers: request.headers.set('Authorization', `Bearer ${authToken}`),
});
return next.handle(authRequest);
}
}π₯2
π Angular Interceptors: Error Handling Interceptor
#angular #interceptor
#angular #interceptor
An error handling interceptor can be used to centralize error handling for HTTP requests. It can capture HTTP errors, log them, and perform appropriate actions like displaying error messages.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpErrorResponse,
} from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler) {
return next.handle(request).pipe(
catchError((error: HttpErrorResponse) => {
// Handle and log the error here
console.error('HTTP Error:', error);
// Optionally rethrow the error to propagate it
return throwError(error);
})
);
}
}π₯2
π Angular Interceptors: Logging Interceptor
#angular #interceptor
#angular #interceptor
A logging interceptor can be used to log the details of HTTP requests and responses, which is helpful for debugging and monitoring.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpResponse,
} from '@angular/common/http';
import { tap } from 'rxjs/operators';
@Injectable()
export class LoggingInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler) {
return next.handle(request).pipe(
tap((event) => {
if (event instanceof HttpResponse) {
console.log('HTTP Response:', event);
}
})
);
}
}π Angular Interceptors: Caching Interceptor
#angular #interceptor
#angular #interceptor
A caching interceptor can be used to implement client-side caching for HTTP responses, reducing the number of unnecessary network requests.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpResponse,
HttpHeaders,
} from '@angular/common/http';
import { tap } from 'rxjs/operators';
@Injectable()
export class CacheInterceptor implements HttpInterceptor {
private cache = new Map<string, HttpResponse<unknown>>();
intercept(
request: HttpRequest<unknown>,
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);
}
})
);
}
}π₯1
π Angular Interceptors: Headers Interceptor
#angular #interceptor
#angular #interceptor
A headers interceptor can be used to add custom headers to outgoing HTTP requests. This is often used to set headers like βContent-Typeβ or include API keys.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';
@Injectable()
export class HeadersInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<unknown>, next: HttpHandler) {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'X-API-Key': 'your-api-key',
});
const headersRequest = request.clone({ headers });
return next.handle(headersRequest);
}
}π₯1
π Angular Interceptors: Loading Indicator Interceptor
#angular #interceptor
#angular #interceptor
A loading indicator interceptor can be used to show and hide loading spinners or progress bars during HTTP requests, providing a better user experience.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';
import { LoadingService } from './loading.service';
import { finalize } from 'rxjs/operators';
@Injectable()
export class LoadingInterceptor implements HttpInterceptor {
constructor(
private loadingService: LoadingService
) {}
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
) {
this.loadingService.showLoading();
return next.handle(request).pipe(
finalize(() => {
this.loadingService.hideLoading();
})
);
}
}π1π₯1
π Angular Interceptors: Timeout Interceptor
#angular #interceptor
#angular #interceptor
A timeout interceptor can be used to set a maximum timeout for HTTP requests. It can be useful to prevent long-running requests from blocking your application.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';
import { throwError, timer, Observable } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';
@Injectable()
export class TimeoutInterceptor implements HttpInterceptor {
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
): Observable<any> {
const timeoutDuration = 10000; // 10 seconds
return next.handle(request).pipe(
timeout(timeoutDuration),
catchError((error) => {
if (error.name === 'TimeoutError') {
// Handle timeout error here
console.error(
'Request timed out:', request.url
);
return throwError('Request timed out');
}
return throwError(error);
})
);
}
}π Angular Interceptors: Base URL Interceptor
#angular #interceptor
#angular #interceptor
A base URL interceptor can be used to prepend a base URL to all HTTP requests, simplifying the configuration of API endpoints.
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';
@Injectable()
export class BaseUrlInterceptor implements HttpInterceptor {
private baseUrl = 'https://api.example.com';
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
) {
const apiRequest = request.clone({
url: `${this.baseUrl}${request.url}`,
});
return next.handle(apiRequest);
}
}