import { Injectable } from '@angular/core' ; import { HttpClient , HttpErrorResponse , HttpEvent , HttpHandler , HttpHeaders , HttpInterceptor , HttpParams , HttpRequest } from '@angular/common/http' ; import { Observable , throwError } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { ConnectionService } from '../connect/connection.service' ; declare type HttpObserve = 'body' | 'events' | 'response' ; export const resp = 'response' ; export const httpObserveOptions = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) , params : { } , observe : resp } ; export const httpObservePostOptions = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) , params : { } , observe : resp } ; export const httpFileOptions = { headers : new HttpHeaders ( { } ) , params : { } , observe : resp , responseType : 'blob' } ; @ Injectable ( { providedIn : 'root' } ) export class ApiCallService { private readonly apiPrefix = 'rest' ; private readonly apiURL ; public static handleError ( err ) { if ( err . error instanceof ErrorEvent ) { console . error ( 'An error occurred:' , err . error . message ) ; } else { console . error ( `Backend returned code ${ err . status }, ` + `body was: ${ err . error }` ) ; } return throwError ( err ) ; } constructor ( private http , private connectionService ) { this . apiURL = `${ this . connectionService . currentConnection }/${ this . apiPrefix }/` ; } public callHead < T > ( route , httpGetOptions = httpObserveOptions ) { return this . http . head < T > ( this . apiURL + route , httpGetOptions ) . pipe ( catchError ( ApiCallService . handleError ) ) ; } public callGet < T > ( route , httpGetOptions = httpObserveOptions ) { return this . http . get < T > ( this . apiURL + route , httpGetOptions ) . pipe ( catchError ( ApiCallService . handleError ) ) ; } public callPost < T > ( route , data , httpPostOptions = httpObserveOptions ) { return this . http . post < T > ( this . apiURL + route , data , httpPostOptions ) . pipe ( catchError ( ApiCallService . handleError ) ) ; } public callPut < T > ( route , data , httpPutOptions = httpObserveOptions ) { return this . http . put < T > ( this . apiURL + route , data , httpPutOptions ) . pipe ( catchError ( ApiCallService . handleError ) ) ; } public callPatch < T > ( route , data , httpPatchOptions = httpObserveOptions ) { return this . http . patch < T > ( this . apiURL + route , data , httpPatchOptions ) . pipe ( catchError ( ApiCallService . handleError ) ) ; } public callDelete < T > ( route , httpDeleteOptions = httpObserveOptions ) { return this . http . delete < T > ( this . apiURL + route , httpDeleteOptions ) . pipe ( catchError ( ApiCallService . handleError ) ) ; } public callDeleteManyRoute < T > ( route , params ? ) { deprecated SD return this . http . delete < T > ( this . apiURL + route , params ) ; } public readCookies ( name ) { const result = new RegExp ( '(?:^|; )' + encodeURIComponent ( name ) + '=([^;]*)' ) . exec ( document . cookie ) ; return result ? result [ 1 ] : 'true' ; } } export interface ApiService { servicePrefix : string ; } export class HttpInterceptorHandler implements HttpHandler { constructor ( private next , private interceptor ) { } handle ( req ) { return this . interceptor . intercept ( req , this . next ) ; } } export class HttpProtocolHelper { param httpProtocol param filter param limit param sort param order param page static createHttpProtocolOptions ( httpProtocol , filter ? , limit ? , sort ? , order ? , page ? ) { const options = Object . assign ( this , httpProtocol ) ; let httpParams = new HttpParams ( ) ; httpParams = httpParams . set ( 'filter' , filter === undefined || filter === 'undefined' ? undefined : filter ) ; httpParams = httpParams . set ( 'limit' , limit === undefined ? '0' : limit . toString ( ) ) ; httpParams = httpParams . set ( 'sort' , sort === undefined ? 'public_id' : sort ) ; httpParams = httpParams . set ( 'order' , order === undefined ? '1' : order . toString ( ) ) ; httpParams = httpParams . set ( 'page' , page === undefined ? '1' : page . toString ( ) ) ; options . params = httpParams ; return options ; } }