import { ODataApi } from '../api' ; import { Parser , SchemaConfig } from '../types' ; import { OData } from '../utils/odata' ; import { ODataAnnotatable } from './annotation' ; import { ODataCallable } from './callable' ; import { ODataEntityContainer } from './entity-container' ; import { ODataEntitySet } from './entity-set' ; import { ODataEnumType } from './enum-type' ; import { ODataStructuredType } from './structured-type' ; export class ODataSchema extends ODataAnnotatable { api : ODataApi ; namespace : string ; alias ? : string ; enums : ODataEnumType < any > [ ] ; entities : ODataStructuredType < any > [ ] ; callables : ODataCallable < any > [ ] ; containers : ODataEntityContainer [ ] ; constructor ( config , api ) { super ( config ) ; this . api = api ; this . namespace = config . namespace ; this . alias = config . alias ; this . enums = ( config . enums || [ ] ) . map ( ( config ) => new ODataEnumType ( config , this ) ) ; this . entities = ( config . entities || [ ] ) . map ( ( config ) => new ODataStructuredType ( config , this ) ) ; this . callables = OData . mergeCallableParameters ( config . callables || [ ] ) . map ( ( config ) => new ODataCallable ( config , this ) ) ; this . containers = ( config . containers || [ ] ) . map ( ( config ) => new ODataEntityContainer ( config , this ) ) ; } isNamespaceOf ( type ) { return ( type . startsWith ( this . namespace ) || ( this . alias && type . startsWith ( this . alias ) ) ) ; } get entitySets ( ) { return this . containers . reduce ( ( acc , container ) => [ ... acc , ... container . entitySets ] , [ ] as ODataEntitySet [ ] ) ; } public findEnumTypeForType < T > ( type ) { return this . enums . find ( ( e ) => e . isTypeOf ( type ) ) as ODataEnumType < T > | undefined ; } public findStructuredTypeForType < T > ( type ) { return this . entities . find ( ( e ) => e . isTypeOf ( type ) ) as ODataStructuredType < T > | undefined ; } public findCallableForType < T > ( type , bindingType ? ) { const bindingStructuredType = bindingType !== undefined ? this . api . findStructuredTypeForType < any > ( bindingType ) : undefined ; return this . callables . find ( ( c ) => { const isCallableType = c . isTypeOf ( type ) ; const callableBindingType = c . binding ( ) ?. type ; const callableBindingStructuredType = callableBindingType !== undefined ? this . api . findStructuredTypeForType ( callableBindingType ) : undefined ; return ( isCallableType && ( ! bindingStructuredType || ( callableBindingStructuredType && bindingStructuredType . isSubtypeOf ( callableBindingStructuredType ) ) ) ) ; } ) as ODataCallable < T > | undefined ; } public findEntitySetForType ( type ) { return this . entitySets . find ( ( e ) => e . isTypeOf ( type ) ) ; } configure ( { parserForType , findOptionsForType , } ) { this . enums . forEach ( ( enu ) => enu . configure ( ) ) ; this . entities . forEach ( ( config ) => config . configure ( { parserForType , findOptionsForType } ) ) ; this . callables . forEach ( ( callable ) => callable . configure ( { parserForType } ) ) ; } }