docusaurus-theme-openapi-docs 1.2.2 → 1.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.
Files changed (64) hide show
  1. package/lib/theme/ApiDemoPanel/Accept/index.js +1 -1
  2. package/lib/theme/ApiDemoPanel/Authorization/index.js +2 -2
  3. package/lib/theme/ApiDemoPanel/Body/index.js +104 -9
  4. package/lib/theme/ApiDemoPanel/ContentType/index.js +1 -1
  5. package/lib/theme/ApiDemoPanel/Curl/index.js +2 -2
  6. package/lib/theme/ApiDemoPanel/Execute/index.js +1 -1
  7. package/lib/theme/ApiDemoPanel/ParamOptions/index.js +1 -1
  8. package/lib/theme/ApiDemoPanel/Request/index.js +2 -2
  9. package/lib/theme/ApiDemoPanel/Response/index.js +1 -1
  10. package/lib/theme/ApiDemoPanel/SecuritySchemes/index.js +1 -1
  11. package/lib/theme/ApiDemoPanel/Server/index.js +27 -10
  12. package/lib/theme/ApiDemoPanel/Server/slice.js +3 -2
  13. package/lib/theme/ApiDemoPanel/index.js +8 -72
  14. package/lib/theme/ApiDemoPanel/persistanceMiddleware.js +22 -0
  15. package/lib/theme/ApiItem/Layout/styles.module.css +11 -3
  16. package/lib/theme/{ApiDemoPanel → ApiItem}/hooks.js +0 -0
  17. package/lib/theme/ApiItem/index.js +101 -22
  18. package/lib/theme/{ApiDemoPanel → ApiItem}/store.js +7 -7
  19. package/lib/theme/MimeTabs/index.js +29 -1
  20. package/lib/theme/MimeTabs/styles.module.css +1 -0
  21. package/lib/theme/SchemaTabs/index.js +4 -1
  22. package/lib-next/theme/ApiDemoPanel/Accept/index.js +1 -1
  23. package/lib-next/theme/ApiDemoPanel/Authorization/index.js +1 -1
  24. package/lib-next/theme/ApiDemoPanel/Body/index.js +114 -7
  25. package/lib-next/theme/ApiDemoPanel/ContentType/index.js +1 -1
  26. package/lib-next/theme/ApiDemoPanel/Curl/index.js +1 -1
  27. package/lib-next/theme/ApiDemoPanel/Execute/index.js +1 -1
  28. package/lib-next/theme/ApiDemoPanel/ParamOptions/index.js +1 -1
  29. package/lib-next/theme/ApiDemoPanel/Request/index.js +1 -1
  30. package/lib-next/theme/ApiDemoPanel/Response/index.js +1 -1
  31. package/lib-next/theme/ApiDemoPanel/SecuritySchemes/index.js +1 -1
  32. package/lib-next/theme/ApiDemoPanel/Server/index.js +38 -10
  33. package/lib-next/theme/ApiDemoPanel/Server/slice.js +5 -3
  34. package/lib-next/theme/ApiDemoPanel/index.js +7 -69
  35. package/lib-next/theme/ApiDemoPanel/persistanceMiddleware.js +20 -0
  36. package/lib-next/theme/ApiItem/Layout/styles.module.css +11 -3
  37. package/lib-next/theme/{ApiDemoPanel → ApiItem}/hooks.js +0 -0
  38. package/lib-next/theme/ApiItem/index.js +104 -26
  39. package/lib-next/theme/{ApiDemoPanel → ApiItem}/store.js +7 -7
  40. package/lib-next/theme/MimeTabs/index.js +29 -1
  41. package/lib-next/theme/MimeTabs/styles.module.css +1 -0
  42. package/lib-next/theme/SchemaTabs/index.js +4 -1
  43. package/package.json +3 -3
  44. package/src/theme/ApiDemoPanel/Accept/index.tsx +1 -1
  45. package/src/theme/ApiDemoPanel/Authorization/index.tsx +1 -1
  46. package/src/theme/ApiDemoPanel/Body/index.tsx +107 -7
  47. package/src/theme/ApiDemoPanel/ContentType/index.tsx +1 -1
  48. package/src/theme/ApiDemoPanel/Curl/index.tsx +1 -1
  49. package/src/theme/ApiDemoPanel/Execute/index.tsx +1 -1
  50. package/src/theme/ApiDemoPanel/ParamOptions/index.tsx +1 -1
  51. package/src/theme/ApiDemoPanel/Request/index.tsx +1 -1
  52. package/src/theme/ApiDemoPanel/Response/index.tsx +1 -1
  53. package/src/theme/ApiDemoPanel/SecuritySchemes/index.tsx +1 -1
  54. package/src/theme/ApiDemoPanel/Server/index.tsx +34 -6
  55. package/src/theme/ApiDemoPanel/Server/slice.ts +6 -7
  56. package/src/theme/ApiDemoPanel/index.tsx +10 -73
  57. package/src/theme/ApiDemoPanel/persistanceMiddleware.ts +21 -1
  58. package/src/theme/ApiItem/Layout/styles.module.css +11 -3
  59. package/src/theme/{ApiDemoPanel → ApiItem}/hooks.ts +0 -0
  60. package/src/theme/ApiItem/index.tsx +103 -27
  61. package/src/theme/{ApiDemoPanel → ApiItem}/store.ts +7 -7
  62. package/src/theme/MimeTabs/index.js +29 -1
  63. package/src/theme/MimeTabs/styles.module.css +1 -0
  64. package/src/theme/SchemaTabs/index.js +4 -1
