@@ -65,6 +65,7 @@ export default function (userOptions: TSGenOptions) {
6565 const visitedGlobalFields = new Set < string > ( )
6666 const visitedContentTypes = new Set < string > ( )
6767 const cachedGlobalFields : GlobalFieldCache = { }
68+ const modularBlockInterfaces = new Set < string > ( )
6869
6970 const typeMap : TypeMap = {
7071 text : { func : type_text , track : true , flag : TypeFlags . BuiltinJS } ,
@@ -215,6 +216,8 @@ export default function (userOptions: TSGenOptions) {
215216 if ( field . multiple ) {
216217 fieldType += "[]" ;
217218 }
219+ } else if ( field . data_type === 'blocks' ) {
220+ fieldType = type_modular_blocks ( field ) ;
218221 }
219222 return [
220223 field . uid + op_required ( field . mandatory ) + ':' ,
@@ -235,7 +238,7 @@ export default function (userOptions: TSGenOptions) {
235238 function visit_content_type (
236239 contentType : ContentstackTypes . ContentType | ContentstackTypes . GlobalField
237240 ) {
238- return [
241+ const contentTypeInterface = [
239242 options . docgen . interface ( contentType . description ) ,
240243 define_interface ( contentType , options . systemFields ) ,
241244 '{' ,
@@ -246,8 +249,10 @@ export default function (userOptions: TSGenOptions) {
246249 ]
247250 . filter ( v => v )
248251 . join ( '\n' )
249- }
250252
253+ return [ ...modularBlockInterfaces , contentTypeInterface ] . join ( '\n\n' ) ;
254+ }
255+
251256 function visit_modular_block (
252257 field : ContentstackTypes . Field ,
253258 block : ContentstackTypes . Block
@@ -260,11 +265,29 @@ export default function (userOptions: TSGenOptions) {
260265 )
261266 }
262267
263- function type_modular_blocks ( field : ContentstackTypes . Field ) {
264- return op_paren (
265- field . blocks . map ( block => visit_modular_block ( field , block ) ) . join ( ' | ' )
266- )
268+ function type_modular_blocks ( field : ContentstackTypes . Field ) : string {
269+ const blockInterfaceName = name_type ( field . uid ) ;
270+ const blockInterfaces = field . blocks . map ( ( block ) => {
271+ const fieldType = block . reference_to && cachedGlobalFields [ name_type ( block . reference_to ) ]
272+ ? name_type ( block . reference_to )
273+ : visit_fields ( block . schema || [ ] ) ;
274+
275+ const schema = block . reference_to ? `${ fieldType } ;` : `{\n ${ fieldType } }` ;
276+ return `${ block . uid } : ${ schema } ` ;
277+ } ) ;
278+
279+ const modularInterface = [
280+ `export interface ${ blockInterfaceName } {` ,
281+ blockInterfaces . join ( '\n' ) ,
282+ '}' ,
283+ ] . join ( '\n' ) ;
284+
285+ // Store or track the generated block interface for later use
286+ modularBlockInterfaces . add ( modularInterface ) ;
287+
288+ return field . multiple ? `${ blockInterfaceName } []` : blockInterfaceName ;
267289 }
290+
268291
269292 function type_group ( field : ContentstackTypes . Field ) {
270293 return [ '{' , visit_fields ( field . schema ) , '}' ] . filter ( v => v ) . join ( '\n' )
0 commit comments