@@ -71,6 +71,8 @@ export default function (userOptions: TSGenOptions) {
7171 const cachedGlobalFields : GlobalFieldCache = { }
7272 const cachedModularBlocks : ModularBlockCache = { }
7373 const modularBlockInterfaces = new Set < string > ( )
74+ const uniqueBlockInterfaces = new Set < string > ( )
75+ let counter = 1
7476
7577 const typeMap : TypeMap = {
7678 text : { func : type_text , track : true , flag : TypeFlags . BuiltinJS } ,
@@ -178,21 +180,6 @@ export default function (userOptions: TSGenOptions) {
178180 return op_paren ( choices . map ( v => get_value ( v ) ) . join ( ' | ' ) )
179181 }
180182
181- function visit_block_names (
182- field : ContentstackTypes . Field ,
183- except : ContentstackTypes . Block
184- ) {
185- const uids : string [ ] = [ ]
186-
187- field . blocks . forEach ( block => {
188- if ( block . uid !== except . uid ) {
189- uids . push ( `${ block . uid } : undefined;` )
190- }
191- } )
192-
193- return uids . join ( '\n' )
194- }
195-
196183 function visit_field_type ( field : ContentstackTypes . Field ) {
197184 let type = 'any'
198185
@@ -260,28 +247,34 @@ export default function (userOptions: TSGenOptions) {
260247 }
261248
262249 function type_modular_blocks ( field : ContentstackTypes . Field ) : string {
263- const blockInterfaceName = name_type ( field . uid ) ;
264- if ( ! cachedModularBlocks [ blockInterfaceName ] ) {
265- const blockInterfaces = field . blocks . map ( ( block ) => {
266- const fieldType = block . reference_to && cachedGlobalFields [ name_type ( block . reference_to ) ]
267- ? name_type ( block . reference_to )
268- : visit_fields ( block . schema || [ ] ) ;
269-
270- const schema = block . reference_to ? `${ fieldType } ;` : `{\n ${ fieldType } }` ;
271- return `${ block . uid } : ${ schema } ` ;
272- } ) ;
273-
250+ let blockInterfaceName = name_type ( field . uid ) ;
251+
252+ const blockInterfaces = field . blocks . map ( ( block ) => {
253+ const fieldType = block . reference_to && cachedGlobalFields [ name_type ( block . reference_to ) ]
254+ ? name_type ( block . reference_to )
255+ : visit_fields ( block . schema || [ ] ) ;
256+
257+ const schema = block . reference_to ? `${ fieldType } ;` : `{\n ${ fieldType } }` ;
258+ return `${ block . uid } : ${ schema } ` ;
259+ } ) ;
260+ const blockInterfacesKey = blockInterfaces . join ( ';' ) ;
261+
262+ if ( ! uniqueBlockInterfaces . has ( blockInterfacesKey ) ) {
263+ uniqueBlockInterfaces . add ( blockInterfacesKey ) ;
264+ // Keep appending a counter until a unique name is found
265+ while ( cachedModularBlocks [ blockInterfaceName ] ) {
266+ blockInterfaceName = `${ blockInterfaceName } ${ counter } ` ;
267+ counter ++ ;
268+ }
274269 const modularInterface = [
275270 `export interface ${ blockInterfaceName } {` ,
276271 blockInterfaces . join ( '\n' ) ,
277272 '}' ,
278273 ] . join ( '\n' ) ;
279-
280- // Store or track the generated block interface for later use
281274 modularBlockInterfaces . add ( modularInterface ) ;
282275 cachedModularBlocks [ blockInterfaceName ] = blockInterfaceName ;
283276 }
284-
277+
285278 return field . multiple ? `${ blockInterfaceName } []` : blockInterfaceName ;
286279 }
287280
0 commit comments