Skip to content

Commit b330873

Browse files
authored
feat: jsdocs for generated types, by using admin.description (#9917)
This makes use of admin.description to generate JSDocs for field, collection and global generated types. ![image](https://github.com/user-attachments/assets/980d825f-49a2-426d-933a-2ff3d205ea24) ![image](https://github.com/user-attachments/assets/d0b1f288-1ea1-4d80-8c05-003d59a4e41a) For the future, we should add a dedicated property to override these JSDocs. You can view the effect of this PR on our test suite generated types here: 05f552b
1 parent 46e50c4 commit b330873

File tree

21 files changed

+323
-47
lines changed

21 files changed

+323
-47
lines changed

packages/payload/src/admin/RichText.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,11 @@ type RichTextAdapterBase<
215215
}) => void
216216
hooks?: RichTextHooks
217217
i18n?: Partial<GenericLanguages>
218-
outputSchema?: ({
219-
collectionIDFieldTypes,
220-
config,
221-
field,
222-
interfaceNameDefinitions,
223-
isRequired,
224-
}: {
218+
outputSchema?: (args: {
225219
collectionIDFieldTypes: { [key: string]: 'number' | 'string' }
226220
config?: SanitizedConfig
227221
field: RichTextField<Value, AdapterProps, ExtraFieldProperties>
222+
i18n?: I18n
228223
/**
229224
* Allows you to define new top-level interfaces that can be re-used in the output schema.
230225
*/

packages/payload/src/bin/generateTypes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { initI18n } from '@payloadcms/translations'
12
import fs from 'fs'
23
import { compile } from 'json-schema-to-typescript'
34

@@ -19,8 +20,9 @@ export async function generateTypes(
1920
if (shouldLog) {
2021
logger.info('Compiling TS types for Collections and Globals...')
2122
}
23+
const i18n = await initI18n({ config: config.i18n, context: 'api', language: 'en' })
2224

23-
const jsonSchema = configToJSONSchema(config, config.db.defaultIDType)
25+
const jsonSchema = configToJSONSchema(config, config.db.defaultIDType, i18n)
2426

2527
const declare = `declare module 'payload' {\n export interface GeneratedTypes extends Config {}\n}`
2628
const declareWithTSIgnoreError = `declare module 'payload' {\n // @ts-ignore \n export interface GeneratedTypes extends Config {}\n}`

packages/payload/src/collections/config/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export type CollectionAdminOptions = {
329329
*/
330330
defaultColumns?: string[]
331331
/**
332-
* Custom description for collection
332+
* Custom description for collection. This will also be used as JSDoc for the generated types
333333
*/
334334
description?: EntityDescription
335335
enableRichTextLink?: boolean

packages/payload/src/fields/config/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ type Admin = {
280280
condition?: Condition
281281
/** Extension point to add your custom data. Available in server and client. */
282282
custom?: Record<string, any>
283+
/**
284+
* The field description will be displayed next to the field in the admin UI. Additionally,
285+
* we use the field description to generate JSDoc comments for the generated TypeScript types.
286+
*/
283287
description?: Description
284288
disableBulkEdit?: boolean
285289
disabled?: boolean

packages/payload/src/queues/config/generateJobsJSONSchemas.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import type { I18n } from '@payloadcms/translations'
12
import type { JSONSchema4 } from 'json-schema'
23

34
import type { SanitizedConfig } from '../../config/types.js'
45
import type { JobsConfig } from './types/index.js'
56

67
import { fieldsToJSONSchema } from '../../utilities/configToJSONSchema.js'
78
import { flattenAllFields } from '../../utilities/flattenAllFields.js'
8-
99
export function generateJobsJSONSchemas(
1010
config: SanitizedConfig,
1111
jobsConfig: JobsConfig,
@@ -16,6 +16,7 @@ export function generateJobsJSONSchemas(
1616
* if they have custom ID fields.
1717
*/
1818
collectionIDFieldTypes: { [key: string]: 'number' | 'string' },
19+
i18n?: I18n,
1920
): {
2021
definitions?: Map<string, JSONSchema4>
2122
properties?: { tasks: JSONSchema4 }
@@ -43,6 +44,7 @@ export function generateJobsJSONSchemas(
4344
flattenAllFields({ fields: task.inputSchema }),
4445
interfaceNameDefinitions,
4546
config,
47+
i18n,
4648
)
4749

4850
const fullInputJsonSchema: JSONSchema4 = {
@@ -61,6 +63,7 @@ export function generateJobsJSONSchemas(
6163
flattenAllFields({ fields: task.outputSchema }),
6264
interfaceNameDefinitions,
6365
config,
66+
i18n,
6467
)
6568

6669
const fullOutputJsonSchema: JSONSchema4 = {
@@ -127,6 +130,7 @@ export function generateJobsJSONSchemas(
127130
flattenAllFields({ fields: workflow.inputSchema }),
128131
interfaceNameDefinitions,
129132
config,
133+
i18n,
130134
)
131135

132136
const fullInputJsonSchema: JSONSchema4 = {

0 commit comments

Comments
 (0)