docusaurus-theme-openapi-docs 4.5.1 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/markdown/schema.js +14 -1
- package/lib/theme/ApiExplorer/Accept/slice.d.ts +5 -2
- package/lib/theme/ApiExplorer/Authorization/index.js +50 -9
- package/lib/theme/ApiExplorer/Authorization/slice.d.ts +145 -3
- package/lib/theme/ApiExplorer/Authorization/slice.js +3 -1
- package/lib/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.d.ts +7 -0
- package/lib/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.js +126 -0
- package/lib/theme/ApiExplorer/Body/FormBodyItem/index.d.ts +9 -0
- package/lib/theme/ApiExplorer/Body/FormBodyItem/index.js +110 -0
- package/lib/theme/ApiExplorer/Body/index.js +322 -193
- package/lib/theme/ApiExplorer/Body/resolveSchemaWithSelections.d.ts +13 -0
- package/lib/theme/ApiExplorer/Body/resolveSchemaWithSelections.js +133 -0
- package/lib/theme/ApiExplorer/Body/slice.d.ts +1056 -11
- package/lib/theme/ApiExplorer/Body/slice.js +22 -2
- package/lib/theme/ApiExplorer/CodeSnippets/index.d.ts +2 -1
- package/lib/theme/ApiExplorer/CodeSnippets/index.js +37 -26
- package/lib/theme/ApiExplorer/CodeTabs/_CodeTabs.scss +5 -1
- package/lib/theme/ApiExplorer/CodeTabs/index.d.ts +3 -3
- package/lib/theme/ApiExplorer/CodeTabs/index.js +2 -2
- package/lib/theme/ApiExplorer/ContentType/slice.d.ts +5 -2
- package/lib/theme/ApiExplorer/FormFileUpload/index.js +6 -1
- package/lib/theme/ApiExplorer/FormItem/index.js +6 -1
- package/lib/theme/ApiExplorer/FormTextInput/index.d.ts +2 -0
- package/lib/theme/ApiExplorer/FormTextInput/index.js +8 -1
- package/lib/theme/ApiExplorer/LiveEditor/index.js +11 -4
- package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamArrayFormItem.js +15 -5
- package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamBooleanFormItem.js +11 -3
- package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamMultiSelectFormItem.js +12 -4
- package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamSelectFormItem.js +11 -2
- package/lib/theme/ApiExplorer/ParamOptions/index.js +11 -1
- package/lib/theme/ApiExplorer/ParamOptions/slice.d.ts +0 -4
- package/lib/theme/ApiExplorer/ParamOptions/slice.js +4 -4
- package/lib/theme/ApiExplorer/Request/index.js +110 -17
- package/lib/theme/ApiExplorer/Request/makeRequest.d.ts +7 -1
- package/lib/theme/ApiExplorer/Request/makeRequest.js +94 -24
- package/lib/theme/ApiExplorer/Response/index.js +34 -14
- package/lib/theme/ApiExplorer/Response/slice.d.ts +31 -7
- package/lib/theme/ApiExplorer/SchemaSelection/index.d.ts +2 -0
- package/lib/theme/ApiExplorer/SchemaSelection/index.js +36 -0
- package/lib/theme/ApiExplorer/SchemaSelection/slice.d.ts +37 -0
- package/lib/theme/ApiExplorer/SchemaSelection/slice.js +39 -0
- package/lib/theme/ApiExplorer/SecuritySchemes/index.js +208 -69
- package/lib/theme/ApiExplorer/Server/index.js +16 -2
- package/lib/theme/ApiExplorer/Server/slice.d.ts +49 -3
- package/lib/theme/ApiExplorer/buildPostmanRequest.js +46 -57
- package/lib/theme/ApiExplorer/index.js +4 -0
- package/lib/theme/ApiExplorer/persistenceMiddleware.d.ts +21 -0
- package/lib/theme/ApiExplorer/{persistanceMiddleware.js → persistenceMiddleware.js} +16 -9
- package/lib/theme/ApiExplorer/storage-utils.d.ts +2 -2
- package/lib/theme/ApiExplorer/storage-utils.js +3 -3
- package/lib/theme/ApiItem/Layout/index.d.ts +1 -1
- package/lib/theme/ApiItem/hooks.d.ts +10 -9
- package/lib/theme/ApiItem/index.js +13 -8
- package/lib/theme/ApiItem/store.d.ts +61 -43
- package/lib/theme/ApiItem/store.js +6 -2
- package/lib/theme/ApiTabs/index.js +6 -1
- package/lib/theme/Example/_Example.scss +11 -0
- package/lib/theme/Example/index.d.ts +24 -0
- package/lib/theme/Example/index.js +170 -0
- package/lib/theme/ParamsDetails/index.js +9 -1
- package/lib/theme/ParamsItem/index.d.ts +1 -1
- package/lib/theme/ParamsItem/index.js +43 -74
- package/lib/theme/RequestSchema/index.js +68 -48
- package/lib/theme/ResponseExamples/index.js +23 -3
- package/lib/theme/ResponseSchema/index.js +97 -82
- package/lib/theme/Schema/index.d.ts +6 -0
- package/lib/theme/Schema/index.js +240 -31
- package/lib/theme/SchemaItem/index.js +64 -36
- package/lib/theme/SchemaTabs/index.d.ts +8 -1
- package/lib/theme/SchemaTabs/index.js +14 -2
- package/lib/theme/StatusCodes/index.d.ts +1 -1
- package/lib/theme/StatusCodes/index.js +11 -2
- package/lib/theme/styles.scss +15 -0
- package/lib/theme/translationIds.d.ts +90 -0
- package/lib/theme/translationIds.js +114 -0
- package/package.json +28 -28
- package/src/markdown/schema.ts +17 -1
- package/src/theme/ApiExplorer/Authorization/index.tsx +51 -10
- package/src/theme/ApiExplorer/Authorization/slice.ts +1 -1
- package/src/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.tsx +77 -0
- package/src/theme/ApiExplorer/Body/FormBodyItem/index.tsx +120 -0
- package/src/theme/ApiExplorer/Body/index.tsx +262 -198
- package/{lib/types.js → src/theme/ApiExplorer/Body/json2xml.d.ts} +2 -2
- package/src/theme/ApiExplorer/Body/resolveSchemaWithSelections.ts +155 -0
- package/src/theme/ApiExplorer/Body/slice.ts +40 -1
- package/src/theme/ApiExplorer/CodeSnippets/index.tsx +43 -29
- package/src/theme/ApiExplorer/CodeTabs/_CodeTabs.scss +5 -1
- package/src/theme/ApiExplorer/CodeTabs/index.tsx +6 -5
- package/src/theme/ApiExplorer/ContentType/index.tsx +1 -1
- package/src/theme/ApiExplorer/FormFileUpload/index.tsx +6 -1
- package/src/theme/ApiExplorer/FormItem/index.tsx +8 -1
- package/src/theme/ApiExplorer/FormTextInput/index.tsx +10 -1
- package/src/theme/ApiExplorer/LiveEditor/index.tsx +11 -4
- package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamArrayFormItem.tsx +16 -6
- package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamBooleanFormItem.tsx +12 -4
- package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamMultiSelectFormItem.tsx +12 -4
- package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamSelectFormItem.tsx +12 -3
- package/src/theme/ApiExplorer/ParamOptions/index.tsx +10 -2
- package/src/theme/ApiExplorer/ParamOptions/slice.ts +1 -1
- package/src/theme/ApiExplorer/Request/index.tsx +108 -17
- package/src/theme/ApiExplorer/Request/makeRequest.ts +106 -25
- package/src/theme/ApiExplorer/Response/index.tsx +30 -8
- package/src/theme/ApiExplorer/SchemaSelection/index.ts +13 -0
- package/src/theme/ApiExplorer/SchemaSelection/slice.ts +46 -0
- package/src/theme/ApiExplorer/SecuritySchemes/index.tsx +157 -69
- package/src/theme/ApiExplorer/Server/index.tsx +12 -4
- package/src/theme/ApiExplorer/buildPostmanRequest.ts +47 -63
- package/src/theme/ApiExplorer/index.tsx +5 -0
- package/src/theme/ApiExplorer/{persistanceMiddleware.ts → persistenceMiddleware.ts} +23 -13
- package/src/theme/ApiExplorer/storage-utils.ts +4 -4
- package/src/theme/ApiItem/Layout/index.tsx +1 -1
- package/src/theme/ApiItem/index.tsx +13 -7
- package/src/theme/ApiItem/store.ts +2 -0
- package/src/theme/ApiTabs/index.tsx +6 -1
- package/src/theme/Example/_Example.scss +11 -0
- package/src/theme/Example/index.tsx +168 -0
- package/src/theme/Markdown/index.d.ts +8 -0
- package/src/theme/ParamsDetails/index.tsx +10 -1
- package/src/theme/ParamsItem/index.tsx +38 -54
- package/src/theme/RequestSchema/index.tsx +60 -35
- package/src/theme/ResponseExamples/index.tsx +23 -3
- package/src/theme/ResponseSchema/index.tsx +73 -61
- package/src/theme/Schema/index.tsx +307 -55
- package/src/theme/SchemaItem/index.tsx +51 -33
- package/src/theme/SchemaTabs/index.tsx +19 -5
- package/src/theme/StatusCodes/index.tsx +13 -3
- package/src/theme/styles.scss +15 -0
- package/src/theme/translationIds.ts +111 -0
- package/src/theme-openapi.d.ts +7 -275
- package/src/{types.ts → types.d.ts} +9 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/lib/theme/ApiExplorer/persistanceMiddleware.d.ts +0 -3
- package/lib/types.d.ts +0 -46
|
@@ -5,28 +5,26 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
* ========================================================================== */
|
|
7
7
|
|
|
8
|
-
import React from "react";
|
|
8
|
+
import React, { useEffect, useMemo } from "react";
|
|
9
|
+
|
|
10
|
+
import { translate } from "@docusaurus/Translate";
|
|
9
11
|
|
|
10
12
|
import json2xml from "@theme/ApiExplorer/Body/json2xml";
|
|
11
13
|
import FormFileUpload from "@theme/ApiExplorer/FormFileUpload";
|
|
12
14
|
import FormItem from "@theme/ApiExplorer/FormItem";
|
|
13
|
-
import FormSelect from "@theme/ApiExplorer/FormSelect";
|
|
14
|
-
import FormTextInput from "@theme/ApiExplorer/FormTextInput";
|
|
15
15
|
import LiveApp from "@theme/ApiExplorer/LiveEditor";
|
|
16
16
|
import { useTypedDispatch, useTypedSelector } from "@theme/ApiItem/hooks";
|
|
17
17
|
import Markdown from "@theme/Markdown";
|
|
18
18
|
import SchemaTabs from "@theme/SchemaTabs";
|
|
19
19
|
import TabItem from "@theme/TabItem";
|
|
20
|
+
import { OPENAPI_BODY, OPENAPI_REQUEST } from "@theme/translationIds";
|
|
20
21
|
import { RequestBodyObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
|
|
22
|
+
import { sampleFromSchema } from "docusaurus-plugin-openapi-docs/src/openapi/createSchemaExample";
|
|
21
23
|
import format from "xml-formatter";
|
|
22
24
|
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
setFileFormBody,
|
|
27
|
-
setFileRawBody,
|
|
28
|
-
setStringFormBody,
|
|
29
|
-
} from "./slice";
|
|
25
|
+
import { clearRawBody, setFileRawBody, setStringRawBody } from "./slice";
|
|
26
|
+
import FormBodyItem from "./FormBodyItem";
|
|
27
|
+
import { resolveSchemaWithSelections } from "./resolveSchemaWithSelections";
|
|
30
28
|
|
|
31
29
|
export interface Props {
|
|
32
30
|
jsonRequestBodyExample: string;
|
|
@@ -67,6 +65,9 @@ function Body({
|
|
|
67
65
|
required,
|
|
68
66
|
}: Props) {
|
|
69
67
|
const contentType = useTypedSelector((state: any) => state.contentType.value);
|
|
68
|
+
const schemaSelections = useTypedSelector(
|
|
69
|
+
(state: any) => state.schemaSelection?.selections ?? {}
|
|
70
|
+
);
|
|
70
71
|
const dispatch = useTypedDispatch();
|
|
71
72
|
|
|
72
73
|
// Lot's of possible content-types:
|
|
@@ -90,15 +91,208 @@ function Body({
|
|
|
90
91
|
// - multipart/form-data
|
|
91
92
|
// - application/x-www-form-urlencoded
|
|
92
93
|
|
|
93
|
-
const
|
|
94
|
+
const rawSchema = requestBodyMetadata?.content?.[contentType]?.schema;
|
|
94
95
|
const example = requestBodyMetadata?.content?.[contentType]?.example;
|
|
95
96
|
const examples = requestBodyMetadata?.content?.[contentType]?.examples;
|
|
96
97
|
|
|
98
|
+
// Resolve the schema based on user's anyOf/oneOf tab selections
|
|
99
|
+
const schema = useMemo(() => {
|
|
100
|
+
if (!rawSchema) return rawSchema;
|
|
101
|
+
return resolveSchemaWithSelections(
|
|
102
|
+
rawSchema,
|
|
103
|
+
schemaSelections,
|
|
104
|
+
"requestBody"
|
|
105
|
+
);
|
|
106
|
+
}, [rawSchema, schemaSelections]);
|
|
107
|
+
|
|
108
|
+
// OpenAPI 3.1 / JSON Schema: schema.examples is an array of example values
|
|
109
|
+
const schemaExamples = schema?.examples as any[] | undefined;
|
|
110
|
+
|
|
111
|
+
// Compute the default body based on content type and schema
|
|
112
|
+
// This needs to be computed before early returns so the useEffect can use it
|
|
113
|
+
const { defaultBody, exampleBody, examplesBodies, language } = useMemo(() => {
|
|
114
|
+
let lang = "plaintext";
|
|
115
|
+
let defBody = "";
|
|
116
|
+
let exBody;
|
|
117
|
+
let exBodies = [] as any;
|
|
118
|
+
|
|
119
|
+
// Skip body generation for binary and form content types
|
|
120
|
+
if (schema?.format === "binary") {
|
|
121
|
+
return {
|
|
122
|
+
defaultBody: defBody,
|
|
123
|
+
exampleBody: exBody,
|
|
124
|
+
examplesBodies: exBodies,
|
|
125
|
+
language: lang,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (
|
|
129
|
+
(contentType === "multipart/form-data" ||
|
|
130
|
+
contentType === "application/x-www-form-urlencoded") &&
|
|
131
|
+
schema?.type === "object"
|
|
132
|
+
) {
|
|
133
|
+
return {
|
|
134
|
+
defaultBody: defBody,
|
|
135
|
+
exampleBody: exBody,
|
|
136
|
+
examplesBodies: exBodies,
|
|
137
|
+
language: lang,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Generate example from the schema for the current content type
|
|
142
|
+
let contentTypeExample;
|
|
143
|
+
if (schema) {
|
|
144
|
+
contentTypeExample = sampleFromSchema(schema, { type: "request" });
|
|
145
|
+
} else if (jsonRequestBodyExample) {
|
|
146
|
+
// Fallback to the build-time generated example if no schema is available
|
|
147
|
+
contentTypeExample = jsonRequestBodyExample;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (
|
|
151
|
+
contentType?.includes("application/json") ||
|
|
152
|
+
contentType?.endsWith("+json")
|
|
153
|
+
) {
|
|
154
|
+
if (contentTypeExample) {
|
|
155
|
+
defBody = JSON.stringify(contentTypeExample, null, 2);
|
|
156
|
+
}
|
|
157
|
+
if (example) {
|
|
158
|
+
exBody = JSON.stringify(example, null, 2);
|
|
159
|
+
}
|
|
160
|
+
if (examples) {
|
|
161
|
+
for (const [key, ex] of Object.entries(examples)) {
|
|
162
|
+
let body = ex.value;
|
|
163
|
+
try {
|
|
164
|
+
// If the value is already valid JSON we shouldn't double encode the value
|
|
165
|
+
JSON.parse(ex.value);
|
|
166
|
+
} catch (e) {
|
|
167
|
+
body = JSON.stringify(ex.value, null, 2);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
exBodies.push({
|
|
171
|
+
label: key,
|
|
172
|
+
body,
|
|
173
|
+
summary: ex.summary,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// OpenAPI 3.1: schema.examples is an array of example values
|
|
178
|
+
if (schemaExamples && Array.isArray(schemaExamples)) {
|
|
179
|
+
schemaExamples.forEach((schemaExample, index) => {
|
|
180
|
+
const body = JSON.stringify(schemaExample, null, 2);
|
|
181
|
+
exBodies.push({
|
|
182
|
+
label: `Example ${index + 1}`,
|
|
183
|
+
body,
|
|
184
|
+
summary: undefined,
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
lang = "json";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (contentType === "application/xml" || contentType?.endsWith("+xml")) {
|
|
192
|
+
if (contentTypeExample) {
|
|
193
|
+
try {
|
|
194
|
+
defBody = format(json2xml(contentTypeExample, ""), {
|
|
195
|
+
indentation: " ",
|
|
196
|
+
lineSeparator: "\n",
|
|
197
|
+
collapseContent: true,
|
|
198
|
+
});
|
|
199
|
+
} catch {
|
|
200
|
+
defBody = json2xml(contentTypeExample);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (example) {
|
|
204
|
+
try {
|
|
205
|
+
exBody = format(json2xml(example, ""), {
|
|
206
|
+
indentation: " ",
|
|
207
|
+
lineSeparator: "\n",
|
|
208
|
+
collapseContent: true,
|
|
209
|
+
});
|
|
210
|
+
} catch {
|
|
211
|
+
exBody = json2xml(example);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (examples) {
|
|
215
|
+
for (const [key, ex] of Object.entries(examples)) {
|
|
216
|
+
let formattedXmlBody;
|
|
217
|
+
try {
|
|
218
|
+
formattedXmlBody = format(ex.value, {
|
|
219
|
+
indentation: " ",
|
|
220
|
+
lineSeparator: "\n",
|
|
221
|
+
collapseContent: true,
|
|
222
|
+
});
|
|
223
|
+
} catch {
|
|
224
|
+
formattedXmlBody = ex.value;
|
|
225
|
+
}
|
|
226
|
+
exBodies.push({
|
|
227
|
+
label: key,
|
|
228
|
+
body: formattedXmlBody,
|
|
229
|
+
summary: ex.summary,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
// OpenAPI 3.1: schema.examples is an array of example values
|
|
234
|
+
if (schemaExamples && Array.isArray(schemaExamples)) {
|
|
235
|
+
schemaExamples.forEach((schemaExample, index) => {
|
|
236
|
+
let formattedXmlBody;
|
|
237
|
+
try {
|
|
238
|
+
formattedXmlBody = format(json2xml(schemaExample, ""), {
|
|
239
|
+
indentation: " ",
|
|
240
|
+
lineSeparator: "\n",
|
|
241
|
+
collapseContent: true,
|
|
242
|
+
});
|
|
243
|
+
} catch {
|
|
244
|
+
formattedXmlBody = json2xml(schemaExample);
|
|
245
|
+
}
|
|
246
|
+
exBodies.push({
|
|
247
|
+
label: `Example ${index + 1}`,
|
|
248
|
+
body: formattedXmlBody,
|
|
249
|
+
summary: undefined,
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
lang = "xml";
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
defaultBody: defBody,
|
|
258
|
+
exampleBody: exBody,
|
|
259
|
+
examplesBodies: exBodies,
|
|
260
|
+
language: lang,
|
|
261
|
+
};
|
|
262
|
+
}, [
|
|
263
|
+
schema,
|
|
264
|
+
contentType,
|
|
265
|
+
example,
|
|
266
|
+
examples,
|
|
267
|
+
schemaExamples,
|
|
268
|
+
jsonRequestBodyExample,
|
|
269
|
+
]);
|
|
270
|
+
|
|
271
|
+
// Create a stable key for the LiveApp component that changes when schema selection changes
|
|
272
|
+
// This forces the editor to remount and pick up the new defaultBody
|
|
273
|
+
const schemaSelectionKey = useMemo(
|
|
274
|
+
() => JSON.stringify(schemaSelections),
|
|
275
|
+
[schemaSelections]
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
// Update body in Redux when content type or schema selection changes
|
|
279
|
+
useEffect(() => {
|
|
280
|
+
if (defaultBody) {
|
|
281
|
+
dispatch(setStringRawBody(defaultBody));
|
|
282
|
+
}
|
|
283
|
+
// Re-run when contentType, schemaSelections, or defaultBody change
|
|
284
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
285
|
+
}, [contentType, schemaSelections, defaultBody]);
|
|
286
|
+
|
|
287
|
+
// Now handle early returns after all hooks have been called
|
|
97
288
|
if (schema?.format === "binary") {
|
|
98
289
|
return (
|
|
99
290
|
<FormItem>
|
|
100
291
|
<FormFileUpload
|
|
101
|
-
placeholder={
|
|
292
|
+
placeholder={
|
|
293
|
+
schema.description ||
|
|
294
|
+
translate({ id: OPENAPI_REQUEST.BODY_TITLE, message: "Body" })
|
|
295
|
+
}
|
|
102
296
|
onChange={(file: any) => {
|
|
103
297
|
if (file === undefined) {
|
|
104
298
|
dispatch(clearRawBody());
|
|
@@ -115,6 +309,7 @@ function Body({
|
|
|
115
309
|
</FormItem>
|
|
116
310
|
);
|
|
117
311
|
}
|
|
312
|
+
|
|
118
313
|
if (
|
|
119
314
|
(contentType === "multipart/form-data" ||
|
|
120
315
|
contentType === "application/x-www-form-urlencoded") &&
|
|
@@ -122,191 +317,46 @@ function Body({
|
|
|
122
317
|
) {
|
|
123
318
|
return (
|
|
124
319
|
<FormItem className="openapi-explorer__form-item-body-container">
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
dispatch(
|
|
145
|
-
setFileFormBody({
|
|
146
|
-
key: key,
|
|
147
|
-
value: {
|
|
148
|
-
src: `/path/to/${file.name}`,
|
|
149
|
-
content: file,
|
|
150
|
-
},
|
|
151
|
-
})
|
|
152
|
-
);
|
|
153
|
-
}}
|
|
154
|
-
/>
|
|
155
|
-
</FormItem>
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (val.enum) {
|
|
160
|
-
return (
|
|
161
|
-
<FormItem
|
|
162
|
-
key={key}
|
|
163
|
-
label={key}
|
|
164
|
-
required={
|
|
165
|
-
Array.isArray(schema.required) &&
|
|
166
|
-
schema.required.includes(key)
|
|
167
|
-
}
|
|
168
|
-
>
|
|
169
|
-
<FormSelect
|
|
170
|
-
options={["---", ...val.enum]}
|
|
171
|
-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
172
|
-
const val = e.target.value;
|
|
173
|
-
if (val === "---") {
|
|
174
|
-
dispatch(clearFormBodyKey(key));
|
|
175
|
-
} else {
|
|
176
|
-
dispatch(
|
|
177
|
-
setStringFormBody({
|
|
178
|
-
key: key,
|
|
179
|
-
value: val,
|
|
180
|
-
})
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
}}
|
|
184
|
-
/>
|
|
185
|
-
</FormItem>
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
// TODO: support all the other types.
|
|
189
|
-
return (
|
|
190
|
-
<FormItem
|
|
191
|
-
key={key}
|
|
192
|
-
label={key}
|
|
193
|
-
required={
|
|
194
|
-
Array.isArray(schema.required) &&
|
|
195
|
-
schema.required.includes(key)
|
|
196
|
-
}
|
|
197
|
-
>
|
|
198
|
-
<FormTextInput
|
|
199
|
-
paramName={key}
|
|
200
|
-
isRequired={
|
|
201
|
-
Array.isArray(schema.required) &&
|
|
202
|
-
schema.required.includes(key)
|
|
203
|
-
}
|
|
204
|
-
placeholder={val.description || key}
|
|
205
|
-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
206
|
-
dispatch(
|
|
207
|
-
setStringFormBody({ key: key, value: e.target.value })
|
|
208
|
-
);
|
|
209
|
-
}}
|
|
210
|
-
/>
|
|
211
|
-
</FormItem>
|
|
212
|
-
);
|
|
213
|
-
})}
|
|
214
|
-
</div>
|
|
320
|
+
{Object.entries(schema.properties ?? {}).map(([key, val]: any) => {
|
|
321
|
+
return (
|
|
322
|
+
<FormItem
|
|
323
|
+
key={key}
|
|
324
|
+
label={key}
|
|
325
|
+
required={
|
|
326
|
+
Array.isArray(schema.required) && schema.required.includes(key)
|
|
327
|
+
}
|
|
328
|
+
>
|
|
329
|
+
<FormBodyItem
|
|
330
|
+
schemaObject={val}
|
|
331
|
+
id={key}
|
|
332
|
+
schema={schema}
|
|
333
|
+
></FormBodyItem>
|
|
334
|
+
</FormItem>
|
|
335
|
+
);
|
|
336
|
+
})}
|
|
215
337
|
</FormItem>
|
|
216
338
|
);
|
|
217
339
|
}
|
|
218
340
|
|
|
219
|
-
let language = "plaintext";
|
|
220
|
-
let defaultBody = ""; //"body content";
|
|
221
|
-
let exampleBody;
|
|
222
|
-
let examplesBodies = [] as any;
|
|
223
|
-
|
|
224
|
-
if (
|
|
225
|
-
contentType.includes("application/json") ||
|
|
226
|
-
contentType.endsWith("+json")
|
|
227
|
-
) {
|
|
228
|
-
if (jsonRequestBodyExample) {
|
|
229
|
-
defaultBody = JSON.stringify(jsonRequestBodyExample, null, 2);
|
|
230
|
-
}
|
|
231
|
-
if (example) {
|
|
232
|
-
exampleBody = JSON.stringify(example, null, 2);
|
|
233
|
-
}
|
|
234
|
-
if (examples) {
|
|
235
|
-
for (const [key, example] of Object.entries(examples)) {
|
|
236
|
-
let body = example.value;
|
|
237
|
-
try {
|
|
238
|
-
// If the value is already valid JSON we shouldn't double encode the value
|
|
239
|
-
JSON.parse(example.value);
|
|
240
|
-
} catch (e) {
|
|
241
|
-
body = JSON.stringify(example.value, null, 2);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
examplesBodies.push({
|
|
245
|
-
label: key,
|
|
246
|
-
body,
|
|
247
|
-
summary: example.summary,
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
language = "json";
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (contentType === "application/xml" || contentType.endsWith("+xml")) {
|
|
255
|
-
if (jsonRequestBodyExample) {
|
|
256
|
-
try {
|
|
257
|
-
defaultBody = format(json2xml(jsonRequestBodyExample, ""), {
|
|
258
|
-
indentation: " ",
|
|
259
|
-
lineSeparator: "\n",
|
|
260
|
-
collapseContent: true,
|
|
261
|
-
});
|
|
262
|
-
} catch {
|
|
263
|
-
defaultBody = json2xml(jsonRequestBodyExample);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
if (example) {
|
|
267
|
-
try {
|
|
268
|
-
exampleBody = format(json2xml(example, ""), {
|
|
269
|
-
indentation: " ",
|
|
270
|
-
lineSeparator: "\n",
|
|
271
|
-
collapseContent: true,
|
|
272
|
-
});
|
|
273
|
-
} catch {
|
|
274
|
-
exampleBody = json2xml(example);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
if (examples) {
|
|
278
|
-
for (const [key, example] of Object.entries(examples)) {
|
|
279
|
-
let formattedXmlBody;
|
|
280
|
-
try {
|
|
281
|
-
formattedXmlBody = format(example.value, {
|
|
282
|
-
indentation: " ",
|
|
283
|
-
lineSeparator: "\n",
|
|
284
|
-
collapseContent: true,
|
|
285
|
-
});
|
|
286
|
-
} catch {
|
|
287
|
-
formattedXmlBody = example.value;
|
|
288
|
-
}
|
|
289
|
-
examplesBodies.push({
|
|
290
|
-
label: key,
|
|
291
|
-
body: formattedXmlBody,
|
|
292
|
-
summary: example.summary,
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
language = "xml";
|
|
297
|
-
}
|
|
298
|
-
|
|
299
341
|
if (exampleBody) {
|
|
300
342
|
return (
|
|
301
343
|
<FormItem>
|
|
302
344
|
<SchemaTabs className="openapi-tabs__schema" lazy>
|
|
303
345
|
{/* @ts-ignore */}
|
|
304
346
|
<TabItem
|
|
305
|
-
label=
|
|
347
|
+
label={translate({
|
|
348
|
+
id: OPENAPI_BODY.EXAMPLE_FROM_SCHEMA,
|
|
349
|
+
message: "Example (from schema)",
|
|
350
|
+
})}
|
|
306
351
|
value="Example (from schema)"
|
|
307
352
|
default
|
|
308
353
|
>
|
|
309
|
-
<LiveApp
|
|
354
|
+
<LiveApp
|
|
355
|
+
key={`${contentType}-${schemaSelectionKey}`}
|
|
356
|
+
action={(code: string) => dispatch(setStringRawBody(code))}
|
|
357
|
+
language={language}
|
|
358
|
+
required={required}
|
|
359
|
+
>
|
|
310
360
|
{defaultBody}
|
|
311
361
|
</LiveApp>
|
|
312
362
|
</TabItem>
|
|
@@ -315,7 +365,8 @@ function Body({
|
|
|
315
365
|
{example.summary && <Markdown>{example.summary}</Markdown>}
|
|
316
366
|
{exampleBody && (
|
|
317
367
|
<LiveApp
|
|
318
|
-
|
|
368
|
+
key={`${contentType}-example`}
|
|
369
|
+
action={(code: string) => dispatch(setStringRawBody(code))}
|
|
319
370
|
language={language}
|
|
320
371
|
required={required}
|
|
321
372
|
>
|
|
@@ -334,26 +385,34 @@ function Body({
|
|
|
334
385
|
<SchemaTabs className="openapi-tabs__schema" lazy>
|
|
335
386
|
{/* @ts-ignore */}
|
|
336
387
|
<TabItem
|
|
337
|
-
label=
|
|
388
|
+
label={translate({
|
|
389
|
+
id: OPENAPI_BODY.EXAMPLE_FROM_SCHEMA,
|
|
390
|
+
message: "Example (from schema)",
|
|
391
|
+
})}
|
|
338
392
|
value="Example (from schema)"
|
|
339
393
|
default
|
|
340
394
|
>
|
|
341
|
-
<LiveApp
|
|
395
|
+
<LiveApp
|
|
396
|
+
key={`${contentType}-${schemaSelectionKey}`}
|
|
397
|
+
action={(code: string) => dispatch(setStringRawBody(code))}
|
|
398
|
+
language={language}
|
|
399
|
+
required={required}
|
|
400
|
+
>
|
|
342
401
|
{defaultBody}
|
|
343
402
|
</LiveApp>
|
|
344
403
|
</TabItem>
|
|
345
|
-
{examplesBodies.map((
|
|
404
|
+
{examplesBodies.map((ex: any) => {
|
|
346
405
|
return (
|
|
347
406
|
// @ts-ignore
|
|
348
|
-
<TabItem
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
{
|
|
407
|
+
<TabItem label={ex.label} value={ex.label} key={ex.label}>
|
|
408
|
+
{ex.summary && <Markdown>{ex.summary}</Markdown>}
|
|
409
|
+
{ex.body && (
|
|
410
|
+
<LiveApp
|
|
411
|
+
key={`${contentType}-${ex.label}`}
|
|
412
|
+
action={(code: string) => dispatch(setStringRawBody(code))}
|
|
413
|
+
language={language}
|
|
414
|
+
>
|
|
415
|
+
{ex.body}
|
|
357
416
|
</LiveApp>
|
|
358
417
|
)}
|
|
359
418
|
</TabItem>
|
|
@@ -366,7 +425,12 @@ function Body({
|
|
|
366
425
|
|
|
367
426
|
return (
|
|
368
427
|
<FormItem>
|
|
369
|
-
<LiveApp
|
|
428
|
+
<LiveApp
|
|
429
|
+
key={`${contentType}-${schemaSelectionKey}`}
|
|
430
|
+
action={(code: string) => dispatch(setStringRawBody(code))}
|
|
431
|
+
language={language}
|
|
432
|
+
required={required}
|
|
433
|
+
>
|
|
370
434
|
{defaultBody}
|
|
371
435
|
</LiveApp>
|
|
372
436
|
</FormItem>
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/* ============================================================================
|
|
3
2
|
* Copyright (c) Palo Alto Networks
|
|
4
3
|
*
|
|
5
4
|
* This source code is licensed under the MIT license found in the
|
|
6
5
|
* LICENSE file in the root directory of this source tree.
|
|
7
6
|
* ========================================================================== */
|
|
8
|
-
|
|
7
|
+
|
|
8
|
+
export default function json2xml(input: any, indent?: any): any;
|