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.
- package/css/generated/shared.css +1 -1
- package/dist/.translations/index.d.ts +63 -20
- package/dist/.translations/keys.js +63 -20
- package/dist/operation.d.ts +6 -1
- package/dist/operation.js +2 -4
- package/dist/packages/shared-api/dist/.translations/index.d.ts +1 -0
- package/dist/packages/shared-api/dist/.translations/keys.js +1 -0
- package/dist/packages/shared-api/dist/auto-anchor/index.js +54 -10
- package/dist/packages/shared-api/dist/components/accordion.js +3 -11
- package/dist/packages/shared-api/dist/components/collapsible.js +2 -2
- package/dist/packages/shared-api/dist/components/schema/client.d.ts +5 -0
- package/dist/packages/shared-api/dist/components/schema/client.js +408 -455
- package/dist/packages/shared-api/dist/components/select-tab.js +7 -12
- package/dist/packages/shared-api/dist/components/select.js +8 -7
- package/dist/playground/auth.d.ts +5 -1
- package/dist/playground/auth.js +7 -2
- package/dist/playground/fetcher.d.ts +8 -0
- package/dist/playground/fetcher.js +30 -1
- package/dist/playground/index.d.ts +2 -1
- package/dist/playground/index.js +2 -1
- package/dist/playground/use-playground.d.ts +62 -0
- package/dist/playground/use-playground.js +140 -0
- package/dist/ui/components/dialog.js +14 -0
- package/dist/ui/components/endpoint.js +40 -0
- package/dist/ui/components/heading.js +1 -1
- package/dist/ui/components/schema.js +1 -1
- package/dist/ui/components/segmented.js +21 -0
- package/dist/ui/operation/index.js +267 -250
- package/dist/ui/playground/client.d.ts +9 -26
- package/dist/ui/playground/client.js +150 -389
- package/dist/ui/playground/components/fields.js +693 -0
- package/dist/ui/playground/components/oauth-panel.js +182 -0
- package/dist/ui/playground/components/request-panel.js +480 -0
- package/dist/ui/playground/components/response-panel.js +150 -0
- package/dist/ui/playground/components/result-display.d.ts +1 -0
- package/dist/ui/playground/components/result-display.js +185 -168
- package/dist/ui/playground/components/server-select.js +100 -89
- package/dist/ui/playground/components/url-bar.js +96 -0
- package/dist/ui/playground/status-info.js +32 -45
- package/dist/utils/document/load.js +1 -1
- package/package.json +12 -12
- package/dist/packages/shared-api/dist/auto-anchor/client.js +0 -20
- package/dist/packages/shared-api/dist/components/collapsible.d.ts +0 -7
- package/dist/packages/shared-api/dist/components/dialog.js +0 -55
- package/dist/packages/shared-api/dist/components/input.js +0 -14
- package/dist/packages/shared-api/dist/components/label.js +0 -11
- package/dist/packages/shared-api/dist/components/playground/inputs.js +0 -439
- package/dist/packages/shared-api/dist/components/popover.js +0 -23
- package/dist/packages/shared-api/dist/utils/merge-refs.js +0 -11
- package/dist/ui/playground/components/oauth-dialog.js +0 -259
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { cn } from "../utils/cn.js";
|
|
3
3
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { createContext, use, useEffect, useMemo, useState } from "react";
|
|
4
|
+
import { AnchorSection, useAnchorLink } from "../auto-anchor/index.js";
|
|
5
|
+
import { createContext, use, useMemo, useState } from "react";
|
|
7
6
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
7
|
//#region ../shared-api/dist/components/select-tab.js
|
|
9
8
|
const Context = createContext(null);
|
|
@@ -19,16 +18,12 @@ function SelectTabs({ defaultValue, children }) {
|
|
|
19
18
|
}
|
|
20
19
|
function SelectTab({ value, anchorSegments, ...props }) {
|
|
21
20
|
const { value: currentValue, setValue } = use(Context);
|
|
22
|
-
const
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
if (id && anchorIdStartsWith(window.location.hash.slice(1), id)) setValue(value);
|
|
25
|
-
}, [
|
|
26
|
-
id,
|
|
27
|
-
value,
|
|
28
|
-
setValue
|
|
29
|
-
]);
|
|
21
|
+
const ref = useAnchorLink(anchorSegments ?? false, () => setValue(value));
|
|
30
22
|
if (value !== currentValue) return;
|
|
31
|
-
const content = /* @__PURE__ */ jsx("div", {
|
|
23
|
+
const content = /* @__PURE__ */ jsx("div", {
|
|
24
|
+
ref,
|
|
25
|
+
...props
|
|
26
|
+
});
|
|
32
27
|
return anchorSegments ? /* @__PURE__ */ jsx(AnchorSection, {
|
|
33
28
|
segments: anchorSegments,
|
|
34
29
|
children: content
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { cn } from "../utils/cn.js";
|
|
2
2
|
import "react";
|
|
3
3
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
-
import { Check, ChevronDown
|
|
4
|
+
import { Check, ChevronDown } from "lucide-react";
|
|
5
5
|
import { Select } from "@base-ui/react/select";
|
|
6
6
|
//#region ../shared-api/dist/components/select.js
|
|
7
7
|
const Select$1 = Select.Root;
|
|
@@ -18,20 +18,21 @@ function SelectTrigger({ className, children, ref, ...props }) {
|
|
|
18
18
|
})]
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
const scrollArrowClassName = "inset-x-0 z-10 h-6 from-fd-popover to-transparent";
|
|
21
22
|
function SelectScrollUpButton({ className, ref, ...props }) {
|
|
22
23
|
return /* @__PURE__ */ jsx(Select.ScrollUpArrow, {
|
|
23
24
|
ref,
|
|
24
|
-
className: cn("
|
|
25
|
+
className: cn(scrollArrowClassName, "top-0 bg-linear-to-b", className),
|
|
25
26
|
...props,
|
|
26
|
-
children:
|
|
27
|
+
children: null
|
|
27
28
|
});
|
|
28
29
|
}
|
|
29
30
|
function SelectScrollDownButton({ className, ref, ...props }) {
|
|
30
31
|
return /* @__PURE__ */ jsx(Select.ScrollDownArrow, {
|
|
31
32
|
ref,
|
|
32
|
-
className: cn("
|
|
33
|
+
className: cn(scrollArrowClassName, "bottom-0 bg-linear-to-t", className),
|
|
33
34
|
...props,
|
|
34
|
-
children:
|
|
35
|
+
children: null
|
|
35
36
|
});
|
|
36
37
|
}
|
|
37
38
|
function SelectContent({ className, children, align = "start", side = "bottom", sideOffset = 4, ref, ...props }) {
|
|
@@ -42,7 +43,7 @@ function SelectContent({ className, children, align = "start", side = "bottom",
|
|
|
42
43
|
className: "z-50",
|
|
43
44
|
children: /* @__PURE__ */ jsxs(Select.Popup, {
|
|
44
45
|
ref,
|
|
45
|
-
className: cn("z-50 min-w-[calc(var(--anchor-width)+--spacing(2))] overflow-hidden rounded-lg border bg-fd-popover text-fd-popover-foreground shadow-md", className),
|
|
46
|
+
className: cn("relative z-50 min-w-[calc(var(--anchor-width)+--spacing(2))] overflow-hidden rounded-lg border bg-fd-popover text-fd-popover-foreground shadow-md", className),
|
|
46
47
|
...props,
|
|
47
48
|
children: [
|
|
48
49
|
/* @__PURE__ */ jsx(SelectScrollUpButton, {}),
|
|
@@ -58,7 +59,7 @@ function SelectContent({ className, children, align = "start", side = "bottom",
|
|
|
58
59
|
function SelectItem({ className, children, ref, ...props }) {
|
|
59
60
|
return /* @__PURE__ */ jsxs(Select.Item, {
|
|
60
61
|
ref,
|
|
61
|
-
className: cn("flex select-none flex-row items-center rounded-md py-1.5 px-2 text-sm outline-none data-[highlighted]:bg-fd-accent data-[highlighted]:text-fd-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50", className),
|
|
62
|
+
className: cn("flex select-none flex-row items-center gap-2 rounded-md py-1.5 px-2 text-sm outline-none data-[highlighted]:bg-fd-accent data-[highlighted]:text-fd-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50", className),
|
|
62
63
|
...props,
|
|
63
64
|
children: [/* @__PURE__ */ jsx(Select.ItemText, { children }), /* @__PURE__ */ jsx(Select.ItemIndicator, {
|
|
64
65
|
className: "ms-auto shrink-0",
|
|
@@ -20,6 +20,8 @@ export interface OAuthFlowInput {
|
|
|
20
20
|
serverUrl: string;
|
|
21
21
|
/** URL of the `createOAuthHandler()` route, defaults to the page */
|
|
22
22
|
redirectUrl?: string;
|
|
23
|
+
/** where the flow started, like an operation, restored when it returns to the page */
|
|
24
|
+
origin?: string;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* Run an OAuth flow and resolve its token.
|
|
@@ -27,7 +29,7 @@ export interface OAuthFlowInput {
|
|
|
27
29
|
* `implicit` and `authorizationCode` leave the page instead: the token is picked up on return,
|
|
28
30
|
* in `usePlaygroundAuth().store`.
|
|
29
31
|
*/
|
|
30
|
-
export declare function requestOAuthToken(scheme: OAuth2SecurityScheme, type: OAuthFlowType, { schemeId, scopes, clientId, clientSecret, username, password, clientAuth, serverUrl, redirectUrl }: OAuthFlowInput): Promise<string | undefined>;
|
|
32
|
+
export declare function requestOAuthToken(scheme: OAuth2SecurityScheme, type: OAuthFlowType, { schemeId, scopes, clientId, clientSecret, username, password, clientAuth, serverUrl, redirectUrl, origin }: OAuthFlowInput): Promise<string | undefined>;
|
|
31
33
|
/** scheme name -> token info */
|
|
32
34
|
type TokenStore = Record<string, TokenInfo | undefined>;
|
|
33
35
|
type TokenInfo = {
|
|
@@ -46,6 +48,8 @@ interface AuthContextType {
|
|
|
46
48
|
store: TokenStore;
|
|
47
49
|
isLoading: boolean;
|
|
48
50
|
error?: unknown;
|
|
51
|
+
/** `origin` of the flow that returned to the page */
|
|
52
|
+
origin?: string;
|
|
49
53
|
/** listen to the tokens of flows returning to the page, returns a cleanup */
|
|
50
54
|
subscribe: (listener: TokenListener) => () => void;
|
|
51
55
|
}
|
package/dist/playground/auth.js
CHANGED
|
@@ -13,7 +13,7 @@ const pendingFlowKey = "fumadocs-openapi-oauth";
|
|
|
13
13
|
* `implicit` and `authorizationCode` leave the page instead: the token is picked up on return,
|
|
14
14
|
* in `usePlaygroundAuth().store`.
|
|
15
15
|
*/
|
|
16
|
-
async function requestOAuthToken(scheme, type, { schemeId, scopes, clientId, clientSecret, username, password, clientAuth, serverUrl, redirectUrl }) {
|
|
16
|
+
async function requestOAuthToken(scheme, type, { schemeId, scopes, clientId, clientSecret, username, password, clientAuth, serverUrl, redirectUrl, origin }) {
|
|
17
17
|
const flows = scheme.flows ?? {};
|
|
18
18
|
const scope = scopes.join(" ");
|
|
19
19
|
if (type === "implicit" || type === "authorizationCode") {
|
|
@@ -26,7 +26,8 @@ async function requestOAuthToken(scheme, type, { schemeId, scopes, clientId, cli
|
|
|
26
26
|
client_secret: clientSecret,
|
|
27
27
|
client_auth: clientAuth,
|
|
28
28
|
redirect_uri: new URL(redirectUrl ?? window.location.pathname, window.location.origin).href,
|
|
29
|
-
token_url: "tokenUrl" in flow ? new URL(flow.tokenUrl, serverUrl).href : void 0
|
|
29
|
+
token_url: "tokenUrl" in flow ? new URL(flow.tokenUrl, serverUrl).href : void 0,
|
|
30
|
+
origin
|
|
30
31
|
};
|
|
31
32
|
sessionStorage.setItem(pendingFlowKey, JSON.stringify(pending));
|
|
32
33
|
if (redirectUrl) document.cookie = `fumadocs-openapi-oauth=${encodeURIComponent(window.location.pathname)}; path=/`;
|
|
@@ -78,6 +79,7 @@ function usePlaygroundAuth() {
|
|
|
78
79
|
}
|
|
79
80
|
function AuthProvider({ children }) {
|
|
80
81
|
const [store, setStore] = useState({});
|
|
82
|
+
const [origin, setOrigin] = useState();
|
|
81
83
|
const listeners = useMemo(() => /* @__PURE__ */ new Set(), []);
|
|
82
84
|
const subscribe = useCallback((listener) => {
|
|
83
85
|
listeners.add(listener);
|
|
@@ -121,6 +123,7 @@ function AuthProvider({ children }) {
|
|
|
121
123
|
token: `${hash.get("token_type") ?? "Bearer"} ${token}`
|
|
122
124
|
});
|
|
123
125
|
else return;
|
|
126
|
+
setOrigin(flow.origin);
|
|
124
127
|
sessionStorage.removeItem(pendingFlowKey);
|
|
125
128
|
window.history.replaceState(null, "", window.location.pathname);
|
|
126
129
|
}, []);
|
|
@@ -129,11 +132,13 @@ function AuthProvider({ children }) {
|
|
|
129
132
|
store,
|
|
130
133
|
isLoading: authCodeQuery.isLoading,
|
|
131
134
|
error: authCodeQuery.error,
|
|
135
|
+
origin,
|
|
132
136
|
subscribe
|
|
133
137
|
}), [
|
|
134
138
|
store,
|
|
135
139
|
authCodeQuery.isLoading,
|
|
136
140
|
authCodeQuery.error,
|
|
141
|
+
origin,
|
|
137
142
|
subscribe
|
|
138
143
|
]),
|
|
139
144
|
children
|
|
@@ -14,6 +14,14 @@ export interface FetchResponseResult {
|
|
|
14
14
|
status: number;
|
|
15
15
|
headers: Headers;
|
|
16
16
|
body: ArrayBuffer;
|
|
17
|
+
/** how long the request took, in milliseconds */
|
|
18
|
+
duration: number;
|
|
19
|
+
/** the media type of `Content-Type` like `application/json` */
|
|
20
|
+
mediaType?: string;
|
|
21
|
+
/** the file name from `Content-Disposition`, without directories */
|
|
22
|
+
fileName?: string;
|
|
23
|
+
/** decode the body with the charset of `Content-Type`, UTF-8 by default */
|
|
24
|
+
text: () => string;
|
|
17
25
|
}
|
|
18
26
|
export interface Fetcher {
|
|
19
27
|
/**
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { resolveMediaAdapter } from "../requests/media/resolve-adapter.js";
|
|
2
|
+
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";
|
|
2
3
|
//#region src/playground/fetcher.ts
|
|
4
|
+
var import_fast_content_type_parse = require_fast_content_type_parse();
|
|
3
5
|
function createBrowserFetcher(adapters, { proxyUrl, proxyForwardCookie = true, requestTimeout = 10, onRequestInit } = {}) {
|
|
4
6
|
return { async fetch(url, data) {
|
|
5
7
|
let requestUrl = new URL(url, document.baseURI);
|
|
@@ -39,13 +41,25 @@ function createBrowserFetcher(adapters, { proxyUrl, proxyForwardCookie = true, r
|
|
|
39
41
|
document.cookie = segs.join("; ");
|
|
40
42
|
}
|
|
41
43
|
if (onRequestInit) requestInit = await onRequestInit(requestInit);
|
|
44
|
+
const start = performance.now();
|
|
42
45
|
return fetch(requestUrl, requestInit).then(async (res) => {
|
|
46
|
+
const body = await res.arrayBuffer();
|
|
47
|
+
const { type, parameters } = (0, import_fast_content_type_parse.safeParse)(res.headers.get("Content-Type") ?? "");
|
|
43
48
|
return {
|
|
44
49
|
type: "response",
|
|
45
50
|
url: res.url,
|
|
46
51
|
status: res.status,
|
|
47
52
|
headers: res.headers,
|
|
48
|
-
body
|
|
53
|
+
body,
|
|
54
|
+
duration: performance.now() - start,
|
|
55
|
+
mediaType: type || void 0,
|
|
56
|
+
fileName: getFileName(res.headers),
|
|
57
|
+
text() {
|
|
58
|
+
try {
|
|
59
|
+
if (parameters.charset) return new TextDecoder(parameters.charset).decode(body);
|
|
60
|
+
} catch {}
|
|
61
|
+
return new TextDecoder().decode(body);
|
|
62
|
+
}
|
|
49
63
|
};
|
|
50
64
|
}).catch((e) => {
|
|
51
65
|
return {
|
|
@@ -56,5 +70,20 @@ function createBrowserFetcher(adapters, { proxyUrl, proxyForwardCookie = true, r
|
|
|
56
70
|
});
|
|
57
71
|
} };
|
|
58
72
|
}
|
|
73
|
+
/** prefers `filename*` (RFC 6266) */
|
|
74
|
+
function getFileName(headers) {
|
|
75
|
+
const disposition = headers.get("Content-Disposition");
|
|
76
|
+
if (!disposition) return;
|
|
77
|
+
const extended = /(?:^|;)\s*filename\*\s*=\s*[^']*'[^']*'([^\s;"]+)/i.exec(disposition);
|
|
78
|
+
let name;
|
|
79
|
+
if (extended) try {
|
|
80
|
+
name = decodeURIComponent(extended[1]);
|
|
81
|
+
} catch {}
|
|
82
|
+
if (!name) {
|
|
83
|
+
const match = /(?:^|;)\s*filename\s*=\s*(?:"((?:\\.|[^"\\])*)"|([^\s;]+))/i.exec(disposition);
|
|
84
|
+
name = match?.[1]?.replace(/\\(.)/g, "$1") ?? match?.[2];
|
|
85
|
+
}
|
|
86
|
+
return name?.replace(/^.*[/\\]/, "") || void 0;
|
|
87
|
+
}
|
|
59
88
|
//#endregion
|
|
60
89
|
export { createBrowserFetcher };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { BrowserFetcherOptions, FetchErrorResult, FetchResponseResult, FetchResult, Fetcher, createBrowserFetcher } from "./fetcher.js";
|
|
2
2
|
import { AuthField, AuthRequirement, OAuthFlowType, requestOAuthToken, useAuthFields, usePlaygroundAuth } from "./auth.js";
|
|
3
|
-
|
|
3
|
+
import { OAuthInput, Playground, PlaygroundAuth, PlaygroundOptions, PlaygroundResponse, RequestBodyInfo, usePlayground } from "./use-playground.js";
|
|
4
|
+
export { type AuthField, type AuthRequirement, type BrowserFetcherOptions, type FetchErrorResult, type FetchResponseResult, type FetchResult, type Fetcher, type OAuthFlowType, type OAuthInput, type Playground, type PlaygroundAuth, type PlaygroundOptions, type PlaygroundResponse, type RequestBodyInfo, createBrowserFetcher, requestOAuthToken, useAuthFields, usePlayground, usePlaygroundAuth };
|
package/dist/playground/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { requestOAuthToken, useAuthFields, usePlaygroundAuth } from "./auth.js";
|
|
2
2
|
import { createBrowserFetcher } from "./fetcher.js";
|
|
3
|
-
|
|
3
|
+
import { usePlayground } from "./use-playground.js";
|
|
4
|
+
export { createBrowserFetcher, requestOAuthToken, useAuthFields, usePlayground, usePlaygroundAuth };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { BrowserFetcherOptions, FetchResult } from "./fetcher.js";
|
|
2
|
+
import { AuthField, AuthRequirement, OAuthFlowInput, OAuthFlowType } from "./auth.js";
|
|
3
|
+
import { JsonSchema } from "@fumadocs/json-schema";
|
|
4
|
+
import { Stf } from "@fumari/stf";
|
|
5
|
+
//#region src/playground/use-playground.d.ts
|
|
6
|
+
export interface PlaygroundOptions {
|
|
7
|
+
/** customise the auth fields, like their default values */
|
|
8
|
+
transformAuthInputs?: (fields: AuthField[]) => AuthField[];
|
|
9
|
+
fetchOptions?: BrowserFetcherOptions;
|
|
10
|
+
}
|
|
11
|
+
export interface RequestBodyInfo {
|
|
12
|
+
/** the preferred one of its media types */
|
|
13
|
+
mediaType: string;
|
|
14
|
+
schema: JsonSchema;
|
|
15
|
+
}
|
|
16
|
+
export interface PlaygroundAuth {
|
|
17
|
+
/** the security requirements of the operation, one of them authorizes the request */
|
|
18
|
+
requirements: AuthRequirement[][];
|
|
19
|
+
/** index of the selected requirement, `-1` when the operation needs no authorization */
|
|
20
|
+
selected: number;
|
|
21
|
+
select: (index: number) => void;
|
|
22
|
+
/** the fields of the selected requirement */
|
|
23
|
+
fields: AuthField[];
|
|
24
|
+
/**
|
|
25
|
+
* Obtain the access token of an OAuth 2.0 field, and write it into the field.
|
|
26
|
+
*
|
|
27
|
+
* `implicit` and `authorizationCode` leave the page without a token, `flowReturned` is set when they return.
|
|
28
|
+
*/
|
|
29
|
+
authorize: (field: AuthField, input: OAuthInput) => Promise<string | undefined>;
|
|
30
|
+
/** an OAuth flow started here has returned to the page */
|
|
31
|
+
flowReturned: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface OAuthInput extends Partial<Pick<OAuthFlowInput, 'clientId' | 'clientSecret' | 'username' | 'password' | 'clientAuth'>> {
|
|
34
|
+
type: OAuthFlowType;
|
|
35
|
+
/** defaults to the scopes required by the operation */
|
|
36
|
+
scopes?: string[];
|
|
37
|
+
}
|
|
38
|
+
export interface PlaygroundResponse {
|
|
39
|
+
/** unique among the responses of a playground */
|
|
40
|
+
id: number;
|
|
41
|
+
result: FetchResult;
|
|
42
|
+
}
|
|
43
|
+
export interface Playground {
|
|
44
|
+
/** the form, holding the `path`, `query`, `header`, `cookie` and `body` of request */
|
|
45
|
+
stf: Stf;
|
|
46
|
+
/** the request body edited in the form */
|
|
47
|
+
body?: RequestBodyInfo;
|
|
48
|
+
auth: PlaygroundAuth;
|
|
49
|
+
/** send the request of form */
|
|
50
|
+
send: () => Promise<void>;
|
|
51
|
+
isSending: boolean;
|
|
52
|
+
/** the response of last request */
|
|
53
|
+
response?: PlaygroundResponse;
|
|
54
|
+
clearResponse: () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The state of an API playground under `<OperationProvider />`: a form starting from the selected example request, its auth and the request it sends.
|
|
58
|
+
*
|
|
59
|
+
* Edits are synced to the example request, so code usages follow them.
|
|
60
|
+
*/
|
|
61
|
+
export declare function usePlayground({ transformAuthInputs, fetchOptions }?: PlaygroundOptions): Playground;
|
|
62
|
+
//#endregion
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { pathnameFromRequest } from "../requests/generators/index.js";
|
|
3
|
+
import { requestOAuthToken, useAuthFields, usePlaygroundAuth } from "./auth.js";
|
|
4
|
+
import { useServer } from "../utils/use-server.js";
|
|
5
|
+
import { useOpenAPI } from "../utils/create-page.js";
|
|
6
|
+
import { getPreferredType } from "../utils/schema.js";
|
|
7
|
+
import { encodeRequestData } from "../requests/media/encode.js";
|
|
8
|
+
import { useExampleRequests, useOperation } from "../operation.js";
|
|
9
|
+
import { createBrowserFetcher } from "./fetcher.js";
|
|
10
|
+
import { useEffect, useEffectEvent, useMemo, useState } from "react";
|
|
11
|
+
import { useStf } from "@fumari/stf";
|
|
12
|
+
//#region src/playground/use-playground.tsx
|
|
13
|
+
/**
|
|
14
|
+
* The state of an API playground under `<OperationProvider />`: a form starting from the selected example request, its auth and the request it sends.
|
|
15
|
+
*
|
|
16
|
+
* Edits are synced to the example request, so code usages follow them.
|
|
17
|
+
*/
|
|
18
|
+
function usePlayground({ transformAuthInputs, fetchOptions } = {}) {
|
|
19
|
+
const { mediaAdapters, proxyUrl, oauthRedirectUrl } = useOpenAPI();
|
|
20
|
+
const { path, method, operation, parameters: groups, requestBody } = useOperation();
|
|
21
|
+
const { items: examples, selected, update } = useExampleRequests();
|
|
22
|
+
const { resolveUrl } = useServer();
|
|
23
|
+
const origin = `${method} ${path}`;
|
|
24
|
+
const flowReturned = usePlaygroundAuth().origin === origin;
|
|
25
|
+
const [response, setResponse] = useState();
|
|
26
|
+
const [isSending, setSending] = useState(false);
|
|
27
|
+
let body;
|
|
28
|
+
if (requestBody) {
|
|
29
|
+
const mediaType = getPreferredType(requestBody.content);
|
|
30
|
+
body = {
|
|
31
|
+
mediaType,
|
|
32
|
+
schema: requestBody.content[mediaType].schema ?? true
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const defaultValues = useMemo(() => {
|
|
36
|
+
const data = examples.find((example) => example.id === selected)?.data;
|
|
37
|
+
return {
|
|
38
|
+
path: data?.path ?? {},
|
|
39
|
+
query: data?.query ?? {},
|
|
40
|
+
header: data?.header ?? {},
|
|
41
|
+
cookie: data?.cookie ?? {},
|
|
42
|
+
body: data?.body ?? {}
|
|
43
|
+
};
|
|
44
|
+
}, [examples, selected]);
|
|
45
|
+
const stf = useStf({ defaultValues });
|
|
46
|
+
const engine = stf.dataEngine;
|
|
47
|
+
const auth = useAuthFields(engine, {
|
|
48
|
+
operation,
|
|
49
|
+
transform: transformAuthInputs
|
|
50
|
+
});
|
|
51
|
+
function getRequestData() {
|
|
52
|
+
return {
|
|
53
|
+
...auth.mapValues(engine.getData()),
|
|
54
|
+
method,
|
|
55
|
+
bodyMediaType: body?.mediaType
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const syncExample = useEffectEvent(() => update(getRequestData()));
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (engine.getData() !== defaultValues) engine.reset(defaultValues);
|
|
61
|
+
}, [engine, defaultValues]);
|
|
62
|
+
const initAuth = auth.init;
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
const reset = initAuth();
|
|
65
|
+
syncExample();
|
|
66
|
+
let timer;
|
|
67
|
+
const listener = { onUpdate() {
|
|
68
|
+
window.clearTimeout(timer);
|
|
69
|
+
timer = window.setTimeout(syncExample, 400);
|
|
70
|
+
} };
|
|
71
|
+
engine.listen(listener);
|
|
72
|
+
return () => {
|
|
73
|
+
engine.unlisten(listener);
|
|
74
|
+
window.clearTimeout(timer);
|
|
75
|
+
reset();
|
|
76
|
+
};
|
|
77
|
+
}, [
|
|
78
|
+
engine,
|
|
79
|
+
defaultValues,
|
|
80
|
+
initAuth
|
|
81
|
+
]);
|
|
82
|
+
return {
|
|
83
|
+
stf,
|
|
84
|
+
body,
|
|
85
|
+
auth: {
|
|
86
|
+
requirements: auth.requirements,
|
|
87
|
+
selected: auth.selected,
|
|
88
|
+
select: auth.select,
|
|
89
|
+
fields: auth.fields,
|
|
90
|
+
async authorize(field, { type, scopes = field.scopes, clientAuth = "header", ...input }) {
|
|
91
|
+
const token = await requestOAuthToken(field.scheme, type, {
|
|
92
|
+
clientId: input.clientId ?? "",
|
|
93
|
+
clientSecret: input.clientSecret ?? "",
|
|
94
|
+
username: input.username ?? "",
|
|
95
|
+
password: input.password ?? "",
|
|
96
|
+
scopes,
|
|
97
|
+
clientAuth,
|
|
98
|
+
schemeId: field.schemeId,
|
|
99
|
+
serverUrl: resolveUrl(),
|
|
100
|
+
redirectUrl: oauthRedirectUrl,
|
|
101
|
+
origin
|
|
102
|
+
});
|
|
103
|
+
if (token) engine.update(field.fieldName, token);
|
|
104
|
+
return token;
|
|
105
|
+
},
|
|
106
|
+
flowReturned
|
|
107
|
+
},
|
|
108
|
+
isSending,
|
|
109
|
+
response,
|
|
110
|
+
async send() {
|
|
111
|
+
if (isSending) return;
|
|
112
|
+
setSending(true);
|
|
113
|
+
const parameters = groups.flatMap((group) => group.items);
|
|
114
|
+
let result;
|
|
115
|
+
try {
|
|
116
|
+
const data = encodeRequestData(getRequestData(), mediaAdapters, parameters);
|
|
117
|
+
result = await createBrowserFetcher(mediaAdapters, {
|
|
118
|
+
proxyUrl,
|
|
119
|
+
...fetchOptions
|
|
120
|
+
}).fetch(resolveUrl(pathnameFromRequest(path, data)), data);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
result = {
|
|
123
|
+
type: "client_error",
|
|
124
|
+
url: path,
|
|
125
|
+
message: String(e)
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
setResponse((prev) => ({
|
|
129
|
+
id: (prev?.id ?? 0) + 1,
|
|
130
|
+
result
|
|
131
|
+
}));
|
|
132
|
+
setSending(false);
|
|
133
|
+
},
|
|
134
|
+
clearResponse() {
|
|
135
|
+
setResponse(void 0);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
export { usePlayground };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "../../utils/cn.js";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
5
|
+
//#region src/ui/components/dialog.tsx
|
|
6
|
+
/** the popup of a dialog, a window on larger screens and the full screen on smaller ones */
|
|
7
|
+
function DialogPopup({ className, children }) {
|
|
8
|
+
return /* @__PURE__ */ jsxs(Dialog.Portal, { children: [/* @__PURE__ */ jsx(Dialog.Backdrop, { className: "fixed inset-0 z-50 bg-black/40 backdrop-blur-xs transition-opacity duration-200 data-ending-style:opacity-0 data-starting-style:opacity-0" }), /* @__PURE__ */ jsx(Dialog.Popup, {
|
|
9
|
+
className: cn("fixed inset-0 z-50 flex flex-col overflow-hidden bg-fd-background text-fd-foreground outline-none transition-[opacity,scale] duration-200 ease-[cubic-bezier(0.16,1,0.3,1)] data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-ending-style:duration-150 data-starting-style:scale-[0.98] data-starting-style:opacity-0 motion-reduce:transition-opacity sm:inset-auto sm:top-1/2 sm:left-1/2 sm:-translate-x-1/2 sm:-translate-y-1/2 sm:rounded-2xl sm:border sm:shadow-2xl", className),
|
|
10
|
+
children
|
|
11
|
+
})] });
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { DialogPopup };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { MethodLabel } from "./method-label.js";
|
|
2
|
+
import { cn } from "../../utils/cn.js";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
//#region src/ui/components/endpoint.tsx
|
|
5
|
+
function EndpointBar({ method, route, deprecated = false, children, ...props }) {
|
|
6
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
7
|
+
...props,
|
|
8
|
+
className: cn("not-prose flex min-h-11 items-center gap-3 rounded-xl border bg-fd-card p-1.5 ps-3.5 text-sm text-fd-card-foreground", props.className),
|
|
9
|
+
children: [
|
|
10
|
+
/* @__PURE__ */ jsx(MethodLabel, {
|
|
11
|
+
className: "text-xs",
|
|
12
|
+
children: method
|
|
13
|
+
}),
|
|
14
|
+
/* @__PURE__ */ jsx(Route, {
|
|
15
|
+
route,
|
|
16
|
+
className: cn("flex-1", deprecated && "line-through")
|
|
17
|
+
}),
|
|
18
|
+
children
|
|
19
|
+
]
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function Route({ route, ...props }) {
|
|
23
|
+
return /* @__PURE__ */ jsx("div", {
|
|
24
|
+
...props,
|
|
25
|
+
className: cn("flex min-w-0 items-center overflow-x-auto font-mono text-[0.8125rem] text-nowrap [scrollbar-width:none]", props.className),
|
|
26
|
+
children: route.split(/(\{[^}]*\}|\/)/).map((part, index) => {
|
|
27
|
+
if (index % 2 === 0) return part;
|
|
28
|
+
if (part === "/") return /* @__PURE__ */ jsx("span", {
|
|
29
|
+
className: "text-fd-muted-foreground",
|
|
30
|
+
children: "/"
|
|
31
|
+
}, index);
|
|
32
|
+
return /* @__PURE__ */ jsx("span", {
|
|
33
|
+
className: "rounded-md bg-fd-primary/10 px-1 text-fd-primary",
|
|
34
|
+
children: part
|
|
35
|
+
}, index);
|
|
36
|
+
})
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { EndpointBar };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useComponents } from "../../utils/create-page.js";
|
|
3
|
-
import { useAnchorId } from "../../packages/shared-api/dist/auto-anchor/
|
|
3
|
+
import { useAnchorId } from "../../packages/shared-api/dist/auto-anchor/index.js";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
//#region src/ui/components/heading.tsx
|
|
6
6
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useComponents, useRenderContext } from "../../utils/create-page.js";
|
|
3
|
-
import { useAnchorId } from "../../packages/shared-api/dist/auto-anchor/
|
|
3
|
+
import { useAnchorId } from "../../packages/shared-api/dist/auto-anchor/index.js";
|
|
4
4
|
import { useMemo } from "react";
|
|
5
5
|
import { jsx } from "react/jsx-runtime";
|
|
6
6
|
//#region src/ui/components/schema.tsx
|
|
@@ -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/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 };
|