From 8b78122d5d2c734b2c8c1e91381ed151ddf4d729 Mon Sep 17 00:00:00 2001 From: Marco Gonzalez Date: Wed, 1 May 2024 16:24:15 -0400 Subject: [PATCH] feat: improve content type sys field type WHAT? Improve the `sys` field type on content types so that they re-use the `Sys` type from the `contentful` module. WHY? The `Sys` type can change over time. For example, the current type is missing the `revision`, `space` and `environment` fields. By re-using there's no need to manually keep these types in sync. HOW? `yarn test` --- src/renderers/contentful/renderContentType.ts | 7 +--- .../contentful/renderContentfulImports.ts | 2 +- .../contentful/renderContentType.test.ts | 21 ++--------- .../renderContentfulImports.test.ts | 6 ++-- test/renderers/render.test.ts | 36 +++++-------------- 5 files changed, 16 insertions(+), 56 deletions(-) diff --git a/src/renderers/contentful/renderContentType.ts b/src/renderers/contentful/renderContentType.ts index 456cf8eb..df03d59e 100644 --- a/src/renderers/contentful/renderContentType.ts +++ b/src/renderers/contentful/renderContentType.ts @@ -59,12 +59,7 @@ function renderContentTypeFields(fields: Field[], localization: boolean): string function renderSys(sys: Sys) { return ` - sys: { - id: string; - type: string; - createdAt: string; - updatedAt: string; - locale: string; + sys: Sys & { contentType: { sys: { id: '${sys.id}'; diff --git a/src/renderers/contentful/renderContentfulImports.ts b/src/renderers/contentful/renderContentfulImports.ts index 2472eb6b..74a9146c 100644 --- a/src/renderers/contentful/renderContentfulImports.ts +++ b/src/renderers/contentful/renderContentfulImports.ts @@ -5,7 +5,7 @@ export default function renderContentfulImports( return ` // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { ${localization ? "" : "Asset, "}Entry } from 'contentful' + import { ${localization ? "" : "Asset, "}Entry, Sys } from 'contentful' ${hasRichText ? "import { Document } from '@contentful/rich-text-types'" : ""} ` } diff --git a/test/renderers/contentful/renderContentType.test.ts b/test/renderers/contentful/renderContentType.test.ts index af9df218..84aaca80 100644 --- a/test/renderers/contentful/renderContentType.test.ts +++ b/test/renderers/contentful/renderContentType.test.ts @@ -65,12 +65,7 @@ describe("renderContentType()", () => { } export interface IMyContentType extends Entry { - sys: { - id: string, - type: string, - createdAt: string, - updatedAt: string, - locale: string, + sys: Sys & { contentType: { sys: { id: \\"myContentType\\", @@ -90,12 +85,7 @@ describe("renderContentType()", () => { /** This is a description */ export interface IMyContentType extends Entry { - sys: { - id: string, - type: string, - createdAt: string, - updatedAt: string, - locale: string, + sys: Sys & { contentType: { sys: { id: \\"myContentType\\", @@ -119,12 +109,7 @@ describe("renderContentType()", () => { } export interface IMyContentType extends Entry { - sys: { - id: string, - type: string, - createdAt: string, - updatedAt: string, - locale: string, + sys: Sys & { contentType: { sys: { id: \\"myContentType\\", diff --git a/test/renderers/contentful/renderContentfulImports.test.ts b/test/renderers/contentful/renderContentfulImports.test.ts index f097ee3f..ff0328b4 100644 --- a/test/renderers/contentful/renderContentfulImports.test.ts +++ b/test/renderers/contentful/renderContentfulImports.test.ts @@ -6,7 +6,7 @@ describe("renderContentfulImports()", () => { expect(format(renderContentfulImports())).toMatchInlineSnapshot(` "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Asset, Entry } from \\"contentful\\"; + import { Asset, Entry, Sys } from \\"contentful\\"; import { Document } from \\"@contentful/rich-text-types\\";" `) }) @@ -15,7 +15,7 @@ describe("renderContentfulImports()", () => { expect(format(renderContentfulImports(true))).toMatchInlineSnapshot(` "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Entry } from \\"contentful\\"; + import { Entry, Sys } from \\"contentful\\"; import { Document } from \\"@contentful/rich-text-types\\";" `) }) @@ -24,7 +24,7 @@ describe("renderContentfulImports()", () => { expect(format(renderContentfulImports(true, false))).toMatchInlineSnapshot(` "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Entry } from \\"contentful\\";" + import { Entry, Sys } from \\"contentful\\";" `) }) }) diff --git a/test/renderers/render.test.ts b/test/renderers/render.test.ts index 7c3a8ecd..32b08c66 100644 --- a/test/renderers/render.test.ts +++ b/test/renderers/render.test.ts @@ -55,7 +55,7 @@ describe("render()", () => { expect(await render(contentTypes, locales)).toMatchInlineSnapshot(` "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Asset, Entry } from \\"contentful\\" + import { Asset, Entry, Sys } from \\"contentful\\" export interface IMyContentTypeFields { /** Array field */ @@ -63,12 +63,7 @@ describe("render()", () => { } export interface IMyContentType extends Entry { - sys: { - id: string - type: string - createdAt: string - updatedAt: string - locale: string + sys: Sys & { contentType: { sys: { id: \\"myContentType\\" @@ -121,7 +116,7 @@ describe("render()", () => { expect(await render(contentTypes, locales)).toMatchInlineSnapshot(` "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Asset, Entry } from \\"contentful\\" + import { Asset, Entry, Sys } from \\"contentful\\" import { Document } from \\"@contentful/rich-text-types\\" export interface IMyContentTypeFields { @@ -130,12 +125,7 @@ describe("render()", () => { } export interface IMyContentType extends Entry { - sys: { - id: string - type: string - createdAt: string - updatedAt: string - locale: string + sys: Sys & { contentType: { sys: { id: \\"myContentType\\" @@ -193,7 +183,7 @@ describe("render()", () => { expect(await render(contentTypes, locales, { localization: true })).toMatchInlineSnapshot(` "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Entry } from \\"contentful\\" + import { Entry, Sys } from \\"contentful\\" export interface IMyContentTypeFields { /** Array field */ @@ -201,12 +191,7 @@ describe("render()", () => { } export interface IMyContentType extends Entry { - sys: { - id: string - type: string - createdAt: string - updatedAt: string - locale: string + sys: Sys & { contentType: { sys: { id: \\"myContentType\\" @@ -256,7 +241,7 @@ describe("render()", () => { expect(await render(contentTypes, locales, { namespace: "Codegen" })).toMatchInlineSnapshot(` "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT. - import { Asset, Entry } from \\"contentful\\" + import { Asset, Entry, Sys } from \\"contentful\\" declare namespace Codegen { export interface IMyContentTypeFields { @@ -265,12 +250,7 @@ describe("render()", () => { } export interface IMyContentType extends Entry { - sys: { - id: string - type: string - createdAt: string - updatedAt: string - locale: string + sys: Sys & { contentType: { sys: { id: \\"myContentType\\"