import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { environment } from 'src/environments/environment' ; import { Token } from 'src/app/storage/Token' ; import { AbstractModelApplication , AbstractNormalizerApplication , AbstractSerializerApplication , } from 'src/app/model/abstract' ; export class AbstractService { public serializer = new AbstractSerializerApplication ( ) ; public normalizer = new AbstractNormalizerApplication ( ) ; protected endpoint = '' ; protected headers : HttpHeaders ; protected http : HttpClient ; public constructor ( http ) { this . http = http ; } static getBaseAPI ( ) { return `${ environment . TRANSPORT }${ environment . API_DOMAIN }` ; } static getBaseAPIEndpoint ( ) { return `${ AbstractService . getBaseAPI ( ) }${ environment . API_ENDPOINT }` ; } getUrl ( ) { return `${ AbstractService . getBaseAPIEndpoint ( ) }/${ this . endpoint }` ; } getOptions ( ) { return { headers : new HttpHeaders ( { Authorization : `Bearer ${ new Token ( ) . get ( ) }` , 'Content-Type' : 'application/json; charset=utf-8' , } ) , } ; } getData ( params = { } ) { return this . http . get ( `${ this . getUrl ( ) }?${ new URLSearchParams ( params ) }` , this . getOptions ( ) ) . toPromise ( ) . then ( ( result ) => ( { count : result [ 'hydra:totalItems' ] , data : this . serializer . serializeMany ( result [ 'hydra:member' ] ?? [ ] ) , } ) ) ; } getDataById ( id ) { return this . http . get ( `${ this . getUrl ( ) }/${ id }` , this . getOptions ( ) ) . toPromise ( ) . then ( ( result ) => this . serializer . serialize ( result ) ) ; } insert ( data ) { return this . http . post ( `${ this . getUrl ( ) }` , this . normalizer . normalize ( data ) , this . getOptions ( ) ) . toPromise ( ) . then ( ( result ) => this . serializer . serialize ( result ) ) ; } delete ( id ) { return this . http . delete ( `${ this . getUrl ( ) }/${ id }` , this . getOptions ( ) ) ; } update ( id , data ) { return this . http . patch ( `${ this . getUrl ( ) }/${ id }` , this . normalizer . normalize ( data ) , { ... this . getOptions ( ) , headers : this . getOptions ( ) . headers . set ( 'Content-Type' , 'application/merge-patch+json; charset=utf-8' ) , } ) . toPromise ( ) . then ( ( result ) => this . serializer . serialize ( result ) ) ; } }