Skip to content

Commit d3b9c44

Browse files
author
Steven Petryk
authored
feat(rendercontenttype): include "sys" with content type ID in the generated 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.
1 parent 9d4c46d commit d3b9c44

File tree

5 files changed

+78
-57
lines changed

5 files changed

+78
-57
lines changed

src/renderers/contentful/renderContentType.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContentType, Field, FieldType } from "contentful"
1+
import { ContentType, Field, FieldType, Sys } from "contentful"
22

33
import renderInterface from "../typescript/renderInterface"
44
import renderField from "./renderField"
@@ -18,6 +18,7 @@ export default function renderContentType(contentType: ContentType) {
1818
renderContentTypeId(contentType.sys.id),
1919
renderContentTypeFields(contentType.fields),
2020
contentType.description,
21+
renderSys(contentType.sys),
2122
)
2223
}
2324

@@ -43,3 +44,22 @@ function renderContentTypeFields(fields: Field[]): string {
4344
})
4445
.join("\n\n")
4546
}
47+
48+
function renderSys(sys: Sys) {
49+
return `
50+
sys: {
51+
id: string;
52+
type: string;
53+
createdAt: string;
54+
updatedAt: string;
55+
locale: string;
56+
contentType: {
57+
sys: {
58+
id: '${sys.id}';
59+
linkType: 'ContentType';
60+
type: 'Link';
61+
}
62+
}
63+
}
64+
`
65+
}

src/renderers/typescript/renderInterface.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ export default function renderInterface(
22
name: string,
33
fields: string,
44
description?: string,
5+
contents?: string,
56
): string {
67
return `
78
export interface ${name}Fields {
89
${fields}
910
};
1011
1112
${descriptionComment(description)}
12-
export interface ${name} extends Entry<${name}Fields> {};
13+
export interface ${name} extends Entry<${name}Fields> {
14+
${contents || ""}
15+
};
1316
`
1417
}
1518

test/renderers/contentful/renderContentType.test.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,30 @@ describe("renderSymbol()", () => {
4545

4646
it("works with miscellaneous field types", () => {
4747
expect(format(renderContentType(contentType))).toMatchInlineSnapshot(`
48-
"export interface IMyContentTypeFields {
49-
/** Symbol Field™ */
50-
symbolField?: string | undefined;
48+
"export interface IMyContentTypeFields {
49+
/** Symbol Field™ */
50+
symbolField?: string | undefined;
5151
52-
/** Array field */
53-
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[];
54-
}
52+
/** Array field */
53+
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[];
54+
}
5555
56-
export interface IMyContentType extends Entry<IMyContentTypeFields> {}"
57-
`)
56+
export interface IMyContentType extends Entry<IMyContentTypeFields> {
57+
sys: {
58+
id: string,
59+
type: string,
60+
createdAt: string,
61+
updatedAt: string,
62+
locale: string,
63+
contentType: {
64+
sys: {
65+
id: \\"myContentType\\",
66+
linkType: \\"ContentType\\",
67+
type: \\"Link\\"
68+
}
69+
}
70+
};
71+
}"
72+
`)
5873
})
5974
})

test/renderers/render.test.ts

+28-13
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,39 @@ describe("render()", () => {
5353
]
5454

5555
expect(await render(contentTypes, locales)).toMatchInlineSnapshot(`
56-
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
56+
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
5757
58-
import { Asset, Entry } from \\"contentful\\"
59-
import { Document } from \\"@contentful/rich-text-types\\"
58+
import { Asset, Entry } from \\"contentful\\"
59+
import { Document } from \\"@contentful/rich-text-types\\"
6060
61-
export interface IMyContentTypeFields {
62-
/** Array field */
63-
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
64-
}
61+
export interface IMyContentTypeFields {
62+
/** Array field */
63+
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
64+
}
6565
66-
export interface IMyContentType extends Entry<IMyContentTypeFields> {}
66+
export interface IMyContentType extends Entry<IMyContentTypeFields> {
67+
sys: {
68+
id: string
69+
type: string
70+
createdAt: string
71+
updatedAt: string
72+
locale: string
73+
contentType: {
74+
sys: {
75+
id: \\"myContentType\\"
76+
linkType: \\"ContentType\\"
77+
type: \\"Link\\"
78+
}
79+
}
80+
}
81+
}
6782
68-
type CONTENT_TYPE = \\"myContentType\\"
83+
type CONTENT_TYPE = \\"myContentType\\"
6984
70-
type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
85+
type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
7186
72-
type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
73-
"
74-
`)
87+
type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
88+
"
89+
`)
7590
})
7691
})

yarn.lock

+2-34
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ debug@^4.0.0, debug@^4.1.0, debug@^4.1.1:
18331833
dependencies:
18341834
ms "^2.1.1"
18351835

1836-
debuglog@*, debuglog@^1.0.1:
1836+
debuglog@^1.0.1:
18371837
version "1.0.1"
18381838
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
18391839
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -3033,7 +3033,7 @@ import-local@^2.0.0:
30333033
pkg-dir "^3.0.0"
30343034
resolve-cwd "^2.0.0"
30353035

3036-
imurmurhash@*, imurmurhash@^0.1.4:
3036+
imurmurhash@^0.1.4:
30373037
version "0.1.4"
30383038
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
30393039
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -4341,11 +4341,6 @@ lockfile@^1.0.4:
43414341
dependencies:
43424342
signal-exit "^3.0.2"
43434343

4344-
lodash._baseindexof@*:
4345-
version "3.1.0"
4346-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
4347-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
4348-
43494344
lodash._baseuniq@~4.6.0:
43504345
version "4.6.0"
43514346
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -4354,33 +4349,11 @@ lodash._baseuniq@~4.6.0:
43544349
lodash._createset "~4.0.0"
43554350
lodash._root "~3.0.0"
43564351

4357-
lodash._bindcallback@*:
4358-
version "3.0.1"
4359-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
4360-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
4361-
4362-
lodash._cacheindexof@*:
4363-
version "3.0.2"
4364-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
4365-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
4366-
4367-
lodash._createcache@*:
4368-
version "3.1.2"
4369-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
4370-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
4371-
dependencies:
4372-
lodash._getnative "^3.0.0"
4373-
43744352
lodash._createset@~4.0.0:
43754353
version "4.0.3"
43764354
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
43774355
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
43784356

4379-
lodash._getnative@*, lodash._getnative@^3.0.0:
4380-
version "3.9.1"
4381-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
4382-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
4383-
43844357
lodash._root@~3.0.0:
43854358
version "3.0.1"
43864359
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@@ -4431,11 +4404,6 @@ lodash.map@^4.5.1:
44314404
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
44324405
integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM=
44334406

4434-
lodash.restparam@*:
4435-
version "3.6.1"
4436-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
4437-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
4438-
44394407
lodash.set@^4.3.2:
44404408
version "4.3.2"
44414409
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"

0 commit comments

Comments
 (0)