fumadocs-openapi 12.4.2 → 12.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/css/generated/shared.css +1 -1
- package/dist/.translations/index.d.ts +12 -1
- package/dist/.translations/keys.js +12 -1
- package/dist/generate-file.js +3 -1
- package/dist/index.browser.d.ts +2 -2
- package/dist/index.d.ts +3 -2
- package/dist/operation.js +2 -2
- package/dist/packages/shared-api/dist/codegen.js +1 -3
- package/dist/packages/shared-api/dist/components/schema/client.js +1 -1
- package/dist/packages/shared-api/dist/components/select.js +6 -2
- package/dist/playground/auth.d.ts +84 -30
- package/dist/playground/auth.js +250 -150
- package/dist/playground/fetcher.js +11 -6
- package/dist/playground/index.d.ts +2 -2
- package/dist/playground/index.js +2 -2
- package/dist/playground/use-playground.d.ts +3 -3
- package/dist/playground/use-playground.js +46 -31
- package/dist/requests/generators/python.d.ts +0 -1
- package/dist/requests/generators/python.js +1 -1
- package/dist/server/index.d.ts +2 -9
- package/dist/server/index.js +2 -9
- package/dist/server/proxy.d.ts +5 -4
- package/dist/server/proxy.js +105 -25
- package/dist/types/openapi.d.ts +7 -2
- package/dist/types.d.ts +1 -0
- package/dist/ui/operation/index.js +66 -38
- package/dist/ui/operation/usage-tabs.js +272 -35
- package/dist/ui/playground/client.js +8 -4
- package/dist/ui/playground/components/fields.js +5 -7
- package/dist/ui/playground/components/oauth-panel.js +150 -39
- package/dist/ui/playground/components/request-panel.js +18 -3
- package/dist/ui/playground/components/response-panel.js +53 -90
- package/dist/utils/create-page.d.ts +2 -1
- package/dist/utils/pages/to-static-data.d.ts +12 -0
- package/dist/utils/use-server.js +1 -1
- package/package.json +8 -7
- package/dist/packages/shared-api/dist/components/accordion.js +0 -63
- package/dist/ui/operation/request-tabs.js +0 -92
- package/dist/ui/operation/response-tabs.js +0 -53
|
@@ -1,17 +1,43 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { pathnameFromRequest } from "../requests/generators/index.js";
|
|
3
|
+
import { useServer } from "../utils/use-server.js";
|
|
3
4
|
import { encodeRequestData } from "../requests/media/encode.js";
|
|
4
5
|
import { getPreferredType } from "../utils/schema.js";
|
|
5
|
-
import { useServer } from "../utils/use-server.js";
|
|
6
6
|
import { useExampleRequests, useOperation } from "../operation.js";
|
|
7
|
-
import { readPendingFlow, requestOAuthToken, useAuthFields, usePlaygroundAuth } from "./auth.js";
|
|
8
|
-
import { useOpenAPI } from "../utils/create-page.js";
|
|
9
7
|
import { createBrowserFetcher } from "./fetcher.js";
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
8
|
+
import { requestOAuthToken, useAuthFields, usePlaygroundAuth } from "./auth.js";
|
|
9
|
+
import { useOpenAPI } from "../utils/create-page.js";
|
|
10
|
+
import { useEffect, useEffectEvent, useMemo, useState } from "react";
|
|
11
|
+
import { DataEngine } from "@fumari/stf";
|
|
13
12
|
//#region src/playground/use-playground.tsx
|
|
14
|
-
|
|
13
|
+
/** the form of a playground, its `auth` is the auth values of page */
|
|
14
|
+
var PlaygroundForm = class extends DataEngine {
|
|
15
|
+
constructor(defaultValues, auth) {
|
|
16
|
+
super(defaultValues);
|
|
17
|
+
this.auth = auth;
|
|
18
|
+
}
|
|
19
|
+
get(key) {
|
|
20
|
+
return key[0] === "auth" ? this.auth.get(key) : super.get(key);
|
|
21
|
+
}
|
|
22
|
+
init(key, defaultValue, ctx) {
|
|
23
|
+
return key[0] === "auth" ? this.auth.init(key, defaultValue, ctx) : super.init(key, defaultValue, ctx);
|
|
24
|
+
}
|
|
25
|
+
update(key, value, ctx) {
|
|
26
|
+
return key[0] === "auth" ? this.auth.update(key, value, ctx) : super.update(key, value, ctx);
|
|
27
|
+
}
|
|
28
|
+
delete(key, ctx) {
|
|
29
|
+
return key[0] === "auth" ? this.auth.delete(key, ctx) : super.delete(key, ctx);
|
|
30
|
+
}
|
|
31
|
+
listen(listener) {
|
|
32
|
+
const [root] = listener.field ?? [];
|
|
33
|
+
if (root !== "auth") super.listen(listener);
|
|
34
|
+
if (root === void 0 || root === "auth") this.auth.listen(listener);
|
|
35
|
+
}
|
|
36
|
+
unlisten(listener) {
|
|
37
|
+
super.unlisten(listener);
|
|
38
|
+
this.auth.unlisten(listener);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
15
41
|
/**
|
|
16
42
|
* The state of an API playground under `<OperationProvider />`: a form starting from the selected example request, its auth and the request it sends.
|
|
17
43
|
*
|
|
@@ -23,12 +49,9 @@ function usePlayground({ authProviders, transformAuthInputs, fetchOptions } = {}
|
|
|
23
49
|
const { items: examples, selected, update } = useExampleRequests();
|
|
24
50
|
const { resolveUrl } = useServer();
|
|
25
51
|
const origin = `${method} ${path}`;
|
|
26
|
-
const
|
|
27
|
-
const returnedScheme =
|
|
28
|
-
|
|
29
|
-
if (flow?.origin === origin) return flow.scheme;
|
|
30
|
-
}, () => void 0);
|
|
31
|
-
const flowReturned = returnedScheme !== void 0 || oauthOrigin === origin;
|
|
52
|
+
const page = usePlaygroundAuth();
|
|
53
|
+
const returnedScheme = page.returned?.origin === origin ? page.returned.scheme : void 0;
|
|
54
|
+
const flowReturned = returnedScheme !== void 0;
|
|
32
55
|
const [response, setResponse] = useState();
|
|
33
56
|
const [isSending, setSending] = useState(false);
|
|
34
57
|
let body;
|
|
@@ -49,7 +72,7 @@ function usePlayground({ authProviders, transformAuthInputs, fetchOptions } = {}
|
|
|
49
72
|
body: data?.body ?? {}
|
|
50
73
|
};
|
|
51
74
|
}, [examples, selected]);
|
|
52
|
-
const stf =
|
|
75
|
+
const stf = useMemo(() => ({ dataEngine: new PlaygroundForm(defaultValues, page.stf.dataEngine) }), [defaultValues, page.stf.dataEngine]);
|
|
53
76
|
const engine = stf.dataEngine;
|
|
54
77
|
const auth = useAuthFields(engine, {
|
|
55
78
|
operation,
|
|
@@ -64,13 +87,12 @@ function usePlayground({ authProviders, transformAuthInputs, fetchOptions } = {}
|
|
|
64
87
|
});
|
|
65
88
|
}
|
|
66
89
|
const syncExample = useEffectEvent(() => update(getRequestData()));
|
|
67
|
-
|
|
68
|
-
|
|
90
|
+
const [selectedFor, setSelectedFor] = useState();
|
|
91
|
+
if (returnedScheme !== selectedFor) {
|
|
92
|
+
setSelectedFor(returnedScheme);
|
|
93
|
+
const idx = auth.requirements.findIndex((req) => req.some((item) => item.id === returnedScheme));
|
|
69
94
|
if (idx !== -1) auth.select(idx);
|
|
70
|
-
}
|
|
71
|
-
useEffect(() => {
|
|
72
|
-
if (engine.getData() !== defaultValues) engine.reset(defaultValues);
|
|
73
|
-
}, [engine, defaultValues]);
|
|
95
|
+
}
|
|
74
96
|
const initAuth = auth.init;
|
|
75
97
|
useEffect(() => {
|
|
76
98
|
const reset = initAuth();
|
|
@@ -86,11 +108,7 @@ function usePlayground({ authProviders, transformAuthInputs, fetchOptions } = {}
|
|
|
86
108
|
window.clearTimeout(timer);
|
|
87
109
|
reset();
|
|
88
110
|
};
|
|
89
|
-
}, [
|
|
90
|
-
engine,
|
|
91
|
-
defaultValues,
|
|
92
|
-
initAuth
|
|
93
|
-
]);
|
|
111
|
+
}, [engine, initAuth]);
|
|
94
112
|
return {
|
|
95
113
|
stf,
|
|
96
114
|
body,
|
|
@@ -99,17 +117,14 @@ function usePlayground({ authProviders, transformAuthInputs, fetchOptions } = {}
|
|
|
99
117
|
selected: auth.selected,
|
|
100
118
|
select: auth.select,
|
|
101
119
|
fields: auth.fields,
|
|
102
|
-
async authorize(field, { type, scopes = field.scopes,
|
|
120
|
+
async authorize(field, { type, scopes = field.scopes, ...input }) {
|
|
103
121
|
const token = await requestOAuthToken(field.scheme, type, {
|
|
104
|
-
|
|
105
|
-
clientSecret: input.clientSecret ?? "",
|
|
106
|
-
username: input.username ?? "",
|
|
107
|
-
password: input.password ?? "",
|
|
122
|
+
...input,
|
|
108
123
|
scopes,
|
|
109
|
-
clientAuth,
|
|
110
124
|
schemeId: field.schemeId,
|
|
111
125
|
serverUrl: resolveUrl(),
|
|
112
126
|
redirectUrl: oauthRedirectUrl,
|
|
127
|
+
proxyUrl,
|
|
113
128
|
origin
|
|
114
129
|
});
|
|
115
130
|
if (token) engine.update(field.fieldName, token);
|
package/dist/server/index.d.ts
CHANGED
|
@@ -3,10 +3,11 @@ import { OpenAPIPageProps_Preloaded, OpenAPIPageProps_Spec } from "../utils/page
|
|
|
3
3
|
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
|
+
import { InternalOpenAPIMeta } from "../utils/pages/to-static-data.js";
|
|
6
7
|
import "../ui/index.js";
|
|
7
8
|
import { DynamicSource, LoaderPlugin, MetaData, Page, PageData, Source } from "fumadocs-core/source";
|
|
8
|
-
import { StructuredData } from "fumadocs-core/mdx-plugins/remark-structure";
|
|
9
9
|
import { TOCItemType } from "fumadocs-core/toc";
|
|
10
|
+
import { StructuredData } from "fumadocs-core/mdx-plugins/remark-structure";
|
|
10
11
|
//#region src/server/index.d.ts
|
|
11
12
|
/**
|
|
12
13
|
* schema ID -> file path, URL, downloaded schema object, or a function returning them
|
|
@@ -54,8 +55,6 @@ export interface OpenAPIServer {
|
|
|
54
55
|
* Fumadocs Source API integration, pass this to `plugins` array in `loader()`.
|
|
55
56
|
*/
|
|
56
57
|
loaderPlugin: () => LoaderPlugin;
|
|
57
|
-
/** @private internal API */
|
|
58
|
-
_getWatchPaths: () => string[];
|
|
59
58
|
}
|
|
60
59
|
export interface OpenAPIPageData extends PageData {
|
|
61
60
|
getOpenAPIPageProps: () => OpenAPIPageProps_Spec;
|
|
@@ -75,12 +74,6 @@ export type OpenAPISourceOptions = SchemaToPagesOptions & {
|
|
|
75
74
|
};
|
|
76
75
|
};
|
|
77
76
|
export declare function createOpenAPI(options?: OpenAPIOptions): OpenAPIServer;
|
|
78
|
-
export interface InternalOpenAPIMeta {
|
|
79
|
-
method?: string;
|
|
80
|
-
webhook?: boolean;
|
|
81
|
-
deprecated?: boolean;
|
|
82
|
-
preload?: string[];
|
|
83
|
-
}
|
|
84
77
|
/**
|
|
85
78
|
* Fumadocs Source API integration, pass this to `plugins` array in `loader()`.
|
|
86
79
|
*/
|
package/dist/server/index.js
CHANGED
|
@@ -8,7 +8,6 @@ import { MethodLabel } from "../ui/components/method-label.js";
|
|
|
8
8
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
9
9
|
import path from "node:path";
|
|
10
10
|
import { PathUtils } from "fumadocs-core/source";
|
|
11
|
-
import fs from "node:fs";
|
|
12
11
|
//#region src/server/index.tsx
|
|
13
12
|
function createOpenAPI(options = {}) {
|
|
14
13
|
const { disableCache = false } = options;
|
|
@@ -16,11 +15,8 @@ function createOpenAPI(options = {}) {
|
|
|
16
15
|
let resolvedInput = {};
|
|
17
16
|
if (Array.isArray(options.input)) for (const item of options.input) resolvedInput[item] = item;
|
|
18
17
|
else if (options.input) resolvedInput = options.input;
|
|
19
|
-
function getSchema(schemaId) {
|
|
20
|
-
if (!(schemaId in
|
|
21
|
-
console.warn(`[Fumadocs OpenAPI] the document "${schemaId}" is not listed in the input array, this may be unexpected and won't be cached properly.`);
|
|
22
|
-
return loadDocument(schemaId);
|
|
23
|
-
}
|
|
18
|
+
async function getSchema(schemaId) {
|
|
19
|
+
if (!Object.hasOwn(resolvedInput, schemaId)) throw new Error(`[Fumadocs OpenAPI] the document "${schemaId}" is not listed in "input", only documents from "input" can be loaded.`);
|
|
24
20
|
if (!disableCache) {
|
|
25
21
|
const cached = docCache.get(schemaId);
|
|
26
22
|
if (cached) return cached;
|
|
@@ -115,9 +111,6 @@ function createOpenAPI(options = {}) {
|
|
|
115
111
|
options,
|
|
116
112
|
createProxy,
|
|
117
113
|
createOAuthHandler,
|
|
118
|
-
_getWatchPaths() {
|
|
119
|
-
return Object.keys(resolvedInput).filter((key) => !URL.canParse(key) && fs.existsSync(key));
|
|
120
|
-
},
|
|
121
114
|
async preloadOpenAPIPage(page) {
|
|
122
115
|
const out = {
|
|
123
116
|
docs: {},
|
package/dist/server/proxy.d.ts
CHANGED
|
@@ -7,11 +7,12 @@ export interface Proxy extends Record<(typeof methods)[number], Handler> {
|
|
|
7
7
|
}
|
|
8
8
|
export interface CreateProxyOptions {
|
|
9
9
|
/**
|
|
10
|
-
* List of allowed origins to proxy to, also enforced on redirects.
|
|
10
|
+
* List of allowed origins to proxy to, also enforced on redirects. Listed origins are trusted,
|
|
11
|
+
* including private ones like `http://localhost:8080`.
|
|
11
12
|
*
|
|
12
|
-
* @defaultValue
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* @defaultValue any public HTTP(S) origin. Hosts resolving to private network addresses (e.g.
|
|
14
|
+
* loopback, link-local) are refused when connecting, this requires Node.js or Deno. Cookies of
|
|
15
|
+
* your site and `Set-Cookie` headers of responses are not forwarded.
|
|
15
16
|
*/
|
|
16
17
|
allowedOrigins?: OriginMatcher[];
|
|
17
18
|
/**
|
package/dist/server/proxy.js
CHANGED
|
@@ -35,42 +35,52 @@ function matchOrigin(origin, matcher) {
|
|
|
35
35
|
function createProxy(options = {}) {
|
|
36
36
|
const { allowedOrigins, filterRequest, overrides } = options;
|
|
37
37
|
const handlers = { handle: handler };
|
|
38
|
-
|
|
38
|
+
function isAllowed(url) {
|
|
39
|
+
if (allowedOrigins) return allowedOrigins.some((matcher) => matchOrigin(url.origin, matcher));
|
|
40
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
41
|
+
}
|
|
39
42
|
async function handler(req) {
|
|
40
|
-
const
|
|
41
|
-
const searchParams = reqUrl.searchParams;
|
|
43
|
+
const searchParams = new URL(req.url).searchParams;
|
|
42
44
|
const rawUrl = searchParams.get("url");
|
|
43
45
|
if (!rawUrl) return Response.json("[Proxy] A `url` query parameter is required for proxy url", { status: 400 });
|
|
44
46
|
const targetUrl = URL.parse(rawUrl);
|
|
45
47
|
if (!targetUrl) return Response.json("[Proxy] Invalid `url` parameter value.", { status: 400 });
|
|
46
|
-
|
|
47
|
-
if (allowed.every((matcher) => !matchOrigin(targetUrl.origin, matcher))) return Response.json(`[Proxy] The origin "${targetUrl.origin}" is not allowed.`, { status: 400 });
|
|
48
|
+
if (!isAllowed(targetUrl)) return Response.json(`[Proxy] The origin "${targetUrl.origin}" is not allowed.`, { status: 400 });
|
|
48
49
|
const proxied = await rewriteRequest(req, targetUrl, searchParams.get("cookie"));
|
|
49
50
|
if (filterRequest && !filterRequest(proxied)) return Response.json("[Proxy] The proxied request is not allowed", { status: 403 });
|
|
50
51
|
try {
|
|
51
|
-
return
|
|
52
|
-
} catch (
|
|
52
|
+
return await proxyFetch(proxied);
|
|
53
|
+
} catch (e) {
|
|
54
|
+
const err = e instanceof Error && e.cause instanceof ProxyError ? e.cause : e;
|
|
53
55
|
if (err instanceof ProxyError) return Response.json(`[Proxy] ${err.message}`, { status: err.status });
|
|
54
56
|
return Response.json(`[Proxy] Failed to proxy request: ${err instanceof Error ? err.message : "unknown reason"}`, { status: 500 });
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
|
-
async function proxyFetch(initial
|
|
59
|
+
async function proxyFetch(initial) {
|
|
60
|
+
const send = allowedOrigins ? fetch : await (publicFetch ??= createPublicFetch());
|
|
58
61
|
let method = initial.method;
|
|
59
|
-
let url = initial.url;
|
|
62
|
+
let url = new URL(initial.url);
|
|
60
63
|
const headers = new Headers(initial.headers);
|
|
61
64
|
let body = initial.body && methodsWithBody.has(method.toUpperCase()) ? await initial.arrayBuffer() : void 0;
|
|
62
65
|
for (let redirects = 0; redirects <= maxRedirects; redirects++) {
|
|
63
|
-
const response = await
|
|
66
|
+
const response = await send(url, {
|
|
64
67
|
method,
|
|
65
68
|
headers,
|
|
66
69
|
body,
|
|
67
70
|
cache: "no-cache",
|
|
68
|
-
redirect: "manual"
|
|
69
|
-
|
|
71
|
+
redirect: "manual",
|
|
72
|
+
signal: initial.signal
|
|
73
|
+
});
|
|
70
74
|
const location = response.headers.get("location");
|
|
71
|
-
if (!redirectStatus.has(response.status) || !location) return response;
|
|
75
|
+
if (!redirectStatus.has(response.status) || !location) return rewriteResponse(response, url);
|
|
76
|
+
await response.body?.cancel();
|
|
72
77
|
const next = new URL(location, url);
|
|
73
|
-
if (
|
|
78
|
+
if (!isAllowed(next)) throw new ProxyError(`The redirect origin "${next.origin}" is not allowed.`);
|
|
79
|
+
if (next.origin !== url.origin) {
|
|
80
|
+
headers.delete("authorization");
|
|
81
|
+
headers.delete("proxy-authorization");
|
|
82
|
+
headers.delete("cookie");
|
|
83
|
+
}
|
|
74
84
|
switch (response.status) {
|
|
75
85
|
case 301:
|
|
76
86
|
case 302: if (method === "GET" || method === "HEAD") break;
|
|
@@ -80,33 +90,53 @@ function createProxy(options = {}) {
|
|
|
80
90
|
headers.delete("content-length");
|
|
81
91
|
headers.delete("content-type");
|
|
82
92
|
}
|
|
83
|
-
url = next
|
|
93
|
+
url = next;
|
|
84
94
|
}
|
|
85
95
|
throw new ProxyError("Too many redirects.", 508);
|
|
86
96
|
}
|
|
87
97
|
async function rewriteRequest(request, url, cookie) {
|
|
88
98
|
const headers = new Headers(request.headers);
|
|
89
99
|
headers.delete("origin");
|
|
100
|
+
if (!allowedOrigins) headers.delete("cookie");
|
|
90
101
|
if (cookie) headers.set("Cookie", cookie);
|
|
91
|
-
const contentLength = headers.get("content-length");
|
|
92
|
-
const hasBody = contentLength && parseInt(contentLength) > 0;
|
|
93
102
|
const proxied = new Request(url, {
|
|
94
103
|
method: request.method,
|
|
95
104
|
cache: "no-cache",
|
|
96
105
|
headers,
|
|
97
|
-
body:
|
|
106
|
+
body: methodsWithBody.has(request.method.toUpperCase()) ? request.body : null,
|
|
107
|
+
signal: request.signal,
|
|
108
|
+
duplex: "half"
|
|
98
109
|
});
|
|
99
110
|
return overrides?.request ? overrides.request(proxied) : proxied;
|
|
100
111
|
}
|
|
101
|
-
async function rewriteResponse(response) {
|
|
112
|
+
async function rewriteResponse(response, url) {
|
|
102
113
|
if (overrides?.response) response = overrides.response(response);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
114
|
+
if (response.status < 200 || response.status > 599) {
|
|
115
|
+
await response.body?.cancel();
|
|
116
|
+
throw new ProxyError(`The upstream responded with an unsupported status ${response.status}.`, 502);
|
|
117
|
+
}
|
|
118
|
+
const headers = new Headers();
|
|
119
|
+
response.headers.forEach((value, key) => {
|
|
120
|
+
if (key.startsWith("access-control-")) return;
|
|
121
|
+
switch (key) {
|
|
122
|
+
case "set-cookie":
|
|
123
|
+
if (!allowedOrigins) return;
|
|
124
|
+
break;
|
|
125
|
+
case "content-encoding":
|
|
126
|
+
case "content-length":
|
|
127
|
+
case "alt-svc":
|
|
128
|
+
case "clear-site-data":
|
|
129
|
+
case "nel":
|
|
130
|
+
case "refresh":
|
|
131
|
+
case "report-to":
|
|
132
|
+
case "reporting-endpoints":
|
|
133
|
+
case "set-login":
|
|
134
|
+
case "strict-transport-security": return;
|
|
135
|
+
}
|
|
136
|
+
headers.append(key, value);
|
|
108
137
|
});
|
|
109
|
-
headers.set("X-Forwarded-Host",
|
|
138
|
+
headers.set("X-Forwarded-Host", url.href);
|
|
139
|
+
headers.append("Content-Security-Policy", "sandbox");
|
|
110
140
|
return new Response(response.body, {
|
|
111
141
|
status: response.status,
|
|
112
142
|
statusText: response.statusText,
|
|
@@ -116,5 +146,55 @@ function createProxy(options = {}) {
|
|
|
116
146
|
for (const key of methods) handlers[key] = handler;
|
|
117
147
|
return handlers;
|
|
118
148
|
}
|
|
149
|
+
const privateRanges = [
|
|
150
|
+
["0.0.0.0", 8],
|
|
151
|
+
["10.0.0.0", 8],
|
|
152
|
+
["100.64.0.0", 10],
|
|
153
|
+
["127.0.0.0", 8],
|
|
154
|
+
["169.254.0.0", 16],
|
|
155
|
+
["172.16.0.0", 12],
|
|
156
|
+
["192.0.0.0", 24],
|
|
157
|
+
["192.168.0.0", 16],
|
|
158
|
+
["240.0.0.0", 4],
|
|
159
|
+
["::", 96],
|
|
160
|
+
["fc00::", 7],
|
|
161
|
+
["fe80::", 10],
|
|
162
|
+
["2001::", 32],
|
|
163
|
+
["2002::", 16],
|
|
164
|
+
["64:ff9b::", 96],
|
|
165
|
+
["64:ff9b:1::", 48]
|
|
166
|
+
];
|
|
167
|
+
let publicFetch;
|
|
168
|
+
/**
|
|
169
|
+
* `fetch()` refusing hosts on private networks, checked against the resolved addresses of every
|
|
170
|
+
* connection.
|
|
171
|
+
*/
|
|
172
|
+
async function createPublicFetch() {
|
|
173
|
+
if ("Bun" in globalThis) throw new ProxyError("Private networks cannot be blocked on Bun, specify `allowedOrigins`.", 500);
|
|
174
|
+
const [{ Agent, fetch }, dns, { BlockList, isIP }] = await Promise.all([
|
|
175
|
+
import("undici"),
|
|
176
|
+
import("node:dns"),
|
|
177
|
+
import("node:net")
|
|
178
|
+
]);
|
|
179
|
+
const ipType = (address) => isIP(address) === 6 ? "ipv6" : "ipv4";
|
|
180
|
+
const blockList = new BlockList();
|
|
181
|
+
for (const [address, prefix] of privateRanges) blockList.addSubnet(address, prefix, ipType(address));
|
|
182
|
+
const isPrivate = (address) => blockList.check(address, ipType(address));
|
|
183
|
+
const refuse = (hostname) => new ProxyError(`The host "${hostname}" doesn't resolve to a public address, add its origin to \`allowedOrigins\` to proxy to private hosts.`);
|
|
184
|
+
const dispatcher = new Agent({ connect: { lookup(hostname, options, callback) {
|
|
185
|
+
dns.lookup(hostname, options, (err, address, family) => {
|
|
186
|
+
if (err || (typeof address === "string" ? [{ address }] : address).some((item) => isPrivate(item.address))) callback(refuse(hostname), address, family);
|
|
187
|
+
else callback(null, address, family);
|
|
188
|
+
});
|
|
189
|
+
} } });
|
|
190
|
+
return async (url, init) => {
|
|
191
|
+
const hostname = url.hostname.replace(/^\[(.*)\]$/, "$1");
|
|
192
|
+
if (isIP(hostname) && isPrivate(hostname)) throw refuse(hostname);
|
|
193
|
+
return fetch(url, {
|
|
194
|
+
...init,
|
|
195
|
+
dispatcher
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
}
|
|
119
199
|
//#endregion
|
|
120
200
|
export { createProxy };
|
package/dist/types/openapi.d.ts
CHANGED
|
@@ -161,7 +161,7 @@ export declare namespace OpenAPIV3_2 {
|
|
|
161
161
|
pathItems?: Record<string, ReferenceObject | PathItemObject>;
|
|
162
162
|
mediaTypes?: Record<string, MediaTypeObject | ReferenceObject>;
|
|
163
163
|
}>;
|
|
164
|
-
type SecuritySchemeObject = (HttpSecurityScheme | ApiKeySecurityScheme | OAuth2SecurityScheme | OpenIdSecurityScheme) & {
|
|
164
|
+
type SecuritySchemeObject = (HttpSecurityScheme | ApiKeySecurityScheme | OAuth2SecurityScheme | OpenIdSecurityScheme | MutualTLSSecurityScheme) & {
|
|
165
165
|
deprecated?: boolean;
|
|
166
166
|
};
|
|
167
167
|
type HttpSecurityScheme = OpenAPIV3_1.HttpSecurityScheme;
|
|
@@ -175,6 +175,7 @@ export declare namespace OpenAPIV3_2 {
|
|
|
175
175
|
flows?: OAuthFlows;
|
|
176
176
|
}>;
|
|
177
177
|
type OpenIdSecurityScheme = OpenAPIV3_1.OpenIdSecurityScheme;
|
|
178
|
+
type MutualTLSSecurityScheme = OpenAPIV3_1.MutualTLSSecurityScheme;
|
|
178
179
|
type TagObject = OpenAPIV3_1.TagObject & {
|
|
179
180
|
summary?: string;
|
|
180
181
|
parent?: string;
|
|
@@ -329,12 +330,16 @@ export declare namespace OpenAPIV3_1 {
|
|
|
329
330
|
callbacks?: Record<string, ReferenceObject | CallbackObject>;
|
|
330
331
|
pathItems?: Record<string, ReferenceObject | PathItemObject>;
|
|
331
332
|
}>;
|
|
332
|
-
type SecuritySchemeObject = OpenAPIV3.SecuritySchemeObject;
|
|
333
|
+
type SecuritySchemeObject = OpenAPIV3.SecuritySchemeObject | MutualTLSSecurityScheme;
|
|
333
334
|
type HttpSecurityScheme = OpenAPIV3.HttpSecurityScheme;
|
|
334
335
|
type ApiKeySecurityScheme = OpenAPIV3.ApiKeySecurityScheme;
|
|
335
336
|
type OAuthFlows = OpenAPIV3.OAuthFlows;
|
|
336
337
|
type OAuth2SecurityScheme = OpenAPIV3.OAuth2SecurityScheme;
|
|
337
338
|
type OpenIdSecurityScheme = OpenAPIV3.OpenIdSecurityScheme;
|
|
339
|
+
type MutualTLSSecurityScheme = {
|
|
340
|
+
type?: 'mutualTLS';
|
|
341
|
+
description?: string;
|
|
342
|
+
};
|
|
338
343
|
type TagObject = OpenAPIV3.TagObject;
|
|
339
344
|
}
|
|
340
345
|
export declare namespace OpenAPIV3 {
|
package/dist/types.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export type CallbackObject = OpenAPIV3_2.CallbackObject;
|
|
|
17
17
|
export type ServerVariableObject = OpenAPIV3.ServerVariableObject;
|
|
18
18
|
export type ResponseObject = OpenAPIV3_2.ResponseObject;
|
|
19
19
|
export type OAuth2SecurityScheme = OpenAPIV3_2.OAuth2SecurityScheme;
|
|
20
|
+
export type OAuthFlows = OpenAPIV3_2.OAuthFlows;
|
|
20
21
|
export type HttpMethods = OpenAPIV3_2.HttpMethods;
|
|
21
22
|
export type ExampleObject = OpenAPIV3_2.ExampleObject;
|
|
22
23
|
export type MediaTypeObject = OpenAPIV3_2.MediaTypeObject;
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { OperationProvider, useOperation } from "../../operation.js";
|
|
3
|
-
import { useOpenAPI, useRenderContext
|
|
3
|
+
import { useOpenAPI, useRenderContext } from "../../utils/create-page.js";
|
|
4
4
|
import { Badge } from "../../packages/shared-api/dist/components/badge.js";
|
|
5
5
|
import { MethodLabel } from "../components/method-label.js";
|
|
6
|
-
import { AnchorSection, useAnchorLink } from "../../packages/shared-api/dist/auto-anchor/index.js";
|
|
7
6
|
import { Markdown } from "../components/markdown.js";
|
|
8
|
-
import { UsageTabs } from "./usage-tabs.js";
|
|
9
|
-
import { SchemaUI } from "../components/schema.js";
|
|
10
|
-
import { RequestTabs } from "./request-tabs.js";
|
|
11
7
|
import { cn } from "../../utils/cn.js";
|
|
8
|
+
import { RequestTabs, UsageTabs } from "./usage-tabs.js";
|
|
9
|
+
import { AnchorSection, useAnchorLink } from "../../packages/shared-api/dist/auto-anchor/index.js";
|
|
10
|
+
import { SchemaUI } from "../components/schema.js";
|
|
12
11
|
import { SelectTab, SelectTabTrigger, SelectTabs } from "../../packages/shared-api/dist/components/select-tab.js";
|
|
13
12
|
import { Heading } from "../components/heading.js";
|
|
14
13
|
import { Segmented, SegmentedList, SegmentedPanel } from "../components/segmented.js";
|
|
@@ -227,7 +226,7 @@ function OperationContent({ showTitle, showDescription, headingLevel = 2 }) {
|
|
|
227
226
|
slots.callbacks
|
|
228
227
|
]
|
|
229
228
|
}), /* @__PURE__ */ jsx("div", {
|
|
230
|
-
className: "@4xl:sticky @4xl:top-[calc(var(--fd-docs-row-
|
|
229
|
+
className: "@4xl:sticky @4xl:top-[calc(var(--fd-docs-row-3,0px)+1rem)] @4xl:flex @4xl:max-h-[calc(var(--fd-docs-height,100dvh)-var(--fd-docs-row-3,0px)-2rem)] @4xl:w-[400px] @4xl:flex-col",
|
|
231
230
|
children: slots.apiExample
|
|
232
231
|
})]
|
|
233
232
|
});
|
|
@@ -257,7 +256,7 @@ function OperationContent({ showTitle, showDescription, headingLevel = 2 }) {
|
|
|
257
256
|
slots.callbacks
|
|
258
257
|
]
|
|
259
258
|
}), /* @__PURE__ */ jsx("div", {
|
|
260
|
-
className: "@4xl:sticky @4xl:top-[calc(var(--fd-docs-row-
|
|
259
|
+
className: "@4xl:sticky @4xl:top-[calc(var(--fd-docs-row-3,0px)+1rem)] @4xl:flex @4xl:max-h-[calc(var(--fd-docs-height,100dvh)-var(--fd-docs-row-3,0px)-2rem)] @4xl:w-[400px] @4xl:flex-col",
|
|
261
260
|
children: slots.requests
|
|
262
261
|
})]
|
|
263
262
|
});
|
|
@@ -287,11 +286,7 @@ function CardSelector({ items }) {
|
|
|
287
286
|
/** the schema of a request body or response in a media type */
|
|
288
287
|
function MediaContent({ schema, required, selector, request = false }) {
|
|
289
288
|
const { method } = useOperation();
|
|
290
|
-
const
|
|
291
|
-
name: request ? "RequestBody" : "ResponseBody",
|
|
292
|
-
readOnly: !request,
|
|
293
|
-
writeOnly: request
|
|
294
|
-
});
|
|
289
|
+
const { doc, generateTypeScriptDefinitions } = useOpenAPI();
|
|
295
290
|
if (!schema) return /* @__PURE__ */ jsx("div", {
|
|
296
291
|
className: "not-prose flex h-9 items-center rounded-xl border bg-fd-card ps-1.5 pe-1 text-[0.8125rem]",
|
|
297
292
|
children: selector
|
|
@@ -302,9 +297,14 @@ function MediaContent({ schema, required, selector, request = false }) {
|
|
|
302
297
|
as: "body",
|
|
303
298
|
required,
|
|
304
299
|
selector,
|
|
305
|
-
actions:
|
|
306
|
-
|
|
307
|
-
|
|
300
|
+
actions: generateTypeScriptDefinitions && /* @__PURE__ */ jsx(CopyTypeScript, {
|
|
301
|
+
name: request ? "request body" : "response body",
|
|
302
|
+
generate: () => generateTypeScriptDefinitions(schema, {
|
|
303
|
+
name: request ? "RequestBody" : "ResponseBody",
|
|
304
|
+
readOnly: !request,
|
|
305
|
+
writeOnly: request,
|
|
306
|
+
doc
|
|
307
|
+
})
|
|
308
308
|
})
|
|
309
309
|
},
|
|
310
310
|
root: schema,
|
|
@@ -312,9 +312,15 @@ function MediaContent({ schema, required, selector, request = false }) {
|
|
|
312
312
|
writeOnly: request && method !== "get"
|
|
313
313
|
});
|
|
314
314
|
}
|
|
315
|
-
function CopyTypeScript({
|
|
315
|
+
function CopyTypeScript({ name, generate }) {
|
|
316
316
|
const t = useTranslations({ note: "TypeScript definitions" });
|
|
317
|
-
const [isChecked, onCopy] = useCopyButton(() =>
|
|
317
|
+
const [isChecked, onCopy] = useCopyButton(() => {
|
|
318
|
+
const code = generate();
|
|
319
|
+
if (typeof code === "string") return navigator.clipboard.writeText(code);
|
|
320
|
+
return Promise.resolve(code).then((code) => {
|
|
321
|
+
if (code) return navigator.clipboard.writeText(code);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
318
324
|
return /* @__PURE__ */ jsxs("button", {
|
|
319
325
|
type: "button",
|
|
320
326
|
title: t("Use the {name} type in TypeScript.", { variables: { name } }),
|
|
@@ -382,24 +388,38 @@ function ResponseContent({ item: { status, response, content }, onLink }) {
|
|
|
382
388
|
}
|
|
383
389
|
function AuthScheme({ scheme, scopes }) {
|
|
384
390
|
const t = useTranslations({ note: "security scheme" });
|
|
385
|
-
let name;
|
|
386
|
-
let type = "<token>";
|
|
387
|
-
let location;
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
391
|
+
let name = t("Authorization");
|
|
392
|
+
let type = t("Bearer <token>");
|
|
393
|
+
let location = "header";
|
|
394
|
+
const configUrl = scheme.type === "openIdConnect" ? scheme.openIdConnectUrl : void 0;
|
|
395
|
+
switch (scheme.type) {
|
|
396
|
+
case "apiKey":
|
|
397
|
+
name = scheme.name;
|
|
398
|
+
type = "<token>";
|
|
399
|
+
location = scheme.in;
|
|
400
|
+
break;
|
|
401
|
+
case "mutualTLS":
|
|
402
|
+
name = t("Client certificate");
|
|
403
|
+
type = void 0;
|
|
404
|
+
location = "tls";
|
|
405
|
+
break;
|
|
406
|
+
case "http": {
|
|
407
|
+
const auth = scheme.scheme?.toLowerCase();
|
|
408
|
+
if (auth === "basic") type = t("Basic <token>");
|
|
409
|
+
else if (auth === "bearer" && scheme.bearerFormat) type = `Bearer <${scheme.bearerFormat}>`;
|
|
410
|
+
else if (scheme.scheme && auth !== "bearer") type = `${scheme.scheme[0].toUpperCase()}${scheme.scheme.slice(1)} <token>`;
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
case "oauth2":
|
|
414
|
+
case "openIdConnect": break;
|
|
415
|
+
default: return null;
|
|
416
|
+
}
|
|
397
417
|
return /* @__PURE__ */ jsxs("div", {
|
|
398
418
|
className: "border-t py-2.5 first:border-t-0",
|
|
399
419
|
children: [/* @__PURE__ */ jsxs("div", {
|
|
400
420
|
className: "flex flex-wrap items-center gap-2 not-prose",
|
|
401
421
|
children: [
|
|
402
|
-
|
|
422
|
+
/* @__PURE__ */ jsx("span", {
|
|
403
423
|
className: "rounded-md border bg-fd-secondary px-1.5 font-mono text-xs leading-5 text-fd-muted-foreground",
|
|
404
424
|
children: location
|
|
405
425
|
}),
|
|
@@ -407,7 +427,7 @@ function AuthScheme({ scheme, scopes }) {
|
|
|
407
427
|
className: "text-[0.8125rem] font-medium text-fd-primary",
|
|
408
428
|
children: name
|
|
409
429
|
}),
|
|
410
|
-
/* @__PURE__ */ jsx("code", {
|
|
430
|
+
type && /* @__PURE__ */ jsx("code", {
|
|
411
431
|
className: "text-xs text-fd-muted-foreground",
|
|
412
432
|
children: type
|
|
413
433
|
}),
|
|
@@ -417,13 +437,21 @@ function AuthScheme({ scheme, scopes }) {
|
|
|
417
437
|
children: t("Deprecated")
|
|
418
438
|
})
|
|
419
439
|
]
|
|
420
|
-
}), (scheme.description || scopes.length > 0) && /* @__PURE__ */ jsxs("div", {
|
|
440
|
+
}), (scheme.description || scopes.length > 0 || configUrl) && /* @__PURE__ */ jsxs("div", {
|
|
421
441
|
className: "mt-1 prose-no-margin text-fd-muted-foreground",
|
|
422
|
-
children: [
|
|
423
|
-
|
|
424
|
-
":
|
|
425
|
-
|
|
426
|
-
|
|
442
|
+
children: [
|
|
443
|
+
scheme.description && /* @__PURE__ */ jsx(Markdown, { md: scheme.description }),
|
|
444
|
+
configUrl && /* @__PURE__ */ jsxs("p", { children: [
|
|
445
|
+
t("OpenID Connect"),
|
|
446
|
+
": ",
|
|
447
|
+
/* @__PURE__ */ jsx("code", { children: configUrl })
|
|
448
|
+
] }),
|
|
449
|
+
scopes.length > 0 && /* @__PURE__ */ jsxs("p", { children: [
|
|
450
|
+
t("Scope"),
|
|
451
|
+
": ",
|
|
452
|
+
/* @__PURE__ */ jsx("code", { children: scopes.join(", ") })
|
|
453
|
+
] })
|
|
454
|
+
]
|
|
427
455
|
})]
|
|
428
456
|
});
|
|
429
457
|
}
|
|
@@ -482,7 +510,7 @@ function Callback({ item, path, headingLevel }) {
|
|
|
482
510
|
}),
|
|
483
511
|
/* @__PURE__ */ jsx("div", {
|
|
484
512
|
ref,
|
|
485
|
-
className: "fd-scroll-container prose min-h-0 flex-1 overflow-y-auto border-t p-5
|
|
513
|
+
className: "fd-scroll-container prose prose-sm min-h-0 flex-1 overflow-y-auto border-t p-5 @container [--fd-docs-row-3:0px]",
|
|
486
514
|
children: /* @__PURE__ */ jsx(AnchorSection, {
|
|
487
515
|
segments,
|
|
488
516
|
children: /* @__PURE__ */ jsx(Operation, {
|