fumadocs-openapi 12.4.2 → 12.5.1

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 (39) hide show
  1. package/css/generated/shared.css +1 -1
  2. package/dist/.translations/index.d.ts +12 -1
  3. package/dist/.translations/keys.js +12 -1
  4. package/dist/generate-file.js +3 -1
  5. package/dist/index.browser.d.ts +2 -2
  6. package/dist/index.d.ts +3 -2
  7. package/dist/operation.js +2 -2
  8. package/dist/packages/shared-api/dist/codegen.js +1 -3
  9. package/dist/packages/shared-api/dist/components/schema/client.js +1 -1
  10. package/dist/packages/shared-api/dist/components/select.js +6 -2
  11. package/dist/playground/auth.d.ts +84 -30
  12. package/dist/playground/auth.js +250 -150
  13. package/dist/playground/fetcher.js +11 -6
  14. package/dist/playground/index.d.ts +2 -2
  15. package/dist/playground/index.js +2 -2
  16. package/dist/playground/use-playground.d.ts +3 -3
  17. package/dist/playground/use-playground.js +46 -31
  18. package/dist/requests/generators/python.d.ts +0 -1
  19. package/dist/requests/generators/python.js +1 -1
  20. package/dist/server/index.d.ts +2 -9
  21. package/dist/server/index.js +2 -9
  22. package/dist/server/proxy.d.ts +5 -4
  23. package/dist/server/proxy.js +105 -25
  24. package/dist/types/openapi.d.ts +7 -2
  25. package/dist/types.d.ts +1 -0
  26. package/dist/ui/operation/index.js +66 -38
  27. package/dist/ui/operation/usage-tabs.js +272 -35
  28. package/dist/ui/playground/client.js +8 -4
  29. package/dist/ui/playground/components/fields.js +5 -7
  30. package/dist/ui/playground/components/oauth-panel.js +150 -39
  31. package/dist/ui/playground/components/request-panel.js +18 -3
  32. package/dist/ui/playground/components/response-panel.js +53 -90
  33. package/dist/utils/create-page.d.ts +2 -1
  34. package/dist/utils/pages/to-static-data.d.ts +12 -0
  35. package/dist/utils/use-server.js +1 -1
  36. package/package.json +8 -7
  37. package/dist/packages/shared-api/dist/components/accordion.js +0 -63
  38. package/dist/ui/operation/request-tabs.js +0 -92
  39. package/dist/ui/operation/response-tabs.js +0 -53
@@ -1,42 +1,44 @@
1
1
  "use client";
2
- import { useCodeUsage, useExampleRequests, useOperation } from "../../operation.js";
3
- import { useRenderContext } from "../../utils/create-page.js";
2
+ import { useCodeUsage, useExampleRequests, useOperation, useResponseExamples } from "../../operation.js";
3
+ import { useOpenAPI, useRenderContext } from "../../utils/create-page.js";
4
+ import { MethodLabel } from "../components/method-label.js";
4
5
  import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../packages/shared-api/dist/components/select.js";
5
6
  import { ClientCodeBlock } from "../components/codeblock.js";
6
- import { ResponseTabs } from "./response-tabs.js";
7
- import { jsx, jsxs } from "react/jsx-runtime";
8
- import { CodeBlockTab, CodeBlockTabs, CodeBlockTabsList, CodeBlockTabsTrigger } from "fumadocs-ui/components/codeblock";
7
+ import { Markdown } from "../components/markdown.js";
8
+ import { cn } from "../../utils/cn.js";
9
+ import { useState, useSyncExternalStore } from "react";
10
+ import { getRaw } from "@scalar/json-magic/magic-proxy";
11
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
12
+ import { useTranslations } from "@fuma-translate/react";
13
+ import { Switch } from "@base-ui/react/switch";
9
14
  //#region src/ui/operation/usage-tabs.tsx
