doccupine 0.0.133 → 0.0.134
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/README.md +40 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +274 -19
- package/dist/lib/config-manager.d.ts +8 -1
- package/dist/lib/config-manager.js +98 -4
- package/dist/lib/openapi-types.d.ts +120 -0
- package/dist/lib/openapi-types.js +14 -0
- package/dist/lib/openapi.d.ts +52 -0
- package/dist/lib/openapi.js +603 -0
- package/dist/lib/structures.js +28 -2
- package/dist/lib/types.d.ts +22 -0
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.js +14 -0
- package/dist/templates/app/api/playground/route.d.ts +1 -0
- package/dist/templates/app/api/playground/route.js +199 -0
- package/dist/templates/components/Docs.d.ts +1 -1
- package/dist/templates/components/Docs.js +11 -2
- package/dist/templates/components/SideBar.d.ts +1 -1
- package/dist/templates/components/SideBar.js +10 -0
- package/dist/templates/components/layout/Accordion.d.ts +1 -1
- package/dist/templates/components/layout/Accordion.js +3 -2
- package/dist/templates/components/layout/ActionBar.d.ts +1 -1
- package/dist/templates/components/layout/ActionBar.js +21 -14
- package/dist/templates/components/layout/ApiPlayground.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlayground.js +1083 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.js +81 -0
- package/dist/templates/components/layout/Code.d.ts +1 -1
- package/dist/templates/components/layout/Code.js +10 -22
- package/dist/templates/components/layout/CopyButton.d.ts +1 -0
- package/dist/templates/components/layout/CopyButton.js +68 -0
- package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
- package/dist/templates/components/layout/DocsComponents.js +39 -0
- package/dist/templates/components/layout/Mermaid.d.ts +1 -1
- package/dist/templates/components/layout/Mermaid.js +21 -3
- package/dist/templates/components/layout/SharedStyles.d.ts +1 -1
- package/dist/templates/components/layout/SharedStyles.js +0 -4
- package/dist/templates/components/layout/Steps.d.ts +1 -1
- package/dist/templates/components/layout/Steps.js +0 -1
- package/dist/templates/components/layout/Update.d.ts +1 -1
- package/dist/templates/components/layout/Update.js +2 -1
- package/dist/templates/mdx/accordion.mdx.d.ts +1 -1
- package/dist/templates/mdx/accordion.mdx.js +19 -1
- package/dist/templates/mdx/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/ai-assistant.mdx.js +3 -2
- package/dist/templates/mdx/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/analytics.mdx.js +2 -2
- package/dist/templates/mdx/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/api-playground.mdx.js +158 -0
- package/dist/templates/mdx/authentication.mdx.d.ts +1 -1
- package/dist/templates/mdx/authentication.mdx.js +3 -3
- package/dist/templates/mdx/buttons.mdx.d.ts +1 -1
- package/dist/templates/mdx/buttons.mdx.js +1 -1
- package/dist/templates/mdx/callouts.mdx.d.ts +1 -1
- package/dist/templates/mdx/callouts.mdx.js +1 -1
- package/dist/templates/mdx/cards.mdx.d.ts +1 -1
- package/dist/templates/mdx/cards.mdx.js +1 -1
- package/dist/templates/mdx/color-swatches.mdx.d.ts +1 -1
- package/dist/templates/mdx/color-swatches.mdx.js +1 -1
- package/dist/templates/mdx/commands.mdx.d.ts +1 -1
- package/dist/templates/mdx/commands.mdx.js +3 -2
- package/dist/templates/mdx/components.mdx.d.ts +1 -1
- package/dist/templates/mdx/components.mdx.js +12 -9
- package/dist/templates/mdx/deployment-and-hosting.mdx.d.ts +1 -1
- package/dist/templates/mdx/deployment-and-hosting.mdx.js +4 -3
- package/dist/templates/mdx/fields.mdx.d.ts +1 -1
- package/dist/templates/mdx/fields.mdx.js +1 -1
- package/dist/templates/mdx/fonts.mdx.d.ts +1 -1
- package/dist/templates/mdx/fonts.mdx.js +2 -2
- package/dist/templates/mdx/footer-links.mdx.d.ts +1 -1
- package/dist/templates/mdx/footer-links.mdx.js +1 -1
- package/dist/templates/mdx/frames.mdx.d.ts +1 -1
- package/dist/templates/mdx/frames.mdx.js +1 -1
- package/dist/templates/mdx/globals.mdx.d.ts +1 -1
- package/dist/templates/mdx/globals.mdx.js +1 -1
- package/dist/templates/mdx/icons.mdx.d.ts +1 -1
- package/dist/templates/mdx/icons.mdx.js +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.d.ts +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.js +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.d.ts +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.js +2 -2
- package/dist/templates/mdx/mermaid.mdx.d.ts +1 -1
- package/dist/templates/mdx/mermaid.mdx.js +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.d.ts +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.js +2 -2
- package/dist/templates/mdx/navigation.mdx.d.ts +1 -1
- package/dist/templates/mdx/navigation.mdx.js +22 -11
- package/dist/templates/mdx/platform/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/ai-assistant.mdx.js +4 -3
- package/dist/templates/mdx/platform/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/analytics.mdx.js +2 -2
- package/dist/templates/mdx/platform/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/api-playground.mdx.js +62 -0
- package/dist/templates/mdx/platform/billing.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/billing.mdx.js +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.js +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.js +12 -2
- package/dist/templates/mdx/platform/custom-domains.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/custom-domains.mdx.js +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/footer-links.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/footer-links.mdx.js +47 -0
- package/dist/templates/mdx/platform/import-from-github.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/import-from-github.mdx.js +91 -0
- package/dist/templates/mdx/platform/mcp.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/mcp.mdx.js +3 -3
- package/dist/templates/mdx/platform/project-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/project-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/self-updating-docs.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/self-updating-docs.mdx.js +113 -0
- package/dist/templates/mdx/platform/team-members.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/team-members.mdx.js +1 -1
- package/dist/templates/mdx/sections.mdx.d.ts +1 -1
- package/dist/templates/mdx/sections.mdx.js +1 -1
- package/dist/templates/mdx/steps.mdx.d.ts +1 -1
- package/dist/templates/mdx/steps.mdx.js +1 -1
- package/dist/templates/mdx/tabs.mdx.d.ts +1 -1
- package/dist/templates/mdx/tabs.mdx.js +1 -1
- package/dist/templates/mdx/theme.mdx.d.ts +1 -1
- package/dist/templates/mdx/theme.mdx.js +1 -1
- package/dist/templates/mdx/update.mdx.d.ts +1 -1
- package/dist/templates/mdx/update.mdx.js +1 -1
- package/dist/templates/mdx/what-is-doccupine.mdx.d.ts +1 -1
- package/dist/templates/mdx/what-is-doccupine.mdx.js +1 -1
- package/dist/templates/package.js +6 -6
- package/dist/templates/services/openapi/playgroundAllowlistStub.d.ts +1 -0
- package/dist/templates/services/openapi/playgroundAllowlistStub.js +4 -0
- package/dist/templates/types/openapi.d.ts +1 -0
- package/dist/templates/types/openapi.js +93 -0
- package/dist/templates/utils/apiAnchors.d.ts +1 -0
- package/dist/templates/utils/apiAnchors.js +39 -0
- package/dist/templates/utils/apiSnippets.d.ts +1 -0
- package/dist/templates/utils/apiSnippets.js +94 -0
- package/dist/templates/utils/orderNavItems.d.ts +1 -1
- package/dist/templates/utils/orderNavItems.js +3 -0
- package/dist/templates/utils/playgroundAllowlist.d.ts +1 -0
- package/dist/templates/utils/playgroundAllowlist.js +70 -0
- package/dist/templates/utils/ssrfGuard.d.ts +1 -0
- package/dist/templates/utils/ssrfGuard.js +217 -0
- package/package.json +3 -2
|
@@ -0,0 +1,1083 @@
|
|
|
1
|
+
export const apiPlaygroundTemplate = `"use client";
|
|
2
|
+
import React, { useCallback, useMemo, useRef, useState } from "react";
|
|
3
|
+
import styled, { css } from "styled-components";
|
|
4
|
+
import {
|
|
5
|
+
styledSmall,
|
|
6
|
+
interactiveStyles,
|
|
7
|
+
Input,
|
|
8
|
+
Textarea,
|
|
9
|
+
Modal,
|
|
10
|
+
} from "cherry-styled-components";
|
|
11
|
+
import { mq, Theme } from "@/app/theme";
|
|
12
|
+
import { Code, CodeTabs } from "@/components/layout/Code";
|
|
13
|
+
import { Icon } from "@/components/layout/Icon";
|
|
14
|
+
import { Accordion } from "@/components/layout/Accordion";
|
|
15
|
+
import { CopyButton } from "@/components/layout/CopyButton";
|
|
16
|
+
import { Spinner } from "@/components/Spinner";
|
|
17
|
+
import { matchAllowlist, matchAllowlistIn } from "@/utils/playgroundAllowlist";
|
|
18
|
+
import type { AllowlistEntry } from "@/utils/playgroundAllowlist";
|
|
19
|
+
import { buildSnippets } from "@/utils/apiSnippets";
|
|
20
|
+
import type { SnippetLanguage } from "@/utils/apiSnippets";
|
|
21
|
+
import type {
|
|
22
|
+
OperationDescriptor,
|
|
23
|
+
ParameterDescriptor,
|
|
24
|
+
ServerDescriptor,
|
|
25
|
+
} from "@/types/openapi";
|
|
26
|
+
|
|
27
|
+
interface PlaygroundResponse {
|
|
28
|
+
status: number;
|
|
29
|
+
statusText: string;
|
|
30
|
+
headers: Record<string, string>;
|
|
31
|
+
contentType: string | null;
|
|
32
|
+
bodyEncoding: "utf8" | "base64";
|
|
33
|
+
body: string;
|
|
34
|
+
truncated: boolean;
|
|
35
|
+
byteLength: number;
|
|
36
|
+
durationMs: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface BuiltRequest {
|
|
40
|
+
url: string;
|
|
41
|
+
headers: Record<string, string>;
|
|
42
|
+
body: string | null;
|
|
43
|
+
secretHeaderNames: string[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ApiPlaygroundProps {
|
|
47
|
+
operation: OperationDescriptor;
|
|
48
|
+
defaultMode?: "proxy" | "direct";
|
|
49
|
+
allowDirect?: boolean;
|
|
50
|
+
// Extra client-side allowlist entries. The server proxy always enforces the
|
|
51
|
+
// build-time allowlist regardless; this only relaxes the client pre-flight
|
|
52
|
+
// (used by the docs demo to run in direct mode without a configured spec).
|
|
53
|
+
allowlist?: AllowlistEntry[];
|
|
54
|
+
snippetLanguages?: SnippetLanguage[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const METHOD_TOKENS: Record<string, keyof Theme["colors"]> = {
|
|
58
|
+
get: "info",
|
|
59
|
+
post: "success",
|
|
60
|
+
put: "warning",
|
|
61
|
+
patch: "warning",
|
|
62
|
+
delete: "error",
|
|
63
|
+
head: "gray",
|
|
64
|
+
options: "gray",
|
|
65
|
+
trace: "gray",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function methodColor(theme: Theme, method: string): string {
|
|
69
|
+
const token = METHOD_TOKENS[method.toLowerCase()] ?? "gray";
|
|
70
|
+
return theme.colors[token];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function schemaTypeLabel(schema: unknown): string {
|
|
74
|
+
if (schema && typeof schema === "object") {
|
|
75
|
+
const record = schema as { type?: unknown; format?: unknown };
|
|
76
|
+
if (typeof record.type === "string") {
|
|
77
|
+
return typeof record.format === "string"
|
|
78
|
+
? record.type + " (" + record.format + ")"
|
|
79
|
+
: record.type;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return "string";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function serverBase(server: ServerDescriptor | undefined): string {
|
|
86
|
+
if (!server) return "";
|
|
87
|
+
let url = server.url;
|
|
88
|
+
const variables = server.variables;
|
|
89
|
+
if (variables) {
|
|
90
|
+
for (const key of Object.keys(variables)) {
|
|
91
|
+
url = url.split("{" + key + "}").join(variables[key].default);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
while (url.endsWith("/")) url = url.slice(0, -1);
|
|
95
|
+
return url;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function applyPathParams(
|
|
99
|
+
template: string,
|
|
100
|
+
values: Record<string, string>,
|
|
101
|
+
): string {
|
|
102
|
+
let result = template;
|
|
103
|
+
for (const key of Object.keys(values)) {
|
|
104
|
+
result = result
|
|
105
|
+
.split("{" + key + "}")
|
|
106
|
+
.join(encodeURIComponent(values[key]));
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function firstJsonExample(op: OperationDescriptor): {
|
|
112
|
+
contentType: string;
|
|
113
|
+
text: string;
|
|
114
|
+
} | null {
|
|
115
|
+
if (!op.requestBody || op.requestBody.content.length === 0) return null;
|
|
116
|
+
const media =
|
|
117
|
+
op.requestBody.content.find((m) => m.contentType.includes("json")) ??
|
|
118
|
+
op.requestBody.content[0];
|
|
119
|
+
let text = "";
|
|
120
|
+
if (media.example !== undefined) {
|
|
121
|
+
try {
|
|
122
|
+
text = JSON.stringify(media.example, null, 2);
|
|
123
|
+
} catch {
|
|
124
|
+
text = "";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return { contentType: media.contentType, text };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function classifyResponse(
|
|
131
|
+
contentType: string | null,
|
|
132
|
+
): "image" | "video" | "audio" | "text" | "binary" {
|
|
133
|
+
if (!contentType) return "text";
|
|
134
|
+
const t = contentType.toLowerCase();
|
|
135
|
+
if (t.startsWith("image/") && !t.includes("svg")) return "image";
|
|
136
|
+
if (t.startsWith("video/")) return "video";
|
|
137
|
+
if (t.startsWith("audio/")) return "audio";
|
|
138
|
+
if (
|
|
139
|
+
t.includes("json") ||
|
|
140
|
+
t.startsWith("text/") ||
|
|
141
|
+
t.includes("xml") ||
|
|
142
|
+
t.includes("svg") ||
|
|
143
|
+
t.includes("html") ||
|
|
144
|
+
t.includes("javascript") ||
|
|
145
|
+
t.includes("csv")
|
|
146
|
+
) {
|
|
147
|
+
return "text";
|
|
148
|
+
}
|
|
149
|
+
return "binary";
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function codeLanguage(contentType: string | null): string {
|
|
153
|
+
if (!contentType) return "text";
|
|
154
|
+
const t = contentType.toLowerCase();
|
|
155
|
+
if (t.includes("json")) return "json";
|
|
156
|
+
if (t.includes("html")) return "html";
|
|
157
|
+
if (t.includes("xml") || t.includes("svg")) return "xml";
|
|
158
|
+
if (t.includes("javascript")) return "javascript";
|
|
159
|
+
return "text";
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function prettyBody(contentType: string | null, body: string): string {
|
|
163
|
+
if (contentType && contentType.toLowerCase().includes("json")) {
|
|
164
|
+
try {
|
|
165
|
+
return JSON.stringify(JSON.parse(body), null, 2);
|
|
166
|
+
} catch {
|
|
167
|
+
return body;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return body;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function bufferToBase64(buffer: ArrayBuffer): string {
|
|
174
|
+
let binary = "";
|
|
175
|
+
const bytes = new Uint8Array(buffer);
|
|
176
|
+
const chunk = 0x8000;
|
|
177
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
178
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
179
|
+
}
|
|
180
|
+
return btoa(binary);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function renderPath(path: string): React.ReactNode {
|
|
184
|
+
return path
|
|
185
|
+
.split(/(\\{[^}]+\\})/g)
|
|
186
|
+
.map((part, index) =>
|
|
187
|
+
part.startsWith("{") ? (
|
|
188
|
+
<PathParam key={index}>{part}</PathParam>
|
|
189
|
+
) : (
|
|
190
|
+
<span key={index}>{part}</span>
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const LOCATIONS: ParameterDescriptor["in"][] = [
|
|
196
|
+
"path",
|
|
197
|
+
"query",
|
|
198
|
+
"header",
|
|
199
|
+
"cookie",
|
|
200
|
+
];
|
|
201
|
+
|
|
202
|
+
const LOCATION_TITLES: Record<string, string> = {
|
|
203
|
+
path: "Path",
|
|
204
|
+
query: "Query",
|
|
205
|
+
header: "Headers",
|
|
206
|
+
cookie: "Cookies",
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/* ---------- compact bar ---------- */
|
|
210
|
+
|
|
211
|
+
const CompactBar = styled.div\`
|
|
212
|
+
display: flex;
|
|
213
|
+
align-items: center;
|
|
214
|
+
gap: 10px;
|
|
215
|
+
margin: 0;
|
|
216
|
+
padding: 8px 8px 8px 12px;
|
|
217
|
+
border: solid 1px \${({ theme }: { theme: Theme }) => theme.colors.grayLight};
|
|
218
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.lg};
|
|
219
|
+
background: \${({ theme }: { theme: Theme }) => theme.colors.light};
|
|
220
|
+
\`;
|
|
221
|
+
|
|
222
|
+
const MethodBadge = styled.span<{ theme: Theme; $method: string }>\`
|
|
223
|
+
font-family: \${({ theme }) => theme.fonts.mono};
|
|
224
|
+
font-weight: 700;
|
|
225
|
+
font-size: 12px;
|
|
226
|
+
letter-spacing: 0.04em;
|
|
227
|
+
text-transform: uppercase;
|
|
228
|
+
color: \${({ theme }) => theme.colors.surface};
|
|
229
|
+
background: \${({ theme, $method }) => methodColor(theme, $method)};
|
|
230
|
+
padding: 0 4px;
|
|
231
|
+
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
232
|
+
flex-shrink: 0;
|
|
233
|
+
\`;
|
|
234
|
+
|
|
235
|
+
const PathText = styled.span\`
|
|
236
|
+
flex: 1;
|
|
237
|
+
min-width: 0;
|
|
238
|
+
font-family: \${({ theme }: { theme: Theme }) => theme.fonts.mono};
|
|
239
|
+
font-weight: 600;
|
|
240
|
+
font-size: 14px;
|
|
241
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.dark};
|
|
242
|
+
overflow-x: auto;
|
|
243
|
+
white-space: nowrap;
|
|
244
|
+
&::-webkit-scrollbar {
|
|
245
|
+
display: none;
|
|
246
|
+
}
|
|
247
|
+
\`;
|
|
248
|
+
|
|
249
|
+
const PathParam = styled.span\`
|
|
250
|
+
font-family: \${({ theme }: { theme: Theme }) => theme.fonts.mono};
|
|
251
|
+
background: \${({ theme }: { theme: Theme }) =>
|
|
252
|
+
\`color-mix(in srgb, \${theme.colors.primaryLight} 20%, transparent)\`};
|
|
253
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.dark};
|
|
254
|
+
padding: 2px 4px;
|
|
255
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.xs};
|
|
256
|
+
overflow-wrap: anywhere;
|
|
257
|
+
\`;
|
|
258
|
+
|
|
259
|
+
const AccentButton = styled.button<{ theme: Theme }>\`
|
|
260
|
+
cursor: pointer;
|
|
261
|
+
display: inline-flex;
|
|
262
|
+
align-items: center;
|
|
263
|
+
gap: 6px;
|
|
264
|
+
flex-shrink: 0;
|
|
265
|
+
font-family: \${({ theme }: { theme: Theme }) => theme.fonts.text};
|
|
266
|
+
font-weight: 600;
|
|
267
|
+
font-size: 14px;
|
|
268
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.surface};
|
|
269
|
+
background: \${({ theme }: { theme: Theme }) => theme.colors.primary};
|
|
270
|
+
border: none;
|
|
271
|
+
padding: 8px 14px;
|
|
272
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.xs};
|
|
273
|
+
\${interactiveStyles};
|
|
274
|
+
|
|
275
|
+
& svg {
|
|
276
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.surface};
|
|
277
|
+
stroke: \${({ theme }: { theme: Theme }) => theme.colors.surface};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
&:hover {
|
|
281
|
+
background: \${({ theme }: { theme: Theme }) => theme.colors.primaryDark};
|
|
282
|
+
}
|
|
283
|
+
&:disabled {
|
|
284
|
+
opacity: 0.6;
|
|
285
|
+
cursor: default;
|
|
286
|
+
}
|
|
287
|
+
\`;
|
|
288
|
+
|
|
289
|
+
/* ---------- modal ---------- */
|
|
290
|
+
|
|
291
|
+
const DialogHeader = styled.div\`
|
|
292
|
+
display: flex;
|
|
293
|
+
align-items: center;
|
|
294
|
+
gap: 10px;
|
|
295
|
+
flex-wrap: wrap;
|
|
296
|
+
margin: 5px 0 0 0;
|
|
297
|
+
\`;
|
|
298
|
+
|
|
299
|
+
const PathBar = styled.div\`
|
|
300
|
+
flex: 1;
|
|
301
|
+
min-width: 200px;
|
|
302
|
+
display: flex;
|
|
303
|
+
align-items: center;
|
|
304
|
+
gap: 8px;
|
|
305
|
+
padding: 8px 8px 8px 12px;
|
|
306
|
+
border: solid 1px \${({ theme }: { theme: Theme }) => theme.colors.grayLight};
|
|
307
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.xs};
|
|
308
|
+
background: \${({ theme }: { theme: Theme }) => theme.colors.light};
|
|
309
|
+
\`;
|
|
310
|
+
|
|
311
|
+
const DialogBody = styled.div\`
|
|
312
|
+
display: grid;
|
|
313
|
+
grid-template-columns: 1fr;
|
|
314
|
+
gap: 20px;
|
|
315
|
+
padding: 0;
|
|
316
|
+
margin: 20px 0 0 0;
|
|
317
|
+
|
|
318
|
+
\${mq("lg")} {
|
|
319
|
+
grid-template-columns: 1fr 1fr;
|
|
320
|
+
}
|
|
321
|
+
\`;
|
|
322
|
+
|
|
323
|
+
const Column = styled.div\`
|
|
324
|
+
display: flex;
|
|
325
|
+
flex-direction: column;
|
|
326
|
+
gap: 20px;
|
|
327
|
+
min-width: 0;
|
|
328
|
+
\`;
|
|
329
|
+
|
|
330
|
+
const Description = styled.p\`
|
|
331
|
+
margin: 0;
|
|
332
|
+
\${({ theme }: { theme: Theme }) => styledSmall(theme)};
|
|
333
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.grayDark};
|
|
334
|
+
\`;
|
|
335
|
+
|
|
336
|
+
const Toolbar = styled.div\`
|
|
337
|
+
display: flex;
|
|
338
|
+
align-items: center;
|
|
339
|
+
gap: 8px;
|
|
340
|
+
flex-wrap: wrap;
|
|
341
|
+
\`;
|
|
342
|
+
|
|
343
|
+
const Toggle = styled.button<{ theme: Theme; $active?: boolean }>\`
|
|
344
|
+
position: relative;
|
|
345
|
+
cursor: pointer;
|
|
346
|
+
text-decoration: none;
|
|
347
|
+
font-size: \${({ theme }) => theme.fontSizes.small.lg};
|
|
348
|
+
line-height: 1;
|
|
349
|
+
color: \${({ theme }) => theme.colors.accent};
|
|
350
|
+
display: flex;
|
|
351
|
+
gap: 6px;
|
|
352
|
+
transition: all 0.3s ease;
|
|
353
|
+
font-weight: 600;
|
|
354
|
+
white-space: nowrap;
|
|
355
|
+
min-width: fit-content;
|
|
356
|
+
background: \${({ theme }) =>
|
|
357
|
+
\`color-mix(in srgb, \${theme.colors.primaryLight} 10%, transparent)\`};
|
|
358
|
+
padding: 6px 8px;
|
|
359
|
+
border: none;
|
|
360
|
+
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
361
|
+
\${interactiveStyles};
|
|
362
|
+
|
|
363
|
+
\${({ $active, theme }) =>
|
|
364
|
+
$active &&
|
|
365
|
+
css\`
|
|
366
|
+
background: \${theme.colors.primary};
|
|
367
|
+
color: \${theme.colors.surface};
|
|
368
|
+
\`}
|
|
369
|
+
\`;
|
|
370
|
+
|
|
371
|
+
/* ---------- collapsible card ---------- */
|
|
372
|
+
|
|
373
|
+
const Card = styled.div\`
|
|
374
|
+
border: solid 1px \${({ theme }: { theme: Theme }) => theme.colors.grayLight};
|
|
375
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.lg};
|
|
376
|
+
overflow: hidden;
|
|
377
|
+
\`;
|
|
378
|
+
|
|
379
|
+
const CardHead = styled.button\`
|
|
380
|
+
cursor: pointer;
|
|
381
|
+
width: 100%;
|
|
382
|
+
display: flex;
|
|
383
|
+
align-items: center;
|
|
384
|
+
justify-content: space-between;
|
|
385
|
+
gap: 8px;
|
|
386
|
+
padding: 12px 20px;
|
|
387
|
+
border: none;
|
|
388
|
+
background: transparent;
|
|
389
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.dark};
|
|
390
|
+
\${({ theme }: { theme: Theme }) => styledSmall(theme)};
|
|
391
|
+
\`;
|
|
392
|
+
|
|
393
|
+
const Inline = styled.span\`
|
|
394
|
+
display: inline-flex;
|
|
395
|
+
align-items: center;
|
|
396
|
+
gap: 8px;
|
|
397
|
+
\`;
|
|
398
|
+
|
|
399
|
+
const CardBody = styled.div\`
|
|
400
|
+
padding: 0 20px 20px;
|
|
401
|
+
display: flex;
|
|
402
|
+
flex-direction: column;
|
|
403
|
+
gap: 16px;
|
|
404
|
+
\`;
|
|
405
|
+
|
|
406
|
+
/* ---------- fields ---------- */
|
|
407
|
+
|
|
408
|
+
// Wraps an accordion section's content and insets it, so full-width inputs'
|
|
409
|
+
// focus rings (a 4px box-shadow) are not clipped by the accordion's own
|
|
410
|
+
// overflow. Keeps the accordion component untouched.
|
|
411
|
+
const SectionInner = styled.div\`
|
|
412
|
+
display: flex;
|
|
413
|
+
flex-direction: column;
|
|
414
|
+
gap: 16px;
|
|
415
|
+
padding: 4px;
|
|
416
|
+
\`;
|
|
417
|
+
|
|
418
|
+
const Field = styled.div\`
|
|
419
|
+
display: flex;
|
|
420
|
+
flex-direction: column;
|
|
421
|
+
gap: 6px;
|
|
422
|
+
\`;
|
|
423
|
+
|
|
424
|
+
const FieldHead = styled.div\`
|
|
425
|
+
display: flex;
|
|
426
|
+
align-items: center;
|
|
427
|
+
gap: 8px;
|
|
428
|
+
flex-wrap: wrap;
|
|
429
|
+
\${({ theme }: { theme: Theme }) => styledSmall(theme)};
|
|
430
|
+
\`;
|
|
431
|
+
|
|
432
|
+
const FieldName = styled.span\`
|
|
433
|
+
font-family: \${({ theme }: { theme: Theme }) => theme.fonts.mono};
|
|
434
|
+
font-weight: 600;
|
|
435
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.dark};
|
|
436
|
+
\`;
|
|
437
|
+
|
|
438
|
+
const TypeChip = styled.span\`
|
|
439
|
+
font-family: \${({ theme }: { theme: Theme }) => theme.fonts.mono};
|
|
440
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.grayDark};
|
|
441
|
+
background: \${({ theme }: { theme: Theme }) =>
|
|
442
|
+
\`color-mix(in srgb, \${theme.colors.gray} 15%, transparent)\`};
|
|
443
|
+
padding: 0 4px;
|
|
444
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.xs};
|
|
445
|
+
\`;
|
|
446
|
+
|
|
447
|
+
const RequiredChip = styled.span\`
|
|
448
|
+
font-family: \${({ theme }: { theme: Theme }) => theme.fonts.mono};
|
|
449
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.error};
|
|
450
|
+
\`;
|
|
451
|
+
|
|
452
|
+
const FieldDesc = styled.p\`
|
|
453
|
+
margin: 0;
|
|
454
|
+
width: 100%;
|
|
455
|
+
\${({ theme }: { theme: Theme }) => styledSmall(theme)};
|
|
456
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.gray};
|
|
457
|
+
\`;
|
|
458
|
+
|
|
459
|
+
/* ---------- response ---------- */
|
|
460
|
+
|
|
461
|
+
const StatusPill = styled.span<{ theme: Theme; $ok: boolean }>\`
|
|
462
|
+
font-family: \${({ theme }) => theme.fonts.mono};
|
|
463
|
+
font-weight: 700;
|
|
464
|
+
color: \${({ theme }) => theme.colors.surface};
|
|
465
|
+
background: \${({ theme, $ok }) =>
|
|
466
|
+
$ok ? theme.colors.success : theme.colors.error};
|
|
467
|
+
padding: 0 4px;
|
|
468
|
+
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
469
|
+
\`;
|
|
470
|
+
|
|
471
|
+
const Meta = styled.span\`
|
|
472
|
+
\${({ theme }: { theme: Theme }) => styledSmall(theme)};
|
|
473
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.gray};
|
|
474
|
+
\`;
|
|
475
|
+
|
|
476
|
+
const ErrorText = styled.p\`
|
|
477
|
+
margin: 0;
|
|
478
|
+
\${({ theme }: { theme: Theme }) => styledSmall(theme)};
|
|
479
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.error};
|
|
480
|
+
\`;
|
|
481
|
+
|
|
482
|
+
const Placeholder = styled.div\`
|
|
483
|
+
border: dashed 1px \${({ theme }: { theme: Theme }) => theme.colors.grayLight};
|
|
484
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.lg};
|
|
485
|
+
padding: 24px;
|
|
486
|
+
min-height: 120px;
|
|
487
|
+
display: flex;
|
|
488
|
+
align-items: center;
|
|
489
|
+
justify-content: center;
|
|
490
|
+
gap: 10px;
|
|
491
|
+
text-align: center;
|
|
492
|
+
\${({ theme }: { theme: Theme }) => styledSmall(theme)};
|
|
493
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.gray};
|
|
494
|
+
\`;
|
|
495
|
+
|
|
496
|
+
// Holds the response (or empty placeholder) and lets the loading spinner sit on
|
|
497
|
+
// top of it, so a re-send keeps the previous content in place instead of
|
|
498
|
+
// collapsing and jumping the layout.
|
|
499
|
+
const ResponseArea = styled.div\`
|
|
500
|
+
position: relative;
|
|
501
|
+
\`;
|
|
502
|
+
|
|
503
|
+
const LoadingOverlay = styled.div\`
|
|
504
|
+
position: absolute;
|
|
505
|
+
inset: 0;
|
|
506
|
+
z-index: 1;
|
|
507
|
+
display: flex;
|
|
508
|
+
align-items: center;
|
|
509
|
+
justify-content: center;
|
|
510
|
+
border: solid 1px \${({ theme }: { theme: Theme }) => theme.colors.grayLight};
|
|
511
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.lg};
|
|
512
|
+
color: \${({ theme }: { theme: Theme }) => theme.colors.grayDark};
|
|
513
|
+
background: \${({ theme }: { theme: Theme }) =>
|
|
514
|
+
\`color-mix(in srgb, \${theme.colors.light} 60%, transparent)\`};
|
|
515
|
+
backdrop-filter: blur(1px);
|
|
516
|
+
\`;
|
|
517
|
+
|
|
518
|
+
const ResponseMedia = styled.img\`
|
|
519
|
+
max-width: 100%;
|
|
520
|
+
border-radius: \${({ theme }: { theme: Theme }) => theme.spacing.radius.xs};
|
|
521
|
+
\`;
|
|
522
|
+
|
|
523
|
+
export function ApiPlayground({
|
|
524
|
+
operation,
|
|
525
|
+
defaultMode = "proxy",
|
|
526
|
+
allowDirect = true,
|
|
527
|
+
allowlist,
|
|
528
|
+
snippetLanguages = ["curl", "javascript", "python"],
|
|
529
|
+
}: ApiPlaygroundProps) {
|
|
530
|
+
const [open, setOpen] = useState(false);
|
|
531
|
+
const [paramValues, setParamValues] = useState<Record<string, string>>({});
|
|
532
|
+
const [authValues, setAuthValues] = useState<Record<string, string>>({});
|
|
533
|
+
const initialBody = useMemo(() => firstJsonExample(operation), [operation]);
|
|
534
|
+
const [bodyText, setBodyText] = useState(initialBody?.text ?? "");
|
|
535
|
+
const [mode, setMode] = useState<"proxy" | "direct">(defaultMode);
|
|
536
|
+
const [showSecrets, setShowSecrets] = useState(false);
|
|
537
|
+
const [loading, setLoading] = useState(false);
|
|
538
|
+
const [response, setResponse] = useState<PlaygroundResponse | null>(null);
|
|
539
|
+
const [error, setError] = useState<string | null>(null);
|
|
540
|
+
const [corsRetry, setCorsRetry] = useState(false);
|
|
541
|
+
const abortRef = useRef<AbortController | null>(null);
|
|
542
|
+
|
|
543
|
+
const paramKey = (p: ParameterDescriptor) => p.in + ":" + p.name;
|
|
544
|
+
|
|
545
|
+
const isAllowed = useCallback(
|
|
546
|
+
(url: string) =>
|
|
547
|
+
Boolean(
|
|
548
|
+
matchAllowlist(url) ??
|
|
549
|
+
(allowlist ? matchAllowlistIn(allowlist, url) : null),
|
|
550
|
+
),
|
|
551
|
+
[allowlist],
|
|
552
|
+
);
|
|
553
|
+
|
|
554
|
+
const buildRequest = useCallback((): BuiltRequest => {
|
|
555
|
+
const base = serverBase(operation.servers[0]);
|
|
556
|
+
const pathValues: Record<string, string> = {};
|
|
557
|
+
const queryParts: string[] = [];
|
|
558
|
+
const headers: Record<string, string> = {};
|
|
559
|
+
const secretHeaderNames: string[] = [];
|
|
560
|
+
let cookie = "";
|
|
561
|
+
|
|
562
|
+
for (const param of operation.parameters) {
|
|
563
|
+
const value = paramValues[paramKey(param)];
|
|
564
|
+
if (value === undefined || value === "") continue;
|
|
565
|
+
if (param.in === "path") pathValues[param.name] = value;
|
|
566
|
+
else if (param.in === "query")
|
|
567
|
+
queryParts.push(
|
|
568
|
+
encodeURIComponent(param.name) + "=" + encodeURIComponent(value),
|
|
569
|
+
);
|
|
570
|
+
else if (param.in === "header") headers[param.name] = value;
|
|
571
|
+
else if (param.in === "cookie")
|
|
572
|
+
cookie += (cookie ? "; " : "") + param.name + "=" + value;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
for (const requirement of operation.security) {
|
|
576
|
+
const scheme = requirement.scheme;
|
|
577
|
+
if (!scheme) continue;
|
|
578
|
+
if (scheme.type === "apiKey" && scheme.name) {
|
|
579
|
+
const value = authValues[requirement.schemeName];
|
|
580
|
+
if (!value) continue;
|
|
581
|
+
if (scheme.in === "header") {
|
|
582
|
+
headers[scheme.name] = value;
|
|
583
|
+
secretHeaderNames.push(scheme.name);
|
|
584
|
+
} else if (scheme.in === "query") {
|
|
585
|
+
queryParts.push(
|
|
586
|
+
encodeURIComponent(scheme.name) + "=" + encodeURIComponent(value),
|
|
587
|
+
);
|
|
588
|
+
} else if (scheme.in === "cookie") {
|
|
589
|
+
cookie += (cookie ? "; " : "") + scheme.name + "=" + value;
|
|
590
|
+
}
|
|
591
|
+
} else if (scheme.type === "http" && scheme.scheme === "basic") {
|
|
592
|
+
const user = authValues[requirement.schemeName + ":user"] ?? "";
|
|
593
|
+
const pass = authValues[requirement.schemeName + ":pass"] ?? "";
|
|
594
|
+
if (user || pass) {
|
|
595
|
+
headers["Authorization"] = "Basic " + btoa(user + ":" + pass);
|
|
596
|
+
secretHeaderNames.push("Authorization");
|
|
597
|
+
}
|
|
598
|
+
} else {
|
|
599
|
+
const token = authValues[requirement.schemeName];
|
|
600
|
+
if (token) {
|
|
601
|
+
headers["Authorization"] = "Bearer " + token;
|
|
602
|
+
secretHeaderNames.push("Authorization");
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (cookie) headers["Cookie"] = cookie;
|
|
608
|
+
|
|
609
|
+
const hasBody =
|
|
610
|
+
operation.requestBody !== undefined &&
|
|
611
|
+
operation.method !== "get" &&
|
|
612
|
+
operation.method !== "head" &&
|
|
613
|
+
bodyText.trim() !== "";
|
|
614
|
+
if (hasBody && !headers["Content-Type"] && initialBody) {
|
|
615
|
+
headers["Content-Type"] = initialBody.contentType;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
const query = queryParts.join("&");
|
|
619
|
+
const url =
|
|
620
|
+
base +
|
|
621
|
+
applyPathParams(operation.path, pathValues) +
|
|
622
|
+
(query ? "?" + query : "");
|
|
623
|
+
|
|
624
|
+
return { url, headers, body: hasBody ? bodyText : null, secretHeaderNames };
|
|
625
|
+
}, [operation, paramValues, authValues, bodyText, initialBody]);
|
|
626
|
+
|
|
627
|
+
const snippets = useMemo(() => {
|
|
628
|
+
const built = buildRequest();
|
|
629
|
+
const headers: Record<string, string> = { ...built.headers };
|
|
630
|
+
if (!showSecrets) {
|
|
631
|
+
for (const name of built.secretHeaderNames) {
|
|
632
|
+
headers[name] = "<YOUR_CREDENTIALS>";
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
return buildSnippets(
|
|
636
|
+
{
|
|
637
|
+
method: operation.method.toUpperCase(),
|
|
638
|
+
url: built.url || operation.path,
|
|
639
|
+
headers,
|
|
640
|
+
body: built.body,
|
|
641
|
+
},
|
|
642
|
+
snippetLanguages,
|
|
643
|
+
);
|
|
644
|
+
}, [buildRequest, showSecrets, operation, snippetLanguages]);
|
|
645
|
+
|
|
646
|
+
const sendProxy = useCallback(
|
|
647
|
+
async (
|
|
648
|
+
built: BuiltRequest,
|
|
649
|
+
signal: AbortSignal,
|
|
650
|
+
): Promise<PlaygroundResponse> => {
|
|
651
|
+
const res = await fetch("/api/playground", {
|
|
652
|
+
method: "POST",
|
|
653
|
+
headers: { "Content-Type": "application/json" },
|
|
654
|
+
signal,
|
|
655
|
+
body: JSON.stringify({
|
|
656
|
+
targetUrl: built.url,
|
|
657
|
+
method: operation.method.toUpperCase(),
|
|
658
|
+
headers: built.headers,
|
|
659
|
+
body: built.body ?? undefined,
|
|
660
|
+
bodyEncoding: "utf8",
|
|
661
|
+
}),
|
|
662
|
+
});
|
|
663
|
+
const data = await res.json();
|
|
664
|
+
if (!res.ok) {
|
|
665
|
+
throw new Error(
|
|
666
|
+
typeof data?.error === "string" ? data.error : "Request failed",
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
return data as PlaygroundResponse;
|
|
670
|
+
},
|
|
671
|
+
[operation],
|
|
672
|
+
);
|
|
673
|
+
|
|
674
|
+
const sendDirect = useCallback(
|
|
675
|
+
async (
|
|
676
|
+
built: BuiltRequest,
|
|
677
|
+
signal: AbortSignal,
|
|
678
|
+
): Promise<PlaygroundResponse> => {
|
|
679
|
+
const start = Date.now();
|
|
680
|
+
const res = await fetch(built.url, {
|
|
681
|
+
method: operation.method.toUpperCase(),
|
|
682
|
+
headers: built.headers,
|
|
683
|
+
body: built.body ?? undefined,
|
|
684
|
+
signal,
|
|
685
|
+
});
|
|
686
|
+
const buffer = await res.arrayBuffer();
|
|
687
|
+
const contentType = res.headers.get("content-type");
|
|
688
|
+
const isText = classifyResponse(contentType) === "text";
|
|
689
|
+
const headers: Record<string, string> = {};
|
|
690
|
+
res.headers.forEach((value, key) => {
|
|
691
|
+
headers[key] = value;
|
|
692
|
+
});
|
|
693
|
+
return {
|
|
694
|
+
status: res.status,
|
|
695
|
+
statusText: res.statusText,
|
|
696
|
+
headers,
|
|
697
|
+
contentType,
|
|
698
|
+
bodyEncoding: isText ? "utf8" : "base64",
|
|
699
|
+
body: isText
|
|
700
|
+
? new TextDecoder().decode(buffer)
|
|
701
|
+
: bufferToBase64(buffer),
|
|
702
|
+
truncated: false,
|
|
703
|
+
byteLength: buffer.byteLength,
|
|
704
|
+
durationMs: Date.now() - start,
|
|
705
|
+
};
|
|
706
|
+
},
|
|
707
|
+
[operation],
|
|
708
|
+
);
|
|
709
|
+
|
|
710
|
+
const send = useCallback(async () => {
|
|
711
|
+
setError(null);
|
|
712
|
+
setCorsRetry(false);
|
|
713
|
+
// Keep any previous response on screen; the loading overlay covers it so
|
|
714
|
+
// the layout does not collapse and jump while the new request is in flight.
|
|
715
|
+
|
|
716
|
+
const built = buildRequest();
|
|
717
|
+
if (!built.url) {
|
|
718
|
+
setError("No server URL is configured for this endpoint.");
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
if (!isAllowed(built.url)) {
|
|
722
|
+
setError("This endpoint's server is not in the allowed list.");
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
if (
|
|
726
|
+
mode === "direct" &&
|
|
727
|
+
typeof window !== "undefined" &&
|
|
728
|
+
window.location.protocol === "https:" &&
|
|
729
|
+
built.url.startsWith("http:")
|
|
730
|
+
) {
|
|
731
|
+
setError(
|
|
732
|
+
"Browsers block HTTP requests from an HTTPS page. Use proxy mode.",
|
|
733
|
+
);
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const controller = new AbortController();
|
|
738
|
+
abortRef.current = controller;
|
|
739
|
+
setLoading(true);
|
|
740
|
+
try {
|
|
741
|
+
const result =
|
|
742
|
+
mode === "direct"
|
|
743
|
+
? await sendDirect(built, controller.signal)
|
|
744
|
+
: await sendProxy(built, controller.signal);
|
|
745
|
+
setResponse(result);
|
|
746
|
+
} catch (err) {
|
|
747
|
+
if (controller.signal.aborted) return;
|
|
748
|
+
if (mode === "direct" && err instanceof TypeError) {
|
|
749
|
+
setCorsRetry(true);
|
|
750
|
+
setError(
|
|
751
|
+
"The browser blocked this cross-origin request (CORS). Run it server-side instead.",
|
|
752
|
+
);
|
|
753
|
+
} else {
|
|
754
|
+
setError(err instanceof Error ? err.message : "Request failed");
|
|
755
|
+
}
|
|
756
|
+
} finally {
|
|
757
|
+
setLoading(false);
|
|
758
|
+
abortRef.current = null;
|
|
759
|
+
}
|
|
760
|
+
}, [buildRequest, isAllowed, mode, sendDirect, sendProxy]);
|
|
761
|
+
|
|
762
|
+
const retryViaProxy = useCallback(() => {
|
|
763
|
+
setMode("proxy");
|
|
764
|
+
setCorsRetry(false);
|
|
765
|
+
void send();
|
|
766
|
+
}, [send]);
|
|
767
|
+
|
|
768
|
+
const groupedParams = LOCATIONS.map((location) => ({
|
|
769
|
+
location,
|
|
770
|
+
params: operation.parameters.filter((p) => p.in === location),
|
|
771
|
+
})).filter((group) => group.params.length > 0);
|
|
772
|
+
// Only the first visible section starts expanded.
|
|
773
|
+
const hasAuth = operation.security.length > 0;
|
|
774
|
+
|
|
775
|
+
const responseKind = response ? classifyResponse(response.contentType) : null;
|
|
776
|
+
const mediaSrc =
|
|
777
|
+
response && responseKind && responseKind !== "text"
|
|
778
|
+
? "data:" +
|
|
779
|
+
(response.contentType ?? "application/octet-stream") +
|
|
780
|
+
";base64," +
|
|
781
|
+
response.body
|
|
782
|
+
: "";
|
|
783
|
+
|
|
784
|
+
const renderParamField = (param: ParameterDescriptor) => (
|
|
785
|
+
<Field key={param.anchorId}>
|
|
786
|
+
<FieldHead>
|
|
787
|
+
<FieldName>{param.name}</FieldName>
|
|
788
|
+
<TypeChip>{schemaTypeLabel(param.schema)}</TypeChip>
|
|
789
|
+
{param.required ? <RequiredChip>required</RequiredChip> : null}
|
|
790
|
+
</FieldHead>
|
|
791
|
+
{param.description ? <FieldDesc>{param.description}</FieldDesc> : null}
|
|
792
|
+
<Input
|
|
793
|
+
$fullWidth
|
|
794
|
+
type="text"
|
|
795
|
+
aria-label={param.name}
|
|
796
|
+
placeholder={
|
|
797
|
+
param.example !== undefined
|
|
798
|
+
? String(param.example)
|
|
799
|
+
: "enter " + param.name
|
|
800
|
+
}
|
|
801
|
+
value={paramValues[paramKey(param)] ?? ""}
|
|
802
|
+
onChange={(e) =>
|
|
803
|
+
setParamValues((prev) => ({
|
|
804
|
+
...prev,
|
|
805
|
+
[paramKey(param)]: e.target.value,
|
|
806
|
+
}))
|
|
807
|
+
}
|
|
808
|
+
/>
|
|
809
|
+
</Field>
|
|
810
|
+
);
|
|
811
|
+
|
|
812
|
+
return (
|
|
813
|
+
<>
|
|
814
|
+
<CompactBar>
|
|
815
|
+
<MethodBadge $method={operation.method}>{operation.method}</MethodBadge>
|
|
816
|
+
<PathText>{renderPath(operation.path)}</PathText>
|
|
817
|
+
<CopyButton
|
|
818
|
+
text={operation.method.toUpperCase() + " " + operation.path}
|
|
819
|
+
label="Copy endpoint"
|
|
820
|
+
size={16}
|
|
821
|
+
/>
|
|
822
|
+
<AccentButton type="button" onClick={() => setOpen(true)}>
|
|
823
|
+
Try it
|
|
824
|
+
<Icon name="play" size={14} />
|
|
825
|
+
</AccentButton>
|
|
826
|
+
</CompactBar>
|
|
827
|
+
|
|
828
|
+
<Modal
|
|
829
|
+
$isOpen={open}
|
|
830
|
+
$onClose={() => setOpen(false)}
|
|
831
|
+
$width={1120}
|
|
832
|
+
$title={
|
|
833
|
+
operation.summary ??
|
|
834
|
+
operation.method.toUpperCase() + " " + operation.path
|
|
835
|
+
}
|
|
836
|
+
>
|
|
837
|
+
<DialogHeader>
|
|
838
|
+
<PathBar>
|
|
839
|
+
<MethodBadge $method={operation.method}>
|
|
840
|
+
{operation.method}
|
|
841
|
+
</MethodBadge>
|
|
842
|
+
<PathText>{renderPath(operation.path)}</PathText>
|
|
843
|
+
<AccentButton
|
|
844
|
+
type="button"
|
|
845
|
+
onClick={() => void send()}
|
|
846
|
+
disabled={loading}
|
|
847
|
+
>
|
|
848
|
+
Send
|
|
849
|
+
{loading ? <Spinner size={14} /> : <Icon name="play" size={14} />}
|
|
850
|
+
</AccentButton>
|
|
851
|
+
</PathBar>
|
|
852
|
+
</DialogHeader>
|
|
853
|
+
|
|
854
|
+
<DialogBody>
|
|
855
|
+
<Column>
|
|
856
|
+
{operation.description || operation.summary ? (
|
|
857
|
+
<Description>
|
|
858
|
+
{operation.description ?? operation.summary}
|
|
859
|
+
</Description>
|
|
860
|
+
) : null}
|
|
861
|
+
|
|
862
|
+
<Toolbar>
|
|
863
|
+
{operation.servers.length > 0 ? (
|
|
864
|
+
<Meta>{serverBase(operation.servers[0])}</Meta>
|
|
865
|
+
) : null}
|
|
866
|
+
{allowDirect ? (
|
|
867
|
+
<>
|
|
868
|
+
<Toggle
|
|
869
|
+
type="button"
|
|
870
|
+
$active={mode === "proxy"}
|
|
871
|
+
onClick={() => setMode("proxy")}
|
|
872
|
+
>
|
|
873
|
+
Proxy
|
|
874
|
+
</Toggle>
|
|
875
|
+
<Toggle
|
|
876
|
+
type="button"
|
|
877
|
+
$active={mode === "direct"}
|
|
878
|
+
onClick={() => setMode("direct")}
|
|
879
|
+
>
|
|
880
|
+
Direct
|
|
881
|
+
</Toggle>
|
|
882
|
+
</>
|
|
883
|
+
) : null}
|
|
884
|
+
<Toggle
|
|
885
|
+
type="button"
|
|
886
|
+
$active={showSecrets}
|
|
887
|
+
onClick={() => setShowSecrets((v) => !v)}
|
|
888
|
+
>
|
|
889
|
+
{showSecrets ? "Hide secrets" : "Show secrets"}
|
|
890
|
+
</Toggle>
|
|
891
|
+
</Toolbar>
|
|
892
|
+
|
|
893
|
+
{operation.security.length > 0 ? (
|
|
894
|
+
<Accordion title="Authorization" defaultOpen>
|
|
895
|
+
<SectionInner>
|
|
896
|
+
{operation.security.map((requirement) => {
|
|
897
|
+
const scheme = requirement.scheme;
|
|
898
|
+
if (
|
|
899
|
+
scheme &&
|
|
900
|
+
scheme.type === "http" &&
|
|
901
|
+
scheme.scheme === "basic"
|
|
902
|
+
) {
|
|
903
|
+
return (
|
|
904
|
+
<Field key={requirement.schemeName}>
|
|
905
|
+
<FieldHead>
|
|
906
|
+
<FieldName>{requirement.schemeName}</FieldName>
|
|
907
|
+
<TypeChip>basic</TypeChip>
|
|
908
|
+
</FieldHead>
|
|
909
|
+
<Input
|
|
910
|
+
$fullWidth
|
|
911
|
+
type="text"
|
|
912
|
+
aria-label={requirement.schemeName + " username"}
|
|
913
|
+
placeholder="username"
|
|
914
|
+
value={
|
|
915
|
+
authValues[requirement.schemeName + ":user"] ?? ""
|
|
916
|
+
}
|
|
917
|
+
onChange={(e) =>
|
|
918
|
+
setAuthValues((prev) => ({
|
|
919
|
+
...prev,
|
|
920
|
+
[requirement.schemeName + ":user"]:
|
|
921
|
+
e.target.value,
|
|
922
|
+
}))
|
|
923
|
+
}
|
|
924
|
+
/>
|
|
925
|
+
<Input
|
|
926
|
+
$fullWidth
|
|
927
|
+
type={showSecrets ? "text" : "password"}
|
|
928
|
+
aria-label={requirement.schemeName + " password"}
|
|
929
|
+
placeholder="password"
|
|
930
|
+
value={
|
|
931
|
+
authValues[requirement.schemeName + ":pass"] ?? ""
|
|
932
|
+
}
|
|
933
|
+
onChange={(e) =>
|
|
934
|
+
setAuthValues((prev) => ({
|
|
935
|
+
...prev,
|
|
936
|
+
[requirement.schemeName + ":pass"]:
|
|
937
|
+
e.target.value,
|
|
938
|
+
}))
|
|
939
|
+
}
|
|
940
|
+
/>
|
|
941
|
+
</Field>
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
const chip =
|
|
945
|
+
scheme?.type === "apiKey"
|
|
946
|
+
? "apiKey"
|
|
947
|
+
: scheme?.scheme === "bearer"
|
|
948
|
+
? "bearer"
|
|
949
|
+
: (scheme?.type ?? "token");
|
|
950
|
+
return (
|
|
951
|
+
<Field key={requirement.schemeName}>
|
|
952
|
+
<FieldHead>
|
|
953
|
+
<FieldName>{requirement.schemeName}</FieldName>
|
|
954
|
+
<TypeChip>{chip}</TypeChip>
|
|
955
|
+
<RequiredChip>required</RequiredChip>
|
|
956
|
+
</FieldHead>
|
|
957
|
+
<Input
|
|
958
|
+
$fullWidth
|
|
959
|
+
type={showSecrets ? "text" : "password"}
|
|
960
|
+
aria-label={requirement.schemeName}
|
|
961
|
+
placeholder={
|
|
962
|
+
chip === "bearer"
|
|
963
|
+
? "enter bearer token"
|
|
964
|
+
: "enter value"
|
|
965
|
+
}
|
|
966
|
+
value={authValues[requirement.schemeName] ?? ""}
|
|
967
|
+
onChange={(e) =>
|
|
968
|
+
setAuthValues((prev) => ({
|
|
969
|
+
...prev,
|
|
970
|
+
[requirement.schemeName]: e.target.value,
|
|
971
|
+
}))
|
|
972
|
+
}
|
|
973
|
+
/>
|
|
974
|
+
</Field>
|
|
975
|
+
);
|
|
976
|
+
})}
|
|
977
|
+
</SectionInner>
|
|
978
|
+
</Accordion>
|
|
979
|
+
) : null}
|
|
980
|
+
|
|
981
|
+
{groupedParams.map((group, index) => (
|
|
982
|
+
<Accordion
|
|
983
|
+
key={group.location}
|
|
984
|
+
title={LOCATION_TITLES[group.location]}
|
|
985
|
+
defaultOpen={!hasAuth && index === 0}
|
|
986
|
+
>
|
|
987
|
+
<SectionInner>
|
|
988
|
+
{group.params.map(renderParamField)}
|
|
989
|
+
</SectionInner>
|
|
990
|
+
</Accordion>
|
|
991
|
+
))}
|
|
992
|
+
|
|
993
|
+
{operation.requestBody &&
|
|
994
|
+
operation.method !== "get" &&
|
|
995
|
+
operation.method !== "head" ? (
|
|
996
|
+
<Accordion
|
|
997
|
+
title="Body"
|
|
998
|
+
defaultOpen={!hasAuth && groupedParams.length === 0}
|
|
999
|
+
>
|
|
1000
|
+
<SectionInner>
|
|
1001
|
+
<Textarea
|
|
1002
|
+
$fullWidth
|
|
1003
|
+
rows={8}
|
|
1004
|
+
aria-label="Request body"
|
|
1005
|
+
spellCheck={false}
|
|
1006
|
+
value={bodyText}
|
|
1007
|
+
onChange={(e) => setBodyText(e.target.value)}
|
|
1008
|
+
/>
|
|
1009
|
+
</SectionInner>
|
|
1010
|
+
</Accordion>
|
|
1011
|
+
) : null}
|
|
1012
|
+
|
|
1013
|
+
{error ? (
|
|
1014
|
+
<Field>
|
|
1015
|
+
<ErrorText>{error}</ErrorText>
|
|
1016
|
+
{corsRetry ? (
|
|
1017
|
+
<Toggle type="button" onClick={retryViaProxy}>
|
|
1018
|
+
Retry via proxy
|
|
1019
|
+
</Toggle>
|
|
1020
|
+
) : null}
|
|
1021
|
+
</Field>
|
|
1022
|
+
) : null}
|
|
1023
|
+
</Column>
|
|
1024
|
+
|
|
1025
|
+
<Column>
|
|
1026
|
+
<ResponseArea>
|
|
1027
|
+
{response ? (
|
|
1028
|
+
<Card>
|
|
1029
|
+
<CardHead as="div">
|
|
1030
|
+
<Inline>
|
|
1031
|
+
<StatusPill
|
|
1032
|
+
$ok={response.status >= 200 && response.status < 400}
|
|
1033
|
+
>
|
|
1034
|
+
{response.status} {response.statusText}
|
|
1035
|
+
</StatusPill>
|
|
1036
|
+
<Meta>{response.durationMs}ms</Meta>
|
|
1037
|
+
<Meta>{response.byteLength} bytes</Meta>
|
|
1038
|
+
{response.truncated ? <Meta>(truncated)</Meta> : null}
|
|
1039
|
+
</Inline>
|
|
1040
|
+
</CardHead>
|
|
1041
|
+
<CardBody>
|
|
1042
|
+
{responseKind === "image" ? (
|
|
1043
|
+
<ResponseMedia src={mediaSrc} alt="Response" />
|
|
1044
|
+
) : responseKind === "video" ? (
|
|
1045
|
+
<video
|
|
1046
|
+
controls
|
|
1047
|
+
src={mediaSrc}
|
|
1048
|
+
style={{ maxWidth: "100%" }}
|
|
1049
|
+
/>
|
|
1050
|
+
) : responseKind === "audio" ? (
|
|
1051
|
+
<audio controls src={mediaSrc} />
|
|
1052
|
+
) : responseKind === "binary" ? (
|
|
1053
|
+
<a href={mediaSrc} download>
|
|
1054
|
+
Download response ({response.byteLength} bytes)
|
|
1055
|
+
</a>
|
|
1056
|
+
) : (
|
|
1057
|
+
<Code
|
|
1058
|
+
language={codeLanguage(response.contentType)}
|
|
1059
|
+
code={prettyBody(response.contentType, response.body)}
|
|
1060
|
+
/>
|
|
1061
|
+
)}
|
|
1062
|
+
</CardBody>
|
|
1063
|
+
</Card>
|
|
1064
|
+
) : (
|
|
1065
|
+
<Placeholder>
|
|
1066
|
+
Send the request to see the live response here.
|
|
1067
|
+
</Placeholder>
|
|
1068
|
+
)}
|
|
1069
|
+
{loading ? (
|
|
1070
|
+
<LoadingOverlay>
|
|
1071
|
+
<Spinner size={22} />
|
|
1072
|
+
</LoadingOverlay>
|
|
1073
|
+
) : null}
|
|
1074
|
+
</ResponseArea>
|
|
1075
|
+
|
|
1076
|
+
{snippets.length > 0 ? <CodeTabs tabs={snippets} /> : null}
|
|
1077
|
+
</Column>
|
|
1078
|
+
</DialogBody>
|
|
1079
|
+
</Modal>
|
|
1080
|
+
</>
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
`;
|