From fc921697b6afdcad20ac893525c98f2b7b6ab69e Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Tue, 3 Sep 2024 20:02:17 +0530 Subject: [PATCH] fix: duplicate modular block --- src/lib/tsgen/factory.ts | 42 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/lib/tsgen/factory.ts b/src/lib/tsgen/factory.ts index 4c06197..5d13536 100644 --- a/src/lib/tsgen/factory.ts +++ b/src/lib/tsgen/factory.ts @@ -31,6 +31,10 @@ type GlobalFieldCache = { [prop: string]: { definition: string }; }; +type ModularBlockCache = { + [prop: string]: string; +}; + enum TypeFlags { BuiltinJS = 1 << 0, BuiltinCS = 1 << 1, @@ -65,6 +69,7 @@ export default function (userOptions: TSGenOptions) { const visitedGlobalFields = new Set() const visitedContentTypes = new Set() const cachedGlobalFields: GlobalFieldCache = {} + const cachedModularBlocks: ModularBlockCache = {} const modularBlockInterfaces = new Set() const typeMap: TypeMap = { @@ -267,23 +272,26 @@ export default function (userOptions: TSGenOptions) { function type_modular_blocks(field: ContentstackTypes.Field): string { const blockInterfaceName = name_type(field.uid); - const blockInterfaces = field.blocks.map((block) => { - const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)] - ? name_type(block.reference_to) - : visit_fields(block.schema || []); - - const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`; - return `${block.uid}: ${schema}`; - }); - - const modularInterface = [ - `export interface ${blockInterfaceName} {`, - blockInterfaces.join('\n'), - '}', - ].join('\n'); - - // Store or track the generated block interface for later use - modularBlockInterfaces.add(modularInterface); + if(!cachedModularBlocks[blockInterfaceName]){ + const blockInterfaces = field.blocks.map((block) => { + const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)] + ? name_type(block.reference_to) + : visit_fields(block.schema || []); + + const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`; + return `${block.uid}: ${schema}`; + }); + + const modularInterface = [ + `export interface ${blockInterfaceName} {`, + blockInterfaces.join('\n'), + '}', + ].join('\n'); + + // Store or track the generated block interface for later use + modularBlockInterfaces.add(modularInterface); + cachedModularBlocks[blockInterfaceName] = blockInterfaceName; + } return field.multiple ? `${blockInterfaceName}[]` : blockInterfaceName; }