fumadocs-openapi 12.1.0 → 12.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/css/generated/shared.css +1 -1
  2. package/dist/.translations/index.d.ts +63 -17
  3. package/dist/.translations/keys.js +63 -17
  4. package/dist/operation.d.ts +6 -1
  5. package/dist/operation.js +2 -4
  6. package/dist/packages/shared-api/dist/components/select.js +8 -7
  7. package/dist/playground/auth.d.ts +13 -3
  8. package/dist/playground/auth.js +58 -46
  9. package/dist/playground/fetcher.d.ts +8 -0
  10. package/dist/playground/fetcher.js +30 -1
  11. package/dist/playground/index.d.ts +2 -1
  12. package/dist/playground/index.js +2 -1
  13. package/dist/playground/use-playground.d.ts +62 -0
  14. package/dist/playground/use-playground.js +140 -0
  15. package/dist/ui/components/endpoint.js +37 -0
  16. package/dist/ui/operation/index.js +6 -14
  17. package/dist/ui/playground/client.d.ts +9 -26
  18. package/dist/ui/playground/client.js +150 -390
  19. package/dist/ui/playground/components/fields.js +693 -0
  20. package/dist/ui/playground/components/oauth-panel.js +182 -0
  21. package/dist/ui/playground/components/request-panel.js +480 -0
  22. package/dist/ui/playground/components/response-panel.js +150 -0
  23. package/dist/ui/playground/components/result-display.d.ts +1 -0
  24. package/dist/ui/playground/components/result-display.js +187 -147
  25. package/dist/ui/playground/components/segmented.js +21 -0
  26. package/dist/ui/playground/components/server-select.js +100 -89
  27. package/dist/ui/playground/components/url-bar.js +96 -0
  28. package/dist/ui/playground/status-info.js +32 -45
  29. package/dist/utils/document/load.js +1 -1
  30. package/package.json +10 -10
  31. package/dist/packages/shared-api/dist/components/collapsible.d.ts +0 -7
  32. package/dist/packages/shared-api/dist/components/collapsible.js +0 -16
  33. package/dist/packages/shared-api/dist/components/dialog.js +0 -55
  34. package/dist/packages/shared-api/dist/components/input.js +0 -14
  35. package/dist/packages/shared-api/dist/components/label.js +0 -11
  36. package/dist/packages/shared-api/dist/components/playground/inputs.js +0 -439
  37. package/dist/ui/playground/components/oauth-dialog.js +0 -259
