import { HttpClient , HttpEvent , HttpHeaders , HttpParams , HttpResponse , } from '@angular/common/http' ; import { Injectable , Injector } from '@angular/core' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { ODataApi } from './api' ; import { ODataCollection , ODataModel } from './models/index' ; import { ODataActionResource , ODataBatchResource , ODataEntityResource , ODataEntitySetResource , ODataFunctionResource , ODataMetadataResource , ODataNavigationPropertyResource , ODataRequest , ODataResource , ODataResponse , ODataSegment , ODataSingletonResource , } from './resources/index' ; import { ODataEntityService } from './services/entity' ; import { ODataSettings } from './settings' ; function addBody < T > ( options , body ) { return { body , etag : options . etag , apiName : options . apiName , fetchPolicy : options . fetchPolicy , headers : options . headers , observe : options . observe , params : options . params , reportProgress : options . reportProgress , responseType : options . responseType , withCredentials : options . withCredentials , } ; } @ Injectable ( { providedIn : 'root' , } ) export class ODataClient { constructor ( private http , private settings , private injector ) { this . settings . configure ( { requester : ( req ) : Observable < any > => this . http . request ( req . method , `${ req . url }` , { body : req . body , headers : req . headers , observe : req . observe , params : req . params , reportProgress : req . reportProgress , responseType : req . responseType , withCredentials : req . withCredentials , } ) , } ) ; } param value returns apiFor ( value ? ) { let api = undefined ; if ( value instanceof ODataResource ) api = this . settings . findApiForTypes ( value . types ( ) ) ; else if ( typeof value === 'string' ) api = this . settings . findApiByName ( value ) || this . settings . findApiForType ( value ) ; return api || this . settings . defaultApi ( ) ; } param type returns parserForType < T > ( type ) { return this . settings . parserForType < T > ( type ) ; } param type returns enumTypeForType < T > ( type ) { return this . settings . enumTypeForType < T > ( type ) ; } param type returns structuredTypeForType < T > ( type ) { return this . settings . structuredTypeForType < T > ( type ) ; } param type returns callableForType < T > ( type ) { return this . settings . callableForType < T > ( type ) ; } param type returns entitySetForType ( type ) { return this . settings . entitySetForType ( type ) ; } param type returns modelForType ( type ) { return this . settings . modelForType ( type ) ; } param type returns collectionForType ( type ) { return this . settings . collectionForType ( type ) ; } param type returns serviceForType ( type ) { return this . injector . get ( this . settings . serviceForType ( type ) ) ; } param type returns serviceForEntityType ( type ) { return this . injector . get ( this . settings . serviceForEntityType ( type ) ) ; } enumTypeByName < T > ( name ) { return this . settings . enumTypeByName < T > ( name ) ; } structuredTypeByName < T > ( name ) { return this . settings . structuredTypeByName < T > ( name ) ; } callableByName < T > ( name ) { return this . settings . callableByName < T > ( name ) ; } entitySetByName ( name ) { return this . settings . entitySetByName ( name ) ; } modelByName ( name ) { return this . settings . modelByName ( name ) ; } collectionByName ( name ) { return this . settings . collectionByName ( name ) ; } serviceByName ( name ) { return this . injector . get ( this . settings . serviceByName ( name ) ) ; } fromJSON < E > ( json , apiNameOrType ? ) ; fromJSON ( json , apiNameOrType ? ) { return this . apiFor ( apiNameOrType ) . fromJSON < any > ( json ) ; } param apiName returns metadata ( apiName ? ) { return this . apiFor ( apiName ) . metadata ( ) ; } param apiName returns batch ( apiName ? ) { return this . apiFor ( apiName ) . batch ( ) ; } param path param apiNameOrType returns singleton < T > ( path , apiNameOrType ? ) { return this . apiFor ( apiNameOrType ) . singleton < T > ( path ) ; } param path param apiNameOrType returns entitySet < T > ( path , apiNameOrType ? ) { return this . apiFor ( apiNameOrType ) . entitySet < T > ( path ) ; } param path param apiNameOrType returns action < P , R > ( path , apiNameOrType ? ) { return this . apiFor ( apiNameOrType ) . action < P , R > ( path ) ; } param path param apiNameOrType returns function < P , R > ( path , apiNameOrType ? ) { return this . apiFor ( apiNameOrType ) . function < P , R > ( path ) ; } request ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request < R > ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request ( method , resource , options ) ; request < R > ( method , resource , options ) ; request ( method , resource , options ) ; request < R > ( method , resource , options ) ; request ( method , resource , options ? ) ; request ( method , resource , options ) { let api = options . apiName ? this . settings . apiByName ( options . apiName ) : resource . api ; if ( ! api ) throw new Error ( `The types: '[${ resource . types ( ) . join ( ', ' ) }]' does not belongs to any known configuration` ) ; const request = new ODataRequest ( { method , api , resource , body : options . body , observe : options . observe === 'events' ? 'events' : 'response' , etag : options . etag , headers : options . headers , reportProgress : options . reportProgress , params : options . params , responseType : options . responseType , fetchPolicy : options . fetchPolicy , withCredentials : options . withCredentials , } ) ; return api . request ( request ) . pipe ( map ( ( res ) => options . observe === undefined || options . observe === 'body' ? res . body : res ) ) ; } delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete < T > ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete ( resource , options ? ) ; delete < T > ( resource , options ? ) ; delete ( resource , options ? ) ; delete < T > ( resource , options ? ) ; delete ( resource , options = { } ) { return this . request < any > ( 'DELETE' , resource , addBody < any > ( options , null ) ) ; } get ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get < T > ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get ( resource , options ) ; get < T > ( resource , options ) ; get ( resource , options ? ) ; get < T > ( resource , options ? ) ; get ( resource , options = { } ) { return this . request < any > ( 'GET' , resource , options as any ) ; } head ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head < T > ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head ( resource , options ) ; head < T > ( resource , options ) ; head ( resource , options ? ) ; head < T > ( resource , options ? ) ; head ( resource , options = { } ) { return this . request < any > ( 'HEAD' , resource , options as any ) ; } jsonp ( resource , callbackParam ) ; jsonp < T > ( resource , callbackParam ) ; jsonp < T > ( resource , callbackParam ) { return this . request < any > ( 'JSONP' , resource , { body : null , params : new HttpParams ( ) . append ( callbackParam , 'JSONP_CALLBACK' ) , observe : 'body' , responseType : 'json' , } ) ; } options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options < T > ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options ( resource , options ) ; options < T > ( resource , options ) ; options ( resource , options ? ) ; options < T > ( resource , options ? ) ; options ( resource , options = { } ) { return this . request < any > ( 'OPTIONS' , resource , options as any ) ; } patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch < T > ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch < T > ( resource , body , options ? ) ; patch ( resource , body , options ? ) ; patch < T > ( resource , body , options ? ) ; patch ( resource , body , options = { } ) { return this . request < any > ( 'PATCH' , resource , addBody ( options , body ) ) ; } post ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post < T > ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post ( resource , body , options ) ; post < T > ( resource , body , options ) ; post ( resource , body , options ? ) ; post < T > ( resource , body , options ? ) ; post ( resource , body , options = { } ) { return this . request < any > ( 'POST' , resource , addBody ( options , body ) ) ; } put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put < T > ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put < T > ( resource , body , options ? ) ; put ( resource , body , options ? ) ; put < T > ( resource , body , options ? ) ; put ( resource , body , options = { } ) { return this . request < any > ( 'PUT' , resource , addBody ( options , body ) ) ; } }