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
8 changes: 7 additions & 1 deletion src/generateTS/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,18 @@ export default function (userOptions: TSGenOptions) {
}

function type_json_rte(field: ContentstackTypes.Field) {
return `{
let json_rte;
if (field.config && field.field_metadata?.extension) {
json_rte = `{ value: { key: string; value: string }[] }`;
} else {
json_rte = `{
type: string;
uid: string;
_version: number;
attrs: Record<string, any>;
children: JSONRTENode[];
}`;
}
return json_rte;
}
}
3 changes: 2 additions & 1 deletion src/generateTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export const generateTSFromContentTypes = async ({
for (const contentType of contentTypes) {
const tsgenResult = tsgen(contentType);
hasJsonField = contentType.schema.some(
(field: { data_type: string }) => field.data_type === "json"
(field: { field_metadata: any; data_type: string }) =>
field.data_type === "json" && field.field_metadata.allow_json_rte
);
if (tsgenResult.isGlobalField) {
globalFields.add(tsgenResult.definition);
Expand Down
2 changes: 2 additions & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export type FieldOptions = {
non_localizable: boolean;
max_instance: boolean | undefined;
display_type: string;
config?: Record<string, any>;
field_metadata?: Record<string, any>;
} & Identifier;

export type Block = {
Expand Down
85 changes: 85 additions & 0 deletions tests/unit/tsgen/custom-field.ct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const customField = {
title: "home",
description: "",
options: {
is_page: false,
singleton: false,
sub_title: [
],
title: "title",
},
schema: [
{
data_type: "text",
display_name: "Title",
field_metadata: {
_default: true,
version: 3,
},
mandatory: true,
uid: "title",
unique: true,
multiple: false,
non_localizable: false,
},
{
display_name: "Custom key value pair",
extension_uid: "uid",
field_metadata: {
extension: true,
},
uid: "custom_key_value_pair",
mandatory: false,
non_localizable: false,
unique: false,
config: {
},
data_type: "json",
multiple: 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-05T07:47:19.736Z",
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: 24,
}

module.exports = {customField}

29 changes: 29 additions & 0 deletions tests/unit/tsgen/custom-field.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const testData = require("./custom-field.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.customField);

test("metadata", () => {
const types = result.metadata.types;

expect([...types.javascript]).toEqual(expect.arrayContaining(["string"]));
});
test("definition", () => {
expect(result.definition).toMatchInlineSnapshot(`
"export interface Home
{
/** Version */
_version: number;
title: string;
custom_key_value_pair?: { value: { key: string; value: string }[] };
}"
`);
});
});