fumadocs-openapi 12.1.1 → 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 +62 -17
  3. package/dist/.translations/keys.js +62 -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 +5 -1
  8. package/dist/playground/auth.js +7 -2
  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 +149 -389
  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 +185 -168
  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,150 @@
1
+ "use client";
2
+ import { useCodeUsage, useOperation, useResponseExamples } from "../../../operation.js";
3
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../packages/shared-api/dist/components/select.js";
4
+ import { Markdown } from "../../components/markdown.js";
5
+ import { ClientCodeBlock } from "../../components/codeblock.js";
6
+ import { cn } from "../../../utils/cn.js";
7
+ import { Segmented, SegmentedList, SegmentedPanel } from "./segmented.js";
8
+ import { DefaultResultDisplay, PanelHeader, panelCodeBlock } from "./result-display.js";
9
+ import { Spinner } from "../../../packages/shared-api/dist/components/spinner.js";
10
+ import { useState } from "react";
11
+ import { jsx, jsxs } from "react/jsx-runtime";
12
+ import { useTranslations } from "@fuma-translate/react";
13
+ import { SendHorizontal } from "lucide-react";
14
+ import { CodeBlockTab, CodeBlockTabs, CodeBlockTabsList, CodeBlockTabsTrigger } from "fumadocs-ui/components/codeblock";
15
+ //#region src/ui/playground/components/response-panel.tsx
16
+ /** the code usages of the request, above its response or the documented examples */
17
+ function ResponsePanel({ response, loading, onReset, ResultDisplay = DefaultResultDisplay, className }) {
18
+ const t = useTranslations({ note: "playground" });
19
+ return /* @__PURE__ */ jsxs("div", {
20
+ className: cn("@container flex min-h-0 flex-col", className),
21
+ children: [/* @__PURE__ */ jsx(RequestExample, {}), /* @__PURE__ */ jsxs("div", {
22
+ className: "relative flex min-h-0 flex-1 flex-col",
23
+ children: [response ? /* @__PURE__ */ jsx(ResultDisplay, {
24
+ data: response.result,
25
+ reset: onReset,
26
+ className: "starting:opacity-0 motion-safe:transition-[opacity,translate] motion-safe:duration-300 motion-safe:starting:translate-y-1"
27
+ }, response.id) : /* @__PURE__ */ jsx(ResponseExamples, {}), loading && /* @__PURE__ */ jsxs("div", {
28
+ className: "absolute inset-0 flex items-center justify-center gap-2 bg-fd-card/70 text-sm text-fd-muted-foreground backdrop-blur-[1px] transition-opacity delay-150 duration-200 starting:opacity-0",
29
+ children: [/* @__PURE__ */ jsx(Spinner, { className: "size-3.5" }), t("Sending request...")]
30
+ })]
31
+ })]
32
+ });
33
+ }
34
+ function RequestExample() {
35
+ const items = Array.from(useOperation().codeUsages.map());
36
+ if (items.length === 0) return null;
37
+ return /* @__PURE__ */ jsxs(CodeBlockTabs, {
38
+ groupId: "fumadocs_openapi_requests",
39
+ defaultValue: items[0][0],
40
+ className: "my-0 shrink-0 rounded-none border-0 border-b bg-transparent",
41
+ children: [/* @__PURE__ */ jsx(CodeBlockTabsList, {
42
+ className: "h-10 items-stretch border-b [scrollbar-width:none]",
43
+ children: items.map(([id, item]) => /* @__PURE__ */ jsx(CodeBlockTabsTrigger, {
44
+ value: id,
45
+ children: item.label ?? item.lang
46
+ }, id))
47
+ }), items.map(([id, item]) => /* @__PURE__ */ jsx(CodeBlockTab, {
48
+ value: id,
49
+ children: /* @__PURE__ */ jsx(UsageCode, {
50
+ id,
51
+ lang: item.lang
52
+ })
53
+ }, id))]
54
+ });
55
+ }
56
+ function UsageCode({ id, lang }) {
57
+ const code = useCodeUsage(id);
58
+ if (!code) return null;
59
+ return /* @__PURE__ */ jsx(ClientCodeBlock, {
60
+ lang,
61
+ code,
62
+ codeblock: {
63
+ className: "rounded-none border-0 bg-transparent shadow-none",
64
+ viewportProps: { className: "max-h-[min(18rem,30vh)]" }
65
+ }
66
+ });
67
+ }
68
+ function ResponseExamples() {
69
+ const t = useTranslations({ note: "playground" });
70
+ const tabs = useResponseExamples();
71
+ const [code, setCode] = useState();
72
+ const [selected, setSelected] = useState(0);
73
+ if (tabs.length === 0) return /* @__PURE__ */ jsxs("div", {
74
+ className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center",
75
+ children: [/* @__PURE__ */ jsx("div", {
76
+ className: "rounded-xl border bg-fd-secondary p-2.5 text-fd-muted-foreground",
77
+ children: /* @__PURE__ */ jsx(SendHorizontal, { className: "size-4" })
78
+ }), /* @__PURE__ */ jsx("p", {
79
+ className: "text-sm text-fd-muted-foreground",
80
+ children: t("Send a request to see its response.")
81
+ })]
82
+ });
83
+ const tab = tabs.find((item) => item.code === code) ?? tabs[0];
84
+ const examples = tab.examples ?? [];
85
+ const example = examples[selected];
86
+ return /* @__PURE__ */ jsxs(Segmented, {
87
+ value: tab.code,
88
+ onValueChange: (value) => {
89
+ setCode(value);
90
+ setSelected(0);
91
+ },
92
+ className: "flex min-h-0 flex-1 flex-col",
93
+ children: [/* @__PURE__ */ jsxs(PanelHeader, { children: [
94
+ /* @__PURE__ */ jsx("span", {
95
+ className: "shrink-0 text-[0.8125rem] font-medium",
96
+ children: t("Response")
97
+ }),
98
+ /* @__PURE__ */ jsx("div", {
99
+ title: tab.response.description,
100
+ className: "min-w-0 flex-1 truncate text-xs text-fd-muted-foreground [&_*]:inline [&_a]:underline",
101
+ children: tab.response.description && /* @__PURE__ */ jsx(Markdown, { md: tab.response.description })
102
+ }),
103
+ examples.length > 1 && /* @__PURE__ */ jsxs(Select, {
104
+ items: examples.map((item, i) => ({
105
+ value: String(i),
106
+ label: item.label
107
+ })),
108
+ value: String(selected),
109
+ onValueChange: (v) => v !== null && setSelected(Number(v)),
110
+ children: [/* @__PURE__ */ jsx(SelectTrigger, {
111
+ className: "h-7 w-auto max-w-40 shrink-0 gap-1.5 border-0 bg-transparent px-2 text-xs hover:bg-fd-accent focus:ring-0 focus-visible:ring-2",
112
+ children: /* @__PURE__ */ jsx(SelectValue, { className: "truncate" })
113
+ }), /* @__PURE__ */ jsx(SelectContent, {
114
+ align: "end",
115
+ children: examples.map((item, i) => /* @__PURE__ */ jsx(SelectItem, {
116
+ value: String(i),
117
+ className: "text-xs",
118
+ children: item.label
119
+ }, i))
120
+ })]
121
+ }),
122
+ /* @__PURE__ */ jsx(SegmentedList, {
123
+ className: "max-w-1/2 shrink-0 overflow-x-auto [scrollbar-width:none]",
124
+ items: tabs.map((item) => ({
125
+ value: item.code,
126
+ label: /* @__PURE__ */ jsx("span", {
127
+ className: "font-mono",
128
+ children: item.code
129
+ })
130
+ }))
131
+ })
132
+ ] }), /* @__PURE__ */ jsx(SegmentedPanel, {
133
+ value: tab.code,
134
+ className: "flex min-h-0 flex-1 flex-col overflow-auto",
135
+ children: example ? /* @__PURE__ */ jsx("div", {
136
+ className: "min-h-0 flex-1",
137
+ children: /* @__PURE__ */ jsx(ClientCodeBlock, {
138
+ lang: "json",
139
+ code: JSON.stringify(example.sample, null, 2),
140
+ codeblock: panelCodeBlock
141
+ })
142
+ }) : /* @__PURE__ */ jsx("p", {
143
+ className: "p-4 text-sm text-fd-muted-foreground",
144
+ children: t("No example available.")
145
+ })
146
+ }, tab.code)]
147
+ });
148
+ }
149
+ //#endregion
150
+ export { ResponsePanel };
@@ -1,5 +1,6 @@
1
1
  import { FetchResult } from "../../../playground/fetcher.js";
