@@ -62,6 +62,8 @@ export class ODataApi {
6262 parsers : Map < string , Parser < any > > ;
6363 // Schemas
6464 schemas : ODataSchema [ ] ;
65+ models : { [ type : string ] : typeof ODataModel < any > } = { } ;
66+ collections : { [ type : string ] : typeof ODataCollection < any , ODataModel < any > > } = { } ;
6567
6668 constructor ( config : ODataApiConfig ) {
6769 this . serviceRootUrl = config . serviceRootUrl ;
@@ -85,6 +87,8 @@ export class ODataApi {
8587 this . parsers = new Map ( Object . entries ( config . parsers ?? EDM_PARSERS ) ) ;
8688
8789 this . schemas = ( config . schemas ?? [ ] ) . map ( ( schema ) => new ODataSchema ( schema , this ) ) ;
90+ this . models = ( config . models ?? { } ) as { [ type : string ] : typeof ODataModel < any > } ;
91+ this . collections = ( config . collections ?? { } ) as { [ type : string ] : typeof ODataCollection < any , ODataModel < any > > } ;
8892 }
8993
9094 configure (
@@ -458,11 +462,11 @@ export class ODataApi {
458462 }
459463 //#endregion
460464
461- public findModel ( type : string ) {
462- return this . findStructuredType < any > ( type ) ?. model ;
465+ public findModel < T > ( type : string ) {
466+ return ( this . models [ type ] ?? this . findStructuredType < any > ( type ) ?. model ) as typeof ODataModel < T > | undefined ;
463467 }
464468
465- public createModel ( structured : ODataStructuredType < any > ) {
469+ public createModel < T > ( structured : ODataStructuredType < T > ) {
466470 if ( structured . model !== undefined ) return structured . model ;
467471 // Build Ad-hoc model
468472 const Model = class extends ODataModel < any > { } as typeof ODataModel ;
@@ -478,40 +482,40 @@ export class ODataApi {
478482 }
479483 // Store New Model for next time
480484 structured . model = Model ;
481- return Model ;
485+ return Model as typeof ODataModel < T > ;
482486 }
483487
484- public modelForType ( type : string ) {
485- let Model = this . findModel ( type ) ;
488+ public modelForType < T > ( type : string ) {
489+ let Model = this . findModel < T > ( type ) ;
486490 if ( Model === undefined ) {
487- const structured = this . findStructuredType < any > ( type ) ;
491+ const structured = this . findStructuredType < T > ( type ) ;
488492 if ( structured === undefined ) throw Error ( `No structured type for ${ type } ` ) ;
489- Model = this . createModel ( structured ) ;
493+ Model = this . createModel < T > ( structured ) ;
490494 }
491495 return Model ;
492496 }
493497
494- public findCollection ( type : string ) {
495- return this . findStructuredType < any > ( type ) ?. collection ;
498+ public findCollection < T > ( type : string ) {
499+ return ( this . collections [ type ] ?? this . findStructuredType < any > ( type ) ?. collection ) as typeof ODataCollection < T , ODataModel < T > > | undefined ;
496500 }
497501
498- public createCollection ( structured : ODataStructuredType < any > , model ?: typeof ODataModel < any > ) {
502+ public createCollection < T > ( structured : ODataStructuredType < T > , model ?: typeof ODataModel < T > ) {
499503 if ( structured . collection !== undefined ) return structured . collection ;
500504 if ( model === undefined ) model = this . createModel ( structured ) ;
501- const Collection = class extends ODataCollection < any , ODataModel < any > > {
505+ const Collection = class extends ODataCollection < T , ODataModel < T > > {
502506 static override model = model ! ;
503507 } as typeof ODataCollection ;
504508 structured . collection = Collection ;
505- return Collection ;
509+ return Collection as typeof ODataCollection < T , ODataModel < T > > ;
506510 }
507511
508- public collectionForType ( type : string ) {
509- let Collection = this . findCollection ( type ) ;
512+ public collectionForType < T > ( type : string ) {
513+ let Collection = this . findCollection < T > ( type ) ;
510514 if ( Collection === undefined ) {
511- const structured = this . findStructuredType < any > ( type ) ;
515+ const structured = this . findStructuredType < T > ( type ) ;
512516 if ( structured === undefined ) throw Error ( `No structured type for ${ type } ` ) ;
513- const Model = this . modelForType ( type ) ;
514- Collection = this . createCollection ( structured , Model ) ;
517+ const Model = this . modelForType < T > ( type ) ;
518+ Collection = this . createCollection < T > ( structured , Model ) as typeof ODataCollection < T , ODataModel < T > > ;
515519 }
516520 return Collection ;
517521 }
0 commit comments