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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/types-generator",
"version": "2.0.3",
"version": "2.1.0",
"description": "Contentstack type definition generation library",
"private": false,
"author": "Contentstack",
Expand Down
65 changes: 45 additions & 20 deletions src/generateTS/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,57 @@ export default function (userOptions: TSGenOptions) {

function visit_field(field: ContentstackTypes.Field) {
let fieldType = "";
if (
field.data_type === "global_field" &&
cachedGlobalFields[name_type(field.reference_to)]
) {
fieldType = name_type(field.reference_to);
// Check if the field is a global field
if (field.data_type === "global_field") {
// Check if the field is cached
const isCached = cachedGlobalFields[name_type(field.reference_to)];

// Generate the referred_content_types array
const referredContentTypes = [
{
title: name_type(field.reference_to),
uid: field.reference_to,
},
];

// Assign the new structure for the global field
fieldType = `referred_content_types: ${JSON.stringify(
referredContentTypes
)}`;

// If it's a multiple field, append '[]' to the fieldType
if (field.multiple) {
fieldType += "[]";
}

// If the field is not cached and there is a reference, update fieldType accordingly
if (!isCached && field.reference_to) {
fieldType = type_reference(field);
}
} else if (field.data_type === "blocks") {
// Handle blocks type (unchanged)
fieldType = type_modular_blocks(field);
} else {
// Default handling if fieldType is still empty
fieldType = visit_field_type(field);
}
return [
field.uid + op_required(field.mandatory) + ":",
fieldType || visit_field_type(field),

// Build and return the final string in the required format
const requiredFlag = op_required(field.mandatory);
const typeModifier =
["isodate", "file", "number"].includes(field.data_type) ||
["radio", "dropdown"].includes(field.display_type)
? field.mandatory
? ""
: "| null"
: "",
";",
].join(" ");
: " | null"
: "";

if (fieldType.startsWith("referred_content_types")) {
// For global_field or referred_content_types, omit field.uid in output
return `${fieldType}`;
}
// Ensure the formatting is correct, and avoid concatenating field.uid directly to a string
return `${field.uid}${requiredFlag}: ${fieldType}${typeModifier};`;
}

function visit_fields(schema: ContentstackTypes.Schema) {
Expand Down Expand Up @@ -275,10 +303,9 @@ export default function (userOptions: TSGenOptions) {
let 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 fieldType = block.reference_to
? name_type(block.reference_to)
: visit_fields(block.schema || []);

const schema = block.reference_to
? `${fieldType};`
Expand Down Expand Up @@ -341,13 +368,11 @@ export default function (userOptions: TSGenOptions) {
function type_global_field(field: ContentstackTypes.GlobalField) {
if (!field.schema) {
throw new Error(
`Schema not found for global field '${field.uid}. Did you forget to include it?`
`Schema not found for global field '${field.uid}'. Did you forget to include it?`
);
}

const name = name_type(field.reference_to);

return name;
return name_type(field.reference_to);
}

function type_reference(field: ContentstackTypes.Field) {
Expand Down
1 change: 0 additions & 1 deletion src/generateTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export const generateTSFromContentTypes = async ({
});
}
}

const output = await format(
[
defaultInterfaces(prefix, systemFields).join("\n\n"),
Expand Down
1 change: 1 addition & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type GlobalField = {
schema: Schema;
schema_type?: string;
_version?: number;
referred_content_types: Array<{ uid: string; title: string }>;
} & FieldOptions;

export type ReferenceField = {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/generateTS/generateTS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe("generateTS function with errors", () => {
});
} catch (err: any) {
expect(err.error_message).toEqual(
"Something went wrong, Schema not found for global field 'global_field. Did you forget to include it?"
"Something went wrong, Schema not found for global field 'global_field'. Did you forget to include it?"
);
}
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/tsgen/boolean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe("builtin boolean field", () => {
{
/** Version */
_version: number;
title: string ;
boolean?: boolean ;
title: string;
boolean?: boolean;
}"
`);
});
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/tsgen/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("default single content block", () => {
{
/** Version */
_version: number;
title: string ;
title: string;
}"
`);
});
Expand All @@ -45,8 +45,8 @@ describe("default single webpage", () => {
{
/** Version */
_version: number;
title: string ;
url: string ;
title: string;
url: string;
}"
`);
});
Expand Down
Loading