forked from PaloAltoNetworks/docusaurus-openapi-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
314 lines (291 loc) · 9.96 KB
/
Copy pathindex.tsx
File metadata and controls
314 lines (291 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* ============================================================================
* Copyright (c) Palo Alto Networks
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
// @ts-nocheck
import React, { useState } from "react";
import { useDoc } from "@docusaurus/plugin-content-docs/client";
import Accept from "@theme/ApiExplorer/Accept";
import Authorization from "@theme/ApiExplorer/Authorization";
import Body from "@theme/ApiExplorer/Body";
import buildPostmanRequest from "@theme/ApiExplorer/buildPostmanRequest";
import ContentType from "@theme/ApiExplorer/ContentType";
import ParamOptions from "@theme/ApiExplorer/ParamOptions";
import {
setResponse,
setCode,
clearCode,
setHeaders,
clearHeaders,
} from "@theme/ApiExplorer/Response/slice";
import Server from "@theme/ApiExplorer/Server";
import { useTypedDispatch, useTypedSelector } from "@theme/ApiItem/hooks";
import { ParameterObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
import { ApiItem } from "docusaurus-plugin-openapi-docs/src/types";
import * as sdk from "postman-collection";
import { FormProvider, useForm } from "react-hook-form";
import makeRequest from "./makeRequest";
import DemoExpandButton from "@theme/ApiExplorer/DemoExpandButton";
function Request({ item }: { item: ApiItem }) {
const postman = new sdk.Request(item.postman);
const metadata = useDoc();
const { proxy, hide_send_button: hideSendButton } = metadata.frontMatter;
const pathParams = useTypedSelector((state: any) => state.params.path);
const queryParams = useTypedSelector((state: any) => state.params.query);
const cookieParams = useTypedSelector((state: any) => state.params.cookie);
const contentType = useTypedSelector((state: any) => state.contentType.value);
const headerParams = useTypedSelector((state: any) => state.params.header);
const body = useTypedSelector((state: any) => state.body);
const accept = useTypedSelector((state: any) => state.accept.value);
const acceptOptions = useTypedDispatch((state: any) => state.accept.options);
const authSelected = useTypedSelector((state: any) => state.auth.selected);
const server = useTypedSelector((state: any) => state.server.value);
const serverOptions = useTypedSelector((state: any) => state.server.options);
const auth = useTypedSelector((state: any) => state.auth);
const dispatch = useTypedDispatch();
const [expandAccept, setExpandAccept] = useState(true);
const [expandAuth, setExpandAuth] = useState(true);
const [expandBody, setExpandBody] = useState(true);
const [expandParams, setExpandParams] = useState(true);
const [expandServer, setExpandServer] = useState(true);
const allParams = [
...pathParams,
...queryParams,
...cookieParams,
...headerParams,
];
const postmanRequest = buildPostmanRequest(postman, {
queryParams,
pathParams,
cookieParams,
contentType,
accept,
headerParams,
body,
server,
auth,
});
const delay = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
const paramsObject = {
path: [] as ParameterObject[],
query: [] as ParameterObject[],
header: [] as ParameterObject[],
cookie: [] as ParameterObject[],
};
item.parameters?.forEach(
(param: { in: "path" | "query" | "header" | "cookie" }) => {
const paramType = param.in;
const paramsArray: ParameterObject[] = paramsObject[paramType];
paramsArray.push(param as ParameterObject);
}
);
const methods = useForm({ shouldFocusError: false });
const handleEventStream = async (res) => {
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
dispatch(setCode(res.status));
const reader = res.body.getReader();
const decoder = new TextDecoder();
let result = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
result += decoder.decode(value, { stream: true });
dispatch(setResponse(result));
}
};
const handleResponse = async (res) => {
dispatch(setResponse(await res.text()));
dispatch(setCode(res.status));
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
};
const onSubmit = async (data) => {
dispatch(setResponse("Fetching..."));
try {
await delay(1200);
const res = await makeRequest(postmanRequest, proxy, body);
if (res.headers.get("content-type")?.includes("text/event-stream")) {
await handleEventStream(res);
} else {
await handleResponse(res);
}
} catch (e) {
console.log(e);
dispatch(setResponse("Connection failed"));
dispatch(clearCode());
dispatch(clearHeaders());
}
};
const showServerOptions = serverOptions.length > 0;
const showAcceptOptions = acceptOptions.length > 1;
const showRequestBody = contentType !== undefined;
const showRequestButton = item.servers && !hideSendButton;
const showAuth = authSelected !== undefined;
const showParams = allParams.length > 0;
const requestBodyRequired = item.requestBody?.required;
if (
!showAcceptOptions &&
!showAuth &&
!showParams &&
!showRequestBody &&
!showServerOptions
) {
return null;
}
const expandAllDetails = () => {
setExpandAccept(true);
setExpandAuth(true);
setExpandBody(true);
setExpandParams(true);
setExpandServer(true);
};
const collapseAllDetails = () => {
setExpandAccept(false);
setExpandAuth(false);
setExpandBody(false);
setExpandParams(false);
setExpandServer(false);
};
const allDetailsExpanded =
expandParams && expandBody && expandServer && expandAuth && expandAccept;
return (
<FormProvider {...methods}>
<form
className="openapi-explorer__request-form"
onSubmit={methods.handleSubmit(onSubmit)}
>
<div className="openapi-explorer__request-header-container">
<span className="openapi-explorer__request-title">Request </span>
{allDetailsExpanded ? (
<span
className="openapi-explorer__expand-details-btn"
onClick={collapseAllDetails}
>
Collapse all
</span>
) : (
<span
className="openapi-explorer__expand-details-btn"
onClick={expandAllDetails}
>
Expand all
</span>
)}
<DemoExpandButton
item={item}
className="openapi-explorer__code-block-code-btn"
/>
</div>
<div className="openapi-explorer__details-outer-container">
{showServerOptions && item.method !== "event" && (
<details
open={expandServer}
className="openapi-explorer__details-container"
>
<summary
className="openapi-explorer__details-summary"
onClick={(e) => {
e.preventDefault();
setExpandServer(!expandServer);
}}
>
Base URL
</summary>
<Server />
</details>
)}
{showAuth && (
<details
open={expandAuth}
className="openapi-explorer__details-container"
>
<summary
className="openapi-explorer__details-summary"
onClick={(e) => {
e.preventDefault();
setExpandAuth(!expandAuth);
}}
>
Auth
</summary>
<Authorization />
</details>
)}
{showParams && (
<details
open={
expandParams || Object.keys(methods.formState.errors).length
}
className="openapi-explorer__details-container"
>
<summary
className="openapi-explorer__details-summary"
onClick={(e) => {
e.preventDefault();
setExpandParams(!expandParams);
}}
>
Parameters
</summary>
<ParamOptions />
</details>
)}
{showRequestBody && (
<details
open={expandBody}
className="openapi-explorer__details-container"
>
<summary
className="openapi-explorer__details-summary"
onClick={(e) => {
e.preventDefault();
setExpandBody(!expandBody);
}}
>
Body
{requestBodyRequired && (
<span className="openapi-schema__required">
required
</span>
)}
</summary>
<>
<ContentType />
<Body
jsonRequestBodyExample={item.jsonRequestBodyExample}
requestBodyMetadata={item.requestBody}
required={requestBodyRequired}
/>
</>
</details>
)}
{showAcceptOptions && (
<details
open={expandAccept}
className="openapi-explorer__details-container"
>
<summary
className="openapi-explorer__details-summary"
onClick={(e) => {
e.preventDefault();
setExpandAccept(!expandAccept);
}}
>
Accept
</summary>
<Accept />
</details>
)}
{showRequestButton && item.method !== "event" && (
<button className="openapi-explorer__request-btn" type="submit">
Send API Request
</button>
)}
</div>
</form>
</FormProvider>
);
}
export default Request;