@@ -5,87 +5,25 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  * ========================================================================== */
7
7
  import React from "react";
8
- import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
9
8
  import sdk from "@paloaltonetworks/postman-collection";
10
- import { Provider } from "react-redux";
11
- import { createAuth } from "./Authorization/slice";
12
9
  import Curl from "./Curl";
13
10
  import MethodEndpoint from "./MethodEndpoint";
14
- import { createPersistanceMiddleware } from "./persistanceMiddleware";
15
11
  import Request from "./Request";
16
12
  import Response from "./Response";
17
13
  import SecuritySchemes from "./SecuritySchemes";
18
- import { createStoreWithState } from "./store";
19
14
  import styles from "./styles.module.css";
20
15
 
21
16
  function ApiDemoPanel({ item, infoPath }) {
22
- const { siteConfig } = useDocusaurusContext();
23
- const themeConfig = siteConfig.themeConfig;
24
- const options = themeConfig.api;
25
17
  const postman = new sdk.Request(item.postman);
26
- const acceptArray = Array.from(
27
- new Set(
28
- Object.values(item.responses ?? {})
29
- .map((response) => Object.keys(response.content ?? {}))
30
- .flat()
31
- )
32
- );
33
- const content = item.requestBody?.content ?? {};
34
- const contentTypeArray = Object.keys(content);
35
- const servers = item.servers ?? [];
36
- const params = {
37
- path: [],
38
- query: [],
39
- header: [],
40
- cookie: [],
41
- };
42
- item.parameters?.forEach((param) => {
43
- const paramType = param.in;
44
- const paramsArray = params[paramType];
45
- paramsArray.push(param);
46
- });
47
- const auth = createAuth({
48
- security: item.security,
49
- securitySchemes: item.securitySchemes,
50
- options,
51
- });
52
- const persistanceMiddleware = createPersistanceMiddleware(options);
53
- const store2 = createStoreWithState(
54
- {
55
- accept: {
56
- value: acceptArray[0],
57
- options: acceptArray,
58
- },
59
- contentType: {
60
- value: contentTypeArray[0],
61
- options: contentTypeArray,
62
- },
63
- server: {
64
- value: servers[0],
65
- options: servers,
66
- },
67
- response: {
68
- value: undefined,
69
- },
70
- body: {
71
- type: "empty",
72
- },
73
- params,
74
- auth,
75
- },
76
- [persistanceMiddleware]
77
- );
78
18
  const { path, method } = item;
79
19
  return (
80
- <Provider store={store2}>
81
- <div className={styles.apiDemoPanelContainer}>
82
- <MethodEndpoint method={method} path={path} />
83
- <SecuritySchemes infoPath={infoPath} />
84
- <Request item={item} />
85
- <Response />
86
- <Curl postman={postman} codeSamples={item["x-code-samples"] ?? []} />
87
- </div>
88
- </Provider>
20
+ <div className={styles.apiDemoPanelContainer}>
21
+ <MethodEndpoint method={method} path={path} />
22
+ <SecuritySchemes infoPath={infoPath} />
23
+ <Request item={item} />
24
+ <Response />
25
+ <Curl postman={postman} codeSamples={item["x-code-samples"] ?? []} />
26
+ </div>
89
27
  );
90
28
  }
