import { ODataCollection } from '../models' ; import { ODataModel } from '../models/model' ; import { Options , StructuredTypeConfig } from '../types' ; import { ODataSchemaElement } from './element' ; import { JsonSchemaOptions , ODataEntityTypeKey , ODataStructuredTypeFieldParser , ODataStructuredTypeParser , } from './parsers' ; import { ODataSchema } from './schema' ; export class ODataStructuredType < T > extends ODataSchemaElement { base ? : string ; open : boolean ; parent ? : ODataStructuredType < any > ; children : ODataStructuredType < any > [ ] = [ ] ; model ? : typeof ODataModel ; collection ? : typeof ODataCollection ; parser : ODataStructuredTypeParser < T > ; constructor ( config , schema ) { super ( config , schema ) ; this . base = config . base ; this . open = config . open || false ; this . parser = new ODataStructuredTypeParser ( config , schema . namespace , schema . alias ) ; this . model = config . model as typeof ODataModel ; this . collection = config . collection as typeof ODataCollection ; if ( this . model !== undefined ) { const options = this . model . hasOwnProperty ( 'options' ) ? this . model . options : { fields : { } } ; this . model . buildMeta < T > ( { options , schema : this } ) ; } if ( this . collection !== undefined ) { this . collection . model = this . model ; } } configure ( { parserForType , findOptionsForType , } ) { if ( this . base ) { const parent = this . api . findStructuredTypeForType ( this . base ) as ODataStructuredType < any > ; parent . children . push ( this ) ; this . parent = parent ; } this . parser . configure ( { parserForType , options : this . api . options } ) ; if ( this . model !== undefined && this . model . options !== null ) { this . model . meta . configure ( { findOptionsForType , options : this . api . options , } ) ; } } param type returns override isSubtypeOf ( schema ) { return ( super . isSubtypeOf ( schema ) || ( this . parent !== undefined && this . parent . isSubtypeOf ( schema ) ) ) ; } returns isSimpleKey ( ) { return this . keys ( ) . length === 1 ; } returns isCompoundKey ( ) { return this . keys ( ) . length > 1 ; } param name returns findFieldByName < F > ( name ) { return this . fields ( { include_parents : true , include_navigation : true , } ) . find ( ( f ) => f . name === name ) as ODataStructuredTypeFieldParser < F > | undefined ; } param predicate returns findParentSchema ( predicate ) { if ( predicate ( this ) ) return this as ODataStructuredType < any > ; if ( this . parent === undefined ) return undefined ; return this . parent . findParentSchema ( predicate ) ; } param field returns findSchemaForField < E > ( field ) { return this . findParentSchema ( ( p ) => p . fields ( { include_parents : false , include_navigation : true } ) . find ( ( f ) => f === field ) !== undefined ) as ODataStructuredType < E > ; } param include_navigation param include_parents returns fields ( { include_navigation , include_parents , } ) { return [ ... ( include_parents && this . parent !== undefined ? this . parent . fields ( { include_parents , include_navigation } ) : [ ] ) , ... this . parser . fields . filter ( ( field ) => include_navigation || ! field . navigation ) , ] ; } param include_parents returns keys ( { include_parents = true , } = { } ) { return [ ... ( include_parents && this . parent !== undefined ? this . parent . keys ( { include_parents } ) : [ ] ) , ... ( this . parser . keys || [ ] ) , ] ; } param attrs param include_parents param include_navigation param include_etag returns pick ( attrs , { include_parents = true , include_navigation = false , include_etag = true , } = { } ) { const names = this . fields ( { include_parents , include_navigation } ) . map ( ( f ) => f . name ) ; let values = Object . keys ( attrs ) . filter ( ( k ) => names . indexOf ( k ) !== - 1 ) . reduce ( ( acc , k ) => Object . assign ( acc , { [ k ] : attrs [ k ] } ) , { } ) ; if ( include_etag ) { const etag = this . api . options . helper . etag ( attrs ) ; this . api . options . helper . etag ( attrs , etag ) ; } return values ; } param value param options returns deserialize ( value , options ? ) { return this . parser . deserialize ( value , options ) ; } param value param options returns serialize ( value , options ? ) { return this . parser . serialize ( value , options ) ; } param value param options returns encode ( value , options ? ) { return this . parser . encode ( value , options ) ; } param attrs returns resolveKey ( attrs ) { return this . parser . resolveKey ( attrs ) ; } returns defaults ( ) { return this . parser . defaults ( ) ; } param options returns toJsonSchema ( options = { } ) { return this . parser . toJsonSchema ( options ) ; } param attrs param method returns validate ( attrs , { method , navigation = false , } = { } ) { return this . parser . validate ( attrs , { method , navigation } ) ; } }