fumadocs-openapi 12.5.1 → 12.5.3
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.
- package/css/generated/shared.css +1 -1
- package/dist/.translations/index.d.ts +0 -61
- package/dist/.translations/keys.js +1 -62
- package/dist/generate-file.js +19 -21
- package/dist/index.browser.d.ts +8 -3
- package/dist/index.browser.js +11 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -1
- package/dist/packages/shared-api/dist/.translations/index.d.ts +52 -4
- package/dist/packages/shared-api/dist/.translations/keys.js +53 -5
- package/dist/packages/shared-api/dist/codegen.d.ts +21 -1
- package/dist/packages/shared-api/dist/codegen.js +63 -1
- package/dist/packages/shared-api/dist/components/copy-typescript.js +31 -0
- package/dist/packages/shared-api/dist/components/defaults.js +13 -19
- package/dist/{ui → packages/shared-api/dist}/components/dialog.js +17 -3
- package/dist/packages/shared-api/dist/components/example.js +125 -0
- package/dist/{ui/playground/components → packages/shared-api/dist/components/playground}/fields.js +14 -11
- package/dist/packages/shared-api/dist/components/playground/json-editor.js +54 -0
- package/dist/packages/shared-api/dist/components/playground/panels.js +330 -0
- package/dist/packages/shared-api/dist/components/playground/param-input.js +54 -0
- package/dist/packages/shared-api/dist/components/playground/result.js +187 -0
- package/dist/packages/shared-api/dist/components/playground/schema.js +5 -3
- package/dist/packages/shared-api/dist/components/playground/server-select.js +130 -0
- package/dist/packages/shared-api/dist/components/schema/client.js +23 -1
- package/dist/{ui → packages/shared-api/dist}/components/segmented.js +2 -2
- package/dist/packages/shared-api/dist/components/select-tab.js +8 -3
- package/dist/packages/shared-api/dist/source.d.ts +11 -0
- package/dist/packages/shared-api/dist/source.js +50 -0
- package/dist/requests/generators/curl.js +6 -9
- package/dist/requests/generators/go.js +5 -5
- package/dist/requests/generators/rust.js +3 -2
- package/dist/requests/media/adapter.js +16 -7
- package/dist/requests/media/resolve-adapter.js +15 -12
- package/dist/requests/string-utils.js +7 -29
- package/dist/server/index.d.ts +2 -7
- package/dist/server/index.js +26 -64
- package/dist/ui/base.js +2 -2
- package/dist/ui/operation/index.js +7 -38
- package/dist/ui/operation/usage-tabs.js +49 -101
- package/dist/ui/playground/client.js +22 -51
- package/dist/ui/playground/components/{oauth-panel.js → auth.js} +20 -8
- package/dist/ui/playground/components/request-panel.js +14 -259
- package/dist/ui/playground/components/response-panel.js +11 -20
- package/dist/ui/playground/components/result-display.d.ts +0 -1
- package/dist/ui/playground/components/result-display.js +46 -182
- package/dist/ui/playground/components/server-select.js +24 -122
- package/dist/ui/playground/components/url-bar.js +8 -66
- package/dist/utils/create-page.d.ts +0 -4
- package/dist/utils/create-page.js +3 -5
- package/dist/utils/pages/builder.js +2 -2
- package/dist/utils/pages/preset-auto.js +139 -137
- package/dist/utils/pages/to-static-data.js +2 -7
- package/dist/utils/pages/to-text.js +2 -2
- package/package.json +6 -6
- package/dist/ui/playground/status-info.js +0 -39
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "../../utils/cn.js";
|
|
3
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../select.js";
|
|
4
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { StfProvider, useFieldValue, useListener, useStf } from "@fumari/stf";
|
|
7
|
+
import { useTranslations } from "@fuma-translate/react";
|
|
8
|
+
import { Check, ChevronDown } from "lucide-react";
|
|
9
|
+
import { Popover } from "@base-ui/react/popover";
|
|
10
|
+
//#region ../shared-api/dist/components/playground/server-select.js
|
|
11
|
+
/**
|
|
12
|
+
* Selects the server in a popover, `children` goes below its options, like `<ServerVariables />`.
|
|
13
|
+
*/
|
|
14
|
+
function ServerPopover({ label, title, options, value, onValueChange, className, children }) {
|
|
15
|
+
const [open, setOpen] = useState(false);
|
|
16
|
+
return /* @__PURE__ */ jsxs(Popover.Root, {
|
|
17
|
+
open,
|
|
18
|
+
onOpenChange: setOpen,
|
|
19
|
+
children: [/* @__PURE__ */ jsxs(Popover.Trigger, {
|
|
20
|
+
className: cn("group flex min-w-0 shrink items-center gap-1 rounded-lg 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 data-popup-open:bg-fd-accent data-popup-open:text-fd-accent-foreground", className),
|
|
21
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
22
|
+
className: "truncate",
|
|
23
|
+
children: label
|
|
24
|
+
}), /* @__PURE__ */ jsx(ChevronDown, { className: "size-3 shrink-0 transition-transform duration-200 group-data-popup-open:rotate-180" })]
|
|
25
|
+
}), /* @__PURE__ */ jsx(Popover.Portal, { children: /* @__PURE__ */ jsx(Popover.Positioner, {
|
|
26
|
+
side: "bottom",
|
|
27
|
+
align: "start",
|
|
28
|
+
sideOffset: 8,
|
|
29
|
+
className: "z-50",
|
|
30
|
+
children: /* @__PURE__ */ jsxs(Popover.Popup, {
|
|
31
|
+
className: "flex max-h-(--available-height) w-[min(26rem,calc(100vw-2rem))] origin-(--transform-origin) flex-col overflow-y-auto rounded-xl border bg-fd-popover p-1 text-sm text-fd-popover-foreground shadow-lg transition-[opacity,scale] duration-150 ease-out outline-none data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0",
|
|
32
|
+
children: [
|
|
33
|
+
/* @__PURE__ */ jsx("p", {
|
|
34
|
+
className: "px-2.5 pt-1.5 pb-1 text-xs font-medium text-fd-muted-foreground",
|
|
35
|
+
children: title
|
|
36
|
+
}),
|
|
37
|
+
options.map((option) => {
|
|
38
|
+
const selected = option.value === value;
|
|
39
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
40
|
+
type: "button",
|
|
41
|
+
"aria-pressed": selected,
|
|
42
|
+
onClick: () => {
|
|
43
|
+
if (!selected) onValueChange(option.value);
|
|
44
|
+
if (!option.hasVariables) setOpen(false);
|
|
45
|
+
},
|
|
46
|
+
className: cn("flex w-full items-start gap-2 rounded-lg px-2.5 py-2 text-start focus-visible:ring-2 focus-visible:ring-fd-ring focus-visible:outline-none focus-visible:ring-inset", selected ? "bg-fd-accent text-fd-accent-foreground" : "hover:bg-fd-accent/50 hover:text-fd-accent-foreground"),
|
|
47
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
48
|
+
className: "min-w-0 flex-1",
|
|
49
|
+
children: option.label
|
|
50
|
+
}), selected && /* @__PURE__ */ jsx(Check, { className: "mt-0.5 size-3.5 shrink-0 text-fd-primary" })]
|
|
51
|
+
}, option.value);
|
|
52
|
+
}),
|
|
53
|
+
children
|
|
54
|
+
]
|
|
55
|
+
})
|
|
56
|
+
}) })]
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Edits the variables of the selected server, they apply once typing stops. Key it by the server.
|
|
61
|
+
*/
|
|
62
|
+
function ServerVariables({ defaultValues, variables, onChange }) {
|
|
63
|
+
const t = useTranslations({ note: "playground server select" });
|
|
64
|
+
const stf = useStf({ defaultValues: () => structuredClone(defaultValues) });
|
|
65
|
+
const timerRef = useRef(void 0);
|
|
66
|
+
useListener({
|
|
67
|
+
stf,
|
|
68
|
+
onUpdate() {
|
|
69
|
+
window.clearTimeout(timerRef.current);
|
|
70
|
+
timerRef.current = window.setTimeout(() => onChange(stf.dataEngine.getData()), 300);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
useEffect(() => () => window.clearTimeout(timerRef.current), []);
|
|
74
|
+
return /* @__PURE__ */ jsx(StfProvider, {
|
|
75
|
+
value: stf,
|
|
76
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
77
|
+
className: "mt-1 flex flex-col gap-2.5 border-t px-2.5 pt-2.5 pb-2",
|
|
78
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
79
|
+
className: "text-xs font-medium text-fd-muted-foreground",
|
|
80
|
+
children: t("Variables")
|
|
81
|
+
}), Object.entries(variables).map(([key, variable]) => /* @__PURE__ */ jsxs("div", {
|
|
82
|
+
className: "grid grid-cols-[minmax(0,2fr)_minmax(0,3fr)] items-center gap-3",
|
|
83
|
+
children: [/* @__PURE__ */ jsxs("label", {
|
|
84
|
+
htmlFor: `server-${key}`,
|
|
85
|
+
className: "min-w-0",
|
|
86
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
87
|
+
className: "block truncate font-mono text-[0.8125rem]",
|
|
88
|
+
children: key
|
|
89
|
+
}), variable.description && /* @__PURE__ */ jsx("span", {
|
|
90
|
+
className: "block text-xs text-fd-muted-foreground",
|
|
91
|
+
children: variable.description
|
|
92
|
+
})]
|
|
93
|
+
}), /* @__PURE__ */ jsx(VariableInput, {
|
|
94
|
+
fieldName: key,
|
|
95
|
+
variable
|
|
96
|
+
})]
|
|
97
|
+
}, key))]
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function VariableInput({ fieldName, variable }) {
|
|
102
|
+
const t = useTranslations({ note: "playground server select" });
|
|
103
|
+
const id = `server-${fieldName}`;
|
|
104
|
+
const [value, setValue] = useFieldValue([fieldName], { compute(currentValue) {
|
|
105
|
+
return typeof currentValue === "string" ? currentValue : void 0;
|
|
106
|
+
} });
|
|
107
|
+
if (variable.enum) return /* @__PURE__ */ jsxs(Select, {
|
|
108
|
+
value: value ?? null,
|
|
109
|
+
onValueChange: (v) => v !== null && setValue(v),
|
|
110
|
+
children: [/* @__PURE__ */ jsx(SelectTrigger, {
|
|
111
|
+
id,
|
|
112
|
+
className: "h-8 py-1 font-mono text-[0.8125rem]",
|
|
113
|
+
children: /* @__PURE__ */ jsx(SelectValue, {})
|
|
114
|
+
}), /* @__PURE__ */ jsx(SelectContent, { children: variable.enum.map((item) => /* @__PURE__ */ jsx(SelectItem, {
|
|
115
|
+
value: item,
|
|
116
|
+
className: "font-mono text-[0.8125rem]",
|
|
117
|
+
children: item
|
|
118
|
+
}, item)) })]
|
|
119
|
+
});
|
|
120
|
+
const example = variable.examples?.[0];
|
|
121
|
+
return /* @__PURE__ */ jsx("input", {
|
|
122
|
+
id,
|
|
123
|
+
value: value ?? "",
|
|
124
|
+
onChange: (e) => setValue(e.target.value),
|
|
125
|
+
placeholder: example ?? t("Enter Value"),
|
|
126
|
+
className: cn("h-8 w-full rounded-md border bg-fd-secondary px-2 font-mono text-[0.8125rem] text-fd-secondary-foreground outline-none placeholder:text-fd-muted-foreground focus-visible:ring-2 focus-visible:ring-fd-ring", example === void 0 && "placeholder:font-sans")
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
export { ServerPopover, ServerVariables };
|
|
@@ -7,8 +7,8 @@ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
|
7
7
|
import { useTranslations } from "@fuma-translate/react";
|
|
8
8
|
import { CheckIcon, ChevronRightIcon, FilterIcon, FilterXIcon, LinkIcon } from "lucide-react";
|
|
9
9
|
import { buttonVariants } from "fumadocs-ui/components/ui/button";
|
|
10
|
-
import { flushSync } from "react-dom";
|
|
11
10
|
import { useCopyButton } from "fumadocs-ui/utils/use-copy-button";
|
|
11
|
+
import { flushSync } from "react-dom";
|
|
12
12
|
import { Menu } from "@base-ui/react/menu";
|
|
13
13
|
import { SchemaUIProvider, useSchemaUI } from "@fumadocs/json-schema/react/client";
|
|
14
14
|
//#region ../shared-api/dist/components/schema/client.js
|
|
@@ -433,6 +433,7 @@ function SchemaDescription({ schema: { description, infoTags = [] }, className }
|
|
|
433
433
|
className: "flex flex-wrap gap-1.5",
|
|
434
434
|
children: infoTags.map((tag, i) => {
|
|
435
435
|
if ("node" in tag) return /* @__PURE__ */ jsx(Fragment, { children: tag.node }, i);
|
|
436
|
+
if ("open" in tag) return /* @__PURE__ */ jsx(OpenTag, { ...tag }, i);
|
|
436
437
|
if ("list" in tag) return /* @__PURE__ */ jsxs("div", {
|
|
437
438
|
className: "not-prose flex w-full flex-wrap items-center gap-1.5 text-xs",
|
|
438
439
|
children: [tag.label, tag.list.map((item, j) => /* @__PURE__ */ jsx("code", {
|
|
@@ -461,5 +462,26 @@ function SchemaDescription({ schema: { description, infoTags = [] }, className }
|
|
|
461
462
|
})]
|
|
462
463
|
});
|
|
463
464
|
}
|
|
465
|
+
/** a tag opening a schema, like the type of a property */
|
|
466
|
+
function OpenTag({ label, value, open: { name, $ref } }) {
|
|
467
|
+
const { open, isExpanded } = use(TypeContext);
|
|
468
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
469
|
+
type: "button",
|
|
470
|
+
"aria-expanded": isExpanded?.(name, $ref),
|
|
471
|
+
onClick: () => open(name, $ref),
|
|
472
|
+
className: cn(tagClassName, "group/open inline-flex max-w-full items-center gap-1.5 text-start transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring aria-expanded:bg-fd-accent"),
|
|
473
|
+
children: [
|
|
474
|
+
/* @__PURE__ */ jsx("span", {
|
|
475
|
+
className: "not-prose shrink-0 text-fd-muted-foreground",
|
|
476
|
+
children: label
|
|
477
|
+
}),
|
|
478
|
+
value && /* @__PURE__ */ jsx("code", {
|
|
479
|
+
className: "not-prose min-w-0 truncate",
|
|
480
|
+
children: value
|
|
481
|
+
}),
|
|
482
|
+
/* @__PURE__ */ jsx(ChevronRightIcon, { className: "size-3 shrink-0 text-fd-muted-foreground transition-transform duration-200 group-hover/open:translate-x-0.5 motion-reduce:transition-none" })
|
|
483
|
+
]
|
|
484
|
+
});
|
|
485
|
+
}
|
|
464
486
|
//#endregion
|
|
465
487
|
export { SchemaUI };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { cn } from "
|
|
2
|
+
import { cn } from "../utils/cn.js";
|
|
3
3
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
import { Tabs } from "@base-ui/react/tabs";
|
|
5
|
-
//#region
|
|
5
|
+
//#region ../shared-api/dist/components/segmented.js
|
|
6
6
|
const Segmented = Tabs.Root;
|
|
7
7
|
const SegmentedPanel = Tabs.Panel;
|
|
8
8
|
/** a pill switch with a sliding indicator, its panels go in `<SegmentedPanel />` */
|
|
@@ -29,16 +29,21 @@ function SelectTab({ value, anchorSegments, ...props }) {
|
|
|
29
29
|
children: content
|
|
30
30
|
}) : content;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
/** selects the tab shown in a card, like a media type, a single item is a plain label */
|
|
33
|
+
function SelectTabTrigger({ items, className, ...props }) {
|
|
33
34
|
const { value, setValue } = use(Context);
|
|
35
|
+
if (items.length === 1) return /* @__PURE__ */ jsx("span", {
|
|
36
|
+
className: "min-w-0 px-1.5 font-medium",
|
|
37
|
+
children: items[0].label
|
|
38
|
+
});
|
|
34
39
|
return /* @__PURE__ */ jsxs(Select, {
|
|
35
40
|
items,
|
|
36
41
|
value,
|
|
37
42
|
onValueChange: setValue,
|
|
38
43
|
children: [/* @__PURE__ */ jsx(SelectTrigger, {
|
|
39
|
-
className: cn("not-prose w-
|
|
44
|
+
className: cn("not-prose h-7 w-auto min-w-0 gap-1 border-0 bg-transparent px-1.5 py-0 text-[0.8125rem] font-medium *:min-w-0 hover:bg-fd-accent hover:text-fd-accent-foreground focus:ring-0 focus-visible:ring-2 focus-visible:ring-inset data-popup-open:bg-fd-accent", className),
|
|
40
45
|
...props,
|
|
41
|
-
children: /* @__PURE__ */ jsx(SelectValue, {
|
|
46
|
+
children: /* @__PURE__ */ jsx(SelectValue, {})
|
|
42
47
|
}), /* @__PURE__ */ jsx(SelectContent, { children: items.map(({ label, value }) => /* @__PURE__ */ jsx(SelectItem, {
|
|
43
48
|
value,
|
|
44
49
|
children: label
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MetaData, PageData } from "fumadocs-core/source";
|
|
2
|
+
//#region ../shared-api/dist/source.d.ts
|
|
3
|
+
//#region src/source.d.ts
|
|
4
|
+
export interface SourceOptions {
|
|
5
|
+
baseDir?: string;
|
|
6
|
+
/** Generate `meta.json` files */
|
|
7
|
+
meta?: boolean | {
|
|
8
|
+
folderStyle?: 'folder' | 'separator';
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import { PathUtils } from "fumadocs-core/source";
|
|
3
|
+
//#region ../shared-api/dist/source.js
|
|
4
|
+
/** convert output entries into virtual files for `loader()` */
|
|
5
|
+
function toVirtualFiles(entries, { baseDir = "", meta = false }, toPage) {
|
|
6
|
+
const { folderStyle = "folder" } = typeof meta === "object" ? meta : {};
|
|
7
|
+
const files = [];
|
|
8
|
+
function scan(entries, parent) {
|
|
9
|
+
for (const entry of entries) {
|
|
10
|
+
if (entry.type === "group") {
|
|
11
|
+
scan(entry.entries, entry);
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
files.push({
|
|
15
|
+
type: "page",
|
|
16
|
+
path: path.join(baseDir, entry.path),
|
|
17
|
+
data: toPage(entry),
|
|
18
|
+
locale: "$"
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
if (!meta) return;
|
|
22
|
+
const pages = getMetaPages(entries, parent, folderStyle);
|
|
23
|
+
if (pages.length === 0) return;
|
|
24
|
+
files.push({
|
|
25
|
+
type: "meta",
|
|
26
|
+
path: path.join(baseDir, parent?.path ?? "", "meta.json"),
|
|
27
|
+
data: {
|
|
28
|
+
title: parent?.info.title,
|
|
29
|
+
description: parent?.info.description,
|
|
30
|
+
pages
|
|
31
|
+
},
|
|
32
|
+
locale: "$"
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
scan(entries);
|
|
36
|
+
return files;
|
|
37
|
+
}
|
|
38
|
+
/** the `pages` of `meta.json` in the folder of `parent`, for its entries */
|
|
39
|
+
function getMetaPages(entries, parent, folderStyle) {
|
|
40
|
+
const pages = [];
|
|
41
|
+
for (const entry of entries) {
|
|
42
|
+
const relativePath = PathUtils.slash(parent ? path.relative(parent.path, entry.path) : entry.path);
|
|
43
|
+
if (entry.type !== "group") pages.push(relativePath.slice(0, -path.extname(entry.path).length));
|
|
44
|
+
else if (folderStyle === "folder") pages.push(relativePath);
|
|
45
|
+
else pages.push(`---${entry.info.title}---`, `...${relativePath}`);
|
|
46
|
+
}
|
|
47
|
+
return pages;
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
export { getMetaPages, toVirtualFiles };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { shellQuote } from "../../packages/shared-api/dist/codegen.js";
|
|
1
2
|
import { inputToString } from "../string-utils.js";
|
|
2
|
-
import {
|
|
3
|
+
import { resolveMediaType } from "../media/resolve-adapter.js";
|
|
3
4
|
//#region src/requests/generators/curl.ts
|
|
4
5
|
const curl = {
|
|
5
6
|
label: "cURL",
|
|
@@ -9,20 +10,16 @@ const curl = {
|
|
|
9
10
|
s.push(`curl -X ${data.method.toUpperCase()} ${shellQuote(data.url)}`);
|
|
10
11
|
for (const header in data.header) s.push(`-H ${shellQuote(`${header}: ${data.header[header].value}`)}`);
|
|
11
12
|
for (const k in data.cookie) s.push(`--cookie ${shellQuote(`${k}=${data.cookie[k].value}`)}`);
|
|
12
|
-
|
|
13
|
+
const type = data.bodyMediaType && resolveMediaType(data.bodyMediaType, mediaAdapters);
|
|
14
|
+
if (data.body && type === "multipart/form-data") {
|
|
13
15
|
if (typeof data.body !== "object") throw new Error("[CURL] request body must be an object.");
|
|
14
16
|
for (const [key, value] of Object.entries(data.body)) s.push(`-F ${shellQuote(`${key}=${inputToString(value)}`)}`);
|
|
15
|
-
} else if (data.body &&
|
|
16
|
-
const escaped = shellQuote(inputToString(data.body, data.bodyMediaType));
|
|
17
|
+
} else if (data.body && type) {
|
|
17
18
|
s.push(`-H 'Content-Type: ${data.bodyMediaType}'`);
|
|
18
|
-
s.push(`-d ${
|
|
19
|
+
s.push(`-d ${shellQuote(inputToString(data.body, type))}`);
|
|
19
20
|
}
|
|
20
21
|
return s.join(" \\\n ");
|
|
21
22
|
}
|
|
22
23
|
};
|
|
23
|
-
/** single quotes keep the string literal, each `'` closes them to add an escaped quote */
|
|
24
|
-
function shellQuote(str) {
|
|
25
|
-
return `'${str.replaceAll("'", `'\\''`)}'`;
|
|
26
|
-
}
|
|
27
24
|
//#endregion
|
|
28
25
|
export { curl };
|
|
@@ -14,14 +14,13 @@ const go = {
|
|
|
14
14
|
lang: "go",
|
|
15
15
|
addImport: (name) => imports.add(name)
|
|
16
16
|
});
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
s.push(`req, _ := http.NewRequest("${data.method.toUpperCase()}", url, ${body ? "body" : "nil"})`);
|
|
17
|
+
const multipart = data.bodyMediaType === "multipart/form-data";
|
|
18
|
+
const s = [`req, _ := http.NewRequest("${data.method.toUpperCase()}", url, ${body ? "body" : "nil"})`];
|
|
20
19
|
for (const k in data.header) s.push(`req.Header.Add(${doubleQuote(k)}, ${doubleQuote(data.header[k].value)})`);
|
|
21
20
|
const cookie = cookieString(data.cookie);
|
|
22
21
|
if (cookie) s.push(`req.Header.Add("Cookie", ${doubleQuote(cookie)})`);
|
|
23
22
|
if (body) {
|
|
24
|
-
const type =
|
|
23
|
+
const type = multipart ? "mp.FormDataContentType()" : doubleQuote(data.bodyMediaType);
|
|
25
24
|
s.push(`req.Header.Add("Content-Type", ${type})`);
|
|
26
25
|
}
|
|
27
26
|
s.push("res, _ := http.DefaultClient.Do(req)", "defer res.Body.Close()", "resBody, _ := io.ReadAll(res.Body)", "", "fmt.Println(res)", "fmt.Println(string(resBody))");
|
|
@@ -32,7 +31,8 @@ ${indent(Array.from(imports, (name) => `"${name}"`).join("\n"))}
|
|
|
32
31
|
)
|
|
33
32
|
|
|
34
33
|
func main() {
|
|
35
|
-
${
|
|
34
|
+
url := ${doubleQuote(data.url)}
|
|
35
|
+
${body ? `${multipart ? indent(body) : ` ${body}`}\n` : ""}${indent(s.join("\n"))}
|
|
36
36
|
}`;
|
|
37
37
|
}
|
|
38
38
|
};
|
|
@@ -6,11 +6,12 @@ const rust = {
|
|
|
6
6
|
lang: "rust",
|
|
7
7
|
generate(data, { mediaAdapters }) {
|
|
8
8
|
const body = generateBodyExample(data, mediaAdapters, { lang: "rust" });
|
|
9
|
+
const multipart = data.bodyMediaType === "multipart/form-data";
|
|
9
10
|
const request = [`.request(Method::${data.method.toUpperCase()}, url)`];
|
|
10
11
|
for (const k in data.header) request.push(`.header(${doubleQuote(k)}, ${doubleQuote(data.header[k].value)})`);
|
|
11
12
|
const cookie = cookieString(data.cookie);
|
|
12
13
|
if (cookie) request.push(`.header("Cookie", ${doubleQuote(cookie)})`);
|
|
13
|
-
if (body &&
|
|
14
|
+
if (body && multipart) request.push(".multipart(body)");
|
|
14
15
|
else if (body) request.push(`.header("Content-Type", ${doubleQuote(data.bodyMediaType)})`, ".body(body)");
|
|
15
16
|
request.push(".send()", ".await?", ".text()", ".await?;");
|
|
16
17
|
return `use reqwest::{Client, Method, Result};
|
|
@@ -20,7 +21,7 @@ async fn main() -> Result<()> {
|
|
|
20
21
|
let client = Client::new();
|
|
21
22
|
|
|
22
23
|
let url = ${doubleQuote(data.url)};
|
|
23
|
-
${body ? `${indent(body)}\n` : ""}
|
|
24
|
+
${body ? `${multipart ? indent(body) : ` ${body}`}\n` : ""}
|
|
24
25
|
let res = client
|
|
25
26
|
${indent(request.join("\n"), 2)}
|
|
26
27
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { __toESM } from "../../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { csharpRawString, goString, javaTextBlock, jsTemplateLiteral, pythonString, rustString } from "../../packages/shared-api/dist/codegen.js";
|
|
2
3
|
import { require_js2xml } from "../../node_modules/.pnpm/xml-js@1.6.11/node_modules/xml-js/lib/js2xml.js";
|
|
3
|
-
import {
|
|
4
|
+
import { inputToString } from "../string-utils.js";
|
|
4
5
|
import { resolveMediaAdapter } from "./resolve-adapter.js";
|
|
5
6
|
//#region src/requests/media/adapter.ts
|
|
6
7
|
var import_js2xml = /* @__PURE__ */ __toESM(require_js2xml(), 1);
|
|
@@ -33,6 +34,14 @@ const defaultAdapters = {
|
|
|
33
34
|
return str(data.body, "application/x-ndjson", ctx);
|
|
34
35
|
}
|
|
35
36
|
},
|
|
37
|
+
"text/plain": {
|
|
38
|
+
encode(data) {
|
|
39
|
+
return inputToString(data.body, "text/plain");
|
|
40
|
+
},
|
|
41
|
+
generateExample(data, ctx) {
|
|
42
|
+
return str(data.body, "text/plain", ctx);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
36
45
|
"application/x-www-form-urlencoded": {
|
|
37
46
|
encode(data) {
|
|
38
47
|
if (typeof data.body !== "object") throw new Error(`Input value must be object, received: ${typeof data.body}`);
|
|
@@ -120,16 +129,16 @@ function str(init, mediaType, ctx) {
|
|
|
120
129
|
if (ctx.lang === "js" && mediaType === "application/json") return `const body = JSON.stringify(${JSON.stringify(init, null, 2)})`;
|
|
121
130
|
const text = inputToString(init, mediaType);
|
|
122
131
|
switch (ctx.lang) {
|
|
123
|
-
case "js": return `const body = ${
|
|
124
|
-
case "python": return `body = ${
|
|
132
|
+
case "js": return `const body = ${jsTemplateLiteral(text)}`;
|
|
133
|
+
case "python": return `body = ${pythonString(text)}`;
|
|
125
134
|
case "go":
|
|
126
135
|
ctx.addImport("strings");
|
|
127
|
-
return `body := strings.NewReader(${
|
|
136
|
+
return `body := strings.NewReader(${goString(text)})`;
|
|
128
137
|
case "java":
|
|
129
138
|
ctx.addImport("java.net.http.HttpRequest.BodyPublishers");
|
|
130
|
-
return `var body = BodyPublishers.ofString(${
|
|
131
|
-
case "csharp": return `var body = new StringContent(
|
|
132
|
-
case "rust": return `let body = ${
|
|
139
|
+
return `var body = BodyPublishers.ofString(${javaTextBlock(text)});`;
|
|
140
|
+
case "csharp": return `var body = new StringContent(${csharpRawString(text)}, Encoding.UTF8, "${mediaType}");`;
|
|
141
|
+
case "rust": return `let body = ${rustString(text)};`;
|
|
133
142
|
}
|
|
134
143
|
}
|
|
135
144
|
//#endregion
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
//#region src/requests/media/resolve-adapter.ts
|
|
2
2
|
const VariantMediaTypeRegex = /^(?<dir>[a-zA-Z0-9._-]+)\/(?<content>[a-zA-Z0-9._-]+)\+(?<variant>[a-zA-Z0-9._-]+)$/;
|
|
3
3
|
/**
|
|
4
|
+
* Resolve the media type of the adapter for a given media type, like `application/json` for `+json` variants.
|
|
5
|
+
*/
|
|
6
|
+
function resolveMediaType(mediaType, adapters) {
|
|
7
|
+
const normalized = mediaType.split(";", 2)[0].trim().toLowerCase();
|
|
8
|
+
if (normalized in adapters) return normalized;
|
|
9
|
+
const match = VariantMediaTypeRegex.exec(normalized);
|
|
10
|
+
if (match?.groups) {
|
|
11
|
+
const baseType = `${match.groups.dir}/${match.groups.variant}`;
|
|
12
|
+
if (baseType in adapters) return baseType;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
4
16
|
* Resolve a media adapter for a given media type.
|
|
5
17
|
* Supports exact matches and pattern matching (e.g., +json suffix).
|
|
6
18
|
*
|
|
@@ -9,17 +21,8 @@ const VariantMediaTypeRegex = /^(?<dir>[a-zA-Z0-9._-]+)\/(?<content>[a-zA-Z0-9._
|
|
|
9
21
|
* @returns The resolved adapter or undefined if not found
|
|
10
22
|
*/
|
|
11
23
|
function resolveMediaAdapter(mediaType, adapters) {
|
|
12
|
-
const
|
|
13
|
-
if (
|
|
14
|
-
const match = VariantMediaTypeRegex.exec(normalized);
|
|
15
|
-
if (match?.groups) {
|
|
16
|
-
const baseType = `${match.groups.dir}/${match.groups.variant}`;
|
|
17
|
-
if (baseType in adapters) return adapters[baseType];
|
|
18
|
-
}
|
|
19
|
-
if (normalized === "text/plain") {
|
|
20
|
-
console.warn("there is no defined behaviour for encoding form values into \"text/plain\", using JSON encoder for now.");
|
|
21
|
-
return adapters["application/json"];
|
|
22
|
-
}
|
|
24
|
+
const type = resolveMediaType(mediaType, adapters);
|
|
25
|
+
if (type) return adapters[type];
|
|
23
26
|
}
|
|
24
27
|
//#endregion
|
|
25
|
-
export { resolveMediaAdapter };
|
|
28
|
+
export { resolveMediaAdapter, resolveMediaType };
|
|
@@ -3,11 +3,10 @@ import { require_js2xml } from "../node_modules/.pnpm/xml-js@1.6.11/node_modules
|
|
|
3
3
|
//#region src/requests/string-utils.ts
|
|
4
4
|
var import_js2xml = /* @__PURE__ */ __toESM(require_js2xml(), 1);
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* The text of a body value in the format of a media type, strings are already serialized.
|
|
7
7
|
*/
|
|
8
8
|
function inputToString(value, format = "application/json") {
|
|
9
9
|
if (typeof value === "string") return value;
|
|
10
|
-
if (format === "application/json") return JSON.stringify(value, null, 2);
|
|
11
10
|
if (format === "application/x-ndjson") return Array.isArray(value) ? value.map((v) => JSON.stringify(v)).join("\n") : JSON.stringify(value, null, 2);
|
|
12
11
|
if (format === "application/x-www-form-urlencoded") {
|
|
13
12
|
const params = new URLSearchParams();
|
|
@@ -15,10 +14,11 @@ function inputToString(value, format = "application/json") {
|
|
|
15
14
|
for (const key in value) if (value[key]) params.set(key, String(value[key]));
|
|
16
15
|
return params.toString();
|
|
17
16
|
}
|
|
18
|
-
return (0, import_js2xml.default)(value, {
|
|
17
|
+
if (format === "application/xml") return (0, import_js2xml.default)(value, {
|
|
19
18
|
compact: true,
|
|
20
19
|
spaces: 2
|
|
21
20
|
});
|
|
21
|
+
return JSON.stringify(value, null, 2);
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Returns the value of a `Cookie` header with the given cookies.
|
|
@@ -35,36 +35,14 @@ function doubleQuote(str) {
|
|
|
35
35
|
return `"${str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
* escaping embedded triple quotes and backslashes.
|
|
38
|
+
* A JSX attribute value, strings don't process escapes, an expression holds the ones with quotes.
|
|
40
39
|
*/
|
|
41
|
-
function
|
|
42
|
-
return
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Returns the input string wrapped in a JavaScript template literal, escaping internal backticks, backslashes and `${`.
|
|
46
|
-
*/
|
|
47
|
-
function backtickQuote(str) {
|
|
48
|
-
return `\`${str.replace(/[\\`]|\$\{/g, "\\$&")}\``;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Returns the input string wrapped inside a Go raw string literal, where backticks can't be escaped and are concatenated instead.
|
|
52
|
-
*/
|
|
53
|
-
function goRawStringLiteral(str) {
|
|
54
|
-
return `\`${str.replaceAll("`", "` + \"`\" + `")}\``;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Returns the input string wrapped inside a Rust string literal,
|
|
58
|
-
* with increasing number of # to escape `"#`
|
|
59
|
-
*/
|
|
60
|
-
function rustRawStringLiteral(str) {
|
|
61
|
-
let hashes = "#";
|
|
62
|
-
while (str.includes(`"${hashes}`)) hashes += "#";
|
|
63
|
-
return `r${hashes}"${str}"${hashes}`;
|
|
40
|
+
function jsxString(str) {
|
|
41
|
+
return str.includes("\"") ? `{${JSON.stringify(str)}}` : `"${str}"`;
|
|
64
42
|
}
|
|
65
43
|
function indent(code, tab = 1) {
|
|
66
44
|
const p = " ".repeat(tab);
|
|
67
45
|
return code.split("\n").map((v) => v.length === 0 ? v : p + v).join("\n");
|
|
68
46
|
}
|
|
69
47
|
//#endregion
|
|
70
|
-
export {
|
|
48
|
+
export { cookieString, doubleQuote, indent, inputToString, jsxString };
|
package/dist/server/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { SchemaToPagesOptions } from "../utils/pages/preset-auto.js";
|
|
|
4
4
|
import { CreateProxyOptions, Proxy, createProxy } from "./proxy.js";
|
|
5
5
|
import { createOAuthHandler } from "./oauth.js";
|
|
6
6
|
import { InternalOpenAPIMeta } from "../utils/pages/to-static-data.js";
|
|
7
|
+
import { SourceOptions } from "../packages/shared-api/dist/source.js";
|
|
7
8
|
import "../ui/index.js";
|
|
8
9
|
import { DynamicSource, LoaderPlugin, MetaData, Page, PageData, Source } from "fumadocs-core/source";
|
|
9
10
|
import { TOCItemType } from "fumadocs-core/toc";
|
|
@@ -66,13 +67,7 @@ export interface OpenAPIPageData extends PageData {
|
|
|
66
67
|
toc: TOCItemType[];
|
|
67
68
|
_openapi: InternalOpenAPIMeta;
|
|
68
69
|
}
|
|
69
|
-
export type OpenAPISourceOptions = SchemaToPagesOptions &
|
|
70
|
-
baseDir?: string;
|
|
71
|
-
/** Generate `meta.json` files */
|
|
72
|
-
meta?: boolean | {
|
|
73
|
-
folderStyle?: 'folder' | 'separator';
|
|
74
|
-
};
|
|
75
|
-
};
|
|
70
|
+
export type OpenAPISourceOptions = SchemaToPagesOptions & SourceOptions;
|
|
76
71
|
export declare function createOpenAPI(options?: OpenAPIOptions): OpenAPIServer;
|
|
77
72
|
/**
|
|
78
73
|
* Fumadocs Source API integration, pass this to `plugins` array in `loader()`.
|