2
2
  import { ComponentProps } from "react";
3
+ import "fumadocs-ui/components/codeblock";
3
4
  //#region src/ui/playground/components/result-display.d.ts
4
5
  export interface ResultDisplayProps extends ComponentProps<'div'> {
5
6
  data: FetchResult;
@@ -2,48 +2,60 @@
2
2
  import { ClientCodeBlock } from "../../components/codeblock.js";
3
3
  import { cn } from "../../../utils/cn.js";
4
4
  import { useStatusInfo } from "../status-info.js";
5
- import { require_fast_content_type_parse } from "../../../node_modules/.pnpm/fast-content-type-parse@4.0.0/node_modules/fast-content-type-parse/index.js";
6
- import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../../../packages/shared-api/dist/components/collapsible.js";
7
- import { Fragment, useEffect, useMemo, useState } from "react";
8
- import { jsx, jsxs } from "react/jsx-runtime";
5
+ import { Segmented, SegmentedList, SegmentedPanel } from "./segmented.js";
6
+ import { Fragment, useCallback, useMemo, useState } from "react";
7
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
9
8
  import { useTranslations } from "@fuma-translate/react";
10
- import { cva } from "class-variance-authority";
11
- import { ChevronDown, CircleX, DownloadIcon, SignpostIcon } from "lucide-react";
9
+ import { DownloadIcon, FileIcon, X } from "lucide-react";
12
10
  import { buttonVariants } from "fumadocs-ui/components/ui/button";
13
11
  //#region src/ui/playground/components/result-display.tsx
14
- var import_fast_content_type_parse = require_fast_content_type_parse();
15
- const panelVariants = cva("flex flex-col gap-3 mt-2 px-3 py-2 border-y bg-fd-secondary text-fd-secondary-foreground");
12
+ const iconButtonClassName = cn(buttonVariants({
13
+ variant: "ghost",
14
+ size: "icon-sm"
15
+ }), "text-fd-muted-foreground [&_svg]:size-3.5");
16
+ /** a code block filling its panel */
17
+ const panelCodeBlock = {
18
+ className: "h-full rounded-none border-0 bg-transparent shadow-none",
19
+ viewportProps: { className: "h-full max-h-none" }
20
+ };
21
+ function PanelHeader({ className, ...props }) {
22
+ return /* @__PURE__ */ jsx("div", {
23
+ ...props,
24
+ className: cn("flex h-10 shrink-0 items-center gap-2.5 border-b ps-4 pe-1.5", className)
25
+ });
26
+ }
16
27
  function DefaultResultDisplay({ data, reset, ...rest }) {
17
28
  const t = useTranslations({ note: "playground result display" });
18
29
  if (data.type === "client_error") return /* @__PURE__ */ jsxs("div", {
19
30
  ...rest,
20
- className: cn(panelVariants(), rest.className),
21
- children: [
22
- /* @__PURE__ */ jsxs("div", {
23
- className: "flex gap-1.5 items-center",
24
- children: [
25
- /* @__PURE__ */ jsx(CircleX, { className: "size-4 text-red-500" }),
26
- /* @__PURE__ */ jsx("p", {
27
- className: "text-sm font-medium me-auto",
28
- children: t("Client Error")
29
- }),
30
- /* @__PURE__ */ jsx("button", {
31
- type: "button",
32
- className: cn(buttonVariants({
33
- size: "sm",
34
- variant: "outline"
35
- })),
36
- onClick: () => reset(),
37
- children: t("Close")
38
- })
39
- ]
31
+ className: cn("flex min-h-0 flex-1 flex-col", rest.className),
32
+ children: [/* @__PURE__ */ jsxs(PanelHeader, { children: [
33
+ /* @__PURE__ */ jsx("span", { className: "size-2 shrink-0 rounded-full bg-red-500" }),
34
+ /* @__PURE__ */ jsx("span", {
35
+ className: "text-[0.8125rem] font-medium",
36
+ children: t("Request Failed")
40
37
  }),
41
- /* @__PURE__ */ jsxs("p", {
42
- className: "flex items-start gap-2 text-xs font-mono text-fd-muted-foreground break-all",
43
- children: [/* @__PURE__ */ jsx(SignpostIcon, { className: "size-4 shrink-0" }), data.url]
44
- }),
45
- /* @__PURE__ */ jsx("p", { children: data.message })
46
- ]
38
+ /* @__PURE__ */ jsx("button", {
39
+ type: "button",
40
+ "aria-label": t("Clear"),
41
+ className: cn(iconButtonClassName, "ms-auto"),
42
+ onClick: reset,
43
+ children: /* @__PURE__ */ jsx(X, {})
44
+ })
45
+ ] }), /* @__PURE__ */ jsxs("div", {
46
+ className: "flex flex-col gap-3 overflow-auto p-4 text-sm",
47
+ children: [
48
+ /* @__PURE__ */ jsx("p", { children: data.message }),
49
+ /* @__PURE__ */ jsx("p", {
50
+ className: "font-mono text-xs break-all text-fd-muted-foreground",
51
+ children: data.url
52
+ }),
53
+ /* @__PURE__ */ jsx("p", {
54
+ className: "rounded-lg border bg-fd-secondary p-3 text-xs text-fd-muted-foreground",
55
+ children: t("Check the server URL. If the API does not allow cross-origin requests from this site, a proxy is needed.")
56
+ })
57
+ ]
58
+ })]
47
59
  });
48
60
  return /* @__PURE__ */ jsx(ResponseResult, {
49
61
  data,
@@ -66,167 +78,172 @@ function getTextFormat(mime) {
66
78
  if (mime.startsWith("text/")) return "text";
67
79
  return null;
68
80
  }
81
+ function formatDuration(ms) {
82
+ return ms < 1e3 ? `${Math.round(ms)} ms` : `${(ms / 1e3).toFixed(2)} s`;
83
+ }
84
+ function formatSize(bytes) {
85
+ if (bytes < 1024) return `${bytes} B`;
86
+ if (bytes < 1048576) return `${(bytes / 1024).toFixed(1)} kB`;
87
+ return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
88
+ }
69
89
  function ResponseResult({ data, reset, ...rest }) {
70
90
  const t = useTranslations({ note: "playground result display" });
71
91
  const statusInfo = useStatusInfo(data.status);
72
- const [objectUrl, setObjectUrl] = useState();
73
- const { parameters, type } = useMemo(() => (0, import_fast_content_type_parse.safeParse)(data.headers.get("Content-Type") ?? "text/plain"), [data.headers]);
74
- useEffect(() => {
75
- const objectUrl = URL.createObjectURL(new Blob([data.body], { type }));
76
- setObjectUrl(objectUrl);
77
- return () => URL.revokeObjectURL(objectUrl);
92
+ const [view, setView] = useState("body");
93
+ const type = data.mediaType ?? "text/plain";
94
+ const headers = Array.from(data.headers);
95
+ const size = data.body.byteLength;
96
+ const bodyUrlRef = useCallback((element) => {
97
+ if (!element) return;
98
+ const url = URL.createObjectURL(new Blob([data.body], { type }));
99
+ if (element instanceof HTMLAnchorElement) element.href = url;
100
+ else element.src = url;
101
+ return () => URL.revokeObjectURL(url);
78
102
  }, [data.body, type]);
79
103
  let body;
80
- if (type.startsWith("image/")) body = objectUrl && /* @__PURE__ */ jsx(ImageResult, {
81
- mime: type,
82
- src: objectUrl
104
+ if (type.startsWith("image/")) body = /* @__PURE__ */ jsx("div", {
105
+ className: "flex min-h-full items-center justify-center p-4",
106
+ children: /* @__PURE__ */ jsx("img", {
107
+ ref: bodyUrlRef,
108
+ alt: "",
109
+ className: "max-w-full rounded-lg border"
110
+ })
83
111
  });
84
- else if (data.body.byteLength > 0) {
112
+ else if (size > 0) {
85
113
  const lang = getTextFormat(type);
86
114
  if (lang) body = /* @__PURE__ */ jsx(TextResult, {
87
115
  lang,
88
- contentType: type,
89
- charset: parameters.charset,
90
116
  data
91
117
  });
92
118
  else body = /* @__PURE__ */ jsxs("div", {
93
- className: "p-2 border rounded-lg bg-fd-card text-fd-card-foreground",
94
- children: [type && /* @__PURE__ */ jsx("p", {
95
- className: "text-xs font-mono text-fd-muted-foreground mb-1",
96
- children: type
97
- }), /* @__PURE__ */ jsx("p", {
98
- className: "font-medium",
99
- children: t("Binary response body, {length} bytes", { variables: { length: String(data.body.byteLength) } })
100
- })]
119
+ className: "flex h-full flex-col items-center justify-center gap-2 p-6 text-center",
120
+ children: [
121
+ /* @__PURE__ */ jsx(FileIcon, { className: "size-5 text-fd-muted-foreground" }),
122
+ /* @__PURE__ */ jsx("p", {
123
+ className: "text-sm font-medium",
124
+ children: t("Binary response body, {length} bytes", { variables: { length: String(size) } })
125
+ }),
126
+ type && /* @__PURE__ */ jsx("p", {
127
+ className: "font-mono text-xs text-fd-muted-foreground",
128
+ children: type
129
+ })
130
+ ]
101
131
  });
102
- }
103
- return /* @__PURE__ */ jsxs("div", {
132
+ } else body = /* @__PURE__ */ jsxs("div", {
133
+ className: "flex h-full flex-col items-center justify-center gap-1 p-6 text-center",
134
+ children: [/* @__PURE__ */ jsx("p", {
135
+ className: "text-4xl font-light tracking-tight tabular-nums",
136
+ children: data.status
137
+ }), /* @__PURE__ */ jsx("p", {
138
+ className: "text-sm text-fd-muted-foreground",
139
+ children: t("Empty response body")
140
+ })]
141
+ });
142
+ return /* @__PURE__ */ jsx("div", {
104
143
  ...rest,
105
- className: cn(panelVariants(), rest.className),
106
- children: [
107
- /* @__PURE__ */ jsxs("div", {
108
- className: "flex items-center gap-1.5",
109
- children: [
110
- /* @__PURE__ */ jsx(statusInfo.icon, { className: cn("size-4 shrink-0", statusInfo.color) }),
144
+ className: cn("flex min-h-0 flex-1 flex-col", rest.className),
145
+ children: /* @__PURE__ */ jsxs(Segmented, {
146
+ value: view,
147
+ onValueChange: setView,
148
+ className: "flex min-h-0 flex-1 flex-col",
149
+ children: [
150
+ /* @__PURE__ */ jsxs(PanelHeader, { children: [
151
+ /* @__PURE__ */ jsx("span", { className: cn("size-2 shrink-0 rounded-full motion-safe:transition-transform motion-safe:duration-300 motion-safe:starting:scale-0", statusInfo.color) }),
111
152
  /* @__PURE__ */ jsxs("p", {
112
- className: "min-w-0 me-auto text-sm font-medium truncate",
153
+ className: "min-w-0 truncate text-[0.8125rem] font-medium",
113
154
  children: [
114
- data.status,
155
+ /* @__PURE__ */ jsx("span", {
156
+ className: "tabular-nums",
157
+ children: data.status
158
+ }),
115
159
  " ",
116
- statusInfo.description
160
+ /* @__PURE__ */ jsx("span", {
161
+ className: "font-normal text-fd-muted-foreground",
162
+ children: statusInfo.description
163
+ })
117
164
  ]
118
165
  }),
119
- data.body.byteLength > 0 && /* @__PURE__ */ jsxs("a", {
120
- href: objectUrl,
121
- download: getFileName(data.headers),
122
- className: cn(buttonVariants({
123
- size: "sm",
124
- variant: "outline"
125
- }), "gap-2"),
126
- children: [/* @__PURE__ */ jsx(DownloadIcon, { className: "size-3.5 text-fd-muted-foreground" }), t("Download")]
166
+ /* @__PURE__ */ jsxs("p", {
167
+ className: "hidden shrink-0 text-xs text-fd-muted-foreground tabular-nums @md:block",
168
+ children: [
169
+ formatDuration(data.duration),
170
+ " · ",
171
+ formatSize(size)
172
+ ]
173
+ }),
174
+ /* @__PURE__ */ jsx(SegmentedList, {
175
+ className: "ms-auto shrink-0",
176
+ items: [{
177
+ value: "body",
178
+ label: t("Body")
179
+ }, {
180
+ value: "headers",
181
+ label: /* @__PURE__ */ jsxs(Fragment$1, { children: [t("Headers"), /* @__PURE__ */ jsx("span", {
182
+ className: "text-fd-muted-foreground tabular-nums",
183
+ children: headers.length
184
+ })] })
185
+ }]
127
186
  }),
128
- /* @__PURE__ */ jsx("button", {
129
- type: "button",
130
- className: buttonVariants({
131
- size: "sm",
132
- variant: "outline"
133
- }),
134
- onClick: () => reset(),
135
- children: t("Close")
187
+ /* @__PURE__ */ jsxs("div", {
188
+ className: "flex shrink-0 items-center",
189
+ children: [size > 0 && /* @__PURE__ */ jsx("a", {
190
+ ref: bodyUrlRef,
191
+ download: data.fileName ?? "response",
192
+ "aria-label": t("Download"),
193
+ title: t("Download"),
194
+ className: iconButtonClassName,
195
+ children: /* @__PURE__ */ jsx(DownloadIcon, {})
196
+ }), /* @__PURE__ */ jsx("button", {
197
+ type: "button",
198
+ "aria-label": t("Clear"),
199
+ title: t("Clear"),
200
+ className: iconButtonClassName,
201
+ onClick: reset,
202
+ children: /* @__PURE__ */ jsx(X, {})
203
+ })]
136
204
  })
137
- ]
138
- }),
139
- /* @__PURE__ */ jsxs("p", {
140
- className: "flex items-start gap-2 text-xs font-mono text-fd-muted-foreground break-all",
141
- children: [/* @__PURE__ */ jsx(SignpostIcon, { className: "size-4 shrink-0" }), data.url]
142
- }),
143
- /* @__PURE__ */ jsx(ResponseHeaders, { headers: data.headers }),
144
- body
145
- ]
205
+ ] }),
206
+ /* @__PURE__ */ jsx(SegmentedPanel, {
207
+ value: "body",
208
+ className: "min-h-0 flex-1 overflow-auto",
209
+ children: body
210
+ }),
211
+ /* @__PURE__ */ jsxs(SegmentedPanel, {
212
+ value: "headers",
213
+ className: "min-h-0 flex-1 overflow-auto",
214
+ children: [/* @__PURE__ */ jsx("p", {
215
+ className: "border-b px-4 py-2.5 font-mono text-xs break-all text-fd-muted-foreground",
216
+ children: data.url
217
+ }), /* @__PURE__ */ jsx("dl", {
218
+ className: "grid grid-cols-[minmax(0,2fr)_minmax(0,3fr)] font-mono text-xs",
219
+ children: headers.map(([name, value]) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", {
220
+ className: "truncate border-b py-2 ps-4 pe-2 text-fd-muted-foreground",
221
+ children: name
222
+ }), /* @__PURE__ */ jsx("dd", {
223
+ className: "border-b border-s px-3 py-2 break-all",
224
+ children: value
225
+ })] }, name))
226
+ })]
227
+ })
228
+ ]
229
+ })
146
230
  });
147
231
  }
148
- function ResponseHeaders({ headers }) {
149
- const t = useTranslations({ note: "playground result display" });
150
- const entries = Array.from(headers);
151
- if (entries.length === 0) return;
152
- return /* @__PURE__ */ jsxs(Collapsible, { children: [/* @__PURE__ */ jsxs(CollapsibleTrigger, {
153
- className: "group inline-flex w-fit items-center gap-1 text-xs font-medium text-fd-muted-foreground hover:text-fd-accent-foreground",
154
- children: [
155
- t("Headers"),
156
- /* @__PURE__ */ jsxs("span", {
157
- className: "font-normal",
158
- children: [
159
- "(",
160
- entries.length,
161
- ")"
162
- ]
163
- }),
164
- /* @__PURE__ */ jsx(ChevronDown, { className: "size-3.5 group-data-[panel-open]:rotate-180" })
165
- ]
166
- }), /* @__PURE__ */ jsx(CollapsibleContent, { children: /* @__PURE__ */ jsx("dl", {
167
- className: "grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 pt-2 font-mono text-xs",
168
- children: entries.map(([name, value]) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", {
169
- className: "text-fd-muted-foreground",
170
- children: name
171
- }), /* @__PURE__ */ jsx("dd", {
172
- className: "break-all",
173
- children: value
174
- })] }, name))
175
- }) })] });
176
- }
177
- function TextResult({ lang, charset, data, contentType }) {
232
+ function TextResult({ lang, data }) {
178
233
  const code = useMemo(() => {
179
- let out;
180
- if (charset) try {
181
- out = new TextDecoder(charset).decode(data.body);
182
- } catch {}
183
- out ??= new TextDecoder("utf-8").decode(data.body);
184
- if (lang === "json") try {
185
- out = JSON.stringify(JSON.parse(out), null, 2);
186
- } catch {}
187
- return out;
188
- }, [
189
- lang,
190
- charset,
191
- data.body
192
- ]);
234
+ const text = data.text();
235
+ if (lang !== "json") return text;
236
+ try {
237
+ return JSON.stringify(JSON.parse(text), null, 2);
238
+ } catch {
239
+ return text;
240
+ }
241
+ }, [lang, data]);
193
242
  return /* @__PURE__ */ jsx(ClientCodeBlock, {
194
243
  lang: code.length > 5e3 ? "text" : lang,
195
244
  code,
196
- codeblock: { title: /* @__PURE__ */ jsx("span", {
197
- className: "text-xs font-mono",
198
- children: contentType
199
- }) }
245
+ codeblock: panelCodeBlock
200
246
  });
201
247
  }
202
- function ImageResult({ mime, src }) {
203
- return /* @__PURE__ */ jsxs("figure", {
204
- className: "w-full p-1 bg-fd-card border rounded-lg shadow-sm",
205
- children: [/* @__PURE__ */ jsx("figcaption", {
206
- className: "text-xs text-fd-muted-foreground font-mono p-1 pb-2",
207
- children: mime
208
- }), /* @__PURE__ */ jsx("img", {
209
- src,
210
- alt: "",
211
- className: "w-full rounded-md"
212
- })]
213
- });
214
- }
215
- /**
216
- * The file name from `Content-Disposition`, preferring `filename*` (RFC 6266), without directories.
217
- */
218
- function getFileName(headers) {
219
- const disposition = headers.get("Content-Disposition") ?? "";
220
- const extended = /(?:^|;)\s*filename\*\s*=\s*[^']*'[^']*'([^\s;"]+)/i.exec(disposition);
221
- let name;
222
- if (extended) try {
223
- name = decodeURIComponent(extended[1]);
224
- } catch {}
225
- if (!name) {
226
- const match = /(?:^|;)\s*filename\s*=\s*(?:"((?:\\.|[^"\\])*)"|([^\s;]+))/i.exec(disposition);
227
- name = match?.[1]?.replace(/\\(.)/g, "$1") ?? match?.[2];
228
- }
229
- return name?.replace(/^.*[/\\]/, "") || "response";
230
- }
231
248
  //#endregion
232
- export { DefaultResultDisplay };
249
+ export { DefaultResultDisplay, PanelHeader, iconButtonClassName, panelCodeBlock };
@@ -0,0 +1,21 @@
1
+ "use client";
2
+ import { cn } from "../../../utils/cn.js";
3
+ import { jsx, jsxs } from "react/jsx-runtime";
4
+ import { Tabs } from "@base-ui/react/tabs";
5
+ //#region src/ui/playground/components/segmented.tsx
6
+ const Segmented = Tabs.Root;
7
+ const SegmentedPanel = Tabs.Panel;
8
+ /** a pill switch with a sliding indicator, its panels go in `<SegmentedPanel />` */
9
+ function SegmentedList({ items, className, ...props }) {
10
+ return /* @__PURE__ */ jsxs(Tabs.List, {
11
+ ...props,
12
+ className: cn("relative z-0 flex items-center rounded-lg bg-fd-secondary p-0.5 text-xs font-medium", className),
13
+ children: [items.map((item) => /* @__PURE__ */ jsx(Tabs.Tab, {
14
+ value: item.value,
15
+ className: "inline-flex h-6 items-center gap-1.5 rounded-md px-2 text-fd-muted-foreground transition-colors hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring data-active:text-fd-foreground",
16
+ children: item.label
17
+ }, item.value)), /* @__PURE__ */ jsx(Tabs.Indicator, { className: "absolute top-(--active-tab-top) left-0 -z-1 h-(--active-tab-height) w-(--active-tab-width) translate-x-(--active-tab-left) rounded-md bg-fd-background shadow-sm ring-1 ring-fd-border transition-[translate,width] duration-250 ease-[cubic-bezier(0.16,1,0.3,1)] motion-reduce:transition-none dark:bg-fd-accent" })]
18
+ });
19
+ }
20
+ //#endregion
21
+ export { Segmented, SegmentedList, SegmentedPanel };