15
+ /** a code block filling its card */
16
+ const cardCodeBlock = {
17
+ className: "flex min-h-0 flex-col rounded-none border-0 bg-transparent shadow-none",
18
+ viewportProps: { className: "min-h-0" }
19
+ };
20
+ /** a code block under its caption */
21
+ const captionedCodeBlock = {
22
+ ...cardCodeBlock,
23
+ viewportProps: { className: "min-h-0 pt-2" }
24
+ };
10
25
  function UsageTabs() {
11
26
  const ctx = useRenderContext();
27
+ const t = useTranslations({ note: "operation page" });
12
28
  const { operation, codeUsages } = useOperation();
13
- let usageTabs;
14
- if (ctx.content?.renderAPIExampleUsageTabs) usageTabs = ctx.content.renderAPIExampleUsageTabs(codeUsages, ctx);
15
- else {
16
- const items = Array.from(codeUsages.map());
17
- usageTabs = items.length > 0 && /* @__PURE__ */ jsxs(CodeBlockTabs, {
18
- groupId: "fumadocs_openapi_requests",
19
- defaultValue: items[0][0],
20
- children: [/* @__PURE__ */ jsx(CodeBlockTabsList, { children: items.map(([id, item]) => /* @__PURE__ */ jsx(CodeBlockTabsTrigger, {
21
- value: id,
22
- children: item.label ?? item.lang
23
- }, id)) }), items.map(([id, item]) => /* @__PURE__ */ jsx(CodeBlockTab, {
24
- value: id,
25
- children: /* @__PURE__ */ jsx(UsageTab, {
26
- id,
27
- lang: item.lang
28
- })
29
- }, id))]
30
- });
31
- }
29
+ const selectable = !operation["x-exclusiveCodeSample"];
32
30
  const slots = {
33
- selector: operation["x-exclusiveCodeSample"] ? null : /* @__PURE__ */ jsx(UsageTabsSelector, {}),
34
- usageTabs,
31
+ selector: ctx.content?.renderAPIExampleUsageTabs && selectable ? /* @__PURE__ */ jsx(UsageTabsSelector, {}) : null,
32
+ usageTabs: ctx.content?.renderAPIExampleUsageTabs ? ctx.content.renderAPIExampleUsageTabs(codeUsages, ctx) : /* @__PURE__ */ jsx(CodeUsages, {
33
+ title: t("Example Request"),
34
+ selectExample: selectable,
35
+ className: "mb-4 flex min-h-0 flex-col overflow-hidden rounded-xl border bg-fd-card"
36
+ }),
35
37
  responseTabs: /* @__PURE__ */ jsx(ResponseTabs, {})
36
38
  };
37
39
  if (ctx.content?.renderAPIExampleLayout) return ctx.content.renderAPIExampleLayout(slots, ctx);
38
40
  return /* @__PURE__ */ jsxs("div", {
39
- className: "prose-no-margin",
41
+ className: "flex min-h-0 flex-col prose-no-margin",
40
42
  children: [
41
43
  slots.selector,
42
44
  slots.usageTabs,
@@ -65,7 +67,7 @@ function UsageTabsSelector() {
65
67
  value: selected,
66
68
  onValueChange: (v) => v !== null && select(v),
67
69
  children: [/* @__PURE__ */ jsx(SelectTrigger, {
68
- className: "not-prose mb-2",
70
+ className: "not-prose mb-4 rounded-xl bg-fd-card px-3",
69
71
  children: /* @__PURE__ */ jsx(SelectValue, {})
70
72
  }), /* @__PURE__ */ jsx(SelectContent, { children: options.map((item) => /* @__PURE__ */ jsx(SelectItem, {
71
73
  value: item.value,
@@ -73,13 +75,248 @@ function UsageTabsSelector() {
73
75
  }, item.value)) })]
74
76
  });
75
77
  }
76
- function UsageTab({ id, lang }) {
77
- const code = useCodeUsage(id);
78
- if (!code) return null;
79
- return /* @__PURE__ */ jsx(ClientCodeBlock, {
80
- lang,
81
- code
78
+ function ExampleHeader({ title, children }) {
79
+ return /* @__PURE__ */ jsxs("div", {
80
+ className: "flex h-10 shrink-0 items-center gap-2 border-b ps-4 pe-1.5 text-[0.8125rem] font-medium",
81
+ children: [title && /* @__PURE__ */ jsx("span", {
82
+ className: "me-auto truncate",
83
+ children: title
84
+ }), children]
85
+ });
86
+ }
87
+ function ExampleSelect({ items, value, onValueChange, align = "end", className }) {
88
+ return /* @__PURE__ */ jsxs(Select, {
89
+ items,
90
+ value,
91
+ onValueChange: (v) => v !== null && onValueChange(v),
92
+ children: [/* @__PURE__ */ jsx(SelectTrigger, {
93
+ className: cn("h-7 w-fit max-w-1/2 shrink-0 gap-1.5 border-0 bg-transparent px-2 py-0 text-[0.8125rem] font-medium text-fd-muted-foreground hover:text-fd-accent-foreground focus:ring-0 focus-visible:ring-2 data-popup-open:bg-fd-accent data-popup-open:text-fd-accent-foreground", className),
94
+ children: /* @__PURE__ */ jsx(SelectValue, { className: "min-w-0 truncate" })
95
+ }), /* @__PURE__ */ jsx(SelectContent, {
96
+ align,
97
+ className: "max-w-80",
98
+ children: items.map((item) => /* @__PURE__ */ jsx(SelectItem, {
99
+ value: item.value,
100
+ description: item.description && /* @__PURE__ */ jsx("div", {
101
+ className: "prose prose-xs prose-no-margin font-normal text-fd-muted-foreground",
102
+ children: /* @__PURE__ */ jsx(Markdown, { md: item.description })
103
+ }),
104
+ className: "text-[0.8125rem] font-medium",
105
+ children: item.label
106
+ }, item.value))
107
+ })]
108
+ });
109
+ }
110
+ const storageKey = "fumadocs_openapi_requests";
111
+ const listeners = /* @__PURE__ */ new Set();
112
+ function subscribe(listener) {
113
+ listeners.add(listener);
114
+ return () => {
115
+ listeners.delete(listener);
116
+ };
117
+ }
118
+ function select(id) {
119
+ sessionStorage.setItem(storageKey, id);
120
+ for (const listener of listeners) listener();
121
+ }
122
+ /** the code usages of the selected example request, with a select of their languages */
123
+ function CodeUsages({ title, selectExample = false, className, viewportClassName }) {
124
+ const generators = useOperation().codeUsages.map();
125
+ const examples = useExampleRequests();
126
+ const selected = useSyncExternalStore(subscribe, () => sessionStorage.getItem(storageKey), () => null);
127
+ const id = selected !== null && generators.has(selected) ? selected : generators.keys().next().value;
128
+ const code = useCodeUsage(id ?? "");
129
+ if (id === void 0) return null;
130
+ const items = Array.from(generators, ([value, item]) => ({
131
+ value,
132
+ label: item.label ?? item.lang
133
+ }));
134
+ const example = selectExample && examples.items.length > 1 ? examples.items.find((item) => item.id === examples.selected) : void 0;
135
+ return /* @__PURE__ */ jsxs("div", {
136
+ className,
137
+ children: [/* @__PURE__ */ jsxs(ExampleHeader, {
138
+ title: !example && title,
139
+ children: [example && /* @__PURE__ */ jsx(ExampleSelect, {
140
+ items: examples.items.map((item) => ({
141
+ value: item.id,
142
+ label: item.name,
143
+ description: item.description
144
+ })),
145
+ value: example.id,
146
+ onValueChange: examples.select,
147
+ align: "start",
148
+ className: "-ms-2 me-auto max-w-none min-w-0 shrink text-fd-foreground"
149
+ }), items.length > 1 ? /* @__PURE__ */ jsx(ExampleSelect, {
150
+ items,
151
+ value: id,
152
+ onValueChange: select
153
+ }) : /* @__PURE__ */ jsx("span", {
154
+ className: "px-2 text-fd-muted-foreground",
155
+ children: items[0].label
156
+ })]
157
+ }), code && /* @__PURE__ */ jsx(ClientCodeBlock, {
158
+ lang: generators.get(id).lang,
159
+ code,
160
+ codeblock: {
161
+ ...cardCodeBlock,
162
+ viewportProps: { className: cn(cardCodeBlock.viewportProps.className, viewportClassName) }
163
+ }
164
+ })]
165
+ });
166
+ }
167
+ function ResponseTabs() {
168
+ const ctx = useRenderContext();
169
+ const tabs = useResponseExamples();
170
+ if (tabs.length === 0) return null;
171
+ if (ctx.content?.renderResponseTabs) return ctx.content.renderResponseTabs({ tabs }, ctx);
172
+ return /* @__PURE__ */ jsx(ResponseTabsDefaultContent, { tabs });
173
+ }
174
+ function ResponseTabsDefaultContent({ tabs }) {
175
+ const t = useTranslations({ note: "operation page" });
176
+ const { resolve } = useOpenAPI().doc;
177
+ const [code, setCode] = useState(tabs[0].code);
178
+ const [index, setIndex] = useState(0);
179
+ const [showSchema, setShowSchema] = useState(false);
180
+ const tab = tabs.find((item) => item.code === code) ?? tabs[0];
181
+ const examples = tab.examples ?? [];
182
+ const example = examples.at(index);
183
+ const schema = tab.mediaType ? resolve(tab.response.content?.[tab.mediaType])?.schema : void 0;
184
+ let body;
185
+ if (showSchema && schema !== void 0) body = /* @__PURE__ */ jsx(ClientCodeBlock, {
186
+ lang: "json",
187
+ code: JSON.stringify(resolve(schema), (_, value) => getRaw(value), 2),
188
+ codeblock: cardCodeBlock
189
+ });
190
+ else if (example) {
191
+ const captioned = examples.length > 1 || example.description !== void 0;
192
+ body = /* @__PURE__ */ jsxs(Fragment$1, { children: [captioned && /* @__PURE__ */ jsxs("div", {
193
+ className: "shrink-0 px-4 pt-2.5",
194
+ children: [examples.length > 1 && /* @__PURE__ */ jsx(ExampleSelect, {
195
+ items: examples.map((item, i) => ({
196
+ value: String(i),
197
+ label: item.label
198
+ })),
199
+ value: String(index),
200
+ onValueChange: (v) => setIndex(Number(v)),
201
+ align: "start",
202
+ className: "-ms-2"
203
+ }), example.description && /* @__PURE__ */ jsx("div", {
204
+ className: "prose prose-xs prose-no-margin text-fd-muted-foreground",
205
+ children: /* @__PURE__ */ jsx(Markdown, { md: example.description })
206
+ })]
207
+ }), /* @__PURE__ */ jsx(ClientCodeBlock, {
208
+ lang: "json",
209
+ code: JSON.stringify(example.sample, null, 2),
210
+ codeblock: captioned ? captionedCodeBlock : cardCodeBlock
211
+ })] });
212
+ } else body = /* @__PURE__ */ jsx("div", {
213
+ className: "px-4 py-3 text-sm text-fd-muted-foreground",
214
+ children: t("Empty")
215
+ });
216
+ return /* @__PURE__ */ jsxs("div", {
217
+ className: "mb-4 flex min-h-0 flex-col overflow-hidden rounded-xl border bg-fd-card",
218
+ children: [/* @__PURE__ */ jsxs(ExampleHeader, {
219
+ title: t("Example Response"),
220
+ children: [schema !== void 0 && /* @__PURE__ */ jsxs("label", {
221
+ className: "flex shrink-0 items-center gap-2 text-fd-muted-foreground",
222
+ children: [t("Schema"), /* @__PURE__ */ jsx(Switch.Root, {
223
+ checked: showSchema,
224
+ onCheckedChange: setShowSchema,
225
+ className: "inline-flex h-4 w-7 shrink-0 items-center rounded-full bg-fd-accent p-0.5 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring data-checked:bg-fd-primary motion-reduce:transition-none",
226
+ children: /* @__PURE__ */ jsx(Switch.Thumb, { className: "size-3 rounded-full bg-fd-muted-foreground transition-[translate,background-color] duration-200 ease-[cubic-bezier(0.16,1,0.3,1)] data-checked:translate-x-3 data-checked:bg-fd-primary-foreground motion-reduce:transition-none" })
227
+ })]
228
+ }), /* @__PURE__ */ jsx(ExampleSelect, {
229
+ items: tabs.map((item) => ({
230
+ value: item.code,
231
+ label: item.code
232
+ })),
233
+ value: tab.code,
234
+ onValueChange: (v) => {
235
+ setCode(v);
236
+ setIndex(0);
237
+ }
238
+ })]
239
+ }), body]
240
+ });
241
+ }
242
+ /** the example requests of a webhook */
243
+ function RequestTabs() {
244
+ const ctx = useRenderContext();
245
+ const { path, method, operation, pathItem } = useOperation();
246
+ const { items } = useExampleRequests();
247
+ if (!operation.requestBody) return null;
248
+ if (ctx.content?.renderRequestTabs) return ctx.content.renderRequestTabs({
249
+ items,
250
+ route: path,
251
+ method,
252
+ pathItem,
253
+ operation
254
+ }, ctx);
255
+ return /* @__PURE__ */ jsx(RequestTabsDefaultContent, {});
256
+ }
257
+ function RequestTabsDefaultContent() {
258
+ const t = useTranslations({ note: "operation page" });
259
+ const { items, selected, select } = useExampleRequests();
260
+ const item = items.find((item) => item.id === selected) ?? items[0];
261
+ const requestData = item.data;
262
+ const displayNames = {
263
+ body: t("Request Body"),
264
+ cookie: t("Cookie Parameters"),
265
+ header: t("Header Parameters"),
266
+ query: t("Query Parameters"),
267
+ path: t("Path Parameters")
268
+ };
269
+ return /* @__PURE__ */ jsxs("div", {
270
+ className: "flex min-h-0 flex-col overflow-hidden rounded-xl border bg-fd-card",
271
+ children: [
272
+ /* @__PURE__ */ jsx(ExampleHeader, {
273
+ title: t("Example Requests"),
274
+ children: items.length > 1 && /* @__PURE__ */ jsx(ExampleSelect, {
275
+ items: items.map((item) => ({
276
+ value: item.id,
277
+ label: item.id === "_default" ? t("Default") : item.name
278
+ })),
279
+ value: item.id,
280
+ onValueChange: select
281
+ })
282
+ }),
283
+ /* @__PURE__ */ jsxs("div", {
284
+ className: "shrink-0 px-4 py-3 text-[0.8125rem]",
285
+ children: [
286
+ /* @__PURE__ */ jsx(MethodLabel, {
287
+ className: "me-2 text-xs",
288
+ children: requestData.method
289
+ }),
290
+ /* @__PURE__ */ jsx("span", {
291
+ className: "font-mono",
292
+ children: item.pathname
293
+ }),
294
+ item.description && /* @__PURE__ */ jsx("div", {
295
+ className: "prose prose-xs prose-no-margin mt-1 text-fd-muted-foreground",
296
+ children: /* @__PURE__ */ jsx(Markdown, { md: item.description })
297
+ })
298
+ ]
299
+ }),
300
+ Object.entries(displayNames).map(([k, v]) => {
301
+ const data = requestData[k];
302
+ if (!data || Object.keys(data).length === 0) return;
303
+ return /* @__PURE__ */ jsxs("div", {
304
+ className: "flex min-h-0 flex-col border-t",
305
+ children: [/* @__PURE__ */ jsxs("div", {
306
+ className: "flex shrink-0 px-4 pt-3 text-xs font-medium text-fd-muted-foreground",
307
+ children: [v, k === "body" && /* @__PURE__ */ jsx("span", {
308
+ className: "ms-auto font-mono font-normal",
309
+ children: requestData.bodyMediaType
310
+ })]
311
+ }), /* @__PURE__ */ jsx(ClientCodeBlock, {
312
+ lang: "json",
313
+ code: JSON.stringify(data, null, 2),
314
+ codeblock: captionedCodeBlock
315
+ })]
316
+ }, k);
317
+ })
318
+ ]
82
319
  });
83
320
  }
84
321
  //#endregion
85
- export { UsageTabs };
322
+ export { CodeUsages, ExampleSelect, RequestTabs, UsageTabs };
@@ -10,29 +10,33 @@ import { EndpointBar } from "../components/endpoint.js";
10
10
  import { DialogPopup } from "../components/dialog.js";
11
11
  import { SchemaProvider } from "../../packages/shared-api/dist/components/playground/schema.js";
12
12
  import { UrlBar } from "./components/url-bar.js";
13
- import { RequestPanel } from "./components/request-panel.js";
13
+ import { RequestPanel, mutualTLSProvider } from "./components/request-panel.js";
14
14
  import { oauthPanelProvider } from "./components/oauth-panel.js";
15
15
  import { ResponsePanel } from "./components/response-panel.js";
16
16
  import { useMemo, useState } from "react";
17
17
  import { jsx, jsxs } from "react/jsx-runtime";
18
18
  import { StfProvider, useFieldValue } from "@fumari/stf";
19
19
  import { useTranslations } from "@fuma-translate/react";
20
- import { useOnChange } from "fumadocs-core/utils/use-on-change";
21
20
  import { Play, X } from "lucide-react";
22
21
  import { buttonVariants } from "fumadocs-ui/components/ui/button";
23
22
  import { Dialog } from "@base-ui/react/dialog";
23
+ import { useOnChange } from "fumadocs-core/utils/use-on-change";
24
24
  //#region src/ui/playground/client.tsx
25
25
  function PlaygroundClient({ writeOnly = true, readOnly = false, authProviders, transformAuthInputs, fetchOptions, components, renderParameterField, renderBodyField, ...rest }) {
26
26
  const t = useTranslations({ note: "playground" });
27
27
  const { doc } = useOpenAPI();
28
28
  const { path, method, operation } = useOperation();
29
- const providers = useMemo(() => [...authProviders ?? [], oauthPanelProvider], [authProviders]);
29
+ const providers = useMemo(() => [
30
+ ...authProviders ?? [],
31
+ oauthPanelProvider,
32
+ mutualTLSProvider
33
+ ], [authProviders]);
30
34
  const playground = usePlayground({
31
35
  authProviders: providers,
32
36
  transformAuthInputs,
33
37
  fetchOptions
34
38
  });
35
- const [open, setOpen] = useState(false);
39
+ const [open, setOpen] = useState(playground.auth.flowReturned);
36
40
  useOnChange(playground.auth.flowReturned, (returned) => {
37
41
  if (returned) setOpen(true);
38
42
  });
@@ -428,14 +428,12 @@ function CellSelect({ id, items, value, onValueChange, placeholder }) {
428
428
  })
429
429
  }), /* @__PURE__ */ jsx(SelectContent, { children: items.map((item) => /* @__PURE__ */ jsx(SelectItem, {
430
430
  value: item.value,
431
+ description: item.description && /* @__PURE__ */ jsx("span", {
432
+ className: "text-xs text-fd-muted-foreground",
433
+ children: item.description
434
+ }),
431
435
  className: "text-[0.8125rem]",
432
- children: item.description ? /* @__PURE__ */ jsxs("span", {
433
- className: "flex flex-col",
434
- children: [item.label, /* @__PURE__ */ jsx("span", {
435
- className: "text-xs text-fd-muted-foreground",
436
- children: item.description
437
- })]
438
- }) : item.label
436
+ children: item.label
439
437
  }, item.value)) })]
440
438
  });
441
439
  }