Skip to content

Commit 8bbedc1

Browse files
committed
fix(render): Extract Asset, Localized field helper types
1 parent 6b56db3 commit 8bbedc1

File tree

8 files changed

+90
-100
lines changed

8 files changed

+90
-100
lines changed

src/renderers/contentful/renderContentfulImports.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,6 @@ export default function renderContentfulImports(localization: boolean = false):
55
66
import { Entry } from 'contentful'
77
import { Document } from '@contentful/rich-text-types'
8-
9-
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
10-
interface Asset {
11-
sys: Sys;
12-
fields: {
13-
title: string;
14-
description: string;
15-
file: {
16-
url: string;
17-
details: {
18-
size: number;
19-
image?: {
20-
width: number;
21-
height: number;
22-
};
23-
};
24-
fileName: string;
25-
contentType: string;
26-
};
27-
};
28-
toPlainObject(): object;
29-
}
308
`
319
}
3210

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/** renders helper types for --localization flag */
2+
export default function renderLocalizedTypes(localization: boolean) {
3+
if (!localization) return null
4+
5+
return `
6+
export type LocalizedField<T> = Partial<Record<LOCALE_CODE, T>>
7+
8+
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
9+
export interface Asset {
10+
sys: Sys
11+
fields: {
12+
title: string
13+
description: string
14+
file: {
15+
url: string
16+
details: {
17+
size: number
18+
image?: {
19+
width: number
20+
height: number
21+
}
22+
}
23+
fileName: string
24+
contentType: string
25+
}
26+
}
27+
toPlainObject(): object
28+
}
29+
`
30+
}

src/renderers/render.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import renderContentType from "./contentful/renderContentType"
77
import renderUnion from "./typescript/renderUnion"
88
import renderAllLocales from "./contentful/renderAllLocales"
99
import renderDefaultLocale from "./contentful/renderDefaultLocale"
10+
import renderLocalizedTypes from "./contentful/renderLocalizedTypes"
1011

1112
interface Options {
1213
localization?: boolean
@@ -26,6 +27,7 @@ export default async function render(
2627
renderAllContentTypeIds(sortedContentTypes),
2728
renderAllLocales(sortedLocales),
2829
renderDefaultLocale(sortedLocales),
30+
renderLocalizedTypes(localization),
2931
].join("\n\n")
3032

3133
const prettierConfig = await resolveConfig(process.cwd())

src/renderers/typescript/renderInterfaceProperty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function renderInterfaceProperty(
1010
name,
1111
required ? "" : "?",
1212
": ",
13-
localization ? `Record<LOCALE_CODE, ${type}>` : type,
13+
localization ? `LocalizedField<${type}>` : type,
1414
required ? "" : " | undefined",
1515
";",
1616
].join("")

test/renderers/contentful/renderContentType.test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,30 @@ describe("renderContentType()", () => {
110110

111111
it("works with localized fields", () => {
112112
expect(format(renderContentType(contentType, true))).toMatchInlineSnapshot(`
113-
"export interface IMyContentTypeFields {
114-
/** Symbol Field™ */
115-
symbolField?: Record<LOCALE_CODE, string> | undefined;
113+
"export interface IMyContentTypeFields {
114+
/** Symbol Field™ */
115+
symbolField?: LocalizedField<string> | undefined;
116116
117-
/** Array field */
118-
arrayField: Record<LOCALE_CODE, (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>;
119-
}
117+
/** Array field */
118+
arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>;
119+
}
120120
121-
export interface IMyContentType extends Entry<IMyContentTypeFields> {
122-
sys: {
123-
id: string,
124-
type: string,
125-
createdAt: string,
126-
updatedAt: string,
127-
locale: string,
128-
contentType: {
129-
sys: {
130-
id: \\"myContentType\\",
131-
linkType: \\"ContentType\\",
132-
type: \\"Link\\"
133-
}
134-
}
135-
};
136-
}"
137-
`)
121+
export interface IMyContentType extends Entry<IMyContentTypeFields> {
122+
sys: {
123+
id: string,
124+
type: string,
125+
createdAt: string,
126+
updatedAt: string,
127+
locale: string,
128+
contentType: {
129+
sys: {
130+
id: \\"myContentType\\",
131+
linkType: \\"ContentType\\",
132+
type: \\"Link\\"
133+
}
134+
}
135+
};
136+
}"
137+
`)
138138
})
139139
})

