1- import { List , ResourceInstance , Create , Get } from './fetch' ;
1+ import { List , ResourceInstance , Create , Get } from './fetch' ;
22// Responsible for handling the schema for a given resource.
33class ResourceSchema {
44 schema : any ;
55 singular_name : string ;
66 plural_name : string ;
77 server_url : string ;
8- parents : Map < string , ResourceInstance > ;
8+ parents : Map < string , string > ;
99
1010 constructor (
1111 singular_name : string ,
@@ -20,22 +20,17 @@ class ResourceSchema {
2020 this . parents = new Map ( ) ;
2121 }
2222
23- private substituteUrlParameters ( url : string ) : string {
23+ public substituteUrlParameters ( url : string ) : string {
2424 const paramRegex = / \{ ( [ ^ } ] + ) \} / g;
2525 let match ;
2626 let resultUrl = url ;
2727
2828 while ( ( match = paramRegex . exec ( url ) ) !== null ) {
2929 const paramName = match [ 1 ] ;
30- const parent = this . parents . get ( paramName ) ;
31-
32- if ( ! parent ) {
33- throw new Error ( `Missing required parent resource: ${ paramName } ` ) ;
34- }
30+ const parentId = this . parents . get ( paramName ) ;
3531
36- const parentId = parent . properties . id ;
3732 if ( ! parentId ) {
38- throw new Error ( `Parent resource ${ paramName } has no id property ` ) ;
33+ throw new Error ( `Missing required parent resource: ${ paramName } ` ) ;
3934 }
4035
4136 resultUrl = resultUrl . replace ( `{${ paramName } }` , parentId ) ;
@@ -60,7 +55,7 @@ class ResourceSchema {
6055 const baseUrl = this . base_url ( ) ;
6156 let url = `${ this . server_url } ${ baseUrl } ` ;
6257 if ( this . properties ( ) . find ( prop => prop . name === 'id' ) ) {
63- url += `?id=${ body . id } ` ;
58+ url += `?id=${ body . id } ` ;
6459 }
6560 url = this . substituteUrlParameters ( url ) ;
6661 return Create ( url , body , headers ) ;
@@ -69,7 +64,7 @@ class ResourceSchema {
6964 base_url ( ) : string {
7065 const pattern = this . schema [ "x-aep-resource" ] [ "patterns" ] [ 0 ] ;
7166 const subset = pattern . substring ( 0 , pattern . lastIndexOf ( "/" ) ) ;
72- if ( subset [ 0 ] != "/" ) {
67+ if ( subset [ 0 ] != "/" ) {
7368 return "/" + subset ;
7469 }
7570 return subset ;
@@ -132,6 +127,34 @@ class OpenAPI {
132127 }
133128 throw new Error ( `Resource not found: ${ plural } ` ) ;
134129 }
130+
131+ childResources ( r : ResourceSchema , id : string ) : ResourceSchema [ ] {
132+ const children : ResourceSchema [ ] = [ ] ;
133+ const allResources = this . resources ( ) ;
134+
135+ for ( const resource of allResources ) {
136+ const parents = resource . schema [ "x-aep-resource" ] [ "parents" ] || [ ] ;
137+ // Get all valid parent names (current resource + its parents)
138+ const validParents = new Set ( [ r . singular_name , ...r . parents . keys ( ) ] ) ;
139+
140+ // Check if all parents in the resource's parents array are valid
141+ const allParentsValid =
142+ parents . length === validParents . size &&
143+ parents . every ( parent => validParents . has ( parent ) ) ;
144+
145+ if ( allParentsValid ) {
146+ // Copy parent relationships from the parent resource
147+ for ( const [ key , value ] of r . parents . entries ( ) ) {
148+ resource . parents . set ( key , value ) ;
149+ }
150+ // Add the parent resource's ID
151+ resource . parents . set ( r . singular_name , id ) ;
152+ children . push ( resource ) ;
153+ }
154+ }
155+
156+ return children ;
157+ }
135158}
136159
137160function parseOpenAPI ( jsonString : string ) : OpenAPI {
0 commit comments