Advertisement
bebo231312312321

Untitled

Feb 28th, 2024
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http";
  2. import { EMPTY, Observable, catchError, tap } from "rxjs";
  3. import { API_URL } from "./constants";
  4. import { Provider } from "@angular/core";
  5.  
  6. export class AppInterceptor implements HttpInterceptor {
  7.     intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  8.        
  9.         let request = req
  10.         if(req.url.startsWith('/api')){
  11.        
  12.             request = req.clone({
  13.                 url: req.url.replace('/api', API_URL)
  14.             })
  15.         }
  16.         return next.handle(request).pipe(tap((req)=>{
  17.             if (req instanceof HttpRequest) {
  18.                 console.log("vlvlvl",req);
  19.               }
  20.         }),
  21.          catchError((err:any)=>{
  22.             console.log(err)
  23.  
  24.             if(err.status === 0 ) return EMPTY;
  25.             return [err]
  26.          })
  27.         )
  28.     }
  29.  
  30. }
  31.  
  32. export const apiInterceptorProvider: Provider ={
  33.     provide: HTTP_INTERCEPTORS,
  34.     multi:true,
  35.     useClass: AppInterceptor
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement