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
184 changes: 93 additions & 91 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/types-generator",
"version": "2.1.0",
"version": "2.2.0",
"description": "Contentstack type definition generation library",
"private": false,
"author": "Contentstack",
Expand Down Expand Up @@ -29,15 +29,15 @@
},
"homepage": "https://github.com/contentstack/types-generator",
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.0",
"@types/jest": "^29.5.14",
"@types/lodash": "^4.17.15",
"@types/node": "^20.12.7",
"axios-mock-adapter": "^1.22.0",
"cross-env": "^7.0.3",
"dotenv": "^16.4.5",
"jest": "^29.7.0",
"nock": "^13.5.4",
"rollup": "^4.30.1",
"rollup": "^4.34.2",
"ts-jest": "^29.1.2",
"tsup": "^8.0.2",
"typescript": "^5.4.5"
Expand Down
12 changes: 12 additions & 0 deletions src/generateTS/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ export default function (userOptions: TSGenOptions) {
} else if (field.data_type === "blocks") {
// Handle blocks type (unchanged)
fieldType = type_modular_blocks(field);
} else if (field.data_type === "json") {
fieldType = type_json_rte(field);
} else {
// Default handling if fieldType is still empty
fieldType = visit_field_type(field);
Expand Down Expand Up @@ -424,4 +426,14 @@ export default function (userOptions: TSGenOptions) {
function type_taxonomy() {
return `${options?.naming?.prefix}Taxonomy`;
}

function type_json_rte(field: ContentstackTypes.Field) {
return `{
type: string;
uid: string;
_version: number;
attrs: Record<string, any>;
children: JSONRTENode[];
}`;
}
}
8 changes: 5 additions & 3 deletions src/generateTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ export const generateTSFromContentTypes = async ({
},
systemFields,
});

let hasJsonField = false;
for (const contentType of contentTypes) {
const tsgenResult = tsgen(contentType);

hasJsonField = contentType.schema.some(
(field: { data_type: string }) => field.data_type === "json"
);
if (tsgenResult.isGlobalField) {
globalFields.add(tsgenResult.definition);
} else {
Expand All @@ -147,7 +149,7 @@ export const generateTSFromContentTypes = async ({
}
const output = await format(
[
defaultInterfaces(prefix, systemFields).join("\n\n"),
defaultInterfaces(prefix, systemFields, hasJsonField).join("\n\n"),
[...globalFields].join("\n\n"),
definitions.join("\n\n"),
].join("\n\n")
Expand Down
31 changes: 30 additions & 1 deletion src/generateTS/stack/builtins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const defaultInterfaces = (prefix = "", systemFields = false) => {
export const defaultInterfaces = (
prefix = "",
systemFields = false,
hasJsonRte?: boolean
) => {
const defaultInterfaces = [
`export interface ${prefix}PublishDetails {
environment: string;
Expand Down Expand Up @@ -36,6 +40,31 @@ export const defaultInterfaces = (prefix = "", systemFields = false) => {
non_localizable: boolean;
}`,
];
if (hasJsonRte) {
defaultInterfaces.push(
`export interface JSONRTENode {
type: string;
uid: string;
_version: number;
attrs: Record<string, any>;
children?: JSONRTENode[];
text?: string;
bold?: boolean;
italic?: boolean;
underline?: boolean;
src?: string;
alt?: string;
href?: string;
target?: string;
embed?: {
type: string;
uid: string;
_version: number;
attrs: Record<string, any>;
};
};`
);
}
if (systemFields) {
defaultInterfaces.push(
`export interface ${prefix}SystemFields {
Expand Down
83 changes: 83 additions & 0 deletions tests/unit/tsgen/json-rte.ct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const jsonRTEContentType = {
title: "home",
description: "",
options: {
is_page: false,
singleton: false,
sub_title: [
],
title: "title",
},
schema: [
{
data_type: "json",
display_name: "JSON Rich Text Editor",
uid: "json_rte",
field_metadata: {
allow_json_rte: true,
embed_entry: false,
description: "",
default_value: "",
multiline: false,
rich_text_type: "advanced",
options: [
],
},
format: "",
error_messages: {
format: "",
},
reference_to: [
"sys_assets",
],
multiple: false,
non_localizable: false,
unique: false,
mandatory: false,
},
],
uid: "home",
DEFAULT_ACL: {
others: {
read: false,
create: false,
},
users: [
],
},
SYS_ACL: {
others: {
read: false,
create: false,
update: false,
delete: false,
sub_acl: {
read: false,
create: false,
update: false,
delete: false,
publish: false,
},
},
roles: [
],
},
created_at: "2025-01-10T09:18:20.450Z",
updated_at: "2025-02-05T06:23:51.672Z",
inbuilt_class: false,
abilities: {
get_one_object: true,
get_all_objects: true,
create_object: true,
update_object: true,
delete_object: true,
delete_all_objects: true,
},
last_activity: {
},
maintain_revisions: true,
_version: 22,
}

module.exports = {jsonRTEContentType}

29 changes: 29 additions & 0 deletions tests/unit/tsgen/json-rte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const testData = require("./json-rte.ct");

import NullDocumentationGenerator from "../../../src/generateTS/docgen/nulldoc";
import tsgenFactory from "../../../src/generateTS/factory";

const tsgen = tsgenFactory({
docgen: new NullDocumentationGenerator(),
});

describe("JSON RTE", () => {
const result = tsgen(testData.jsonRTEContentType);

test("definition", () => {
expect(result.definition).toMatchInlineSnapshot(`
"export interface Home
{
/** Version */
_version: number;
json_rte?: {
type: string;
uid: string;
_version: number;
attrs: Record<string, any>;
children: JSONRTENode[];
};
}"
`);
});
});