Skip to content

Commit

Permalink
Merge pull request #32 from contentstack/bug-fix-modular-block
Browse files Browse the repository at this point in the history
Bug fix in modular block interface
  • Loading branch information
praveen-mohan-cs authored Oct 18, 2024
2 parents d4b65c3 + e6865e9 commit 3073105
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/types-generator",
"version": "2.0.0",
"version": "2.0.1",
"description": "Contentstack type definition generation library",
"private": false,
"author": "Contentstack",
Expand Down Expand Up @@ -42,9 +42,9 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@contentstack/delivery-sdk": "^4.1.0",
"@contentstack/delivery-sdk": "^4.2.0",
"@gql2ts/from-schema": "^2.0.0-4",
"axios": "^1.7.4",
"axios": "^1.7.5",
"lodash": "^4.17.21",
"prettier": "^3.3.3"
},
Expand Down
35 changes: 19 additions & 16 deletions src/generateTS/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,7 +69,10 @@ 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 uniqueBlockInterfaces = new Set<string>();
let counter = 1;

const typeMap: TypeMap = {
text: { func: type_text, track: true, flag: TypeFlags.BuiltinJS },
Expand Down Expand Up @@ -188,21 +195,6 @@ export default function (userOptions: TSGenOptions) {
return op_paren(choices.map((v) => get_value(v)).join(" | "));
}

function visit_block_names(
field: ContentstackTypes.Field,
except: ContentstackTypes.Block
) {
const uids: string[] = [];

field.blocks.forEach((block) => {
if (block.uid !== except.uid) {
uids.push(`${block.uid}: undefined;`);
}
});

return uids.join("\n");
}

function visit_field_type(field: ContentstackTypes.Field) {
let type = "any";

Expand Down Expand Up @@ -280,7 +272,8 @@ export default function (userOptions: TSGenOptions) {
}

function type_modular_blocks(field: ContentstackTypes.Field): string {
const blockInterfaceName = name_type(field.uid);
let blockInterfaceName = name_type(field.uid);

const blockInterfaces = field.blocks.map((block) => {
const fieldType =
block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
Expand All @@ -292,6 +285,16 @@ export default function (userOptions: TSGenOptions) {
: `{\n ${fieldType} }`;
return `${block.uid}: ${schema}`;
});
const blockInterfacesKey = blockInterfaces.join(";");

if (!uniqueBlockInterfaces.has(blockInterfacesKey)) {
uniqueBlockInterfaces.add(blockInterfacesKey);
// Keep appending a counter until a unique name is found
while (cachedModularBlocks[blockInterfaceName]) {
blockInterfaceName = `${blockInterfaceName}${counter}`;
counter++;
}
}

const modularInterface = [
`export interface ${blockInterfaceName} {`,
Expand Down

0 comments on commit 3073105

Please sign in to comment.