Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e9c36b

Browse files
committedJun 3, 2020
fix(renderField): use field.localized
1 parent 96d29a7 commit 5e9c36b

File tree

6 files changed

+45
-11
lines changed

6 files changed

+45
-11
lines changed
 

‎src/renderers/contentful/renderField.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,12 @@ export default function renderField(
66
type: string,
77
localization: boolean = false,
88
): string {
9-
return renderInterfaceProperty(field.id, type, field.required, localization, field.name)
9+
return renderInterfaceProperty(
10+
field.id,
11+
type,
12+
field.required,
13+
localization,
14+
field.localized,
15+
field.name,
16+
)
1017
}

‎src/renderers/contentful/renderLocalizedTypes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ export default function renderLocalizedTypes(localization: boolean) {
33
if (!localization) return null
44

55
return `
6-
export type LocalizedField<T> = Partial<Record<LOCALE_CODE, T>>
6+
export type DefaultLocalizedField<T> = Record<CONTENTFUL_DEFAULT_LOCALE_CODE, T>
7+
export type LocalizedField<T> = DefaultLocalizedField<T> & Partial<Record<LOCALE_CODE, T>>
78
89
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
910
export interface Asset {

‎src/renderers/typescript/renderInterfaceProperty.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ export default function renderInterfaceProperty(
33
type: string,
44
required: boolean,
55
localization: boolean,
6+
localized: boolean,
67
description?: string,
78
): string {
89
return [
910
descriptionComment(description),
1011
name,
1112
required ? "" : "?",
1213
": ",
13-
localization ? `LocalizedField<${type}>` : type,
14+
localization ? renderLocalizedField(localized, type) : type,
1415
required ? "" : " | undefined",
1516
";",
1617
].join("")
1718
}
1819

20+
function renderLocalizedField(localized: boolean, type: string) {
21+
return localized ? `LocalizedField<${type}>` : `DefaultLocalizedField<${type}>`
22+
}
23+
1924
function descriptionComment(description: string | undefined) {
2025
if (description) {
2126
return `/** ${description} */\n`

‎test/renderers/contentful/renderContentType.test.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ describe("renderContentType()", () => {
1818
localized: false,
1919
type: "Symbol",
2020
},
21+
{
22+
id: "localizedSymbolField",
23+
name: "Localized Symbol Field™",
24+
required: true,
25+
validations: [],
26+
disabled: false,
27+
omitted: false,
28+
localized: true,
29+
type: "Symbol",
30+
},
2131
{
2232
id: "arrayField",
2333
name: "Array field",
@@ -60,6 +70,9 @@ describe("renderContentType()", () => {
6070
/** Symbol Field™ */
6171
symbolField?: string | undefined;
6272
73+
/** Localized Symbol Field™ */
74+
localizedSymbolField: string;
75+
6376
/** Array field */
6477
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[];
6578
}
@@ -112,10 +125,13 @@ describe("renderContentType()", () => {
112125
expect(format(renderContentType(contentType, true))).toMatchInlineSnapshot(`
113126
"export interface IMyContentTypeFields {
114127
/** Symbol Field™ */
115-
symbolField?: LocalizedField<string> | undefined;
128+
symbolField?: DefaultLocalizedField<string> | undefined;
129+
130+
/** Localized Symbol Field™ */
131+
localizedSymbolField: LocalizedField<string>;
116132
117133
/** Array field */
118-
arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>;
134+
arrayField: DefaultLocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>;
119135
}
120136
121137
export interface IMyContentType extends Entry<IMyContentTypeFields> {

‎test/renderers/render.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe("render()", () => {
130130
131131
export interface IMyContentTypeFields {
132132
/** Array field */
133-
arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
133+
arrayField: DefaultLocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
134134
}
135135
136136
export interface IMyContentType extends Entry<IMyContentTypeFields> {
@@ -156,7 +156,8 @@ describe("render()", () => {
156156
157157
export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
158158
159-
export type LocalizedField<T> = Partial<Record<LOCALE_CODE, T>>
159+
export type DefaultLocalizedField<T> = Record<CONTENTFUL_DEFAULT_LOCALE_CODE, T>
160+
export type LocalizedField<T> = DefaultLocalizedField<T> & Partial<Record<LOCALE_CODE, T>>
160161
161162
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
162163
export interface Asset {

‎test/renderers/typescript/renderInterfaceProperty.test.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,32 @@ import renderInterfaceProperty from "../../../src/renderers/typescript/renderInt
22

33
describe("renderInterfaceProperty()", () => {
44
it("works with unrequired properties", () => {
5-
expect(renderInterfaceProperty("property", "type", false, false).trim()).toMatchInlineSnapshot(
5+
expect(renderInterfaceProperty("property", "type", false, false, false).trim()).toMatchInlineSnapshot(
66
`"property?: type | undefined;"`,
77
)
88
})
99

1010
it("works with required properties", () => {
11-
expect(renderInterfaceProperty("property", "type", true, false).trim()).toMatchInlineSnapshot(
11+
expect(renderInterfaceProperty("property", "type", true, false, false).trim()).toMatchInlineSnapshot(
1212
`"property: type;"`,
1313
)
1414
})
1515

1616
it("adds descriptions", () => {
17-
expect(renderInterfaceProperty("property", "type", false, false, "Description").trim())
17+
expect(renderInterfaceProperty("property", "type", false, false, false,"Description").trim())
1818
.toMatchInlineSnapshot(`
1919
"/** Description */
2020
property?: type | undefined;"
2121
`)
2222
})
2323

2424
it("supports localized fields", () => {
25-
expect(renderInterfaceProperty("property", "type", false, true).trim()).toMatchInlineSnapshot(
25+
expect(renderInterfaceProperty("property", "type", false, true, true).trim()).toMatchInlineSnapshot(
2626
`"property?: LocalizedField<type> | undefined;"`,
2727
)
28+
29+
expect(renderInterfaceProperty("property", "type", false, true, false).trim()).toMatchInlineSnapshot(
30+
`"property?: DefaultLocalizedField<type> | undefined;"`,
31+
)
2832
})
2933
})

0 commit comments

Comments
 (0)
Please sign in to comment.