Skip to content

Commit

Permalink
feat(rendercontenttype): include "sys" with content type ID in the ge…
Browse files Browse the repository at this point in the history
…nerated interface (#4)

It's helpful to be able to inspect an entry's `sys.contentType.sys.id` field to see what type it is,
especially when a link can be one of many types. This makes the types support such a check.

BREAKING CHANGE: Users who use one content type's interface to accept another content type in a
function (for example, if one content type is a superset of another one) will need to use a union
type and allow all possible content types now.
  • Loading branch information
Steven Petryk authored Oct 9, 2019
1 parent 9d4c46d commit d3b9c44
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 57 deletions.
22 changes: 21 additions & 1 deletion src/renderers/contentful/renderContentType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContentType, Field, FieldType } from "contentful"
import { ContentType, Field, FieldType, Sys } from "contentful"

import renderInterface from "../typescript/renderInterface"
import renderField from "./renderField"
Expand All @@ -18,6 +18,7 @@ export default function renderContentType(contentType: ContentType) {
renderContentTypeId(contentType.sys.id),
renderContentTypeFields(contentType.fields),
contentType.description,
renderSys(contentType.sys),
)
}

Expand All @@ -43,3 +44,22 @@ function renderContentTypeFields(fields: Field[]): string {
})
.join("\n\n")
}

function renderSys(sys: Sys) {
return `
sys: {
id: string;
type: string;
createdAt: string;
updatedAt: string;
locale: string;
contentType: {
sys: {
id: '${sys.id}';
linkType: 'ContentType';
type: 'Link';
}
}
}
`
}
5 changes: 4 additions & 1 deletion src/renderers/typescript/renderInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ export default function renderInterface(
name: string,
fields: string,
description?: string,
contents?: string,
): string {
return `
export interface ${name}Fields {
${fields}
};
${descriptionComment(description)}
export interface ${name} extends Entry<${name}Fields> {};
export interface ${name} extends Entry<${name}Fields> {
${contents || ""}
};
`
}

Expand Down
31 changes: 23 additions & 8 deletions test/renderers/contentful/renderContentType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,30 @@ describe("renderSymbol()", () => {

it("works with miscellaneous field types", () => {
expect(format(renderContentType(contentType))).toMatchInlineSnapshot(`
"export interface IMyContentTypeFields {
/** Symbol Field™ */
symbolField?: string | undefined;
"export interface IMyContentTypeFields {
/** Symbol Field™ */
symbolField?: string | undefined;
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[];
}
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[];
}
export interface IMyContentType extends Entry<IMyContentTypeFields> {}"
`)
export interface IMyContentType extends Entry<IMyContentTypeFields> {
sys: {
id: string,
type: string,
createdAt: string,
updatedAt: string,
locale: string,
contentType: {
sys: {
id: \\"myContentType\\",
linkType: \\"ContentType\\",
type: \\"Link\\"
}
}
};
}"
`)
})
})
41 changes: 28 additions & 13 deletions test/renderers/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,39 @@ describe("render()", () => {
]

expect(await render(contentTypes, locales)).toMatchInlineSnapshot(`
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
import { Asset, Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"
import { Asset, Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"
export interface IMyContentTypeFields {
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
}
export interface IMyContentTypeFields {
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
}
export interface IMyContentType extends Entry<IMyContentTypeFields> {}
export interface IMyContentType extends Entry<IMyContentTypeFields> {
sys: {
id: string
type: string
createdAt: string
updatedAt: string
locale: string
contentType: {
sys: {
id: \\"myContentType\\"
linkType: \\"ContentType\\"
type: \\"Link\\"
}
}
}
}
type CONTENT_TYPE = \\"myContentType\\"
type CONTENT_TYPE = \\"myContentType\\"
type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
"
`)
type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
"
`)
})
})
36 changes: 2 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ debug@^4.0.0, debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3033,7 +3033,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -4341,11 +4341,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -4354,33 +4349,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -4431,11 +4404,6 @@ lodash.map@^4.5.1:
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

0 comments on commit d3b9c44

Please sign in to comment.