Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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