diff --git a/package-lock.json b/package-lock.json index 22d30e4..7c13b8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "@contentstack/types-generator", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/types-generator", - "version": "2.0.0", + "version": "2.0.1", "license": "MIT", "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" }, @@ -611,14 +611,14 @@ } }, "node_modules/@contentstack/delivery-sdk": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/@contentstack/delivery-sdk/-/delivery-sdk-4.1.0.tgz", - "integrity": "sha512-AlYRoRpkEI+AtSf5EKxoAOnfqycF1ji5WnW/3CdF6XOk3VtZmWD6dat3R8BNObo4vuVNq+hWYPMz29EgBiogsQ==", + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/@contentstack/delivery-sdk/-/delivery-sdk-4.3.0.tgz", + "integrity": "sha512-N4hqG6UXKWO5WQyPwZVkJ3gANtnTYoAozIINkLlKu8m48GOUFihFF08Yilvr7smybT2dnoNbkCnlqI64iNPPQA==", "dependencies": { "@contentstack/core": "^1.1.0", "@contentstack/utils": "^1.3.8", "@types/humps": "^2.0.6", - "axios": "^1.7.2", + "axios": "^1.7.4", "dotenv": "^16.3.1", "humps": "^2.0.1" } @@ -2055,9 +2055,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.7.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz", - "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==", + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", "dependencies": { "follow-redirects": "^1.15.6", "form-data": "^4.0.0", diff --git a/package.json b/package.json index b713c5a..bdfbe13 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/src/generateTS/factory.ts b/src/generateTS/factory.ts index 5f135d6..65a7790 100644 --- a/src/generateTS/factory.ts +++ b/src/generateTS/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,7 +69,10 @@ export default function (userOptions: TSGenOptions) { const visitedGlobalFields = new Set(); const visitedContentTypes = new Set(); const cachedGlobalFields: GlobalFieldCache = {}; + const cachedModularBlocks: ModularBlockCache = {}; const modularBlockInterfaces = new Set(); + const uniqueBlockInterfaces = new Set(); + let counter = 1; const typeMap: TypeMap = { text: { func: type_text, track: true, flag: TypeFlags.BuiltinJS }, @@ -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"; @@ -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)] @@ -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} {`,