import { EMPTY , Observable } from 'rxjs' ; import { concatMap , expand , map , toArray } from 'rxjs/operators' ; import { ODataApi } from '../../api' ; import { ODataCollection , ODataModel } from '../../models' ; import { ODataStructuredType } from '../../schema/structured-type' ; import { PathSegmentNames , QueryOptionNames } from '../../types' ; import { ODataPathSegments } from '../path' ; import { ODataQueryOptions } from '../query' ; import { ODataResource } from '../resource' ; import { ODataEntities , ODataEntity } from '../responses' ; import { ODataActionResource } from './action' ; import { ODataCountResource } from './count' ; import { ODataEntityResource } from './entity' ; import { ODataFunctionResource } from './function' ; import { ODataOptions } from './options' ; export class ODataEntitySetResource < T > extends ODataResource < T > { static factory < E > ( api , { path , schema , query , } ) { const segments = new ODataPathSegments ( ) ; const segment = segments . add ( PathSegmentNames . entitySet , path ) ; if ( schema !== undefined ) segment . type ( schema . type ( ) ) ; return new ODataEntitySetResource < E > ( api , { segments , query , schema } ) ; } entity ( key ? ) { const entity = ODataEntityResource . factory < T > ( this . api , { schema : this . schema as ODataStructuredType < T > , segments : this . cloneSegments ( ) , query : this . cloneQuery < T > ( ) , } ) ; if ( key !== undefined ) { return entity . key ( key ) ; } return entity ; } action < P , R > ( path ) { return ODataActionResource . fromResource < P , R > ( this , path ) ; } function < P , R > ( path ) { return ODataFunctionResource . fromResource < P , R > ( this , path ) ; } count ( ) { return ODataCountResource . factory < T > ( this . api , { segments : this . cloneSegments ( ) , query : this . cloneQuery < T > ( ) , } ) ; } cast < C > ( type ) { const baseSchema = this . schema as ODataStructuredType < T > ; const castSchema = this . api . findStructuredTypeForType < C > ( type ) ; if ( castSchema !== undefined && baseSchema !== undefined && ! castSchema . isSubtypeOf ( baseSchema ) ) throw new Error ( `Cannot cast to ${ type }` ) ; const segments = this . cloneSegments ( ) ; segments . add ( PathSegmentNames . type , type ) . type ( type ) ; return new ODataEntitySetResource < C > ( this . api , { segments , schema : castSchema , query : this . cloneQuery < C > ( ) , } ) ; } protected override post ( attrs , options = { } ) { return super . post ( attrs , { responseType : 'entity' , ... options } ) ; } protected override get ( options = { } ) { return super . get ( { responseType : 'entities' , ... options } ) ; } create ( attrs , options ? ) { return this . post ( attrs , options ) ; } fetch ( options ? ) { return this . get ( options ) ; } fetchEntities ( options ? ) { return this . fetch ( options ) . pipe ( map ( ( { entities } ) => entities ) ) ; } fetchCollection < M extends ODataModel < T > , C extends ODataCollection < T , M > > ( options ? ) { return this . fetch ( options ) . pipe ( map ( ( { entities , annots } ) => entities ? this . asCollection < M , C > ( entities , { annots , reset : true } ) : null ) ) ; } fetchAll ( options ? ) { let res = this . clone < ODataEntitySetResource < T > > ( ) ; res . query ( ( q ) => q . clearPaging ( ) ) ; let fetch = ( opts ? ) : Observable < ODataEntities < T > > => { if ( opts ) { res . query ( ( q ) => q . paging ( opts ) ) ; } return res . fetch ( options ) ; } ; return fetch ( ) . pipe ( expand ( ( { annots : meta } ) => meta . skip || meta . skiptoken ? fetch ( meta ) : EMPTY ) , concatMap ( ( { entities } ) => entities || [ ] ) , toArray ( ) ) ; } }