@angular/common/http#HttpEvent TypeScript Examples

The following examples show how to use @angular/common/http#HttpEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: unauthorized.interceptor.ts    From Smersh with MIT License 6 votes vote down vote up
intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      catchError((error: HttpErrorResponse) => {
        if (error && [401, 403].includes(error.status)) {
          this.router.navigateByUrl(DashboardRouter.redirectToList());
        }
        return throwError(error);
      })
    );
  }
Example #2
Source File: rubic-exchange-interceptor.ts    From rubic-app with GNU General Public License v3.0 6 votes vote down vote up
intercept(httpRequest: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    if (!httpRequest.url.includes(this.DOMAIN_SUBSTRING)) {
      return next.handle(httpRequest);
    }

    let newRequest = this.setDefaultParams(httpRequest);
    newRequest = this.addIframeHostDomain(newRequest);
    newRequest = this.addTokenHeader(newRequest);
    return next.handle(newRequest);
  }
Example #3
Source File: fake-backend.service.ts    From ng-conf-2020-workshop with MIT License 6 votes vote down vote up
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        this.users = JSON.parse(this.localStorage.getItem('users')) || [];
        return of(null).pipe(mergeMap(() => {
            // login user
            if (request.url.endsWith('/login') && request.method === 'POST') {
                return this.loginHandle(request);
            }

            // register user
            if (request.url.endsWith('/register') && request.method === 'POST') {
                const user = this.getStorageUser(request);
                return this.registerHandle(user);
            }

            // login user with external provider
            if (request.url.endsWith('/extlogin') && request.method === 'POST') {
                const user = this.getStorageExtUser(request);
                return this.registerHandle(user, true);
            }

            // Microsoft-specific OIDC discovery URI
            if (request.url.endsWith('ms-discovery/keys') && request.method === 'GET') {
                return of(new HttpResponse({ status: 200, body: msKeys }));
            }

            return next.handle(request);
        }))
        .pipe(materialize())
        .pipe(dematerialize());
    }
Example #4
Source File: api-http.interceptor.ts    From The-TypeScript-Workshop with MIT License 6 votes vote down vote up
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (!req.url.startsWith('/api/')) {
      return next.handle(req);
  }

    const relativeUrl = req.url.replace('/api/', '');
    const newRequest = req.clone({ url: `https://jsonplaceholder.typicode.com/${relativeUrl}` });

    return next.handle(newRequest);
  }
Example #5
Source File: auth-interceptor.ts    From NetCoreTutes with MIT License 6 votes vote down vote up
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    var token = this.authService.getToken();
    if (token) {
      var header = "Bearer " + token;
      var reqWithAuth = req.clone({ headers: req.headers.set("Authorization", header) });
      return next.handle(reqWithAuth);
    }

    return next.handle(req);
  }
Example #6
Source File: auth.interceptor.ts    From svvs with MIT License 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const accessToken = this.authStorage.getAccessToken()

    if (accessToken) {
      req = req.clone({
        headers: req.headers.set('Authorization', `Bearer ${accessToken}`),
      })
    }
    return next.handle(req)
  }
Example #7
Source File: default-headers.interceptor.ts    From WowUp with GNU General Public License v3.0 6 votes vote down vote up
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Get the auth token from the service.

    // Clone the request and replace the original headers with
    // cloned headers, updated with the authorization.
    // const cloneReq = req.clone({
    //   headers: req.headers.set("startTimestamp", Date.now().toString()),
    // });
    const start = Date.now();
    const method = req.method;
    const url = req.urlWithParams;

    // send cloned request with header to the next handler.
    return next.handle(req).pipe(
      tap((response: any) => {
        try {
          if (response instanceof HttpResponse) {
            const t = Date.now() - start;
            console.log(`[${method}] ${url} ${response.status} ${t}ms`);
          }
        } catch (e) {
          console.error(e);
        }
      })
    );
  }
Example #8
Source File: auth.interceptor.ts    From ReCapProject-Frontend with MIT License 6 votes vote down vote up
intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<unknown>> {
    let tokenModel: TokenModel | null = this.localStorageService.get<TokenModel>(
      'tokenModel'
    );

    let newRequest: HttpRequest<any> = request.clone({
      headers: request.headers.set(
        'Authorization',
        `Bearer ${tokenModel?.token}`
      ),
    });

    return next.handle(newRequest);
  }
Example #9
Source File: auth.interceptor.ts    From ngx-admin-dotnet-starter with MIT License 6 votes vote down vote up
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req)
      .pipe(catchError((error: HttpErrorResponse) => {
        if (error.status === 401) {
          this.router.navigate(['auth/login']);
        }
        // TODO: handle 403 error ?
        return throwError(error);
      }));
  }
Example #10
Source File: authorization.interceptor.ts    From alura_angular_rxjs_1 with MIT License 6 votes vote down vote up
intercept(
    original_request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    let requestResult: HttpRequest<any>;
    if (this.isUrlNeedsProAuth(original_request.url)) {
      requestResult = this.appendTokenToRequest(original_request);
    } else {
      requestResult = original_request.clone();
    }
    return next.handle(requestResult);
  }
Example #11
Source File: http-error.interceptor.ts    From angular-padroes-e-boas-praticas with MIT License 6 votes vote down vote up
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request).pipe(
      catchError((httpError: HttpErrorResponse) => {
        console.error('Error from interceptor', httpError);
        this.notification.error(httpError.error || 'An unexpected error occurred!');
        return throwError(httpError);
      })
    ) as Observable<HttpEvent<any>>;
  }
Example #12
Source File: attribution.service.ts    From barista with Apache License 2.0 5 votes vote down vote up
public attributionByScanIdIdGet(id: number, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Array<ProjectDistinctLicenseAttributionDto>>>;