### File

`projects/commons/src/lib/core/http-client/services/http-client-request.service.ts`

### Index

###### Methods
Private checkError Private handleError500 Private handleRefreshToken Public intercept

### Constructor

`constructor(toastService: ToastService, environment)`
Defined in projects/commons/src/lib/core/http-client/services/http-client-request.service.ts:10
Parameters :
Name | Type | Optional
toastService | `ToastService` | No
environment |  | No

### Methods

Private checkError
`checkError(error)`
Defined in projects/commons/src/lib/core/http-client/services/http-client-request.service.ts:31
Parameters :
Name | Optional
error | No
Returns : `void`

Private handleError500
`handleError500(undefined)`
Defined in projects/commons/src/lib/core/http-client/services/http-client-request.service.ts:39
Parameters :
Name | Optional
No
Returns : `void`

Private handleRefreshToken
`handleRefreshToken(event)`
Defined in projects/commons/src/lib/core/http-client/services/http-client-request.service.ts:49
Parameters :
Name | Optional
event | No
Returns : `void`

Public intercept
`intercept(request: HttpRequest, next: HttpHandler)`
Defined in projects/commons/src/lib/core/http-client/services/http-client-request.service.ts:16
Parameters :
Name | Type | Optional
request | `HttpRequest<any>` | No
next | `HttpHandler` | No
Returns : `Observable<HttpEvent<any>>`

    import { Observable, throwError } from 'rxjs';
    import { catchError, map } from 'rxjs/operators';
    import { Inject, Injectable } from '@angular/core';
    import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';

    import { colorsEnum } from '../../../shared';
    import { ToastService, toastPositionEnum } from '../../../elements';

    @Injectable()
    export class HttpClientRequestService implements HttpInterceptor {
        constructor(
            private readonly toastService: ToastService,
            @Inject('Environment') private readonly environment,
        ) {}

        public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
            const { url } = request;

            return next.handle(request.clone({
                url: `${ this.environment.api }${ url }`,
            })).pipe(
                map((event: HttpEvent<any>) => event),
                catchError((event) => {
                    this.checkError(event);

                    return throwError(event.error);
                })
            );
        }

        private checkError(error) {
            if (error.status >= 500) {
                this.handleError500(error);
            } else if (error.status === 403) {
                this.handleRefreshToken(error);
            }
        }

        private handleError500({ error }): void {
            this.toastService.open({
                message: error.title,
                type: colorsEnum.error,
                duration: 30000000,
                dismissible: true,
                position: toastPositionEnum.topCenter
            });
        }

        private handleRefreshToken(event): void {
            console.log(event);
        }
    }

# result-matching ""

# No results matching ""