Skip to content

Add --link-type option #114

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

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
6 changes: 6 additions & 0 deletions src/contentful-typescript-codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const cli = meow(
Assets, or Rich Text. This is useful for ensuring raw
Contentful responses will be compatible with your code.
--localization -l Output fields with localized values
--link-type Output links wrapped in this type, eg LinkType<Entry<...>>

Examples
$ contentful-typescript-codegen -o src/@types/generated/contentful.d.ts
Expand All @@ -35,6 +36,10 @@ const cli = meow(
type: "boolean",
isRequired: false,
},
linkType: {
type: "string",
isRequired: false,
},
poll: {
type: "boolean",
alias: "p",
Expand Down Expand Up @@ -74,6 +79,7 @@ async function runCodegen(outputFile: string) {
output = await render(contentTypes.items, locales.items, {
localization: cli.flags.localization,
namespace: cli.flags.namespace,
linkType: cli.flags.linkType,
})
}

Expand Down
5 changes: 3 additions & 2 deletions src/renderers/contentful/fields/renderArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Field } from "contentful"
import renderSymbol from "./renderSymbol"
import renderLink from "./renderLink"
import renderArrayOf from "../../typescript/renderArrayOf"
import { ContentfulRenderOptions } from "../options"

export default function renderArray(field: Field): string {
export default function renderArray(field: Field, options: ContentfulRenderOptions): string {
if (!field.items) {
throw new Error(`Cannot render non-array field ${field.id} as an array`)
}
Expand All @@ -20,7 +21,7 @@ export default function renderArray(field: Field): string {
}

case "Link": {
return renderArrayOf(renderLink(fieldWithValidations))
return renderArrayOf(renderLink(fieldWithValidations, options))
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/renderers/contentful/fields/renderLink.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Field } from "contentful"
import renderContentTypeId from "../renderContentTypeId"
import { renderUnionValues } from "../../typescript/renderUnion"
import { ContentfulRenderOptions } from "../options"

export default function renderLink(field: Field): string {
export default function renderLink(field: Field, { linkType }: ContentfulRenderOptions): string {
const baseType = renderLinkBaseType(field)
return linkType === undefined ? baseType : `${linkType}<${baseType}>`
}

function renderLinkBaseType(field: Field): string {
if (field.linkType === "Asset") {
return "Asset"
}
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/contentful/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ContentfulRenderOptions {
localization?: boolean
linkType?: string
}

export const DEFAULT_RENDER_OPTIONS: ContentfulRenderOptions = {}
17 changes: 12 additions & 5 deletions src/renderers/contentful/renderContentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import renderNumber from "./fields/renderNumber"
import renderObject from "./fields/renderObject"
import renderRichText from "./fields/renderRichText"
import renderSymbol from "./fields/renderSymbol"
import { ContentfulRenderOptions } from "./options"

export default function renderContentType(contentType: ContentType, localization: boolean): string {
export default function renderContentType(
contentType: ContentType,
options: ContentfulRenderOptions,
): string {
const name = renderContentTypeId(contentType.sys.id)
const fields = renderContentTypeFields(contentType.fields, localization)
const fields = renderContentTypeFields(contentType.fields, options)
const sys = renderSys(contentType.sys)

return `
Expand All @@ -34,11 +38,14 @@ function descriptionComment(description: string | undefined) {
return ""
}

function renderContentTypeFields(fields: Field[], localization: boolean): string {
function renderContentTypeFields(fields: Field[], options: ContentfulRenderOptions): string {
return fields
.filter(field => !field.omitted)
.map<string>(field => {
const functionMap: Record<FieldType, (field: Field) => string> = {
const functionMap: Record<
FieldType,
(field: Field, options: ContentfulRenderOptions) => string
> = {
Array: renderArray,
Boolean: renderBoolean,
Date: renderSymbol,
Expand All @@ -52,7 +59,7 @@ function renderContentTypeFields(fields: Field[], localization: boolean): string
Text: renderSymbol,
}

return renderField(field, functionMap[field.type](field), localization)
return renderField(field, functionMap[field.type](field, options), options.localization)
})
.join("\n\n")
}
Expand Down
4 changes: 3 additions & 1 deletion src/renderers/contentful/renderContentfulImports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default function renderContentfulImports(localization: boolean = false): string {
import { DEFAULT_RENDER_OPTIONS } from "./options"

export default function renderContentfulImports({ localization } = DEFAULT_RENDER_OPTIONS): string {
if (localization) {
return `
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
Expand Down
21 changes: 13 additions & 8 deletions src/renderers/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,44 @@ import renderAllLocales from "./contentful/renderAllLocales"
import renderDefaultLocale from "./contentful/renderDefaultLocale"
import renderNamespace from "./contentful/renderNamespace"
import renderLocalizedTypes from "./contentful/renderLocalizedTypes"
import { ContentfulRenderOptions } from "./contentful/options"

interface Options {
localization?: boolean
namespace?: string
linkType?: string
}

export default async function render(
contentTypes: ContentType[],
locales: Locale[],
{ namespace, localization = false }: Options = {},
{ namespace, linkType, localization = false }: Options = {},
) {
const options = { linkType, localization }
const sortedContentTypes = contentTypes.sort((a, b) => a.sys.id.localeCompare(b.sys.id))
const sortedLocales = locales.sort((a, b) => a.code.localeCompare(b.code))

const typingsSource = [
renderAllContentTypes(sortedContentTypes, localization),
renderAllContentTypes(sortedContentTypes, { linkType, localization }),
renderAllContentTypeIds(sortedContentTypes),
renderAllLocales(sortedLocales),
renderDefaultLocale(sortedLocales),
renderLocalizedTypes(localization),
].join("\n\n")

const source = [
renderContentfulImports(localization),
renderNamespace(typingsSource, namespace),
].join("\n\n")
const source = [renderContentfulImports(options), renderNamespace(typingsSource, namespace)].join(
"\n\n",
)

const prettierConfig = await resolveConfig(process.cwd())
return format(source, { ...prettierConfig, parser: "typescript" })
}

function renderAllContentTypes(contentTypes: ContentType[], localization: boolean): string {
return contentTypes.map(contentType => renderContentType(contentType, localization)).join("\n\n")
function renderAllContentTypes(
contentTypes: ContentType[],
options: ContentfulRenderOptions,
): string {
return contentTypes.map(contentType => renderContentType(contentType, options)).join("\n\n")
}

function renderAllContentTypeIds(contentTypes: ContentType[]): string {
Expand Down
Loading