Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fileignoreconfig:
ignore_detectors:
- filecontent
- filename: package-lock.json
checksum: 57ee816f79e4f41c43091caf1bcfded44ea48f592e3f437d9aee81e5c8156382
checksum: 06ab85506c97fe9ae2f99461afe8e142c262cc540e240f4f3fc10382bc3cb835
- filename: .husky/pre-commit
checksum: 5baabd7d2c391648163f9371f0e5e9484f8fb90fa2284cfc378732ec3192c193
- filename: src/graphqlTS/index.ts
Expand All @@ -26,4 +26,8 @@ fileignoreconfig:
checksum: d8e9492f9294725f54711be06cef880993ab91a4ece37cf361c34ac18fc18a7c
- filename: tests/integration/graphqlTS/graphqlTS.test.ts
checksum: b111cb55740d871a3031bc0214eb445239cd6f62ebbdd922eb34af47f0714a54
- filename: src/generateTS/stack/builtins.ts
checksum: de5f21a1e1a6e5672231671a275e2e0f97b3eea0f5de4651d827e5afa82dbb86
- filename: tests/unit/tsgen/custom-field.test.ts
checksum: e76d815eaed0481ed928430548bb1b70c392e13db0a712a1365a179997226795
version: "1.0"
510 changes: 230 additions & 280 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/types-generator",
"version": "3.8.0",
"version": "3.9.0",
"description": "Contentstack type definition generation library",
"private": false,
"author": "Contentstack",
Expand Down Expand Up @@ -42,16 +42,16 @@
"jest": "^29.7.0",
"jest-json-reporter": "^1.2.2",
"nock": "^13.5.6",
"rollup": "^4.48.0",
"rollup": "^4.53.3",
"ts-jest": "^29.4.0",
"tsup": "^8.5.0",
"typescript": "^5.7.3"
"tsup": "^8.5.1",
"typescript": "^5.9.3"
},
"dependencies": {
"@contentstack/delivery-sdk": "^4.10.0",
"@contentstack/delivery-sdk": "^4.10.3",
"@gql2ts/from-schema": "^2.0.0-4",
"async": "^3.2.6",
"axios": "^1.12.2",
"axios": "^1.13.2",
"lodash": "^4.17.21",
"prettier": "^3.6.2"
},
Expand Down
62 changes: 54 additions & 8 deletions src/generateTS/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ export default function (userOptions: TSGenOptions) {
const reason = `Unknown field type: ${field.data_type}`;
skippedFields.push({ uid: field.uid, path: field.uid, reason });
logger?.warn(
ERROR_MESSAGES.SKIPPED_FIELD_UNKNOWN_TYPE(field.uid, field.data_type, reason)
ERROR_MESSAGES.SKIPPED_FIELD_UNKNOWN_TYPE(
field.uid,
field.data_type,
reason
)
);
type = "Record<string, unknown>"; // Use Record<string, unknown> for balanced type safety
}
Expand All @@ -295,7 +299,11 @@ export default function (userOptions: TSGenOptions) {
if (exclusionCheck.shouldExclude) {
skippedFields.push(exclusionCheck.record!);
logger?.warn(
ERROR_MESSAGES.SKIPPED_GLOBAL_FIELD_REFERENCE(field.uid, field.reference_to, NUMERIC_IDENTIFIER_EXCLUSION_REASON)
ERROR_MESSAGES.SKIPPED_GLOBAL_FIELD_REFERENCE(
field.uid,
field.reference_to,
NUMERIC_IDENTIFIER_EXCLUSION_REASON
)
);
return "string"; // Use string as fallback for global field references
}
Expand Down Expand Up @@ -349,7 +357,11 @@ export default function (userOptions: TSGenOptions) {
if (exclusionCheck.shouldExclude) {
skippedFields.push(exclusionCheck.record!);
logger?.warn(
ERROR_MESSAGES.SKIPPED_FIELD_AT_PATH(field.uid, fieldPath, NUMERIC_IDENTIFIER_EXCLUSION_REASON)
ERROR_MESSAGES.SKIPPED_FIELD_AT_PATH(
field.uid,
fieldPath,
NUMERIC_IDENTIFIER_EXCLUSION_REASON
)
);
continue;
}
Expand Down Expand Up @@ -412,7 +424,11 @@ export default function (userOptions: TSGenOptions) {
if (exclusionCheck.shouldExclude) {
skippedBlocks.push(exclusionCheck.record!);
logger?.warn(
ERROR_MESSAGES.SKIPPED_BLOCK_AT_PATH(block.uid, blockPath, NUMERIC_IDENTIFIER_EXCLUSION_REASON)
ERROR_MESSAGES.SKIPPED_BLOCK_AT_PATH(
block.uid,
blockPath,
NUMERIC_IDENTIFIER_EXCLUSION_REASON
)
);
return null; // Return null to filter out later
}
Expand All @@ -433,6 +449,12 @@ export default function (userOptions: TSGenOptions) {

// If all blocks were skipped, return a more specific fallback type
if (modularBlockDefinitions.length === 0) {
if (options.systemFields) {
const modularBlocksType = `${options.naming?.prefix || ""}ModularBlocksExtension`;
return field.multiple
? `${modularBlocksType}<Record<string, unknown>>[]`
: `${modularBlocksType}<Record<string, unknown>>`;
}
return field.multiple
? "Record<string, unknown>[]"
: "Record<string, unknown>";
Expand All @@ -445,6 +467,13 @@ export default function (userOptions: TSGenOptions) {
const existingInterfaceName =
blockInterfacesKeyToName[modularBlockSignature];
if (existingInterfaceName) {
// Wrap with ModularBlocks type to add _metadata support only when systemFields is enabled
if (options.systemFields) {
const modularBlocksType = `${options.naming?.prefix || ""}ModularBlocksExtension`;
return field.multiple
? `${modularBlocksType}<${existingInterfaceName}>[]`
: `${modularBlocksType}<${existingInterfaceName}>`;
}
return field.multiple
? `${existingInterfaceName}[]`
: existingInterfaceName;
Expand All @@ -468,6 +497,14 @@ export default function (userOptions: TSGenOptions) {
modularBlockInterfaces.add(modularBlockInterfaceDefinition);
cachedModularBlocks[modularBlockInterfaceName] = modularBlockSignature;
blockInterfacesKeyToName[modularBlockSignature] = modularBlockInterfaceName;

// Wrap with ModularBlocks type to add _metadata support only when systemFields is enabled
if (options.systemFields) {
const modularBlocksType = `${options.naming?.prefix || ""}ModularBlocksExtension`;
return field.multiple
? `${modularBlocksType}<${modularBlockInterfaceName}>[]`
: `${modularBlocksType}<${modularBlockInterfaceName}>`;
}
return field.multiple
? `${modularBlockInterfaceName}[]`
: modularBlockInterfaceName;
Expand Down Expand Up @@ -514,7 +551,10 @@ export default function (userOptions: TSGenOptions) {
if (exclusionCheck.shouldExclude) {
skippedFields.push(exclusionCheck.record!);
logger?.warn(
ERROR_MESSAGES.SKIPPED_GLOBAL_FIELD(field.uid, NUMERIC_IDENTIFIER_EXCLUSION_REASON)
ERROR_MESSAGES.SKIPPED_GLOBAL_FIELD(
field.uid,
NUMERIC_IDENTIFIER_EXCLUSION_REASON
)
);
return "string"; // Use string as fallback for global fields
}
Expand Down Expand Up @@ -560,7 +600,10 @@ export default function (userOptions: TSGenOptions) {
references.push(name_type(v));
} else {
logger?.warn(
ERROR_MESSAGES.SKIPPED_REFERENCE(v, NUMERIC_IDENTIFIER_EXCLUSION_REASON)
ERROR_MESSAGES.SKIPPED_REFERENCE(
v,
NUMERIC_IDENTIFIER_EXCLUSION_REASON
)
);
}
});
Expand All @@ -570,7 +613,10 @@ export default function (userOptions: TSGenOptions) {
references.push(name_type(field.reference_to));
} else {
logger?.warn(
ERROR_MESSAGES.SKIPPED_REFERENCE(field.reference_to, NUMERIC_IDENTIFIER_EXCLUSION_REASON)
ERROR_MESSAGES.SKIPPED_REFERENCE(
field.reference_to,
NUMERIC_IDENTIFIER_EXCLUSION_REASON
)
);
}
}
Expand Down Expand Up @@ -669,7 +715,7 @@ export default function (userOptions: TSGenOptions) {
function type_json_rte(field: ContentstackTypes.Field) {
let json_rte;
if (field.config && field.field_metadata?.extension) {
json_rte = `{ value: { key: string; value: string }[] }`;
json_rte = `unknown`;
} else {
json_rte = `{
type: string;
Expand Down
8 changes: 7 additions & 1 deletion src/generateTS/stack/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export const defaultInterfaces = (

// Conditionally include ReferencedEntry interface
if (includeReferencedEntry) {
const extendsClause = systemFields ? ` extends ${prefix}SystemFields` : "";
defaultInterfaces.push(
`export interface ${prefix}ReferencedEntry {
`export interface ${prefix}ReferencedEntry${extendsClause} {
uid: string;
_content_type_uid: string;
}`
Expand Down Expand Up @@ -124,6 +125,11 @@ export const defaultInterfaces = (
title?: string;
}`
);
defaultInterfaces.push(
`export type ${prefix}ModularBlocksExtension<T> = {
[P in keyof T]?: T[P] & { _metadata?: { uid?: string } };
}`
);
return defaultInterfaces;
} else {
return defaultInterfaces;
Expand Down
1 change: 1 addition & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ export type ContentType = {
reference_to?: string;
data_type?: string;
schema_type?: string;
title?: string;
} & Identifier;
2 changes: 1 addition & 1 deletion tests/unit/tsgen/custom-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("JSON RTE", () => {
{
_version?: number;
title: string;
custom_key_value_pair?: { value: { key: string; value: string }[] };
custom_key_value_pair?: unknown;
}"
`);
});
Expand Down