本文整理匯總了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');
}
}
示例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);
}
示例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);
}
示例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);
}
示例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);
});
}
示例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);
}
示例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 });
}
}
}
}
);
}
示例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);
}
示例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();
}
}
}
)
);
}
示例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);
}