import { HttpClient , HttpHeaders , HttpParams } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { catchError , map } from 'rxjs/internal/operators' ; import { throwError } from 'rxjs' ; import { URL_SERVICES } from '../../config/config' ; import * as _ from 'lodash' ; @ Injectable ( ) export class ApiService { public API = URL_SERVICES ; constructor ( protected http ) { } public handleError ( error ) { console . warn ( error ) ; const result = { } ; result . status = error . status ; switch ( error . status ) { case 400 : result . text = error . error . message ; break ; case 404 : result . text = error . error . message ; break ; case 500 : result . text = error . error . message ; break ; case 401 : result . text = '401' ; result . nextPage = 'logout' ; break ; case 403 : result . text = result . text = error . error . message ; result . nextPage = 'home' ; break ; default : if ( error . statusText === 'Unknow Error' ) { result . text = ' - Error del servidor' ; } break ; } return throwError ( result ) ; } private getHeaders ( ) { const headers = new HttpHeaders ( ) ; headers . append ( 'Content-Type' , 'application/json' ) ; return headers ; } private getSearchParamToken ( ) { const token = sessionStorage . getItem ( 'token' ) ; let params = new HttpParams ( ) ; if ( token ) { params = params . set ( 'token' , token ) ; } return params ; } public get ( route , skipToken ? ) { let options ; if ( ! skipToken ) { options = { headers : this . getHeaders ( ) , params : this . getSearchParamToken ( ) } ; } else { options = { headers : this . getHeaders ( ) } ; } return this . http . get ( this . API + route , options ) . pipe ( map ( response => response || { } ) , catchError ( this . handleError ) ) ; } public getParams ( route , params ) { let search = this . getSearchParamToken ( ) ; _ . forEach ( params , ( value , key ) => { if ( ! _ . isNil ( value ) ) { if ( _ . isEqual ( typeof value , 'object' ) ) { search = search . append ( String ( key ) , value ) ; } else { search = search . append ( String ( key ) , value ) ; } } } ) ; const options = { headers : this . getHeaders ( ) , params : search } ; return this . http . get ( this . API + route , options ) . pipe ( map ( response => response || { } ) , catchError ( this . handleError ) ) ; } public delete ( route ) { const options = { headers : this . getHeaders ( ) , params : this . getSearchParamToken ( ) } ; return this . http . delete ( this . API + route , options ) . pipe ( map ( response => response || { } ) , catchError ( this . handleError ) ) ; } public post ( route , body , skipToken ? ) { let options ; if ( ! skipToken ) { options = { headers : this . getHeaders ( ) , params : this . getSearchParamToken ( ) } ; } else { options = { headers : this . getHeaders ( ) } ; } return this . http . post ( this . API + route , body , options ) . pipe ( map ( response => response || { } ) , catchError ( this . handleError ) ) ; } public put ( route , body ) { const options = { headers : this . getHeaders ( ) , params : this . getSearchParamToken ( ) } ; return this . http . put ( this . API + route , body , options ) . pipe ( map ( response => response || { } ) , catchError ( this . handleError ) ) ; } }