1 # Resources app — References architecture
2
3 > **Shipping something new?** Add it to the docs homepage's What's New list in
4 > `www/apps/book/components/Homepage/WhatsNewSection/data.ts`. See
5 > [`www/apps/book/CLAUDE.md`](../book/CLAUDE.md) for the entry format and rules.
6
7 This documents how the API **references** (`/references/...`) are generated and
8 served. Non-reference pages in this app are ordinary MDX under `app/`; this file
9 is only about references.
10
11 ## TL;DR
12
13 References are generated from TypeScript source into a **JSON doc-model** (one
14 `page.json` per page under `references/`), stored in R2, fetched at request
15 time, and rendered with React components. There is **no MDX** in the references
16 pipeline anymore — neither as a generated artifact nor at serve time. Internal
17 links are resolved to their final slugs at generation time, so there is no
18 runtime link-fixing.
19
20 ## Data model: `DocPage` / `DocBlock`
21
22 Each reference page serializes to a `DocPage`: `{ slug, title, frontmatter, toc,
23 blocks }`, where `blocks` is an ordered list of typed `DocBlock`s (`markdown`,
24 `heading`, `typeList`, `codeTabs`, `workflowDiagram`, `note`, `sourceCodeLink`,
25 `linkList`, `table`, `badges`, `workflowEvents`). Type/link data is structured
26 JSON; links are already final site URLs.
27
28 The contract is defined **twice** (separate yarn workspace roots — keep in sync):
29
30 - Generator side: `www/utils/packages/types/lib/index.d.ts` (`DocPage`, `DocBlock`, ...)
31 - Site side: `www/packages/types/src/references.ts` (reuses `Workflow` + `FrontMatter`)
32
33 ## Generation (in `www/utils`)
34
35 Run from `www/utils/packages/typedoc-generate-references`:
36 `yarn start generate all --merge` (or `yarn generate:references` from `www/utils`).
37
38 Two phases, both TypeDoc:
39
40 1. **Generate** — per reference, convert TS source to a TypeDoc reflection JSON
41 in `www/utils/generated/typedoc-json-output/<name>.json`. Enriched by the
42 converter plugins `typedoc-plugin-custom` (DML, internal-resolve, namespaces,
43 events) and `typedoc-plugin-workflows` (workflow/step/hook parsing). These
44 are unchanged by the JSON migration.
45 2. **Merge** — read all reflection JSON back in (`entryPointStrategy: "merge"`)
46 and render the doc-model into `www/apps/resources/references/` (`page.json`).
47
48 The renderer is the **`json` theme** in
49 `www/utils/packages/typedoc-plugin-medusa-theme`:
50
51 - `src/json-theme.ts` — `JsonTheme extends MarkdownTheme`. It **reuses**
52 `MarkdownTheme`'s URL/slug/mapping logic, the registered Handlebars helpers,
53 and `reflection-formatter` (the `TypeList` data). It only overrides output
54 (`.json`), `render` (returns `JSON.stringify(DocPage)`), and `getRelativeUrl`
55 (returns each target's final slug — this is what pre-resolves links).
56 - `src/utils/build-doc-page.ts` — builds a `DocPage` from a reflection (title,
57 frontmatter, prose, TypeList for params/returns/properties, linkList for
58 namespaces/modules, DML entities, workflow dispatch).
59 - `src/utils/build-workflow-blocks.ts` — workflow/step pages (diagram, input,
60 output, hooks, emitted events).
61 - `src/utils/parse-code-tabs.ts` — splits helper-produced MDX fragments into
62 structured blocks (`<CodeTabs>`, `<TypeList>`, `:::note`, headings, ...).
63 - `src/utils/resolve-page-slug.ts` — builds the page-URL -> final-slug map.
64
65 **Important:** `MarkdownTheme` and the Handlebars `.hbs` templates still exist in
66 that package — they are the generation-engine internals `JsonTheme` extends, not
67 MDX output. Do not delete them.
68
69 Merge options: `src/constants/merger-json-options.ts` (theme `json`, `out` =
70 `references/`, entry `_index.json`); it extends the shared `merger-options.ts`
71 (the regex-keyed `formatting` map, `allowedProjectDocuments`, etc.).
72
73 ## Build-time prep (in this app, `yarn prep`)
74
75 - `scripts/generate-files-map.mjs` — scans `references/` for `page.json` and
76 keys each entry by the DocPage's baked-in `slug` (so slug-overridden pages
77 resolve with a direct lookup); also scans `app/` pages. Output:
78 `generated/files-map.mjs`.
79 - `scripts/generate-slug-changes.mjs` — app pages only (references no longer
80 need slug-changes; their slug is baked in).
81
82 ## Serving
83
84 - `app/references/[...slug]/page.tsx` — renders `components/ReferenceJSON`
85 (client) and reads page metadata from the DocPage.
86 - `components/ReferenceJSON` — SWR-fetches `/api/references/<slug>` and renders
87 it with `ReferenceContent` from `docs-ui`.
88 - `app/api/references/[...slug]/route.ts` — resolves slug -> `page.json` via
89 `files-map`, loads it (R2 binding `REFERENCES_R2_BUCKET` -> public URL ->
90 local fs), and returns the DocPage JSON **as-is**. No `serialize()`, no
91 link-fixer plugins. Cached (`unstable_cache` + `Cache-Control` +
92 OpenNext R2 incremental cache).
93 - `docs-ui` `ReferenceContent` (`www/packages/docs-ui/src/components/ReferenceContent`)
94 maps each `DocBlock` to a component (`TypeList`, `CodeTabs`/`CodeBlock`,
95 `WorkflowDiagram`, `Note`, `SourceCodeLink`, `MarkdownContent`, headings,
96 linkList, `WorkflowEvents`).
97
98 ## Markdown output (`md-content`, for LLMs / copy-as-markdown)
99
100 `app/md-content/[[...slug]]/route.ts` serves plain Markdown. For `page.json`
101 files it uses `docPageToMarkdown` (`www/packages/docs-utils/src/doc-page-to-markdown.ts`),
102 the doc-model analogue of `getCleanMd`; app pages still use `getCleanMd`.
103
104 ## R2 storage / CI
105
106 - Keys: `resources/references/<relPath>/page.json` in bucket `docs-references`,
107 read via the `REFERENCES_R2_BUCKET` binding (see `wrangler.jsonc`).
108 - `scripts/upload-references-to-r2.mjs` — full or selective (`--upload`/`--remove`)
109 upload of `references/` `page.json`.
110 - `.github/workflows/sync-resources-references-to-r2.yml` — syncs changed
111 `references/**` `page.json` to R2 on push to `develop`.
112 - `.github/workflows/generate-public-references.yml` — regenerates references
113 and opens the automated PR (commits `references/**`).
114
115 ## Gotchas
116
117 - Links in the doc-model are final site slugs (`/references/...`); `md-content`
118 absolutizes them with `NEXT_PUBLIC_BASE_URL`.
119 - `react-query` reference rendering exists in the theme but is not exercised —
120 no reference in the current set produces `UseMutationResult` /
121 `UseQueryOptionsWrapper` types.
122 - Generating the module `*-models` references needs `eslint-plugin-markdown`
123 installed in the generation environment (used by the DML/eslint converter
124 plugin); the merge step itself does not.