-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for locales=* Contentful responses (#24)
Fixes #20 ## Overview Add a new `--localization` flag. When enabled, generated fields will wrapped in `LocalizedField` helper , for example: ```ts export interface IMyContentTypeFields { /** Array field */ arrayField: LocalizedField<(\"one\" | \"of\" | \"the\" | \"above\")[]> } ``` This flag is very useful when entries are being fetched with `locale="*"`. ## Notes * `field.required` is ignored, because contentful api ignores it as well. * we have to use a custom localized version of `Asset`
- Loading branch information
Showing
11 changed files
with
247 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import { Field } from "contentful" | ||
import renderInterfaceProperty from "../typescript/renderInterfaceProperty" | ||
|
||
export default function renderField(field: Field, type: string): string { | ||
return renderInterfaceProperty(field.id, type, field.required, field.name) | ||
export default function renderField( | ||
field: Field, | ||
type: string, | ||
localization: boolean = false, | ||
): string { | ||
return renderInterfaceProperty(field.id, type, field.required, localization, field.name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** renders helper types for --localization flag */ | ||
export default function renderLocalizedTypes(localization: boolean) { | ||
if (!localization) return null | ||
|
||
return ` | ||
export type LocalizedField<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 { | ||
sys: Sys | ||
fields: { | ||
title: LocalizedField<string> | ||
description: LocalizedField<string> | ||
file: LocalizedField<{ | ||
url: string | ||
details: { | ||
size: number | ||
image?: { | ||
width: number | ||
height: number | ||
} | ||
} | ||
fileName: string | ||
contentType: string | ||
}> | ||
} | ||
toPlainObject(): object | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.