Skip to content

Commit

Permalink
fix: generate types when no en language is defined in i18n (#10181)
Browse files Browse the repository at this point in the history
Previously, if you had for example only `de` in `i18n` -
`generate:types` would fail
Fixes #10145
  • Loading branch information
r1tsuu authored Dec 26, 2024
1 parent 466f109 commit 8debb68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/payload/src/bin/generateTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AcceptedLanguages } from '@payloadcms/translations'

import { initI18n } from '@payloadcms/translations'
import fs from 'fs'
import { compile } from 'json-schema-to-typescript'
Expand All @@ -20,7 +22,12 @@ export async function generateTypes(
if (shouldLog) {
logger.info('Compiling TS types for Collections and Globals...')
}
const i18n = await initI18n({ config: config.i18n, context: 'api', language: 'en' })

const languages = Object.keys(config.i18n.supportedLanguages) as AcceptedLanguages[]

const language = languages.includes('en') ? 'en' : config.i18n.fallbackLanguage

const i18n = await initI18n({ config: config.i18n, context: 'api', language })

const jsonSchema = configToJSONSchema(config, config.db.defaultIDType, i18n)

Expand Down
11 changes: 4 additions & 7 deletions packages/payload/src/utilities/configToJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,9 @@ export function withNullableJSONSchemaType(
}

function entityOrFieldToJsDocs({
config,
entity,
i18n,
}: {
config: SanitizedConfig
entity: FlattenedField | SanitizedCollectionConfig | SanitizedGlobalConfig
i18n?: I18n
}): string | undefined {
Expand All @@ -213,11 +211,10 @@ function entityOrFieldToJsDocs({
if (typeof entity?.admin?.description === 'string') {
description = entity?.admin?.description
} else if (typeof entity?.admin?.description === 'object') {
const defaultLocale = config?.localization ? config?.localization?.defaultLocale : undefined
if (entity?.admin?.description?.en) {
description = entity?.admin?.description?.en
} else if (entity?.admin?.description?.[defaultLocale]) {
description = entity?.admin?.description?.[defaultLocale]
} else if (entity?.admin?.description?.[i18n.language]) {
description = entity?.admin?.description?.[i18n.language]
}
} else if (typeof entity?.admin?.description === 'function' && i18n) {
description = entity?.admin?.description(i18n)
Expand Down Expand Up @@ -255,7 +252,7 @@ export function fieldsToJSONSchema(
requiredFieldNames.add(field.name)
}

const fieldDescription = entityOrFieldToJsDocs({ config, entity: field, i18n })
const fieldDescription = entityOrFieldToJsDocs({ entity: field, i18n })
const baseFieldSchema: JSONSchema4 = {}
if (fieldDescription) {
baseFieldSchema.description = fieldDescription
Expand Down Expand Up @@ -710,7 +707,7 @@ export function entityToJSONSchema(
),
}

const entityDescription = entityOrFieldToJsDocs({ config, entity, i18n })
const entityDescription = entityOrFieldToJsDocs({ entity, i18n })

if (entityDescription) {
jsonSchema.description = entityDescription
Expand Down

0 comments on commit 8debb68

Please sign in to comment.