fumadocs-openapi 12.1.1 → 12.3.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 (50) hide show
  1. package/css/generated/shared.css +1 -1
  2. package/dist/.translations/index.d.ts +63 -20
  3. package/dist/.translations/keys.js +63 -20
  4. package/dist/operation.d.ts +6 -1
  5. package/dist/operation.js +2 -4
  6. package/dist/packages/shared-api/dist/.translations/index.d.ts +1 -0
  7. package/dist/packages/shared-api/dist/.translations/keys.js +1 -0
  8. package/dist/packages/shared-api/dist/auto-anchor/index.js +54 -10
  9. package/dist/packages/shared-api/dist/components/accordion.js +3 -11
  10. package/dist/packages/shared-api/dist/components/collapsible.js +2 -2
  11. package/dist/packages/shared-api/dist/components/schema/client.d.ts +5 -0
  12. package/dist/packages/shared-api/dist/components/schema/client.js +408 -455
  13. package/dist/packages/shared-api/dist/components/select-tab.js +7 -12
  14. package/dist/packages/shared-api/dist/components/select.js +8 -7
  15. package/dist/playground/auth.d.ts +5 -1
  16. package/dist/playground/auth.js +7 -2
  17. package/dist/playground/fetcher.d.ts +8 -0
  18. package/dist/playground/fetcher.js +30 -1
  19. package/dist/playground/index.d.ts +2 -1
  20. package/dist/playground/index.js +2 -1
  21. package/dist/playground/use-playground.d.ts +62 -0
  22. package/dist/playground/use-playground.js +140 -0
  23. package/dist/ui/components/dialog.js +14 -0
  24. package/dist/ui/components/endpoint.js +40 -0
  25. package/dist/ui/components/heading.js +1 -1
  26. package/dist/ui/components/schema.js +1 -1
  27. package/dist/ui/components/segmented.js +21 -0
  28. package/dist/ui/operation/index.js +267 -250
  29. package/dist/ui/playground/client.d.ts +9 -26
  30. package/dist/ui/playground/client.js +150 -389
  31. package/dist/ui/playground/components/fields.js +693 -0
  32. package/dist/ui/playground/components/oauth-panel.js +182 -0
  33. package/dist/ui/playground/components/request-panel.js +480 -0
  34. package/dist/ui/playground/components/response-panel.js +150 -0
  35. package/dist/ui/playground/components/result-display.d.ts +1 -0
  36. package/dist/ui/playground/components/result-display.js +185 -168
  37. package/dist/ui/playground/components/server-select.js +100 -89
  38. package/dist/ui/playground/components/url-bar.js +96 -0
  39. package/dist/ui/playground/status-info.js +32 -45
  40. package/dist/utils/document/load.js +1 -1
  41. package/package.json +12 -12
  42. package/dist/packages/shared-api/dist/auto-anchor/client.js +0 -20
  43. package/dist/packages/shared-api/dist/components/collapsible.d.ts +0 -7
  44. package/dist/packages/shared-api/dist/components/dialog.js +0 -55
  45. package/dist/packages/shared-api/dist/components/input.js +0 -14
  46. package/dist/packages/shared-api/dist/components/label.js +0 -11
  47. package/dist/packages/shared-api/dist/components/playground/inputs.js +0 -439
  48. package/dist/packages/shared-api/dist/components/popover.js +0 -23
  49. package/dist/packages/shared-api/dist/utils/merge-refs.js +0 -11
  50. package/dist/ui/playground/components/oauth-dialog.js +0 -259
@@ -1,12 +1,16 @@
1
1
  import { ParameterObject } from "../../types.js";
2
- import { BrowserFetcherOptions } from "../../playground/fetcher.js";
3
2
  import { DefaultResultDisplay, ResultDisplayProps } from "./components/result-display.js";
4
- import { Collapsible } from "../../packages/shared-api/dist/components/collapsible.js";
5
- import { AuthField } from "../../playground/auth.js";
3
+ import { PlaygroundOptions, RequestBodyInfo } from "../../playground/use-playground.js";
6
4
  import { ComponentProps, FC, ReactNode } from "react";
7
- import { JsonSchema } from "@fumadocs/json-schema";
8
5
  import { FieldKey } from "@fumari/stf";
9
6
  //#region src/ui/playground/client.d.ts
