1 import type { OpenAPI } from "types"
2 import { singular } from "pluralize"
3 import generateSchemaExamples from "@/utils/generate-schema-examples"
4 import schemaToMarkdown from "./schema-to-markdown"
5 import { MarkdownContext } from "./shared"
6
7 /**
8 * Converts a tag's associated schema (the "{Name} Object") to Markdown,
9 * mirroring `Tags/Section/Schema`: heading, the commerce-modules note, the full
10 * field list, and a complete example (including non-required fields).
11 */
12 export default function schemaSectionToMarkdown(
13 schema: OpenAPI.SchemaObject,
14 tagName: string,
15 ctx: MarkdownContext
16 ): string {
17 const formattedName = singular(tagName).replaceAll(" ", "")
18 const parts: string[] = [`## ${formattedName} Object`]
19
20 parts.push(
21 `> This object's schema is as returned by Medusa's ${ctx.area} API routes. ` +
22 `However, the related model in the Medusa application may support more fields and relations. ` +
23 `To view the models in the Medusa application and their relations, visit the ` +
24 `[Commerce Modules Documentation](https://docs.medusajs.com/resources/commerce-modules).`
25 )
26
27 parts.push(`### Fields\n\n${schemaToMarkdown(schema, ctx)}`)
28
29 const examples = generateSchemaExamples({
30 schema,
31 options: { skipNonRequired: false },
32 })
33 if (examples[0]?.content) {
34 parts.push(
35 `### The ${formattedName} Object\n\n\`\`\`json\n${examples[0].content}\n\`\`\``
36 )
37 }
38
39 return parts.join("\n\n")
40 }