Skip to content

Commit 3073105

Browse files
Merge pull request #32 from contentstack/bug-fix-modular-block
Bug fix in modular block interface
2 parents d4b65c3 + e6865e9 commit 3073105

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

package-lock.json

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/types-generator",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Contentstack type definition generation library",
55
"private": false,
66
"author": "Contentstack",
@@ -42,9 +42,9 @@
4242
"typescript": "^5.4.5"
4343
},
4444
"dependencies": {
45-
"@contentstack/delivery-sdk": "^4.1.0",
45+
"@contentstack/delivery-sdk": "^4.2.0",
4646
"@gql2ts/from-schema": "^2.0.0-4",
47-
"axios": "^1.7.4",
47+
"axios": "^1.7.5",
4848
"lodash": "^4.17.21",
4949
"prettier": "^3.3.3"
5050
},

src/generateTS/factory.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type GlobalFieldCache = {
3131
[prop: string]: { definition: string };
3232
};
3333

34+
type ModularBlockCache = {
35+
[prop: string]: string;
36+
};
37+
3438
enum TypeFlags {
3539
BuiltinJS = 1 << 0,
3640
BuiltinCS = 1 << 1,
@@ -65,7 +69,10 @@ export default function (userOptions: TSGenOptions) {
6569
const visitedGlobalFields = new Set<string>();
6670
const visitedContentTypes = new Set<string>();
6771
const cachedGlobalFields: GlobalFieldCache = {};
72+
const cachedModularBlocks: ModularBlockCache = {};
6873
const modularBlockInterfaces = new Set<string>();
74+
const uniqueBlockInterfaces = new Set<string>();
75+
let counter = 1;
6976

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

191-
function visit_block_names(
192-
field: ContentstackTypes.Field,
193-
except: ContentstackTypes.Block
194-
) {
195-
const uids: string[] = [];
196-
197-
field.blocks.forEach((block) => {
198-
if (block.uid !== except.uid) {
199-
uids.push(`${block.uid}: undefined;`);
200-
}
201-
});
202-
203-
return uids.join("\n");
204-
}
205-
206198
function visit_field_type(field: ContentstackTypes.Field) {
207199
let type = "any";
208200

@@ -280,7 +272,8 @@ export default function (userOptions: TSGenOptions) {
280272
}
281273

282274
function type_modular_blocks(field: ContentstackTypes.Field): string {
283-
const blockInterfaceName = name_type(field.uid);
275+
let blockInterfaceName = name_type(field.uid);
276+
284277
const blockInterfaces = field.blocks.map((block) => {
285278
const fieldType =
286279
block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
@@ -292,6 +285,16 @@ export default function (userOptions: TSGenOptions) {
292285
: `{\n ${fieldType} }`;
293286
return `${block.uid}: ${schema}`;
294287
});
288+
const blockInterfacesKey = blockInterfaces.join(";");
289+
290+
if (!uniqueBlockInterfaces.has(blockInterfacesKey)) {
291+
uniqueBlockInterfaces.add(blockInterfacesKey);
292+
// Keep appending a counter until a unique name is found
293+
while (cachedModularBlocks[blockInterfaceName]) {
294+
blockInterfaceName = `${blockInterfaceName}${counter}`;
295+
counter++;
296+
}
297+
}
295298

296299
const modularInterface = [
297300
`export interface ${blockInterfaceName} {`,

0 commit comments

Comments
 (0)