7
+ export interface PlaygroundClientProps extends ComponentProps<'div'>, PlaygroundClientOptions {
8
+ /** @deprecated it defaults to `true` for requests */
9
+ writeOnly?: boolean;
10
+ /** @deprecated it defaults to `false` for requests */
11
+ readOnly?: boolean;
12
+ }
13
+ /** @deprecated use `usePlayground().stf` for the form */
10
14
  export interface FormValues extends Record<string, unknown> {
11
15
  path: Record<string, unknown>;
12
16
  query: Record<string, unknown>;
@@ -14,23 +18,9 @@ export interface FormValues extends Record<string, unknown> {
14
18
  cookie: Record<string, unknown>;
15
19
  body: unknown;
16
20
  }
17
- export interface PlaygroundClientProps extends Omit<ComponentProps<'form'>, 'method'>, PlaygroundClientOptions {
18
- writeOnly: boolean;
19
- readOnly: boolean;
20
- }
21
- export interface CollapsiblePanelProps extends Omit<ComponentProps<typeof Collapsible>, 'title'> {
22
- 'data-type': 'authorization' | 'body' | ParamType;
23
- title: ReactNode;
24
- }
25
- export interface PlaygroundClientOptions {
26
- /**
27
- * transform fields for auth-specific parameters (e.g. header)
28
- */
29
- transformAuthInputs?: (fields: AuthField[]) => AuthField[];
30
- fetchOptions?: BrowserFetcherOptions;
21
+ export interface PlaygroundClientOptions extends PlaygroundOptions {
31
22
  components?: {
32
23
  ResultDisplay?: FC<ResultDisplayProps>;
33
- CollapsiblePanel?: FC<CollapsiblePanelProps>;
34
24
  };
35
25
  /**
36
26
  * render the parameter inputs of API endpoint.
@@ -48,14 +38,7 @@ export interface PlaygroundClientOptions {
48
38
  */
49
39
  renderBodyField?: (fieldName: 'body', info: RequestBodyInfo) => ReactNode;
50
40
  }
51
- interface RequestBodyInfo {
52
- schema: JsonSchema;
53
- mediaType: string;
54
- }
55
41
  export default function PlaygroundClient({ writeOnly, readOnly, transformAuthInputs, fetchOptions, components, renderParameterField, renderBodyField, ...rest }: PlaygroundClientProps): import("react").JSX.Element;
56
- declare const ParamTypes: readonly ['path', 'header', 'cookie', 'query'];
57
- type ParamType = (typeof ParamTypes)[number];
58
- export declare function DefaultCollapsiblePanel({ title, children, ...props }: CollapsiblePanelProps): import("react").JSX.Element;
59
42
  export declare const Custom: {
60
43
  useController(fieldName: FieldKey, options?: {
61
44
  defaultValue?: unknown;
@@ -1,421 +1,182 @@
1
1
  "use client";
2
- import { pathnameFromRequest } from "../../requests/generators/index.js";
3
- import { useQuery } from "../../packages/shared-api/dist/utils/use-query.js";
4
- import { useAuthFields, usePlaygroundAuth } from "../../playground/auth.js";
5
- import { useServer } from "../../utils/use-server.js";
6
2
  import { useOpenAPI } from "../../utils/create-page.js";
7
- import { encodeRequestData } from "../../requests/media/encode.js";
8
3
  import { useExampleRequests, useOperation } from "../../operation.js";
9
- import { MethodLabel } from "../components/method-label.js";
4
+ import { usePlayground } from "../../playground/use-playground.js";
10
5
  import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../packages/shared-api/dist/components/select.js";
11
- import { Markdown } from "../components/markdown.js";
12
6
  import { cn } from "../../utils/cn.js";
13
- import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../../packages/shared-api/dist/components/collapsible.js";
14
- import { DefaultResultDisplay } from "./components/result-display.js";
15
- import { SchemaProvider, anyFields, useResolvedSchema } from "../../packages/shared-api/dist/components/playground/schema.js";
16
- import { Label } from "../../packages/shared-api/dist/components/label.js";
17
- import ServerSelect from "./components/server-select.js";
18
- import { FieldInput, FieldSet, JsonInput, ObjectInput } from "../../packages/shared-api/dist/components/playground/inputs.js";
19
- import { OAuthDialog, OAuthDialogContent, OAuthDialogTrigger } from "./components/oauth-dialog.js";
20
- import { Spinner } from "../../packages/shared-api/dist/components/spinner.js";
21
- import { Fragment, createContext, use, useEffect, useMemo, useRef, useState } from "react";
22
- import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
23
- import { StfProvider, useDataEngine, useFieldValue, useListener, useStf } from "@fumari/stf";
24
- import { stringifyFieldKey } from "@fumari/stf/lib/utils";
7
+ import { Segmented, SegmentedList } from "../components/segmented.js";
8
+ import { DefaultResultDisplay, iconButtonClassName } from "./components/result-display.js";
9
+ import { EndpointBar } from "../components/endpoint.js";
10
+ import { DialogPopup } from "../components/dialog.js";
11
+ import { SchemaProvider } from "../../packages/shared-api/dist/components/playground/schema.js";
12
+ import { UrlBar } from "./components/url-bar.js";
13
+ import { RequestPanel } from "./components/request-panel.js";
14
+ import { ResponsePanel } from "./components/response-panel.js";
15
+ import { useState } from "react";
16
+ import { jsx, jsxs } from "react/jsx-runtime";
17
+ import { StfProvider, useFieldValue } from "@fumari/stf";
25
18
  import { useTranslations } from "@fuma-translate/react";
26
- import { ChevronDown, LoaderCircle, PlusIcon } from "lucide-react";
19
+ import { Play, X } from "lucide-react";
27
20
  import { buttonVariants } from "fumadocs-ui/components/ui/button";
21
+ import { Dialog } from "@base-ui/react/dialog";
28
22
  import { useOnChange } from "fumadocs-core/utils/use-on-change";
29
23
  //#region src/ui/playground/client.tsx
30
- const OptionsContext = createContext({});
31
- function usePlaygroundOptions() {
32
- return use(OptionsContext);
33
- }
34
- function PlaygroundClient({ writeOnly, readOnly, transformAuthInputs, fetchOptions, components, renderParameterField, renderBodyField, ...rest }) {
24
+ function PlaygroundClient({ writeOnly = true, readOnly = false, transformAuthInputs, fetchOptions, components, renderParameterField, renderBodyField, ...rest }) {
35
25
  const t = useTranslations({ note: "playground" });
36
- const { doc, mediaAdapters, proxyUrl } = useOpenAPI();
37
- const { dereferenced } = doc;
38
- const { path: route, method, operation, parameters: groups, requestBody } = useOperation();
39
- const options = useMemo(() => ({
40
- transformAuthInputs,
41
- fetchOptions,
42
- components,
43
- renderParameterField,
44
- renderBodyField
45
- }), [
26
+ const { doc } = useOpenAPI();
27
+ const { path, method, operation } = useOperation();
28
+ const playground = usePlayground({
46
29
  transformAuthInputs,
47
- fetchOptions,
48
- components,
49
- renderParameterField,
50
- renderBodyField
51
- ]);
52
- const parameters = useMemo(() => groups.flatMap((group) => group.items), [groups]);
53
- const body = useMemo(() => {
54
- if (!requestBody) return;
55
- const mediaType = "application/json" in requestBody.content ? "application/json" : Object.keys(requestBody.content)[0];
56
- return {
57
- mediaType,
58
- schema: requestBody.content[mediaType].schema ?? true
59
- };
60
- }, [requestBody]);
61
- const { items: examples, selected: exampleId, update } = useExampleRequests();
62
- const { resolveUrl } = useServer();
63
- const { ResultDisplay = DefaultResultDisplay, CollapsiblePanel = DefaultCollapsiblePanel } = components ?? {};
64
- const defaultValues = useMemo(() => {
65
- const requestData = examples.find((example) => example.id === exampleId)?.data;
66
- return {
67
- path: requestData?.path ?? {},
68
- query: requestData?.query ?? {},
69
- header: requestData?.header ?? {},
70
- body: requestData?.body ?? {},
71
- cookie: requestData?.cookie ?? {}
72
- };
73
- }, [examples, exampleId]);
74
- const stf = useStf({ defaultValues });
75
- const auth = useAuthFields(stf.dataEngine, {
76
- operation,
77
- transform: transformAuthInputs
30
+ fetchOptions
78
31
  });
79
- const testQuery = useQuery(async (input) => {
80
- const fetcher = await import("../../playground/index.js").then((mod) => mod.createBrowserFetcher(mediaAdapters, {
81
- proxyUrl,
82
- ...fetchOptions
83
- }));
84
- const encoded = encodeRequestData({
85
- ...auth.mapValues(input),
86
- method,
87
- bodyMediaType: body?.mediaType
88
- }, mediaAdapters, parameters);
89
- return fetcher.fetch(resolveUrl(pathnameFromRequest(route, encoded)), encoded);
90
- });
91
- const timerRef = useRef(null);
92
- const stfSync = useRef(false);
93
- function triggerExampleUpdate() {
94
- const data = {
95
- ...auth.mapValues(stf.dataEngine.getData()),
96
- method,
97
- bodyMediaType: body?.mediaType
98
- };
99
- update(data);
100
- }
101
- useListener({
102
- stf,
103
- onUpdate() {
104
- if (!stfSync.current) return;
105
- if (timerRef.current) window.clearTimeout(timerRef.current);
106
- timerRef.current = window.setTimeout(triggerExampleUpdate, 400);
107
- }
32
+ const [open, setOpen] = useState(false);
33
+ useOnChange(playground.auth.flowReturned, (returned) => {
34
+ if (returned) setOpen(true);
108
35
  });
109
- useEffect(() => {
110
- if (stf.dataEngine.getData() === defaultValues) return;
111
- stf.dataEngine.reset(defaultValues);
112
- }, [defaultValues]);
113
- useEffect(() => {
114
- const reset = auth.init();
115
- triggerExampleUpdate();
116
- stfSync.current = true;
117
- return () => {
118
- stfSync.current = false;
119
- reset();
120
- };
121
- }, [defaultValues, auth.init]);
122
- return /* @__PURE__ */ jsx(OptionsContext, {
123
- value: options,
124
- children: /* @__PURE__ */ jsx(StfProvider, {
125
- value: stf,
126
- children: /* @__PURE__ */ jsx(SchemaProvider, {
127
- docRoot: dereferenced,
128
- writeOnly,
129
- readOnly,
130
- children: /* @__PURE__ */ jsxs("form", {
36
+ return /* @__PURE__ */ jsx(StfProvider, {
37
+ value: playground.stf,
38
+ children: /* @__PURE__ */ jsx(SchemaProvider, {
39
+ docRoot: doc.dereferenced,
40
+ writeOnly,
41
+ readOnly,
42
+ children: /* @__PURE__ */ jsxs(Dialog.Root, {
43
+ open,
44
+ onOpenChange: setOpen,
45
+ children: [/* @__PURE__ */ jsx(EndpointBar, {
46
+ method,
47
+ route: path,
48
+ deprecated: operation.deprecated,
131
49
  ...rest,
132
- className: cn("not-prose flex flex-col rounded-xl border shadow-md overflow-hidden bg-fd-card text-fd-card-foreground", rest.className),
133
- onSubmit: (e) => {
134
- testQuery.start(stf.dataEngine.getData());
135
- e.preventDefault();
136
- },
137
- children: [
138
- /* @__PURE__ */ jsx(ServerSelect, { className: "border-b" }),
139
- /* @__PURE__ */ jsxs("div", {
140
- className: "flex flex-row items-center gap-2 text-sm p-3 not-last:pb-0",
141
- children: [
142
- /* @__PURE__ */ jsx(MethodLabel, { children: method }),
143
- /* @__PURE__ */ jsx(Route, {
144
- route,
145
- className: cn("flex-1", operation.deprecated && "line-through")
146
- }),
147
- /* @__PURE__ */ jsx("button", {
148
- type: "submit",
149
- className: cn(buttonVariants({
150
- variant: "default",
151
- size: "sm"
152
- }), "w-14 py-1.5"),
153
- disabled: testQuery.isLoading,
154
- children: testQuery.isLoading ? /* @__PURE__ */ jsx(LoaderCircle, { className: "size-4 animate-spin" }) : t("Send")
155
- })
156
- ]
157
- }),
158
- testQuery.data ? /* @__PURE__ */ jsx(ResultDisplay, {
159
- data: testQuery.data,
160
- reset: testQuery.reset
161
- }) : null,
162
- auth.requirements.length > 0 && /* @__PURE__ */ jsx(SecurityRequirements, {
163
- requirements: auth.requirements,
164
- selected: auth.selected,
165
- select: auth.select,
166
- children: auth.fields.map((field) => /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(AuthInput, { field }) }, stringifyFieldKey(field.fieldName)))
167
- }),
168
- /* @__PURE__ */ jsx(ParametersForm, { parameters }),
169
- body && /* @__PURE__ */ jsx(CollapsiblePanel, {
170
- "data-type": "body",
171
- title: t("Body"),
172
- children: renderBodyField ? renderBodyField("body", body) : /* @__PURE__ */ jsx(BodyInput, { field: body.schema })
173
- })
174
- ]
175
- })
50
+ children: /* @__PURE__ */ jsxs(Dialog.Trigger, {
51
+ className: cn(buttonVariants({
52
+ variant: "primary",
53
+ size: "sm"
54
+ }), "group shrink-0 gap-1.5 rounded-lg px-3 transition-[background-color,scale] active:scale-[0.97] motion-reduce:transition-none"),
55
+ children: [/* @__PURE__ */ jsx(Play, { className: "size-3 fill-current transition-transform duration-200 group-hover:translate-x-px motion-reduce:transition-none" }), t("Try in Playground")]
56
+ })
57
+ }), /* @__PURE__ */ jsx(DialogPopup, {
58
+ className: "sm:h-[min(52rem,calc(100dvh-3rem))] sm:w-[min(80rem,calc(100vw-3rem))]",
59
+ children: /* @__PURE__ */ jsx(PlaygroundDialog, {
60
+ playground,
61
+ renderParameterField,
62
+ renderBodyField,
63
+ ResultDisplay: components?.ResultDisplay
64
+ })
65
+ })]
176
66
  })
177
67
  })
178
68
  });
179
69
  }
180
- function SecurityRequirements({ requirements, select, selected, children }) {
70
+ function PlaygroundDialog({ playground, renderParameterField, renderBodyField, ResultDisplay }) {
181
71
  const t = useTranslations({ note: "playground" });
182
- const { isLoading, error } = usePlaygroundAuth();
183
- const defaultOpen = isLoading || error != null;
184
- const [open, setOpen] = useState(defaultOpen);
185
- const { CollapsiblePanel = DefaultCollapsiblePanel } = usePlaygroundOptions().components ?? {};
186
- useOnChange(defaultOpen, () => {
187
- if (defaultOpen) setOpen(true);
188
- });
189
- const items = requirements.map((requirement, i) => ({
190
- value: i,
191
- label: /* @__PURE__ */ jsx("span", {
192
- className: "inline-flex items-center gap-1 font-mono font-medium",
193
- children: requirement.map((item, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && /* @__PURE__ */ jsx(PlusIcon, { className: "text-fd-muted-foreground size-3.5" }), /* @__PURE__ */ jsx("span", {
194
- className: cn(item.scheme.deprecated && "text-fd-muted-foreground line-through"),
195
- children: item.id
196
- })] }, i))
197
- })
198
- }));
199
- return /* @__PURE__ */ jsxs(CollapsiblePanel, {
200
- title: /* @__PURE__ */ jsxs(Fragment$1, { children: [t("Authorization"), isLoading && /* @__PURE__ */ jsxs("span", {
201
- className: "border-s ps-2 inline-flex items-center gap-1.5 text-fd-muted-foreground text-xs font-mono",
202
- children: [
203
- /* @__PURE__ */ jsx(Spinner, {}),
204
- " ",
205
- t("Fetching token...")
206
- ]
207
- })] }),
208
- "data-type": "authorization",
209
- open,
210
- onOpenChange: setOpen,
72
+ const { title, method, path, operation, parameters } = useOperation();
73
+ const { response, isSending, send, clearResponse } = playground;
74
+ const [view, setView] = useState("request");
75
+ return /* @__PURE__ */ jsxs("form", {
76
+ noValidate: true,
77
+ className: "flex min-h-0 flex-1 flex-col",
78
+ onSubmit: (e) => {
79
+ e.preventDefault();
80
+ send();
81
+ setView("response");
82
+ },
83
+ onKeyDown: (e) => {
84
+ if (e.key !== "Enter" || !(e.metaKey || e.ctrlKey)) return;
85
+ e.preventDefault();
86
+ e.currentTarget.requestSubmit();
87
+ },
211
88
  children: [
212
- error != null && /* @__PURE__ */ jsxs("div", {
213
- className: "p-2 border rounded-lg bg-fd-secondary",
214
- children: [/* @__PURE__ */ jsx("p", {
215
- className: "text-fd-muted-foreground font-medium mb-1",
216
- children: t("Failed to fetch token")
217
- }), /* @__PURE__ */ jsx("p", { children: String(error) })]
218
- }),
219
89
  /* @__PURE__ */ jsxs("div", {
220
- className: "overflow-hidden rounded-md border bg-fd-secondary focus-within:ring focus-within:ring-fd-ring",
221
- children: [/* @__PURE__ */ jsxs(Select, {
222
- items,
223
- value: selected,
224
- onValueChange: (v) => v !== null && select(v),
225
- children: [/* @__PURE__ */ jsx(SelectTrigger, {
226
- className: "rounded-t-md rounded-b-none border-0 bg-transparent focus:ring-0",
227
- children: /* @__PURE__ */ jsx(SelectValue, {})
228
- }), /* @__PURE__ */ jsx(SelectContent, { children: items.map(({ value, label }) => /* @__PURE__ */ jsx(SelectItem, {
229
- value,
230
- children: label
231
- }, value)) })]
232
- }), /* @__PURE__ */ jsx("div", {
233
- className: "grid grid-cols-[auto_1fr] gap-2 border-t px-2 py-1.5 text-xs text-fd-muted-foreground empty:hidden",
234
- children: requirements[selected]?.map(({ id, scheme }, _, arr) => {
235
- if (!scheme.description) return;
236
- return /* @__PURE__ */ jsxs(Fragment, { children: [arr.length > 1 && /* @__PURE__ */ jsx("span", {
237
- className: "font-medium font-mono text-fd-primary",
238
- children: id
239
- }), /* @__PURE__ */ jsx(Markdown, { md: scheme.description })] }, id);
90
+ className: "flex h-12 shrink-0 items-center gap-2 ps-4 pe-2 sm:ps-5",
91
+ children: [
92
+ /* @__PURE__ */ jsx(Dialog.Title, {
93
+ className: "truncate text-sm font-medium",
94
+ children: title
95
+ }),
96
+ operation.deprecated && /* @__PURE__ */ jsx("span", {
97
+ className: "rounded-md bg-amber-500/10 px-1.5 py-0.5 text-xs font-medium text-amber-600 dark:text-amber-400",
98
+ children: t("Deprecated")
99
+ }),
100
+ /* @__PURE__ */ jsxs("div", {
101
+ className: "ms-auto flex items-center gap-1",
102
+ children: [/* @__PURE__ */ jsx(ExampleSelect, {}), /* @__PURE__ */ jsx(Dialog.Close, {
103
+ "aria-label": t("Close"),
104
+ className: iconButtonClassName,
105
+ children: /* @__PURE__ */ jsx(X, {})
106
+ })]
240
107
  })
241
- })]
108
+ ]
242
109
  }),
243
- children
244
- ]
245
- });
246
- }
247
- const ParamTypes = [
248
- "path",
249
- "header",
250
- "cookie",
251
- "query"
252
- ];
253
- function ParameterItem({ type, parameters }) {
254
- const { renderParameterField } = usePlaygroundOptions();
255
- return parameters.map((field) => {
256
- const fieldName = [type, field.name];
257
- if (renderParameterField) return renderParameterField(fieldName, field);
258
- const contentTypes = field.content && Object.keys(field.content);
259
- const schema = field.content && contentTypes && contentTypes.length > 0 ? field.content[contentTypes[0]].schema : field.schema;
260
- return /* @__PURE__ */ jsx(FieldSet, {
261
- name: field.name,
262
- fieldName,
263
- field: schema ?? anyFields,
264
- isRequired: field.required
265
- }, stringifyFieldKey(fieldName));
266
- });
267
- }
268
- function ParametersForm({ parameters }) {
269
- const { CollapsiblePanel = DefaultCollapsiblePanel } = usePlaygroundOptions().components ?? {};
270
- const t = useTranslations({ note: "playground" });
271
- const displayNames = {
272
- header: t("Header"),
273
- cookie: t("Cookies"),
274
- query: t("Query"),
275
- path: t("Path")
276
- };
277
- return ParamTypes.map((type) => {
278
- const items = parameters.filter((v) => v.in === type);
279
- if (items.length === 0) return;
280
- return /* @__PURE__ */ jsx(CollapsiblePanel, {
281
- "data-type": type,
282
- title: displayNames[type],
283
- children: /* @__PURE__ */ jsx(ParameterItem, {
284
- parameters: items,
285
- type
110
+ /* @__PURE__ */ jsx("div", {
111
+ className: "shrink-0 px-3 pb-3 sm:px-4",
112
+ children: /* @__PURE__ */ jsx(UrlBar, {
113
+ method,
114
+ route: path,
115
+ parameters: parameters.find((group) => group.in === "path")?.items ?? [],
116
+ deprecated: operation.deprecated,
117
+ loading: isSending
118
+ })
119
+ }),
120
+ /* @__PURE__ */ jsx(Segmented, {
121
+ value: view,
122
+ onValueChange: setView,
123
+ className: "shrink-0 px-3 pb-3 md:hidden",
124
+ children: /* @__PURE__ */ jsx(SegmentedList, {
125
+ className: "*:flex-1 *:justify-center",
126
+ items: [{
127
+ value: "request",
128
+ label: t("Request")
129
+ }, {
130
+ value: "response",
131
+ label: t("Response")
132
+ }]
133
+ })
134
+ }),
135
+ /* @__PURE__ */ jsxs("div", {
136
+ className: "mx-3 mb-3 grid min-h-0 flex-1 overflow-hidden rounded-xl border bg-fd-card sm:mx-4 sm:mb-4 md:grid-cols-[minmax(0,11fr)_minmax(0,9fr)]",
137
+ children: [/* @__PURE__ */ jsx(RequestPanel, {
138
+ playground,
139
+ renderParameterField,
140
+ renderBodyField,
141
+ className: cn(view !== "request" && "max-md:hidden")
142
+ }), /* @__PURE__ */ jsx(ResponsePanel, {
143
+ response,
144
+ loading: isSending,
145
+ onReset: clearResponse,
146
+ ResultDisplay,
147
+ className: cn("md:border-s", view !== "response" && "max-md:hidden")
148
+ })]
286
149
  })
287
- }, type);
288
- });
289
- }
290
- function BodyInput({ field: _field }) {
291
- const field = useResolvedSchema(_field);
292
- const [isJson, setIsJson] = useState(false);
293
- const t = useTranslations({ note: "playground" });
294
- if (field.format === "binary") return /* @__PURE__ */ jsx(FieldSet, {
295
- field,
296
- fieldName: ["body"],
297
- isRequired: true
298
- });
299
- if (isJson) return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("button", {
300
- className: cn(buttonVariants({
301
- variant: "secondary",
302
- size: "sm",
303
- className: "w-fit font-mono p-2"
304
- })),
305
- onClick: () => setIsJson(false),
306
- type: "button",
307
- children: t("Close JSON Editor")
308
- }), /* @__PURE__ */ jsx(JsonInput, { fieldName: ["body"] })] });
309
- return /* @__PURE__ */ jsx(FieldSet, {
310
- field,
311
- fieldName: ["body"],
312
- collapsible: false,
313
- isRequired: true,
314
- name: /* @__PURE__ */ jsx("button", {
315
- type: "button",
316
- className: cn(buttonVariants({
317
- variant: "secondary",
318
- size: "sm",
319
- className: "p-2"
320
- })),
321
- onClick: () => setIsJson(true),
322
- children: t("Open JSON Editor")
323
- })
324
- });
325
- }
326
- function AuthInput({ field }) {
327
- const t = useTranslations({ note: "playground" });
328
- const { fieldName, scheme } = field;
329
- if (scheme.type === "oauth2") return /* @__PURE__ */ jsx(OAuth2Input, { field });
330
- if (scheme.type === "http" && scheme.scheme === "basic") return /* @__PURE__ */ jsx(ObjectInput, {
331
- field: {
332
- type: "object",
333
- properties: {
334
- username: { type: "string" },
335
- password: { type: "string" }
336
- }
337
- },
338
- fieldName
339
- });
340
- if (scheme.type === "apiKey") return /* @__PURE__ */ jsx(FieldSet, {
341
- fieldName,
342
- name: `${scheme.name} (${scheme.in})`,
343
- field: { type: "string" }
150
+ ]
344
151
  });
345
- return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(FieldSet, {
346
- name: `${t("Authorization")} (${t("Header")})`,
347
- fieldName,
348
- field: { type: "string" }
349
- }), scheme.type !== "http" && /* @__PURE__ */ jsx("p", {
350
- className: "text-fd-muted-foreground text-xs",
351
- children: t("OpenID Connect is not supported at the moment, you can still set an access token here.")
352
- })] });
353
152
  }
354
- function OAuth2Input({ field }) {
355
- const { fieldName } = field;
356
- const [open, setOpen] = useState(false);
357
- const engine = useDataEngine();
153
+ function ExampleSelect() {
154
+ const { items, selected, select } = useExampleRequests();
358
155
  const t = useTranslations({ note: "playground" });
359
- return /* @__PURE__ */ jsxs("fieldset", {
360
- className: "flex flex-col gap-2",
361
- children: [/* @__PURE__ */ jsx(Label, {
362
- htmlFor: stringifyFieldKey(fieldName),
363
- children: t("Access Token")
364
- }), /* @__PURE__ */ jsxs("div", {
365
- className: "flex gap-2",
366
- children: [/* @__PURE__ */ jsx(FieldInput, {
367
- fieldName,
368
- field: { type: "string" },
369
- className: "flex-1"
370
- }), /* @__PURE__ */ jsxs(OAuthDialog, {
371
- open,
372
- onOpenChange: setOpen,
373
- children: [/* @__PURE__ */ jsx(OAuthDialogTrigger, {
374
- type: "button",
375
- className: cn(buttonVariants({
376
- size: "sm",
377
- variant: "secondary"
378
- })),
379
- children: t("Authorize")
380
- }), /* @__PURE__ */ jsx(OAuthDialogContent, {
381
- setOpen,
382
- schemeId: field.schemeId,
383
- scopes: field.scopes,
384
- setToken: (token) => engine.update(["header", "Authorization"], token)
385
- })]
386
- })]
156
+ if (items.length <= 1) return null;
157
+ const options = items.map((item) => ({
158
+ value: item.id,
159
+ label: item.name
160
+ }));
161
+ return /* @__PURE__ */ jsxs(Select, {
162
+ items: options,
163
+ value: selected ?? null,
164
+ onValueChange: (v) => v !== null && select(v),
165
+ children: [/* @__PURE__ */ jsxs(SelectTrigger, {
166
+ className: "h-8 w-auto max-w-64 gap-1.5 border-0 bg-transparent px-2 text-xs hover:bg-fd-accent focus:ring-0 focus-visible:ring-2",
167
+ children: [/* @__PURE__ */ jsx("span", {
168
+ className: "text-fd-muted-foreground",
169
+ children: t("Example")
170
+ }), /* @__PURE__ */ jsx(SelectValue, { className: "truncate font-medium" })]
171
+ }), /* @__PURE__ */ jsx(SelectContent, {
172
+ align: "end",
173
+ children: options.map((option) => /* @__PURE__ */ jsx(SelectItem, {
174
+ value: option.value,
175
+ children: option.label
176
+ }, option.value))
387
177
  })]
388
178
  });
389
179
  }
390
- function Route({ route, ...props }) {
391
- return /* @__PURE__ */ jsx("div", {
392
- ...props,
393
- className: cn("flex flex-row items-center gap-0.5 overflow-auto text-nowrap", props.className),
394
- children: route.split("/").map((part, index) => /* @__PURE__ */ jsxs(Fragment, { children: [index > 0 && /* @__PURE__ */ jsx("span", {
395
- className: "text-fd-muted-foreground",
396
- children: "/"
397
- }), part.startsWith("{") && part.endsWith("}") ? /* @__PURE__ */ jsx("code", {
398
- className: "bg-fd-primary/10 text-fd-primary",
399
- children: part
400
- }) : /* @__PURE__ */ jsx("code", {
401
- className: "text-fd-foreground",
402
- children: part
403
- })] }, index))
404
- });
405
- }
406
- function DefaultCollapsiblePanel({ title, children, ...props }) {
407
- return /* @__PURE__ */ jsxs(Collapsible, {
408
- ...props,
409
- className: cn("border-b last:border-b-0", props.className),
410
- children: [/* @__PURE__ */ jsxs(CollapsibleTrigger, {
411
- className: "group w-full flex items-center gap-2 p-3 text-sm font-medium",
412
- children: [title, /* @__PURE__ */ jsx(ChevronDown, { className: "ms-auto size-3.5 text-fd-muted-foreground group-data-[panel-open]:rotate-180" })]
413
- }), /* @__PURE__ */ jsx(CollapsibleContent, { children: /* @__PURE__ */ jsx("div", {
414
- className: "flex flex-col gap-3 p-3 pt-1",
415
- children
416
- }) })]
417
- });
418
- }
419
180
  const Custom = { useController(fieldName, options) {
420
181
  const [value, setValue] = useFieldValue(fieldName, options);
421
182
  return {
@@ -424,4 +185,4 @@ const Custom = { useController(fieldName, options) {
424
185
  };
425
186
  } };
426
187
  //#endregion
427
- export { Custom, DefaultCollapsiblePanel, DefaultResultDisplay, PlaygroundClient as default };
188
+ export { Custom, DefaultResultDisplay, PlaygroundClient as default };