1- import type { FilterType } from "@mustache/types" ;
1+ import type { MustacheFilter } from "@mustache/types" ;
22import type { TypeSchemaIndex } from "@root/typeschema/utils" ;
3- import type { Identifier , RegularTypeSchema , TypeSchema } from "@typeschema/types" ;
3+ import { type Identifier , isComplexTypeIdentifier , isResourceIdentifier , type TypeSchema } from "@typeschema/types" ;
44
55export class SchemaLoaderFacade {
6- private readonly complexTypesByUri : Record < string , TypeSchema > = { } ;
7- private readonly resourcesByUri : Record < string , TypeSchema > = { } ;
6+ private readonly tsIndex : TypeSchemaIndex ;
7+ private readonly filters : MustacheFilter ;
88
9- private readonly childResourceUrisByParentUri : Record < string , string [ ] > = { } ;
10- private readonly childComplexTypeUrisByParentUri : Record < string , string [ ] > = { } ;
11-
12- private complexTypeRefs : Identifier [ ] | undefined = undefined ;
13- private resourceRefs : Identifier [ ] | undefined = undefined ;
14-
15- constructor (
16- private readonly tsIndex : TypeSchemaIndex ,
17- private readonly filters : { resource ?: FilterType ; complexType ?: FilterType } ,
18- ) {
19- this . tsIndex . collectComplexTypes ( ) . forEach ( ( schema : RegularTypeSchema ) => {
20- this . complexTypesByUri [ schema . identifier . url ] = schema ;
21- if ( schema . base ) {
22- if ( ! this . childComplexTypeUrisByParentUri [ schema . base . url ] ) {
23- this . childComplexTypeUrisByParentUri [ schema . base . url ] = [ ] ;
24- }
25- this . childComplexTypeUrisByParentUri [ schema . base . url ] ?. push ( schema . identifier . url ) ;
26- }
27- } ) ;
28- this . tsIndex . collectResources ( ) . forEach ( ( schema : RegularTypeSchema ) => {
29- this . resourcesByUri [ schema . identifier . url ] = schema ;
30- if ( schema . base ) {
31- if ( ! this . childResourceUrisByParentUri [ schema . base . url ] ) {
32- this . childResourceUrisByParentUri [ schema . base . url ] = [ ] ;
33- }
34- this . childResourceUrisByParentUri [ schema . base . url ] ?. push ( schema . identifier . url ) ;
35- }
36- } ) ;
9+ constructor ( tsIndex : TypeSchemaIndex , filters : MustacheFilter ) {
10+ this . tsIndex = tsIndex ;
11+ this . filters = filters ;
3712 }
3813
3914 public get ( typeRef : Identifier ) {
@@ -47,46 +22,40 @@ export class SchemaLoaderFacade {
4722 }
4823
4924 public getComplexType ( typeRef : Identifier ) : TypeSchema | undefined {
50- return this . complexTypesByUri [ typeRef . url ] ;
25+ return this . tsIndex . resolve ( typeRef ) ;
5126 }
5227 public getResource ( typeRef : Identifier ) : TypeSchema | undefined {
53- return this . resourcesByUri [ typeRef . url ] ;
28+ return this . tsIndex . resolve ( typeRef ) ;
5429 }
5530
5631 public getChildComplexTypes ( parentTypeRef : Identifier ) : Identifier [ ] {
57- return ( this . childComplexTypeUrisByParentUri [ parentTypeRef . url ] ?? [ ] )
58- . map ( ( uri ) => this . complexTypesByUri [ uri ] )
59- . map ( ( t : any ) => t . identifier )
60- . filter ( ( t : any ) => this . _checkFilter ( t ) )
61- . sort ( ( a : any , b : any ) => a . url . localeCompare ( b . url ) ) ;
32+ return this . tsIndex
33+ . resourceChildren ( parentTypeRef )
34+ . filter ( isComplexTypeIdentifier )
35+ . filter ( ( i ) => this . _checkFilter ( i ) )
36+ . sort ( ( a , b ) => a . url . localeCompare ( b . url ) ) ;
6237 }
6338 public getChildResources ( parentTypeRef : Identifier ) : Identifier [ ] {
64- return ( this . childResourceUrisByParentUri [ parentTypeRef . url ] ?? [ ] )
65- . map ( ( uri ) => this . resourcesByUri [ uri ] )
66- . map ( ( t : any ) => t . identifier )
67- . filter ( ( t : any ) => this . _checkFilter ( t ) )
68- . sort ( ( a : any , b : any ) => a . url . localeCompare ( b . url ) ) ;
39+ return this . tsIndex
40+ . resourceChildren ( parentTypeRef )
41+ . filter ( isResourceIdentifier )
42+ . filter ( ( i ) => this . _checkFilter ( i ) )
43+ . sort ( ( a , b ) => a . url . localeCompare ( b . url ) ) ;
6944 }
7045
7146 public getComplexTypes ( ) : Identifier [ ] {
72- if ( this . complexTypeRefs === undefined ) {
73- this . complexTypeRefs = this . tsIndex
74- . collectComplexTypes ( )
75- . map ( ( t : any ) => t . identifier )
76- . filter ( ( t : any ) => this . _checkFilter ( t ) )
77- . sort ( ( a : any , b : any ) => a . url . localeCompare ( b . url ) ) ;
78- }
79- return this . complexTypeRefs ;
47+ return this . tsIndex
48+ . collectComplexTypes ( )
49+ . map ( ( i ) => i . identifier )
50+ . filter ( ( i ) => this . _checkFilter ( i ) )
51+ . sort ( ( a , b ) => a . url . localeCompare ( b . url ) ) ;
8052 }
8153 public getResources ( ) : Identifier [ ] {
82- if ( this . resourceRefs === undefined ) {
83- this . resourceRefs = this . tsIndex
84- . collectResources ( )
85- . map ( ( t : any ) => t . identifier )
86- . filter ( ( t : any ) => this . _checkFilter ( t ) )
87- . sort ( ( a : any , b : any ) => a . url . localeCompare ( b . url ) ) ;
88- }
89- return this . resourceRefs ;
54+ return this . tsIndex
55+ . collectResources ( )
56+ . map ( ( i ) => i . identifier )
57+ . filter ( ( i ) => this . _checkFilter ( i ) )
58+ . sort ( ( a , b ) => a . url . localeCompare ( b . url ) ) ;
9059 }
9160
9261 private _checkFilter ( type : Identifier ) : boolean {
0 commit comments