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

feat: jsdocs for generated types, by using admin.description #9917

Merged
merged 6 commits into from
Dec 20, 2024
Merged
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: 2 additions & 7 deletions packages/payload/src/admin/RichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,11 @@ type RichTextAdapterBase<
}) => void
hooks?: RichTextHooks
i18n?: Partial<GenericLanguages>
outputSchema?: ({
collectionIDFieldTypes,
config,
field,
interfaceNameDefinitions,
isRequired,
}: {
outputSchema?: (args: {
collectionIDFieldTypes: { [key: string]: 'number' | 'string' }
config?: SanitizedConfig
field: RichTextField<Value, AdapterProps, ExtraFieldProperties>
i18n?: I18n
/**
* Allows you to define new top-level interfaces that can be re-used in the output schema.
*/
Expand Down
4 changes: 3 additions & 1 deletion packages/payload/src/bin/generateTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { initI18n } from '@payloadcms/translations'
import fs from 'fs'
import { compile } from 'json-schema-to-typescript'

Expand All @@ -19,8 +20,9 @@ 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 jsonSchema = configToJSONSchema(config, config.db.defaultIDType)
const jsonSchema = configToJSONSchema(config, config.db.defaultIDType, i18n)

const declare = `declare module 'payload' {\n export interface GeneratedTypes extends Config {}\n}`
const declareWithTSIgnoreError = `declare module 'payload' {\n // @ts-ignore \n export interface GeneratedTypes extends Config {}\n}`
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/collections/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export type CollectionAdminOptions = {
*/
defaultColumns?: string[]
/**
* Custom description for collection
* Custom description for collection. This will also be used as JSDoc for the generated types
*/
description?: EntityDescription
enableRichTextLink?: boolean
Expand Down
4 changes: 4 additions & 0 deletions packages/payload/src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ type Admin = {
condition?: Condition
/** Extension point to add your custom data. Available in server and client. */
custom?: Record<string, any>
/**
* The field description will be displayed next to the field in the admin UI. Additionally,
* we use the field description to generate JSDoc comments for the generated TypeScript types.
*/
description?: Description
disableBulkEdit?: boolean
disabled?: boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { I18n } from '@payloadcms/translations'
import type { JSONSchema4 } from 'json-schema'

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

import { fieldsToJSONSchema } from '../../utilities/configToJSONSchema.js'
import { flattenAllFields } from '../../utilities/flattenAllFields.js'

export function generateJobsJSONSchemas(
config: SanitizedConfig,
jobsConfig: JobsConfig,
Expand All @@ -16,6 +16,7 @@ export function generateJobsJSONSchemas(
* if they have custom ID fields.
*/
collectionIDFieldTypes: { [key: string]: 'number' | 'string' },
i18n?: I18n,
): {
definitions?: Map<string, JSONSchema4>
properties?: { tasks: JSONSchema4 }
Expand Down Expand Up @@ -43,6 +44,7 @@ export function generateJobsJSONSchemas(
flattenAllFields({ fields: task.inputSchema }),
interfaceNameDefinitions,
config,
i18n,
)

const fullInputJsonSchema: JSONSchema4 = {
Expand All @@ -61,6 +63,7 @@ export function generateJobsJSONSchemas(
flattenAllFields({ fields: task.outputSchema }),
interfaceNameDefinitions,
config,
i18n,
)

const fullOutputJsonSchema: JSONSchema4 = {
Expand Down Expand Up @@ -127,6 +130,7 @@ export function generateJobsJSONSchemas(
flattenAllFields({ fields: workflow.inputSchema }),
interfaceNameDefinitions,
config,
i18n,
)

const fullInputJsonSchema: JSONSchema4 = {
Expand Down
Loading
Loading