|
1
|
import { Sidebar } from "types" |
|
2
|
import { ItemsToAdd } from "../index.js" |
|
3
|
import path from "path" |
|
4
|
import { pathToFileURL } from "url" |
|
5
|
import pkg from "pluralize" |
|
6
|
|
|
7
|
const { singular } = pkg |
|
8
|
|
|
9
|
type GeneratedOperation = { |
|
10
|
slug: string |
|
11
|
path: string |
|
12
|
oldHash: string |
|
13
|
title: string |
|
14
|
method: string |
|
15
|
} |
|
16
|
|
|
17
|
type GeneratedTag = { |
|
18
|
name: string |
|
19
|
path: string |
|
20
|
schemaPath: string | null |
|
21
|
operations: Record<string, GeneratedOperation> |
|
22
|
} |
|
23
|
|
|
24
|
type ApiRefPathsModule = { |
|
25
|
apiRefIntroSections: Record<string, { slug: string; title: string }[]> |
|
26
|
apiRefPaths: Record< |
|
27
|
string, |
|
28
|
{ intro: Record<string, string>; tags: Record<string, GeneratedTag> } |
|
29
|
> |
|
30
|
} |
|
31
|
|
|
32
|
// Node-only dynamic import of the app's generated map (resolved from cwd at |
|
33
|
// prep time). Wrapped in `new Function` so bundlers/analyzers (e.g. webpack |
|
34
|
// tracing next.config's dependency graph) don't try to statically resolve the |
|
35
|
// computed specifier and emit a build-dependency warning. |
|
36
|
const dynamicImport = new Function("specifier", "return import(specifier)") as ( |
|
37
|
specifier: string |
|
38
|
) => Promise<unknown> |
|
39
|
|
|
40
|
// Mirror of the api-reference `MethodLabel` component so the always-visible |
|
41
|
// sidebar can render method badges from serializable data. |
|
42
|
function getMethodBadge(method: string): Sidebar.SidebarItemLink["badge"] { |
|
43
|
const lower = method.toLowerCase() |
|
44
|
return { |
|
45
|
variant: lower === "get" ? "green" : lower === "post" ? "blue" : "red", |
|
46
|
text: |
|
47
|
lower === "delete" |
|
48
|
? "Del" |
|
49
|
: lower.charAt(0).toUpperCase() + lower.slice(1), |
|
50
|
} |
|
51
|
} |
|
52
|
|
|
53
|
export default async function getApiRefSidebarChildren( |
|
54
|
sidebar?: Sidebar.RawSidebar |
|
55
|
): Promise<ItemsToAdd[]> { |
|
56
|
if (!sidebar) { |
|
57
|
return [] |
|
58
|
} |
|
59
|
|
|
60
|
const projPath = path.resolve() |
|
61
|
const area = sidebar.sidebar_id |
|
62
|
|
|
63
|
const { apiRefIntroSections, apiRefPaths } = (await dynamicImport( |
|
64
|
pathToFileURL(path.join(projPath, "generated", "api-ref-paths.mjs")).href |
|
65
|
)) as ApiRefPathsModule |
|
66
|
|
|
67
|
const items: ItemsToAdd[] = [ |
|
68
|
{ |
|
69
|
type: "link", |
|
70
|
title: "Introduction", |
|
71
|
path: `/${area}`, |
|
72
|
loaded: true, |
|
73
|
}, |
|
74
|
] |
|
75
|
|
|
76
|
// intro-section pages (Authentication, Pagination, ...) |
|
77
|
for (const section of apiRefIntroSections[area] ?? []) { |
|
78
|
items.push({ |
|
79
|
type: "link", |
|
80
|
title: section.title, |
|
81
|
path: `/${area}/${section.slug}`, |
|
82
|
loaded: true, |
|
83
|
}) |
|
84
|
} |
|
85
|
|
|
86
|
const tags = apiRefPaths[area]?.tags ?? {} |
|
87
|
|
|
88
|
if (Object.keys(tags).length) { |
|
89
|
items.push({ |
|
90
|
type: "separator", |
|
91
|
}) |
|
92
|
} |
|
93
|
|
|
94
|
// one category per tag, with all its operations (and schema) as children |
|
95
|
for (const tag of Object.values(tags)) { |
|
96
|
const category: ItemsToAdd = { |
|
97
|
type: "category", |
|
98
|
title: tag.name, |
|
99
|
path: tag.path, |
|
100
|
loaded: true, |
|
101
|
children: [], |
|
102
|
} |
|
103
|
|
|
104
|
if (tag.schemaPath) { |
|
105
|
const formattedName = singular(tag.name).replaceAll(" ", "") |
|
106
|
category.children!.push({ |
|
107
|
type: "link", |
|
108
|
path: tag.schemaPath, |
|
109
|
title: `${formattedName} Object`, |
|
110
|
loaded: true, |
|
111
|
badge: { |
|
112
|
variant: "neutral", |
|
113
|
text: "Schema", |
|
114
|
}, |
|
115
|
}) |
|
116
|
} |
|
117
|
|
|
118
|
for (const operation of Object.values(tag.operations)) { |
|
119
|
category.children!.push({ |
|
120
|
type: "link", |
|
121
|
path: operation.path, |
|
122
|
title: operation.title, |
|
123
|
loaded: true, |
|
124
|
badge: getMethodBadge(operation.method), |
|
125
|
}) |
|
126
|
} |
|
127
|
|
|
128
|
items.push(category) |
|
129
|
} |
|
130
|
|
|
131
|
return items |
|
132
|
} |