@@ -31,6 +31,10 @@ type GlobalFieldCache = {
31
31
[ prop : string ] : { definition : string } ;
32
32
} ;
33
33
34
+ type ModularBlockCache = {
35
+ [ prop : string ] : string ;
36
+ } ;
37
+
34
38
enum TypeFlags {
35
39
BuiltinJS = 1 << 0 ,
36
40
BuiltinCS = 1 << 1 ,
@@ -65,7 +69,10 @@ export default function (userOptions: TSGenOptions) {
65
69
const visitedGlobalFields = new Set < string > ( ) ;
66
70
const visitedContentTypes = new Set < string > ( ) ;
67
71
const cachedGlobalFields : GlobalFieldCache = { } ;
72
+ const cachedModularBlocks : ModularBlockCache = { } ;
68
73
const modularBlockInterfaces = new Set < string > ( ) ;
74
+ const uniqueBlockInterfaces = new Set < string > ( ) ;
75
+ let counter = 1 ;
69
76
70
77
const typeMap : TypeMap = {
71
78
text : { func : type_text , track : true , flag : TypeFlags . BuiltinJS } ,
@@ -188,21 +195,6 @@ export default function (userOptions: TSGenOptions) {
188
195
return op_paren ( choices . map ( ( v ) => get_value ( v ) ) . join ( " | " ) ) ;
189
196
}
190
197
191
- function visit_block_names (
192
- field : ContentstackTypes . Field ,
193
- except : ContentstackTypes . Block
194
- ) {
195
- const uids : string [ ] = [ ] ;
196
-
197
- field . blocks . forEach ( ( block ) => {
198
- if ( block . uid !== except . uid ) {
199
- uids . push ( `${ block . uid } : undefined;` ) ;
200
- }
201
- } ) ;
202
-
203
- return uids . join ( "\n" ) ;
204
- }
205
-
206
198
function visit_field_type ( field : ContentstackTypes . Field ) {
207
199
let type = "any" ;
208
200
@@ -280,7 +272,8 @@ export default function (userOptions: TSGenOptions) {
280
272
}
281
273
282
274
function type_modular_blocks ( field : ContentstackTypes . Field ) : string {
283
- const blockInterfaceName = name_type ( field . uid ) ;
275
+ let blockInterfaceName = name_type ( field . uid ) ;
276
+
284
277
const blockInterfaces = field . blocks . map ( ( block ) => {
285
278
const fieldType =
286
279
block . reference_to && cachedGlobalFields [ name_type ( block . reference_to ) ]
@@ -292,6 +285,16 @@ export default function (userOptions: TSGenOptions) {
292
285
: `{\n ${ fieldType } }` ;
293
286
return `${ block . uid } : ${ schema } ` ;
294
287
} ) ;
288
+ const blockInterfacesKey = blockInterfaces . join ( ";" ) ;
289
+
290
+ if ( ! uniqueBlockInterfaces . has ( blockInterfacesKey ) ) {
291
+ uniqueBlockInterfaces . add ( blockInterfacesKey ) ;
292
+ // Keep appending a counter until a unique name is found
293
+ while ( cachedModularBlocks [ blockInterfaceName ] ) {
294
+ blockInterfaceName = `${ blockInterfaceName } ${ counter } ` ;
295
+ counter ++ ;
296
+ }
297
+ }
295
298
296
299
const modularInterface = [
297
300
`export interface ${ blockInterfaceName } {` ,
0 commit comments