From 80e08adf006e3a9e663315c22cd719a239f7cc34 Mon Sep 17 00:00:00 2001 From: kouhei Date: Sat, 26 Feb 2022 15:17:43 +0900 Subject: [PATCH 1/2] fix(renderContentfulImport): Only import Document when needed --- .../contentful/renderContentfulImports.ts | 18 ++++++------------ src/renderers/render.ts | 16 +++++++++++----- test/renderers/render.test.ts | 3 --- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/renderers/contentful/renderContentfulImports.ts b/src/renderers/contentful/renderContentfulImports.ts index cf8a8b98..2472eb6b 100644 --- a/src/renderers/contentful/renderContentfulImports.ts +++ b/src/renderers/contentful/renderContentfulImports.ts @@ -1,17 +1,11 @@ -export default function renderContentfulImports(localization: boolean = false): string { - if (localization) { - return ` - // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - - import { Entry } from 'contentful' - import { Document } from '@contentful/rich-text-types' - ` - } - +export default function renderContentfulImports( + localization: boolean = false, + hasRichText: boolean = true, +): string { return ` // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Asset, Entry } from 'contentful' - import { Document } from '@contentful/rich-text-types' + import { ${localization ? "" : "Asset, "}Entry } from 'contentful' + ${hasRichText ? "import { Document } from '@contentful/rich-text-types'" : ""} ` } diff --git a/src/renderers/render.ts b/src/renderers/render.ts index bddb2d2e..32573af3 100644 --- a/src/renderers/render.ts +++ b/src/renderers/render.ts @@ -2,14 +2,14 @@ import { ContentType, Locale } from "contentful" import { format, resolveConfig } from "prettier" +import renderAllLocales from "./contentful/renderAllLocales" import renderContentfulImports from "./contentful/renderContentfulImports" import renderContentType from "./contentful/renderContentType" -import renderUnion from "./typescript/renderUnion" -import renderAllLocales from "./contentful/renderAllLocales" +import renderContentTypeId from "./contentful/renderContentTypeId" import renderDefaultLocale from "./contentful/renderDefaultLocale" -import renderNamespace from "./contentful/renderNamespace" import renderLocalizedTypes from "./contentful/renderLocalizedTypes" -import renderContentTypeId from "./contentful/renderContentTypeId" +import renderNamespace from "./contentful/renderNamespace" +import renderUnion from "./typescript/renderUnion" interface Options { localization?: boolean @@ -34,7 +34,7 @@ export default async function render( ].join("\n\n") const source = [ - renderContentfulImports(localization), + renderContentfulImports(localization, hasRichText(contentTypes)), renderNamespace(typingsSource, namespace), ].join("\n\n") @@ -59,3 +59,9 @@ function renderEntryType(contentTypes: ContentType[]) { contentTypes.map(contentType => renderContentTypeId(contentType.sys.id)), ) } + +function hasRichText(contentTypes: ContentType[]): boolean { + return contentTypes.some(sortedContentType => + sortedContentType.fields.some(f => !f.omitted && f.type === "RichText"), + ) +} diff --git a/test/renderers/render.test.ts b/test/renderers/render.test.ts index 017a9aeb..c7e2ba49 100644 --- a/test/renderers/render.test.ts +++ b/test/renderers/render.test.ts @@ -56,7 +56,6 @@ describe("render()", () => { "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. import { Asset, Entry } from \\"contentful\\" - import { Document } from \\"@contentful/rich-text-types\\" export interface IMyContentTypeFields { /** Array field */ @@ -128,7 +127,6 @@ describe("render()", () => { "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. import { Entry } from \\"contentful\\" - import { Document } from \\"@contentful/rich-text-types\\" export interface IMyContentTypeFields { /** Array field */ @@ -192,7 +190,6 @@ describe("render()", () => { "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. import { Asset, Entry } from \\"contentful\\" - import { Document } from \\"@contentful/rich-text-types\\" declare namespace Codegen { export interface IMyContentTypeFields { From 148dea459ea9391046643d5397d0292c6daa7906 Mon Sep 17 00:00:00 2001 From: kouhei Date: Sat, 26 Feb 2022 16:24:26 +0900 Subject: [PATCH 2/2] test(renderContentfulImports, renderers): Add tests --- .../renderContentfulImports.test.ts | 8 +++ test/renderers/render.test.ts | 67 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/test/renderers/contentful/renderContentfulImports.test.ts b/test/renderers/contentful/renderContentfulImports.test.ts index 5c8f3c87..f097ee3f 100644 --- a/test/renderers/contentful/renderContentfulImports.test.ts +++ b/test/renderers/contentful/renderContentfulImports.test.ts @@ -19,4 +19,12 @@ describe("renderContentfulImports()", () => { import { Document } from \\"@contentful/rich-text-types\\";" `) }) + + it("renders the top of the codegen file without import 'Document' statement", () => { + expect(format(renderContentfulImports(true, false))).toMatchInlineSnapshot(` + "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. + + import { Entry } from \\"contentful\\";" + `) + }) }) diff --git a/test/renderers/render.test.ts b/test/renderers/render.test.ts index c7e2ba49..7c3a8ecd 100644 --- a/test/renderers/render.test.ts +++ b/test/renderers/render.test.ts @@ -90,6 +90,73 @@ describe("render()", () => { `) }) + it("renders a given content type (with RichText)", async () => { + const contentTypes: ContentType[] = [ + { + sys: { + id: "myContentType", + } as Sys, + fields: [ + { + id: "richTextField", + name: "richText field", + required: true, + validations: [{}], + items: { + type: "Symbol", + validations: [], + }, + disabled: false, + omitted: false, + localized: false, + type: "RichText", + }, + ], + description: "", + displayField: "", + name: "", + toPlainObject: () => ({} as ContentType), + }, + ] + expect(await render(contentTypes, locales)).toMatchInlineSnapshot(` + "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. + + import { Asset, Entry } from \\"contentful\\" + import { Document } from \\"@contentful/rich-text-types\\" + + export interface IMyContentTypeFields { + /** richText field */ + richTextField: Document + } + + export interface IMyContentType extends Entry { + sys: { + id: string + type: string + createdAt: string + updatedAt: string + locale: string + contentType: { + sys: { + id: \\"myContentType\\" + linkType: \\"ContentType\\" + type: \\"Link\\" + } + } + } + } + + export type CONTENT_TYPE = \\"myContentType\\" + + export type IEntry = IMyContentType + + export type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\" + + export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\" + " + `) + }) + it("renders a given localized content type", async () => { const contentTypes: ContentType[] = [ {