91
29
 
@@ -31,6 +31,26 @@ export function createPersistanceMiddleware(options) {
31
31
  }
32
32
  }
33
33
 
34
+ if (action.type === "contentType/setContentType") {
35
+ storage.setItem("contentType", action.payload);
36
+ }
37
+
38
+ if (action.type === "accept/setAccept") {
39
+ storage.setItem("accept", action.payload);
40
+ }
41
+
42
+ if (action.type === "server/setServer") {
43
+ storage.setItem("server", action.payload);
44
+ }
45
+
46
+ if (action.type === "server/setServerVariable") {
47
+ const server = storage.getItem("server") ?? "{}";
48
+ const variables = JSON.parse(action.payload);
49
+ let serverObject = JSON.parse(server);
50
+ serverObject.variables[variables.key].default = variables.value;
51
+ storage.setItem("server", JSON.stringify(serverObject));
52
+ }
53
+
34
54
  return result;
35
55
  };
36
56
 
@@ -154,12 +154,15 @@
154
154
  margin-bottom: 0;
155
155
  }
156
156
 
157
- :global(.theme-api-markdown li::before) {
157
+ :global([class^="paramsItem"]::before),
158
+ :global([class^="schemaItem"]::before) {
158
159
  position: absolute;
159
160
  top: 10px;
160
161
  left: 0;
161
- width: 0.7rem; /* width of horizontal line */
162
- height: 0.5rem; /* vertical position of line */
162
+ width: 0.7rem;
163
+ /* width of horizontal line */
164
+ height: 0.5rem;
165
+ /* vertical position of line */
163
166
  vertical-align: top;
164
167
  border-bottom: thin solid var(--ifm-color-gray-500);
165
168
  content: "";
@@ -410,3 +413,8 @@
410
413
  max-height: 500px;
411
414
  overflow: auto;
412
415
  }
416
+
417
+ /* Prism code styles */
418
+ :global(.prism-code.language-json) {
419
+ white-space: pre !important;
420
+ }
@@ -7,9 +7,14 @@
7
7
  import React from "react";
8
8
  import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
9
9
  import { HtmlClassNameProvider } from "@docusaurus/theme-common";
10
+ import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
10
11
  import clsx from "clsx";
12
+ import { Provider } from "react-redux";
13
+ import { createAuth } from "../ApiDemoPanel/Authorization/slice";
14
+ import { createPersistanceMiddleware } from "../ApiDemoPanel/persistanceMiddleware";
11
15
  import DocItemLayout from "./Layout";
12
16
  import DocItemMetadata from "./Metadata";
17
+ import { createStoreWithState } from "./store";
13
18
 
14
19
  const { DocProvider } = require("@docusaurus/theme-common/internal");
15
20
 
@@ -21,40 +26,113 @@ let ApiDemoPanel = (_) => (
21
26
  />
22
27
  );
23
28
 
29
+ let DocItem = (props) => {
30
+ return (
31
+ <div
32
+ style={{
33
+ marginTop: "3.5em",
34
+ }}
35
+ />
36
+ );
37
+ };
38
+
24
39
  if (ExecutionEnvironment.canUseDOM) {
25
40
  ApiDemoPanel = require("@theme/ApiDemoPanel").default;
26
- }
27
-
28
- export default function DocItem(props) {
29
- const docHtmlClassName = `docs-doc-id-${props.content.metadata.unversionedId}`;
30
41
 
31
- const DocContent = () => {
42
+ DocItem = function DocItem(props) {
43
+ const docHtmlClassName = `docs-doc-id-${props.content.metadata.unversionedId}`;
32
44
  const MDXComponent = props.content;
33
45
  const { frontMatter } = MDXComponent;
34
46
  const { info_path: infoPath } = frontMatter;
35
47
  const { api } = frontMatter;
36
- return (
37
- <div className="row">
38
- <div className={clsx("col", api ? "col--7" : "col--12")}>
39
- <MDXComponent />
40
- </div>
41
- {api && (
42
- <div className="col col--5">
43
- <ApiDemoPanel item={api} infoPath={infoPath} />
48
+ const { siteConfig } = useDocusaurusContext();
49
+ const themeConfig = siteConfig.themeConfig;
50
+ const options = themeConfig.api;
51
+ const acceptArray = Array.from(
52
+ new Set(
53
+ Object.values(api?.responses ?? {})
54
+ .map((response) => Object.keys(response.content ?? {}))
55
+ .flat()
56
+ )
57
+ );
58
+ const content = api?.requestBody?.content ?? {};
59
+ const contentTypeArray = Object.keys(content);
60
+ const servers = api?.servers ?? [];
61
+ const params = {
62
+ path: [],
63
+ query: [],
64
+ header: [],
65
+ cookie: [],
66
+ };
67
+ api?.parameters?.forEach((param) => {
68
+ const paramType = param.in;
69
+ const paramsArray = params[paramType];
70
+ paramsArray.push(param);
71
+ });
72
+ const auth = createAuth({
73
+ security: api?.security,
74
+ securitySchemes: api?.securitySchemes,
75
+ options,
76
+ });
77
+ const persistanceMiddleware = createPersistanceMiddleware(options);
78
+ const acceptValue = window?.sessionStorage.getItem("accept");
79
+ const contentTypeValue = window?.sessionStorage.getItem("contentType");
80
+ const server = window?.sessionStorage.getItem("server");
81
+ const serverObject = JSON.parse(server) ?? {};
82
+ const store2 = createStoreWithState(
83
+ {
84
+ accept: {
85
+ value: acceptValue || acceptArray[0],
86
+ options: acceptArray,
87
+ },
88
+ contentType: {
89
+ value: contentTypeValue || contentTypeArray[0],
90
+ options: contentTypeArray,
91
+ },
92
+ server: {
93
+ value: serverObject.url ? serverObject : undefined,
94
+ options: servers,
95
+ },
96
+ response: {
97
+ value: undefined,
98
+ },
99
+ body: {
100
+ type: "empty",
101
+ },
102
+ params,
103
+ auth,
104
+ },
105
+ [persistanceMiddleware]
106
+ );
107
+
108
+ const DocContent = () => {
109
+ return (
110
+ <div className="row">
111
+ <div className={clsx("col", api ? "col--7" : "col--12")}>
112
+ <MDXComponent />
44
113
  </div>
45
- )}
46
- </div>
114
+ {api && (
115
+ <div className="col col--5">
116
+ <ApiDemoPanel item={api} infoPath={infoPath} />
117
+ </div>
118
+ )}
119
+ </div>
120
+ );
121
+ };
122
+
123
+ return (
124
+ <Provider store={store2}>
125
+ <DocProvider content={props.content}>
126
+ <HtmlClassNameProvider className={docHtmlClassName}>
127
+ <DocItemMetadata />
128
+ <DocItemLayout>
129
+ <DocContent />
130
+ </DocItemLayout>
131
+ </HtmlClassNameProvider>
132
+ </DocProvider>
133
+ </Provider>
47
134
  );
48
135
  };
49
-
50
- return (
51
- <DocProvider content={props.content}>
52
- <HtmlClassNameProvider className={docHtmlClassName}>
53
- <DocItemMetadata />
54
- <DocItemLayout>
55
- <DocContent />
56
- </DocItemLayout>
57
- </HtmlClassNameProvider>
58
- </DocProvider>
59
- );
60
136
  }
137
+
138
+ export default DocItem;
@@ -5,13 +5,13 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  * ========================================================================== */
7
7
  import { configureStore, combineReducers } from "@reduxjs/toolkit";
8
- import accept from "./Accept/slice";
9
- import auth from "./Authorization/slice";
10
- import body from "./Body/slice";
11
- import contentType from "./ContentType/slice";
12
- import params from "./ParamOptions/slice";
13
- import response from "./Response/slice";
14
- import server from "./Server/slice";
8
+ import accept from "../ApiDemoPanel/Accept/slice";
9
+ import auth from "../ApiDemoPanel/Authorization/slice";
10
+ import body from "../ApiDemoPanel/Body/slice";
11
+ import contentType from "../ApiDemoPanel/ContentType/slice";
12
+ import params from "../ApiDemoPanel/ParamOptions/slice";
13
+ import response from "../ApiDemoPanel/Response/slice";
14
+ import server from "../ApiDemoPanel/Server/slice";
15
15
  const rootReducer = combineReducers({
16
16
  accept,
17
17
  contentType,
@@ -18,6 +18,9 @@ import { duplicates } from "@docusaurus/theme-common";
18
18
  import useIsBrowser from "@docusaurus/useIsBrowser";
19
19
  import clsx from "clsx";
20
20
 
21
+ import { setAccept } from "../ApiDemoPanel/Accept/slice";
22
+ import { setContentType } from "../ApiDemoPanel/ContentType/slice";
23
+ import { useTypedDispatch, useTypedSelector } from "../ApiItem/hooks";
21
24
  import styles from "./styles.module.css"; // A very rough duck type, but good enough to guard against mistakes while
22
25
 
23
26
  const {
@@ -39,6 +42,7 @@ function MimeTabsComponent(props) {
39
42
  values: valuesProp,
40
43
  groupId,
41
44
  className,
45
+ schemaType,
42
46
  } = props;
43
47
  const children = Children.map(props.children, (child) => {
44
48
  if (isValidElement(child) && isTabItem(child)) {
@@ -105,12 +109,22 @@ function MimeTabsComponent(props) {
105
109
  }
106
110
  }
107
111
 
112
+ const dispatch = useTypedDispatch();
113
+ const isRequestSchema = schemaType?.toLowerCase() === "request";
114
+
108
115
  const handleTabChange = (event) => {
116
+ event.preventDefault();
109
117
  const newTab = event.currentTarget;
110
118
  const newTabIndex = tabRefs.indexOf(newTab);
111
119
  const newTabValue = values[newTabIndex].value;
112
120
 
113
121
  if (newTabValue !== selectedValue) {
122
+ if (isRequestSchema) {
123
+ dispatch(setContentType(newTabValue));
124
+ } else {
125
+ dispatch(setAccept(newTabValue));
126
+ }
127
+
114
128
  blockElementScrollPositionUntilNextRender(newTab);
115
129
  setSelectedValue(newTabValue);
116
130
 
@@ -120,6 +134,20 @@ function MimeTabsComponent(props) {
120
134
  }
121
135
  };
122
136
 
137
+ const contentTypeVal = useTypedSelector((state) => state.contentType.value);
138
+ const acceptTypeVal = useTypedSelector((state) => state.accept.value);
139
+
140
+ useEffect(() => {
141
+ if (tabRefs.length > 1) {
142
+ if (isRequestSchema) {
143
+ setSelectedValue(contentTypeVal);
144
+ } else {
145
+ setSelectedValue(acceptTypeVal);
146
+ }
147
+ }
148
+ // eslint-disable-next-line react-hooks/exhaustive-deps
149
+ }, [contentTypeVal, acceptTypeVal]);
150
+
123
151
  const handleKeydown = (event) => {
124
152
  let focusElement = null;
125
153
 
@@ -197,7 +225,7 @@ function MimeTabsComponent(props) {
197
225
  ref={(tabControl) => tabRefs.push(tabControl)}
198
226
  onKeyDown={handleKeydown}
199
227
  onFocus={handleTabChange}
200
- onClick={handleTabChange}
228
+ onClick={(e) => handleTabChange(e)}
201
229
  {...attributes}
202
230
  className={clsx(
203
231
  "tabs__item",
@@ -17,6 +17,7 @@
17
17
  font-weight: var(--ifm-font-weight-normal);
18
18
  /* color: var(--openapi-code-dim-dark); */
19
19
  font-size: 12px;
20
+ white-space: nowrap;
20
21
  }
21
22
 
22
23
  .tabItem:active {
@@ -227,7 +227,10 @@ function SchemaTabsComponent(props) {
227
227
  cloneElement(
228
228
  children.filter(
229
229
  (tabItem) => tabItem.props.value === selectedValue
230
- )[0],
230
+ )[0] ?? // TODO: see if there's a better way to handle this
231
+ children.filter(
232
+ (tabItem) => tabItem.props.value === defaultValue
233
+ )[0],
231
234
  {
232
235
  className: "margin-vert--md",
233
236
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-theme-openapi-docs",
3
3
  "description": "OpenAPI theme for Docusaurus.",
4
- "version": "1.2.2",
4
+ "version": "1.3.0",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -50,7 +50,7 @@
50
50
  "buffer": "^6.0.3",
51
51
  "clsx": "^1.1.1",
52
52
  "crypto-js": "^4.1.1",
53
- "docusaurus-plugin-openapi-docs": "^1.2.2",
53
+ "docusaurus-plugin-openapi-docs": "^1.3.0",
54
54
  "immer": "^9.0.7",
55
55
  "lodash": "^4.17.20",
56
56
  "process": "^0.11.10",
@@ -70,5 +70,5 @@
70
70
  "engines": {
71
71
  "node": ">=14"
72
72
  },
73
- "gitHead": "cfeb5216f143133a1c35587fef21f75a44b75854"
73
+ "gitHead": "b599d062a8e4f1cff86b01ac9b82dc564defbf5f"
74
74
  }
@@ -7,7 +7,7 @@
7
7
 
8
8
  import React from "react";
9
9
 
10
- import { useTypedDispatch, useTypedSelector } from "../hooks";
10
+ import { useTypedDispatch, useTypedSelector } from "../../ApiItem/hooks";
11
11
  import FormItem from "./../FormItem";
12
12
  import FormSelect from "./../FormSelect";
13
13
  import { setAccept } from "./slice";
@@ -7,10 +7,10 @@
7
7
 
8
8
  import React from "react";
9
9
 
10
+ import { useTypedDispatch, useTypedSelector } from "../../ApiItem/hooks";
10
11
  import FormItem from "../FormItem";
11
12
  import FormSelect from "../FormSelect";
12
13
  import FormTextInput from "../FormTextInput";
13
- import { useTypedDispatch, useTypedSelector } from "../hooks";
14
14
  import { setAuthData, setSelectedAuth } from "./slice";
15
15
 
16
16
  function Authorization() {
@@ -7,12 +7,15 @@
7
7
 
8
8
  import React from "react";
9
9
 
10
+ import TabItem from "@theme/TabItem";
10
11
  import { RequestBodyObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
11
12
  import format from "xml-formatter";
12
13
 
14
+ import { useTypedDispatch, useTypedSelector } from "../../ApiItem/hooks";
15
+ // @ts-ignore
16
+ import SchemaTabs from "../../SchemaTabs";
13
17
  import ContentType from "../ContentType";
14
18
  import FormSelect from "../FormSelect";
15
- import { useTypedDispatch, useTypedSelector } from "../hooks";
16
19
  import LiveApp from "../LiveEditor";
17
20
  import FormFileUpload from "./../FormFileUpload";
18
21
  import FormItem from "./../FormItem";
@@ -82,6 +85,8 @@ function Body({ requestBodyMetadata, jsonRequestBodyExample }: Props) {
82
85
  // - application/x-www-form-urlencoded
83
86
 
84
87
  const schema = requestBodyMetadata?.content?.[contentType]?.schema;
88
+ const example = requestBodyMetadata?.content?.[contentType]?.example;
89
+ const examples = requestBodyMetadata?.content?.[contentType]?.examples;
85
90
 
86
91
  if (schema?.format === "binary") {
87
92
  return (
@@ -209,34 +214,129 @@ function Body({ requestBodyMetadata, jsonRequestBodyExample }: Props) {
209
214
  }
210
215
 
211
216
  let language = "plaintext";
212
- let exampleBodyString = ""; //"body content";
217
+ let defaultBody = ""; //"body content";
218
+ let exampleBody;
219
+ let examplesBodies = [] as any;
213
220
 
214
221
  if (contentType === "application/json" || contentType.endsWith("+json")) {
215
222
  if (jsonRequestBodyExample) {
216
- exampleBodyString = JSON.stringify(jsonRequestBodyExample, null, 2);
223
+ defaultBody = JSON.stringify(jsonRequestBodyExample, null, 2);
224
+ }
225
+ if (example) {
226
+ exampleBody = JSON.stringify(example, null, 2);
227
+ }
228
+ if (examples) {
229
+ for (const [key, example] of Object.entries(examples)) {
230
+ examplesBodies.push({
231
+ label: key,
232
+ body: JSON.stringify(example.value, null, 2),
233
+ summary: example.summary,
234
+ });
235
+ }
217
236
  }
218
237
  language = "json";
219
238
  }
220
239
 
221
- if (contentType === "application/xml") {
240
+ if (contentType === "application/xml" || contentType.endsWith("+xml")) {
222
241
  if (jsonRequestBodyExample) {
223
242
  try {
224
- exampleBodyString = format(json2xml(jsonRequestBodyExample, ""), {
243
+ defaultBody = format(json2xml(jsonRequestBodyExample, ""), {
244
+ indentation: " ",
245
+ lineSeparator: "\n",
246
+ collapseContent: true,
247
+ });
248
+ } catch {
249
+ defaultBody = json2xml(jsonRequestBodyExample);
250
+ }
251
+ }
252
+ if (example) {
253
+ try {
254
+ exampleBody = format(json2xml(example, ""), {
225
255
  indentation: " ",
226
256
  lineSeparator: "\n",
227
257
  collapseContent: true,
228
258
  });
229
259
  } catch {
230
- exampleBodyString = json2xml(jsonRequestBodyExample);
260
+ exampleBody = json2xml(example);
261
+ }
262
+ }
263
+ if (examples) {
264
+ for (const [key, example] of Object.entries(examples)) {
265
+ let formattedXmlBody;
266
+ try {
267
+ formattedXmlBody = format(example.value, {
268
+ indentation: " ",
269
+ lineSeparator: "\n",
270
+ collapseContent: true,
271
+ });
272
+ } catch {
273
+ formattedXmlBody = example.value;
274
+ }
275
+ examplesBodies.push({
276
+ label: key,
277
+ body: formattedXmlBody,
278
+ summary: example.summary,
279
+ });
231
280
  }
232
281
  }
233
282
  language = "xml";
234
283
  }
235
284
 
285
+ if (exampleBody) {
286
+ return (
287
+ <FormItem label="Body" required={required}>
288
+ <SchemaTabs lazy>
289
+ <TabItem label="Default" value="default" default>
290
+ <LiveApp action={dispatch} language={language}>
291
+ {defaultBody}
292
+ </LiveApp>
293
+ </TabItem>
294
+ <TabItem label="Example" value="example">
295
+ {exampleBody && (
296
+ <LiveApp action={dispatch} language={language}>
297
+ {exampleBody}
298
+ </LiveApp>
299
+ )}
300
+ </TabItem>
301
+ </SchemaTabs>
302
+ </FormItem>
303
+ );
304
+ }
305
+
306
+ if (examplesBodies && examplesBodies.length > 0) {
307
+ return (
308
+ <FormItem label="Body" required={required}>
309
+ <SchemaTabs lazy>
310
+ <TabItem label="Default" value="default" default>
311
+ <LiveApp action={dispatch} language={language}>
312
+ {defaultBody}
313
+ </LiveApp>
314
+ </TabItem>
315
+ {examplesBodies.map((example: any) => {
316
+ return (
317
+ <TabItem
318
+ label={example.label}
319
+ value={example.label}
320
+ key={example.label}
321
+ >
322
+ {example.summary && <p>{example.summary}</p>}
323
+ {example.body && (
324
+ <LiveApp action={dispatch} language={language}>
325
+ {example.body}
326
+ </LiveApp>
327
+ )}
328
+ </TabItem>
329
+ );
330
+ })}
331
+ </SchemaTabs>
332
+ </FormItem>
333
+ );
334
+ }
335
+
236
336
  return (
237
337
  <FormItem label="Body" required={required}>
238
338
  <LiveApp action={dispatch} language={language}>
239
- {exampleBodyString}
339
+ {defaultBody}
240
340
  </LiveApp>
241
341
  </FormItem>
242
342
  );
@@ -7,7 +7,7 @@
7
7
 
8
8
  import React from "react";
9
9
 
10
- import { useTypedDispatch, useTypedSelector } from "../hooks";
10
+ import { useTypedDispatch, useTypedSelector } from "../../ApiItem/hooks";
11
11
  import FormItem from "./../FormItem";
12
12
  import FormSelect from "./../FormSelect";
13
13
  import { setContentType } from "./slice";
@@ -13,8 +13,8 @@ import sdk from "@paloaltonetworks/postman-collection";
13
13
  import CodeBlock from "@theme/CodeBlock";
14
14
  import clsx from "clsx";
15
15
 
16
+ import { useTypedSelector } from "../../ApiItem/hooks";
16
17
  import CodeTabs from "../CodeTabs";
17
- import { useTypedSelector } from "../hooks";
18
18
  import buildPostmanRequest from "./../buildPostmanRequest";
19
19
  import styles from "./styles.module.css";
20
20
 
@@ -10,7 +10,7 @@ import React from "react";
10
10
  import sdk from "@paloaltonetworks/postman-collection";
11
11
  import Modal from "react-modal";
12
12
 
13
- import { useTypedDispatch, useTypedSelector } from "../hooks";
13
+ import { useTypedDispatch, useTypedSelector } from "../../ApiItem/hooks";
14
14
  import { Param } from "../ParamOptions/slice";
15
15
  import { setResponse } from "../Response/slice";
16
16
  import buildPostmanRequest from "./../buildPostmanRequest";
@@ -9,7 +9,7 @@ import React, { useState, useEffect } from "react";
9
9
 
10
10
  import { nanoid } from "@reduxjs/toolkit";
11
11
 
12
- import { useTypedDispatch, useTypedSelector } from "../hooks";
12
+ import { useTypedDispatch, useTypedSelector } from "../../ApiItem/hooks";
13
13
  import FormItem from "./../FormItem";
14
14
  import FormMultiSelect from "./../FormMultiSelect";
15
15
  import FormSelect from "./../FormSelect";