Skip to content

Commit

Permalink
Merge pull request #124 from contentstack/fix/DX-1333-duplicate-block
Browse files Browse the repository at this point in the history
Fix/dx 1333 duplicate block
  • Loading branch information
aman19K authored Sep 4, 2024
2 parents b106ee0 + 4c271ae commit f85dd83
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/lib/tsgen/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type GlobalFieldCache = {
[prop: string]: { definition: string };
};

type ModularBlockCache = {
[prop: string]: string;
};

enum TypeFlags {
BuiltinJS = 1 << 0,
BuiltinCS = 1 << 1,
Expand Down Expand Up @@ -65,6 +69,7 @@ export default function (userOptions: TSGenOptions) {
const visitedGlobalFields = new Set<string>()
const visitedContentTypes = new Set<string>()
const cachedGlobalFields: GlobalFieldCache = {}
const cachedModularBlocks: ModularBlockCache = {}
const modularBlockInterfaces = new Set<string>()

const typeMap: TypeMap = {
Expand Down Expand Up @@ -256,23 +261,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;
}
Expand Down

0 comments on commit f85dd83

Please sign in to comment.