當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript http.HttpHandler類代碼示例

本文整理匯總了TypeScript中@angular/common/http.HttpHandler的典型用法代碼示例。如果您正苦於以下問題:TypeScript HttpHandler類的具體用法?TypeScript HttpHandler怎麽用?TypeScript HttpHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了HttpHandler類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: intercept

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.headers.get('No-Auth') === 'True') {
        return next.handle(req.clone());
    }

    if (localStorage.getItem('currentUser') != null) {
        const clonedreq = req.clone({
            headers: req.headers.set('Authorization', 'Bearer ' + localStorage.getItem('currentUser'))
        });
        return next.handle(clonedreq)
            .do(
            succ => { },
            err => {
                if (err.status === 401) {
                    this.router.navigateByUrl('/login');
                }
            }
            );
    } else {
        this.router.navigateByUrl('/login');
    }
}
開發者ID:bob1457,項目名稱:REALClient,代碼行數:22,代碼來源:auth.interceptor.ts

示例2: intercept

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = JSON.parse(localStorage.getItem('currentUser'));
        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: {
                    Authorization: `Bearer ${currentUser.token}`
                }
            });
        }

        return next.handle(request);
    }
開發者ID:matKudDev,項目名稱:CarRentSystem---frontend-,代碼行數:13,代碼來源:jwt.interceptor.ts

示例3: intercept

	intercept(req : HttpRequest <any>, next : HttpHandler) : Observable <HttpEvent<any>> {
			// Clone the request to add the new header
      const token = new TokenStorage();
      const tokenVal = token.getToken();
			const clonedRequest = req.clone({
			headers: req
				.headers
				.set('Authorization', tokenVal ? `Bearer ${ tokenVal}` : '')
		});

		// Pass the cloned request instead of the original request to the next handle
		return next.handle(clonedRequest);
	}
開發者ID:linnovate,項目名稱:mean,代碼行數:13,代碼來源:header.interceptor.ts

示例4: intercept

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = this.authenticationService.currentUserValue;
        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: { 
                    Authorization: `Bearer ${currentUser.token}`
                }
            });
        }

        return next.handle(request);
    }
開發者ID:OmkarSadegaonkar,項目名稱:BookRecommender,代碼行數:13,代碼來源:jwt.interceptor.ts

示例5: intercept

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // Add content-type for POST, PUT, PATCH automatically
        if (request.method === 'POST' || request.method === 'PUT' || request.method === 'PATCH') {
            request = request.clone({
                setHeaders: {
                    'Content-Type': 'application/json; charset=utf-8'
                }
            });
        }

        return next
            .handle(request)
            .catch(response => {
                let message: string;

                if (response instanceof HttpErrorResponse) {
                    switch (response.status) {
                        case 0: {
                            message = "No network connection";
                            break;
                        }
                        case 401: {
                            message = "Unauthenticated";
                            break;
                        }
                        case 403: {
                            message = "Unauthorized";
                            break;
                        }
                        case 404: {
                            message = "Resource Not Found";
                            break;
                        }
                        case 500: {
                            message = "Server error";
                            break;
                        }
                        default: {
                            message = response.status.toString();
                            break;
                        }
                    }
                }

                this.snackBar.open(message, null, {
                    duration: 2000
                });

                return Observable.throw(response);
            });
    }
開發者ID:pete33221100,項目名稱:test-project-28032018,代碼行數:51,代碼來源:http-custom.interceptor.ts

示例6: intercept

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

 request = request.clone({
    headers: new HttpHeaders(
      {
        'Content-Type':  'application/json',
        'Authorization': 'desafio-4-devs' 
      }
    )  
  });
  
 return next.handle(request);

 }
開發者ID:gardheamsantos00,項目名稱:4devs,代碼行數:14,代碼來源:interceptor.module.ts

示例7: intercept

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
     return next.handle(request).do(
         (event: HttpEvent<any>) => {},
         (err: any) => {
             if (err instanceof HttpErrorResponse) {
                 if (!(err.status === 401 && (err.message === '' || (err.url && err.url.indexOf('/api/account') === 0)))) {
                     if (this.eventManager !== undefined) {
                         this.eventManager.broadcast({ name: 'jHipsterRegistryApp.httpError', content: err });
                     }
                 }
             }
         }
     );
 }
開發者ID:gjik911,項目名稱:git_01,代碼行數:14,代碼來源:errorhandler.interceptor.ts

示例8: intercept

 intercept(
     request: HttpRequest<any>,
     next: HttpHandler
 ): Observable<HttpEvent<any>> {
     const user: User = this.sessionStorage.retrieve('user');
     if (user && user.token) {
         request = request.clone({
             setHeaders: {
                 JWT: user.token,
             },
         });
     }
     return next.handle(request);
 }
開發者ID:RCata,項目名稱:Work-Work,代碼行數:14,代碼來源:token-interceptor.ts

示例9: intercept

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
     return next.handle(request).pipe(
         tap(
             (event: HttpEvent<any>) => {},
             (err: any) => {
                 if (err instanceof HttpErrorResponse) {
                     if (err.status === 401) {
                         this.loginService.logout();
                     }
                 }
             }
         )
     );
 }
開發者ID:Doha2012,項目名稱:tutorials,代碼行數:14,代碼來源:auth-expired.interceptor.ts

示例10: intercept

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        console.log(req.method);
        if (req.method == 'OPTIONS') {
            console.log("OPTIONS!");
        }
        console.log(req.body);
/*         if (req.url.startsWith("http://localhost:8080/")) {
            const newBody = this.toJsonString(req.body);
            const modified = req.clone({body: newBody});
            return next.handle(modified);
        } */
        console.log(req.url);
        return next.handle(req);
    }
開發者ID:Kerrex,項目名稱:TrainingCenter_Frontend,代碼行數:14,代碼來源:api.interceptor.ts


注:本文中的@angular/common/http.HttpHandler類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。