@@ -89,10 +89,11 @@ export default function (userOptions: TSGenOptions) {
8989 if ( flag === TypeFlags . BuiltinJS ) {
9090 visitedJSTypes . add ( type )
9191 } else if ( flag === TypeFlags . UserGlobalField ) {
92- visitedGlobalFields . add ( type )
92+ const _type = name_type ( field . reference_to )
93+ visitedGlobalFields . add ( _type )
9394
94- if ( ! cachedGlobalFields [ type ] ) {
95- cachedGlobalFields [ type ] = {
95+ if ( ! cachedGlobalFields [ _type ] ) {
96+ cachedGlobalFields [ _type ] = {
9697 definition : visit_content_type ( field ) ,
9798 }
9899 }
@@ -116,7 +117,7 @@ export default function (userOptions: TSGenOptions) {
116117 function define_interface (
117118 contentType : ContentstackTypes . ContentType | ContentstackTypes . GlobalField
118119 ) {
119- return [ 'export interface' , name_type ( contentType . uid ) ] . join ( ' ' )
120+ return [ 'export interface' , name_type ( contentType . data_type === 'global_field' ? ( contentType . reference_to as string ) : contentType . uid ) ] . join ( ' ' )
120121 }
121122
122123 function op_array ( type : string , field : ContentstackTypes . Field ) {
@@ -195,9 +196,13 @@ export default function (userOptions: TSGenOptions) {
195196 }
196197
197198 function visit_field ( field : ContentstackTypes . Field ) {
199+ let fieldType = ''
200+ if ( field . data_type === 'global_field' && cachedGlobalFields [ name_type ( field . reference_to ) ] ) {
201+ fieldType = name_type ( field . reference_to )
202+ }
198203 return [
199204 field . uid + op_required ( field . mandatory ) + ':' ,
200- visit_field_type ( field ) + ';' ,
205+ fieldType || visit_field_type ( field ) + ';' ,
201206 ] . join ( ' ' )
202207 }
203208
@@ -231,9 +236,7 @@ export default function (userOptions: TSGenOptions) {
231236 ) {
232237 return (
233238 '{' +
234- [ block . uid + ':' , '{' + visit_fields ( block . schema || [ ] ) + '};' ] . join (
235- ' '
236- ) +
239+ [ block . uid + ':' , block . reference_to ? name_type ( block . reference_to as string ) + ';' : '{' + visit_fields ( block . schema || [ ] ) + '};' ] . join ( ' ' ) +
237240 visit_block_names ( field , block ) +
238241 '}'
239242 )
@@ -276,7 +279,7 @@ export default function (userOptions: TSGenOptions) {
276279 )
277280 }
278281
279- const name = name_type ( field . uid )
282+ const name = name_type ( field . reference_to )
280283
281284 return name
282285 }
@@ -293,7 +296,19 @@ export default function (userOptions: TSGenOptions) {
293296 return [ '(' , references . join ( ' | ' ) , ')' , '[]' ] . join ( '' )
294297 }
295298
296- return function ( contentType : ContentstackTypes . ContentType ) : TSGenResult {
299+ return function ( contentType : ContentstackTypes . ContentType ) : TSGenResult | any {
300+ if ( contentType . schema_type === 'global_field' ) {
301+ const name = name_type ( contentType . uid )
302+ if ( ! cachedGlobalFields [ name ] ) {
303+ cachedGlobalFields [ name ] = {
304+ definition : visit_content_type ( contentType ) ,
305+ }
306+ }
307+ return {
308+ definition : cachedGlobalFields [ name ] . definition ,
309+ isGlobalField : true ,
310+ }
311+ }
297312 return {
298313 definition : visit_content_type ( contentType ) ,
299314 metadata : {
0 commit comments