@@ -0,0 +1,182 @@
1
+ "use client";
2
+ import { useQuery } from "../../../packages/shared-api/dist/utils/use-query.js";
3
+ import { usePlaygroundAuth } from "../../../playground/auth.js";
4
+ import { useServer } from "../../../utils/use-server.js";
5
+ import { Markdown } from "../../components/markdown.js";
6
+ import { cn } from "../../../utils/cn.js";
7
+ import { Spinner } from "../../../packages/shared-api/dist/components/spinner.js";
8
+ import { CheckRow, SelectRow, TextRow, ValueRow } from "./fields.js";
9
+ import { useState } from "react";
10
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
11
+ import { StfProvider, useStf } from "@fumari/stf";
12
+ import { useTranslations } from "@fuma-translate/react";
13
+ import { buttonVariants } from "fumadocs-ui/components/ui/button";
14
+ //#region src/ui/playground/components/oauth-panel.tsx
15
+ /** obtain the access token of an OAuth 2.0 scheme */
16
+ function OAuthPanel({ field, authorize, onAuthorized }) {
17
+ const t = useTranslations({ note: "OAuth dialog" });
18
+ const { resolveUrl } = useServer();
19
+ const tokenInfo = usePlaygroundAuth().store[field.schemeId];
20
+ const scheme = field.scheme;
21
+ const flows = scheme.flows ?? {};
22
+ const [type, setType] = useState(() => Object.keys(flows)[0]);
23
+ const [clientAuth, setClientAuth] = useState(() => tokenInfo?.type === "authorization_code" ? tokenInfo.client_auth : "header");
24
+ const [scopes, setScopes] = useState(() => new Set(field.scopes));
25
+ const stf = useStf({ defaultValues: () => ({
26
+ client_id: tokenInfo?.client_id,
27
+ client_secret: tokenInfo?.type === "authorization_code" ? tokenInfo.client_secret : void 0
28
+ }) });
29
+ const flowInfo = {
30
+ authorizationCode: {
31
+ name: t("Authorization code"),
32
+ description: t("Authenticate with 3rd party services")
33
+ },
34
+ clientCredentials: {
35
+ name: t("Client Credentials"),
36
+ description: t("Intended for the server-to-server authentication.")
37
+ },
38
+ implicit: {
39
+ name: t("Implicit"),
40
+ description: t("Retrieve the access token directly.")
41
+ },
42
+ password: {
43
+ name: t("Resource Owner Password Flow"),
44
+ description: t("Authenticate using username and password.")
45
+ },
46
+ deviceAuthorization: {
47
+ name: t("Device Authorization"),
48
+ description: t("Authenticate with device.")
49
+ }
50
+ };
51
+ const flow = flows[type];
52
+ const supported = type !== "deviceAuthorization";
53
+ const redirects = type === "authorizationCode" || type === "implicit";
54
+ const hasSecret = type === "authorizationCode" || type === "clientCredentials" || type === "password";
55
+ const serverUrl = resolveUrl();
56
+ const scopeOptions = { ...flow?.scopes };
57
+ for (const scope of field.scopes) scopeOptions[scope] ??= "";
58
+ const query = useQuery(async () => {
59
+ const credentials = stf.dataEngine.getData();
60
+ if (await authorize({
61
+ type,
62
+ scopes: Array.from(scopes),
63
+ clientId: credentials.client_id ?? "",
64
+ clientSecret: credentials.client_secret ?? "",
65
+ username: credentials.username ?? "",
66
+ password: credentials.password ?? "",
67
+ clientAuth
68
+ })) onAuthorized();
69
+ });
70
+ return /* @__PURE__ */ jsx(StfProvider, {
71
+ value: stf,
72
+ children: /* @__PURE__ */ jsxs("div", {
73
+ onKeyDown: (e) => {
74
+ if (e.key !== "Enter" || !(e.target instanceof HTMLInputElement)) return;
75
+ e.preventDefault();
76
+ e.stopPropagation();
77
+ if (supported && !query.isLoading) query.start();
78
+ },
79
+ children: [
80
+ scheme.description && /* @__PURE__ */ jsx("div", {
81
+ className: "border-b px-4 py-2.5 text-xs text-fd-muted-foreground [&_a]:underline",
82
+ children: /* @__PURE__ */ jsx(Markdown, { md: scheme.description })
83
+ }),
84
+ /* @__PURE__ */ jsx(SelectRow, {
85
+ name: "flow",
86
+ value: type,
87
+ onValueChange: (v) => setType(v),
88
+ items: Object.keys(flows).map((key) => ({
89
+ value: key,
90
+ label: flowInfo[key].name,
91
+ description: flowInfo[key].description
92
+ }))
93
+ }),
94
+ supported && /* @__PURE__ */ jsx(ValueRow, {
95
+ name: "client_id",
96
+ required: type !== "password",
97
+ description: t("The client ID of your OAuth application."),
98
+ fieldName: ["client_id"],
99
+ field: { type: "string" }
100
+ }),
101
+ hasSecret && /* @__PURE__ */ jsx(ValueRow, {
102
+ name: "client_secret",
103
+ required: type !== "password",
104
+ description: t("The client secret of your OAuth application."),
105
+ fieldName: ["client_secret"],
106
+ field: {
107
+ type: "string",
108
+ format: "password"
109
+ }
110
+ }),
111
+ type === "password" && /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(ValueRow, {
112
+ name: "username",
113
+ required: true,
114
+ fieldName: ["username"],
115
+ field: { type: "string" }
116
+ }), /* @__PURE__ */ jsx(ValueRow, {
117
+ name: "password",
118
+ required: true,
119
+ fieldName: ["password"],
120
+ field: {
121
+ type: "string",
122
+ format: "password"
123
+ }
124
+ })] }),
125
+ hasSecret && /* @__PURE__ */ jsx(SelectRow, {
126
+ name: "client_auth",
127
+ value: clientAuth,
128
+ onValueChange: (v) => setClientAuth(v),
129
+ items: [{
130
+ value: "header",
131
+ label: t("Send as Basic Auth header"),
132
+ description: t("Send the client ID and secret in the Authorization header.")
133
+ }, {
134
+ value: "body",
135
+ label: t("Send client credentials in body"),
136
+ description: t("Include the client ID and secret in the token request body.")
137
+ }]
138
+ }),
139
+ flow && "authorizationUrl" in flow && flow.authorizationUrl && /* @__PURE__ */ jsx(TextRow, {
140
+ name: "authorization_url",
141
+ children: new URL(flow.authorizationUrl, serverUrl).href
142
+ }),
143
+ flow && "tokenUrl" in flow && flow.tokenUrl && /* @__PURE__ */ jsx(TextRow, {
144
+ name: "token_url",
145
+ children: new URL(flow.tokenUrl, serverUrl).href
146
+ }),
147
+ Object.keys(scopeOptions).length > 0 && /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("p", {
148
+ className: "flex h-9 items-center border-b px-4 text-xs font-medium text-fd-muted-foreground",
149
+ children: t("Scopes")
150
+ }), Object.entries(scopeOptions).map(([scope, description]) => /* @__PURE__ */ jsx(CheckRow, {
151
+ name: scope,
152
+ checked: scopes.has(scope),
153
+ onCheckedChange: (checked) => {
154
+ const next = new Set(scopes);
155
+ if (checked) next.add(scope);
156
+ else next.delete(scope);
157
+ setScopes(next);
158
+ },
159
+ children: description
160
+ }, scope))] }),
161
+ /* @__PURE__ */ jsxs("div", {
162
+ className: "flex items-center gap-3 px-4 py-3",
163
+ children: [/* @__PURE__ */ jsx("p", {
164
+ className: cn("min-w-0 flex-1 text-xs", query.error ? "text-red-400" : "text-fd-muted-foreground"),
165
+ children: query.error ? String(query.error) : !supported ? t("Unsupported") : redirects && t("You will be redirected to authorize, then back to this page.")
166
+ }), /* @__PURE__ */ jsxs("button", {
167
+ type: "button",
168
+ disabled: !supported || query.isLoading,
169
+ onClick: () => void query.start(),
170
+ className: cn(buttonVariants({
171
+ variant: "primary",
172
+ size: "sm"
173
+ }), "shrink-0 gap-1.5 px-3"),
174
+ children: [query.isLoading && /* @__PURE__ */ jsx(Spinner, { className: "size-3" }), t("Authorize")]
175
+ })]
176
+ })
177
+ ]
178
+ })
179
+ });
180
+ }
181
+ //#endregion
182
+ export { OAuthPanel };
@@ -0,0 +1,480 @@
1
+ "use client";
2
+ import { usePlaygroundAuth } from "../../../playground/auth.js";
3
+ import { useOperation } from "../../../operation.js";
4
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../packages/shared-api/dist/components/select.js";
5
+ import { cn } from "../../../utils/cn.js";
6
+ import { Segmented, SegmentedList } from "./segmented.js";
7
+ import { anyFields, useResolvedSchema, useSchemaUtils } from "../../../packages/shared-api/dist/components/playground/schema.js";
8
+ import { Spinner } from "../../../packages/shared-api/dist/components/spinner.js";
9
+ import { FieldRow, FieldRows, LinkRow, ValueRow, getNestedFields, getRemoval, resolveField } from "./fields.js";
10
+ import { OAuthPanel } from "./oauth-panel.js";
11
+ import { Fragment, useState } from "react";
12
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
13
+ import { useDataEngine, useFieldValue } from "@fumari/stf";
14
+ import { stringifyFieldKey } from "@fumari/stf/lib/utils";
15
+ import { useTranslations } from "@fuma-translate/react";
16
+ import { ArrowLeft, Braces, ChevronRight, Cookie, KeyRound, PlusIcon, Route, Rows3, SlidersHorizontal } from "lucide-react";
17
+ import { buttonVariants } from "fumadocs-ui/components/ui/button";
18
+ //#region src/ui/playground/components/request-panel.tsx
19
+ const sectionIcons = {
20
+ path: Route,
21
+ query: SlidersHorizontal,
22
+ header: Rows3,
23
+ cookie: Cookie
24
+ };
25
+ const motionClassNames = {
26
+ forward: "motion-safe:starting:translate-x-6",
27
+ back: "motion-safe:starting:-translate-x-6",
28
+ next: "motion-safe:starting:translate-y-4",
29
+ previous: "motion-safe:starting:-translate-y-4"
30
+ };
31
+ const transitionClassName = "motion-safe:transition-[opacity,translate] motion-safe:duration-300 motion-safe:ease-[cubic-bezier(0.16,1,0.3,1)]";
32
+ /** the overview of all request inputs, nested fields open in their own panel */
33
+ function RequestPanel({ playground: { auth, body }, className, ...options }) {
34
+ const t = useTranslations({ note: "playground" });
35
+ const engine = useDataEngine();
36
+ const { generateDefault } = useSchemaUtils();
37
+ const [stack, setStack] = useState([]);
38
+ const [oauth, setOAuth] = useState(null);
39
+ const [motion, setMotion] = useState("forward");
40
+ const current = stack.at(-1);
41
+ const sectionNames = {
42
+ path: t("Path"),
43
+ query: t("Query"),
44
+ header: t("Header"),
45
+ cookie: t("Cookies"),
46
+ body: t("Body")
47
+ };
48
+ function navigate(next, nextMotion) {
49
+ const target = next.at(-1);
50
+ if (target) engine.init(target.fieldName, () => {
51
+ const field = resolveField(target.schema, void 0);
52
+ return field ? generateDefault(field) : void 0;
53
+ });
54
+ setMotion(nextMotion ?? (next.length >= stack.length ? "forward" : "back"));
55
+ setStack(next);
56
+ }
57
+ function openNested(parents) {
58
+ return (entry, siblings) => navigate([...parents, {
59
+ ...entry,
60
+ siblings: getNestedFields(engine, siblings)
61
+ }]);
62
+ }
63
+ let panel = null;
64
+ if (oauth) panel = /* @__PURE__ */ jsx(Panel, {
65
+ id: "oauth",
66
+ motion: "forward",
67
+ nav: /* @__PURE__ */ jsx(PanelNav, {
68
+ root: t("Authorization"),
69
+ onRoot: () => setOAuth(null),
70
+ onBack: () => setOAuth(null),
71
+ children: /* @__PURE__ */ jsx(Crumb, { children: /* @__PURE__ */ jsx("span", {
72
+ "aria-current": "page",
73
+ className: "truncate px-1.5 py-1 font-mono font-medium",
74
+ children: oauth.schemeId
75
+ }) })
76
+ }),
77
+ children: /* @__PURE__ */ jsx(OAuthPanel, {
78
+ field: oauth,
79
+ authorize: (input) => auth.authorize(oauth, input),
80
+ onAuthorized: () => setOAuth(null)
81
+ })
82
+ });
83
+ else if (current) panel = /* @__PURE__ */ jsx(Panel, {
84
+ id: stringifyFieldKey(current.fieldName),
85
+ motion,
86
+ nav: /* @__PURE__ */ jsx(Breadcrumbs, {
87
+ root: sectionNames[stack[0].fieldName[0]],
88
+ stack,
89
+ onNavigate: navigate
90
+ }),
91
+ children: /* @__PURE__ */ jsx(FieldRows, {
92
+ entry: current,
93
+ showDescription: true,
94
+ onNavigate: openNested(stack)
95
+ })
96
+ });
97
+ return /* @__PURE__ */ jsxs("div", {
98
+ className: cn("@container grid min-h-0", className),
99
+ children: [/* @__PURE__ */ jsx("div", {
100
+ className: cn("fd-scroll-container min-h-0 overflow-y-auto [grid-area:1/1]", transitionClassName, panel && "invisible opacity-0 motion-safe:-translate-x-6"),
101
+ children: /* @__PURE__ */ jsx(Overview, {
102
+ ...options,
103
+ auth,
104
+ body,
105
+ sectionNames,
106
+ onNavigate: openNested([]),
107
+ onOAuth: setOAuth
108
+ })
109
+ }), panel]
110
+ });
111
+ }
112
+ /** a panel over the overview, its content animates in when `id` changes */
113
+ function Panel({ id, motion, nav, children }) {
114
+ return /* @__PURE__ */ jsxs("div", {
115
+ className: "flex min-h-0 flex-col [grid-area:1/1]",
116
+ children: [nav, /* @__PURE__ */ jsx("div", {
117
+ className: cn("fd-scroll-container min-h-0 flex-1 overflow-y-auto starting:opacity-0", transitionClassName, motionClassNames[motion]),
118
+ children
119
+ }, id)]
120
+ });
121
+ }
122
+ const crumbClassName = "truncate rounded-md px-1.5 py-1 text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring";
123
+ /** the bar on top of a panel, going back to the overview from `root` */
124
+ function PanelNav({ root, onRoot, onBack, end, children }) {
125
+ const t = useTranslations({ note: "playground" });
126
+ return /* @__PURE__ */ jsxs("nav", {
127
+ "aria-label": t("Breadcrumb"),
128
+ className: "flex h-10 shrink-0 items-center gap-1 border-b ps-1.5 pe-1.5 text-[0.8125rem]",
129
+ children: [
130
+ /* @__PURE__ */ jsx("button", {
131
+ type: "button",
132
+ "aria-label": t("Back"),
133
+ onClick: onBack,
134
+ className: cn(buttonVariants({
135
+ variant: "ghost",
136
+ size: "icon-xs"
137
+ }), "me-1 text-fd-muted-foreground"),
138
+ children: /* @__PURE__ */ jsx(ArrowLeft, {})
139
+ }),
140
+ /* @__PURE__ */ jsxs("ol", {
141
+ className: "flex min-w-0 items-center gap-0.5",
142
+ children: [/* @__PURE__ */ jsx("li", {
143
+ className: "flex min-w-0 shrink-0 items-center",
144
+ children: /* @__PURE__ */ jsx("button", {
145
+ type: "button",
146
+ onClick: onRoot,
147
+ className: crumbClassName,
148
+ children: root
149
+ })
150
+ }), children]
151
+ }),
152
+ end
153
+ ]
154
+ });
155
+ }
156
+ function Crumb({ children }) {
157
+ return /* @__PURE__ */ jsxs("li", {
158
+ className: "flex min-w-0 items-center gap-0.5 last:shrink-0 motion-safe:transition-opacity motion-safe:starting:opacity-0",
159
+ children: [/* @__PURE__ */ jsx(ChevronRight, { className: "size-3.5 shrink-0 text-fd-muted-foreground/60" }), children]
160
+ });
161
+ }
162
+ function Breadcrumbs({ root, stack, onNavigate }) {
163
+ const t = useTranslations({ note: "playground" });
164
+ const engine = useDataEngine();
165
+ const current = stack[stack.length - 1];
166
+ return /* @__PURE__ */ jsx(PanelNav, {
167
+ root,
168
+ onRoot: () => onNavigate([]),
169
+ onBack: () => onNavigate(stack.slice(0, -1)),
170
+ end: current.removal && /* @__PURE__ */ jsx("button", {
171
+ type: "button",
172
+ onClick: () => {
173
+ engine.delete(current.fieldName);
174
+ onNavigate(stack.slice(0, -1));
175
+ },
176
+ className: cn(buttonVariants({
177
+ variant: "ghost",
178
+ size: "sm"
179
+ }), "ms-auto shrink-0 text-fd-muted-foreground"),
180
+ children: current.removal === "remove" ? t("Remove") : t("Unset")
181
+ }),
182
+ children: stack.map((entry, i) => {
183
+ const { siblings } = entry;
184
+ let node;
185
+ if (i < stack.length - 1) node = /* @__PURE__ */ jsx("button", {
186
+ type: "button",
187
+ onClick: () => onNavigate(stack.slice(0, i + 1)),
188
+ className: cn(crumbClassName, "font-mono"),
189
+ children: entry.name
190
+ });
191
+ else if (siblings && siblings.length > 1) node = /* @__PURE__ */ jsx(SiblingSelect, {
192
+ entry,
193
+ siblings,
194
+ onSelect: (sibling, motion) => onNavigate([...stack.slice(0, -1), {
195
+ ...sibling,
196
+ siblings
197
+ }], motion)
198
+ });
199
+ else node = /* @__PURE__ */ jsx("span", {
200
+ "aria-current": "page",
201
+ className: "truncate px-1.5 py-1 font-mono font-medium",
202
+ children: entry.name
203
+ });
204
+ return /* @__PURE__ */ jsx(Crumb, { children: node }, stringifyFieldKey(entry.fieldName));
205
+ })
206
+ });
207
+ }
208
+ /** the current field, switches to the other nested fields of its parent */
209
+ function SiblingSelect({ entry, siblings, onSelect }) {
210
+ const t = useTranslations({ note: "playground" });
211
+ const current = stringifyFieldKey(entry.fieldName);
212
+ const items = siblings.map((item) => ({
213
+ value: stringifyFieldKey(item.fieldName),
214
+ label: item.name
215
+ }));
216
+ return /* @__PURE__ */ jsxs(Select, {
217
+ items,
218
+ value: current,
219
+ onValueChange: (value) => {
220
+ const next = items.findIndex((item) => item.value === value);
221
+ if (next === -1 || value === current) return;
222
+ const prev = items.findIndex((item) => item.value === current);
223
+ onSelect(siblings[next], next > prev ? "next" : "previous");
224
+ },
225
+ children: [/* @__PURE__ */ jsx(SelectTrigger, {
226
+ "aria-label": t("Switch Field"),
227
+ className: "h-7 w-auto min-w-0 gap-1 border-0 bg-transparent px-1.5 py-0 font-mono text-[0.8125rem] font-medium text-fd-foreground hover:bg-fd-accent focus:ring-0 focus-visible:ring-2 data-popup-open:bg-fd-accent",
228
+ children: /* @__PURE__ */ jsx(SelectValue, { className: "truncate" })
229
+ }), /* @__PURE__ */ jsx(SelectContent, { children: items.map((item) => /* @__PURE__ */ jsx(SelectItem, {
230
+ value: item.value,
231
+ className: "font-mono text-[0.8125rem]",
232
+ children: item.label
233
+ }, item.value)) })]
234
+ });
235
+ }
236
+ function Overview({ auth, body, sectionNames, onNavigate, onOAuth, renderParameterField, renderBodyField }) {
237
+ const t = useTranslations({ note: "playground" });
238
+ const { parameters } = useOperation();
239
+ if (auth.requirements.length === 0 && parameters.length === 0 && !body) return /* @__PURE__ */ jsx("p", {
240
+ className: "p-6 text-center text-sm text-fd-muted-foreground",
241
+ children: t("This endpoint has no parameters.")
242
+ });
243
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [
244
+ auth.requirements.length > 0 && /* @__PURE__ */ jsx(AuthSection, {
245
+ auth,
246
+ onOAuth
247
+ }),
248
+ parameters.map(({ in: type, items }) => {
249
+ const entries = items.map((param) => ({
250
+ fieldName: [type, param.name],
251
+ name: param.name,
252
+ schema: getParameterSchema(param),
253
+ removal: getRemoval(param.required ?? false, false)
254
+ }));
255
+ return /* @__PURE__ */ jsx(Section, {
256
+ icon: sectionIcons[type],
257
+ title: sectionNames[type],
258
+ count: items.length,
259
+ children: items.map((param, i) => renderParameterField ? /* @__PURE__ */ jsx(Fragment, { children: renderParameterField(entries[i].fieldName, param) }, param.name) : /* @__PURE__ */ jsx(FieldRow, {
260
+ entry: entries[i],
261
+ required: param.required,
262
+ description: param.description,
263
+ onNavigate: () => onNavigate(entries[i], entries)
264
+ }, param.name))
265
+ }, type);
266
+ }),
267
+ body && /* @__PURE__ */ jsx(BodySection, {
268
+ body,
269
+ renderBodyField,
270
+ onNavigate
271
+ })
272
+ ] });
273
+ }
274
+ function getParameterSchema(param) {
275
+ if (param.content) for (const type in param.content) return param.content[type].schema ?? anyFields;
276
+ return param.schema ?? anyFields;
277
+ }
278
+ function Section({ icon: Icon, title, count, actions, children }) {
279
+ return /* @__PURE__ */ jsxs("section", {
280
+ className: "border-b last:border-b-0",
281
+ children: [/* @__PURE__ */ jsxs("div", {
282
+ className: "sticky top-0 z-10 flex h-12 items-center gap-2 border-b bg-fd-card ps-4 pe-2 @sm:pe-7.5",
283
+ children: [
284
+ /* @__PURE__ */ jsx(Icon, { className: "size-3.5 shrink-0 text-fd-primary" }),
285
+ /* @__PURE__ */ jsx("h3", {
286
+ className: "text-[0.8125rem] font-medium",
287
+ children: title
288
+ }),
289
+ count !== void 0 && /* @__PURE__ */ jsx("span", {
290
+ className: "text-xs text-fd-muted-foreground tabular-nums",
291
+ children: count
292
+ }),
293
+ /* @__PURE__ */ jsx("div", {
294
+ className: "flex min-w-0 flex-1 items-center justify-end gap-2",
295
+ children: actions
296
+ })
297
+ ]
298
+ }), children]
299
+ });
300
+ }
301
+ function AuthSection({ auth, onOAuth }) {
302
+ const t = useTranslations({ note: "playground" });
303
+ const { isLoading, error } = usePlaygroundAuth();
304
+ const { requirements, selected, select, fields } = auth;
305
+ const items = requirements.map((requirement, i) => ({
306
+ value: i,
307
+ label: /* @__PURE__ */ jsx("span", {
308
+ className: "inline-flex items-center gap-1 font-mono",
309
+ children: requirement.map((item, j) => /* @__PURE__ */ jsxs(Fragment, { children: [j > 0 && /* @__PURE__ */ jsx(PlusIcon, { className: "size-3 text-fd-muted-foreground" }), /* @__PURE__ */ jsx("span", {
310
+ className: cn(item.scheme.deprecated && "text-fd-muted-foreground line-through"),
311
+ children: item.id
312
+ })] }, item.id))
313
+ })
314
+ }));
315
+ return /* @__PURE__ */ jsxs(Section, {
316
+ icon: KeyRound,
317
+ title: t("Authorization"),
318
+ actions: /* @__PURE__ */ jsxs(Fragment$1, { children: [isLoading && /* @__PURE__ */ jsxs("span", {
319
+ className: "inline-flex items-center gap-1.5 text-xs text-fd-muted-foreground",
320
+ children: [/* @__PURE__ */ jsx(Spinner, { className: "size-3" }), t("Fetching token...")]
321
+ }), items.length > 1 ? /* @__PURE__ */ jsxs(Select, {
322
+ items,
323
+ value: selected,
324
+ onValueChange: (v) => v !== null && select(v),
325
+ children: [/* @__PURE__ */ jsx(SelectTrigger, {
326
+ className: "h-7 w-auto max-w-60 gap-1.5 @sm:-me-2 border-0 bg-transparent px-2 text-xs hover:bg-fd-accent focus:ring-0 focus-visible:ring-2",
327
+ children: /* @__PURE__ */ jsx(SelectValue, { className: "truncate" })
328
+ }), /* @__PURE__ */ jsx(SelectContent, {
329
+ align: "end",
330
+ children: items.map(({ value, label }) => /* @__PURE__ */ jsx(SelectItem, {
331
+ value,
332
+ className: "text-xs",
333
+ children: label
334
+ }, value))
335
+ })]
336
+ }) : /* @__PURE__ */ jsx("span", {
337
+ className: "truncate text-xs text-fd-muted-foreground",
338
+ children: items[0]?.label
339
+ })] }),
340
+ children: [error != null && /* @__PURE__ */ jsxs("div", {
341
+ className: "border-b px-4 py-2.5 text-xs",
342
+ children: [/* @__PURE__ */ jsx("p", {
343
+ className: "font-medium text-red-400",
344
+ children: t("Failed to fetch token")
345
+ }), /* @__PURE__ */ jsx("p", {
346
+ className: "text-fd-muted-foreground",
347
+ children: String(error)
348
+ })]
349
+ }), fields.map((field) => /* @__PURE__ */ jsx(AuthRows, {
350
+ field,
351
+ onOAuth
352
+ }, stringifyFieldKey(field.fieldName)))]
353
+ });
354
+ }
355
+ function AuthRows({ field, onOAuth }) {
356
+ const t = useTranslations({ note: "playground" });
357
+ const { fieldName, scheme } = field;
358
+ if (scheme.type === "http" && scheme.scheme === "basic") return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(ValueRow, {
359
+ name: "username",
360
+ type: "basic",
361
+ description: scheme.description,
362
+ fieldName: [...fieldName, "username"],
363
+ field: { type: "string" }
364
+ }), /* @__PURE__ */ jsx(ValueRow, {
365
+ name: "password",
366
+ fieldName: [...fieldName, "password"],
367
+ field: {
368
+ type: "string",
369
+ format: "password"
370
+ }
371
+ })] });
372
+ if (scheme.type === "apiKey") return /* @__PURE__ */ jsx(ValueRow, {
373
+ name: scheme.name,
374
+ type: scheme.in,
375
+ description: scheme.description,
376
+ fieldName,
377
+ field: { type: "string" }
378
+ });
379
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(ValueRow, {
380
+ name: "Authorization",
381
+ type: "header",
382
+ description: scheme.type === "http" || scheme.type === "oauth2" ? scheme.description : t("OpenID Connect is not supported at the moment, you can still set an access token here."),
383
+ fieldName,
384
+ field: { type: "string" }
385
+ }), scheme.type === "oauth2" && /* @__PURE__ */ jsx(LinkRow, {
386
+ name: field.schemeId,
387
+ onClick: () => onOAuth(field),
388
+ children: t("Authorize")
389
+ })] });
390
+ }
391
+ function BodySection({ body, renderBodyField, onNavigate }) {
392
+ const t = useTranslations({ note: "playground" });
393
+ const [mode, setMode] = useState("form");
394
+ const root = useResolvedSchema(body.schema);
395
+ const entry = {
396
+ fieldName: ["body"],
397
+ name: "body",
398
+ schema: body.schema
399
+ };
400
+ const allowJson = !renderBodyField && body.mediaType !== "multipart/form-data" && !(root.type === "string" && root.format === "binary");
401
+ let content;
402
+ if (renderBodyField) content = renderBodyField("body", body);
403
+ else if (allowJson && mode === "json") content = /* @__PURE__ */ jsx(JsonEditor, { fieldName: entry.fieldName });
404
+ else content = /* @__PURE__ */ jsx(FieldRows, {
405
+ entry,
406
+ onNavigate
407
+ });
408
+ return /* @__PURE__ */ jsx(Section, {
409
+ icon: Braces,
410
+ title: t("Body"),
411
+ actions: /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("code", {
412
+ className: "min-w-0 truncate text-xs text-fd-muted-foreground",
413
+ children: body.mediaType
414
+ }), allowJson && /* @__PURE__ */ jsx(Segmented, {
415
+ value: mode,
416
+ onValueChange: setMode,
417
+ className: "@sm:-me-2.5",
418
+ children: /* @__PURE__ */ jsx(SegmentedList, {
419
+ "aria-label": t("Editor"),
420
+ items: [{
421
+ value: "form",
422
+ label: t("Form")
423
+ }, {
424
+ value: "json",
425
+ label: "JSON"
426
+ }]
427
+ })
428
+ })] }),
429
+ children: content
430
+ });
431
+ }
432
+ function JsonEditor({ fieldName }) {
433
+ const engine = useDataEngine();
434
+ const [value] = useFieldValue(fieldName);
435
+ const [draft, setDraft] = useState(() => ({
436
+ value,
437
+ text: formatJson(value),
438
+ error: ""
439
+ }));
440
+ if (draft.value !== value) setDraft({
441
+ value,
442
+ text: formatJson(value),
443
+ error: ""
444
+ });
445
+ return /* @__PURE__ */ jsxs("div", {
446
+ className: "flex flex-col",
447
+ children: [/* @__PURE__ */ jsx("textarea", {
448
+ "aria-label": "JSON",
449
+ value: draft.text,
450
+ spellCheck: false,
451
+ onChange: (e) => {
452
+ const text = e.target.value;
453
+ try {
454
+ const parsed = JSON.parse(text);
455
+ setDraft({
456
+ value: parsed,
457
+ text,
458
+ error: ""
459
+ });
460
+ engine.update(fieldName, parsed);
461
+ } catch (err) {
462
+ setDraft({
463
+ ...draft,
464
+ text,
465
+ error: err instanceof Error ? err.message : ""
466
+ });
467
+ }
468
+ },
469
+ className: "min-h-60 w-full resize-none bg-transparent px-4 py-3 font-mono text-[0.8125rem] leading-relaxed outline-none field-sizing-content"
470
+ }), draft.error && /* @__PURE__ */ jsx("p", {
471
+ className: "border-t px-4 py-2 font-mono text-xs text-red-400",
472
+ children: draft.error
473
+ })]
474
+ });
475
+ }
476
+ function formatJson(value) {
477
+ return JSON.stringify(value ?? {}, null, 2);
478
+ }
479
+ //#endregion
480
+ export { RequestPanel };