test/renderers/contentful/renderContentfulImports.test.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,19 @@ import format from "../../support/format"
44
describe("renderContentfulImports()", () => {
55
it("renders the top of the codegen file", () => {
66
expect(format(renderContentfulImports())).toMatchInlineSnapshot(`
7-
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
7+
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
88
9-
import { Asset, Entry } from \\"contentful\\";
10-
import { Document } from \\"@contentful/rich-text-types\\";"
11-
`)
9+
import { Asset, Entry } from \\"contentful\\";
10+
import { Document } from \\"@contentful/rich-text-types\\";"
11+
`)
1212
})
1313

1414
it("renders the localized top of the codegen file", () => {
1515
expect(format(renderContentfulImports(true))).toMatchInlineSnapshot(`
1616
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
1717
1818
import { Entry } from \\"contentful\\";
19-
import { Document } from \\"@contentful/rich-text-types\\";
20-
21-
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
22-
interface Asset {
23-
sys: Sys;
24-
fields: {
25-
title: string,
26-
description: string,
27-
file: {
28-
url: string,
29-
details: {
30-
size: number,
31-
image?: {
32-
width: number,
33-
height: number
34-
}
35-
},
36-
fileName: string,
37-
contentType: string
38-
}
39-
};
40-
toPlainObject(): object;
41-
}"
19+
import { Document } from \\"@contentful/rich-text-types\\";"
4220
`)
4321
})
4422
})

test/renderers/render.test.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,31 +128,9 @@ describe("render()", () => {
128128
import { Entry } from \\"contentful\\"
129129
import { Document } from \\"@contentful/rich-text-types\\"
130130
131-
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
132-
interface Asset {
133-
sys: Sys
134-
fields: {
135-
title: string
136-
description: string
137-
file: {
138-
url: string
139-
details: {
140-
size: number
141-
image?: {
142-
width: number
143-
height: number
144-
}
145-
}
146-
fileName: string
147-
contentType: string
148-
}
149-
}
150-
toPlainObject(): object
151-
}
152-
153131
export interface IMyContentTypeFields {
154132
/** Array field */
155-
arrayField: Record<LOCALE_CODE, (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
133+
arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
156134
}
157135
158136
export interface IMyContentType extends Entry<IMyContentTypeFields> {
@@ -177,6 +155,30 @@ describe("render()", () => {
177155
export type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
178156
179157
export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
158+
159+
export type LocalizedField<T> = Partial<Record<LOCALE_CODE, T>>
160+
161+
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
162+
export interface Asset {
163+
sys: Sys
164+
fields: {
165+
title: string
166+
description: string
167+
file: {
168+
url: string
169+
details: {
170+
size: number
171+
image?: {
172+
width: number
173+
height: number
174+
}
175+
}
176+
fileName: string
177+
contentType: string
178+
}
179+
}
180+
toPlainObject(): object
181+
}
180182
"
181183
`)
182184
})

test/renderers/typescript/renderInterfaceProperty.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ describe("renderInterfaceProperty()", () => {
1616
it("adds descriptions", () => {
1717
expect(renderInterfaceProperty("property", "type", false, false, "Description").trim())
1818
.toMatchInlineSnapshot(`
19-
"/** Description */
20-
property?: type | undefined;"
21-
`)
19+
"/** Description */
20+
property?: type | undefined;"
21+
`)
2222
})
2323

2424
it("supports localized fields", () => {
2525
expect(renderInterfaceProperty("property", "type", false, true).trim()).toMatchInlineSnapshot(
26-
`"property?: Record<LOCALE_CODE, type> | undefined;"`,
26+
`"property?: LocalizedField<type> | undefined;"`,
2727
)
2828
})
2929
})

0 commit comments

Comments
 (0)