import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Profile } from '@ngx-starter-kit/models' ; import { environment } from '@env/environment' ; import { catchError } from 'rxjs/operators' ; import { EMPTY , throwError } from 'rxjs' ; import { retryWithBackoff } from '@ngx-starter-kit/utils' ; @ Injectable ( { providedIn : 'root' , } ) export class ProfileService { private baseUrl = environment . API_BASE_URL ; private readonly entityPath = 'user/profile' ; constructor ( private httpClient ) { } getMyProfile ( ) { return this . httpClient . get < Profile > ( `${ this . baseUrl }/${ this . entityPath }/myprofile` ) . pipe ( retryWithBackoff ( 1000 , 3 ) , catchError ( ( error ) => { this . handleError ( error ) ; return EMPTY ; } ) ) ; } create ( entity ) { return this . httpClient . post < Profile > ( `${ this . baseUrl }/${ this . entityPath }` , entity , { reportProgress : true , observe : 'events' , } ) . pipe ( catchError ( this . handleError ) ) ; } update ( id , entity ) { return this . httpClient . patch < Profile > ( `${ this . baseUrl }/${ this . entityPath }/${ id }` , entity , { reportProgress : true , observe : 'events' , } ) . pipe ( catchError ( this . handleError ) ) ; } delete ( id ) { return this . httpClient . delete ( `${ this . baseUrl }/${ this . entityPath }/${ id }` ) . pipe ( catchError ( this . handleError ) ) ; } private handleError ( error ) { let errorMessage ; if ( typeof error === 'string' ) { errorMessage = error ; } else { if ( error . error instanceof ErrorEvent ) { errorMessage = `An error occurred: ${ error . error . message }` ; } else { errorMessage = `Backend returned code ${ error . status }, with body ${ error . message }` ; } } console . error ( error ) ; return throwError ( errorMessage ) ; } }