Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(renderField): use field.localized #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/renderers/contentful/renderField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ export default function renderField(
type: string,
localization: boolean = false,
): string {
return renderInterfaceProperty(field.id, type, field.required, localization, field.name)
return renderInterfaceProperty(
field.id,
type,
field.required,
localization,
field.localized,
field.name,
)
}
3 changes: 2 additions & 1 deletion src/renderers/contentful/renderLocalizedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export default function renderLocalizedTypes(localization: boolean) {
if (!localization) return null

return `
export type LocalizedField<T> = Partial<Record<LOCALE_CODE, T>>
export type DefaultLocalizedField<T> = Record<CONTENTFUL_DEFAULT_LOCALE_CODE, T>
Copy link
Contributor Author

@zernie zernie Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we're using locale=* even non-localized fields will be rendered as


"field": {
   "<CONTENTFUL_DEFAULT_LOCALE_CODE>": "value"
},

export type LocalizedField<T> = DefaultLocalizedField<T> & Partial<Record<LOCALE_CODE, T>>

// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
export interface Asset {
Expand Down
7 changes: 6 additions & 1 deletion src/renderers/typescript/renderInterfaceProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ export default function renderInterfaceProperty(
type: string,
required: boolean,
localization: boolean,
localized: boolean,
description?: string,
): string {
return [
descriptionComment(description),
name,
required ? "" : "?",
": ",
localization ? `LocalizedField<${type}>` : type,
localization ? renderLocalizedField(localized, type) : type,
required ? "" : " | undefined",
";",
].join("")
}

function renderLocalizedField(localized: boolean, type: string) {
return localized ? `LocalizedField<${type}>` : `DefaultLocalizedField<${type}>`
}

function descriptionComment(description: string | undefined) {
if (description) {
return `/** ${description} */\n`
Expand Down
20 changes: 18 additions & 2 deletions test/renderers/contentful/renderContentType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ describe("renderContentType()", () => {
localized: false,
type: "Symbol",
},
{
id: "localizedSymbolField",
name: "Localized Symbol Field™",
required: true,
validations: [],
disabled: false,
omitted: false,
localized: true,
type: "Symbol",
},
{
id: "arrayField",
name: "Array field",
Expand Down Expand Up @@ -60,6 +70,9 @@ describe("renderContentType()", () => {
/** Symbol Field™ */
symbolField?: string | undefined;

/** Localized Symbol Field™ */
localizedSymbolField: string;

/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[];
}
Expand Down Expand Up @@ -112,10 +125,13 @@ describe("renderContentType()", () => {
expect(format(renderContentType(contentType, true))).toMatchInlineSnapshot(`
"export interface IMyContentTypeFields {
/** Symbol Field™ */
symbolField?: LocalizedField<string> | undefined;
symbolField?: DefaultLocalizedField<string> | undefined;

/** Localized Symbol Field™ */
localizedSymbolField: LocalizedField<string>;

/** Array field */
arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>;
arrayField: DefaultLocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>;
}

export interface IMyContentType extends Entry<IMyContentTypeFields> {
Expand Down
5 changes: 3 additions & 2 deletions test/renderers/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("render()", () => {

export interface IMyContentTypeFields {
/** Array field */
arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
arrayField: DefaultLocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
}

export interface IMyContentType extends Entry<IMyContentTypeFields> {
Expand All @@ -156,7 +156,8 @@ describe("render()", () => {

export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"

export type LocalizedField<T> = Partial<Record<LOCALE_CODE, T>>
export type DefaultLocalizedField<T> = Record<CONTENTFUL_DEFAULT_LOCALE_CODE, T>
export type LocalizedField<T> = DefaultLocalizedField<T> & Partial<Record<LOCALE_CODE, T>>

// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
export interface Asset {
Expand Down
12 changes: 8 additions & 4 deletions test/renderers/typescript/renderInterfaceProperty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@ import renderInterfaceProperty from "../../../src/renderers/typescript/renderInt

describe("renderInterfaceProperty()", () => {
it("works with unrequired properties", () => {
expect(renderInterfaceProperty("property", "type", false, false).trim()).toMatchInlineSnapshot(
expect(renderInterfaceProperty("property", "type", false, false, false).trim()).toMatchInlineSnapshot(
`"property?: type | undefined;"`,
)
})

it("works with required properties", () => {
expect(renderInterfaceProperty("property", "type", true, false).trim()).toMatchInlineSnapshot(
expect(renderInterfaceProperty("property", "type", true, false, false).trim()).toMatchInlineSnapshot(
`"property: type;"`,
)
})

it("adds descriptions", () => {
expect(renderInterfaceProperty("property", "type", false, false, "Description").trim())
expect(renderInterfaceProperty("property", "type", false, false, false,"Description").trim())
.toMatchInlineSnapshot(`
"/** Description */
property?: type | undefined;"
`)
})

it("supports localized fields", () => {
expect(renderInterfaceProperty("property", "type", false, true).trim()).toMatchInlineSnapshot(
expect(renderInterfaceProperty("property", "type", false, true, true).trim()).toMatchInlineSnapshot(
`"property?: LocalizedField<type> | undefined;"`,
)

expect(renderInterfaceProperty("property", "type", false, true, false).trim()).toMatchInlineSnapshot(
`"property?: DefaultLocalizedField<type> | undefined;"`,
)
})
})