docusaurus-plugin-openapi-docs 1.0.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/LICENSE +21 -0
- package/README.md +194 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +194 -0
- package/lib/markdown/createDeprecationNotice.d.ts +6 -0
- package/lib/markdown/createDeprecationNotice.js +19 -0
- package/lib/markdown/createDescription.d.ts +1 -0
- package/lib/markdown/createDescription.js +16 -0
- package/lib/markdown/createDetails.d.ts +2 -0
- package/lib/markdown/createDetails.js +18 -0
- package/lib/markdown/createDetailsSummary.d.ts +2 -0
- package/lib/markdown/createDetailsSummary.js +18 -0
- package/lib/markdown/createFullWidthTable.d.ts +2 -0
- package/lib/markdown/createFullWidthTable.js +18 -0
- package/lib/markdown/createParamsDetails.d.ts +7 -0
- package/lib/markdown/createParamsDetails.js +44 -0
- package/lib/markdown/createParamsTable.d.ts +7 -0
- package/lib/markdown/createParamsTable.js +80 -0
- package/lib/markdown/createRequestBodyDetails.d.ts +6 -0
- package/lib/markdown/createRequestBodyDetails.js +14 -0
- package/lib/markdown/createRequestBodyTable.d.ts +6 -0
- package/lib/markdown/createRequestBodyTable.js +14 -0
- package/lib/markdown/createSchemaDetails.d.ts +14 -0
- package/lib/markdown/createSchemaDetails.js +241 -0
- package/lib/markdown/createSchemaTable.d.ts +14 -0
- package/lib/markdown/createSchemaTable.js +217 -0
- package/lib/markdown/createStatusCodes.d.ts +6 -0
- package/lib/markdown/createStatusCodes.js +47 -0
- package/lib/markdown/createVersionBadge.d.ts +1 -0
- package/lib/markdown/createVersionBadge.js +20 -0
- package/lib/markdown/index.d.ts +3 -0
- package/lib/markdown/index.js +43 -0
- package/lib/markdown/schema.d.ts +3 -0
- package/lib/markdown/schema.js +100 -0
- package/lib/markdown/schema.test.d.ts +1 -0
- package/lib/markdown/schema.test.js +171 -0
- package/lib/markdown/utils.d.ts +7 -0
- package/lib/markdown/utils.js +33 -0
- package/lib/openapi/createExample.d.ts +2 -0
- package/lib/openapi/createExample.js +113 -0
- package/lib/openapi/index.d.ts +1 -0
- package/lib/openapi/index.js +12 -0
- package/lib/openapi/openapi.d.ts +11 -0
- package/lib/openapi/openapi.js +233 -0
- package/lib/openapi/openapi.test.d.ts +1 -0
- package/lib/openapi/openapi.test.js +33 -0
- package/lib/openapi/types.d.ts +331 -0
- package/lib/openapi/types.js +8 -0
- package/lib/options.d.ts +4 -0
- package/lib/options.js +18 -0
- package/lib/sidebars/index.d.ts +3 -0
- package/lib/sidebars/index.js +89 -0
- package/lib/types.d.ts +68 -0
- package/lib/types.js +8 -0
- package/package.json +58 -0
- package/src/index.ts +244 -0
- package/src/markdown/createDeprecationNotice.ts +30 -0
- package/src/markdown/createDescription.ts +13 -0
- package/src/markdown/createDetails.ts +16 -0
- package/src/markdown/createDetailsSummary.ts +16 -0
- package/src/markdown/createFullWidthTable.ts +16 -0
- package/src/markdown/createParamsDetails.ts +53 -0
- package/src/markdown/createParamsTable.ts +102 -0
- package/src/markdown/createRequestBodyDetails.ts +17 -0
- package/src/markdown/createRequestBodyTable.ts +17 -0
- package/src/markdown/createSchemaDetails.ts +302 -0
- package/src/markdown/createSchemaTable.ts +275 -0
- package/src/markdown/createStatusCodes.ts +52 -0
- package/src/markdown/createVersionBadge.ts +18 -0
- package/src/markdown/index.ts +55 -0
- package/src/markdown/schema.test.ts +196 -0
- package/src/markdown/schema.ts +115 -0
- package/src/markdown/utils.ts +39 -0
- package/src/openapi/__fixtures__/examples/openapi.yaml +13 -0
- package/src/openapi/__fixtures__/examples/yogurtstore/_category_.json +4 -0
- package/src/openapi/__fixtures__/examples/yogurtstore/froyo.yaml +13 -0
- package/src/openapi/__fixtures__/examples/yogurtstore/nested/nested.yaml +13 -0
- package/src/openapi/createExample.ts +143 -0
- package/src/openapi/index.ts +8 -0
- package/src/openapi/openapi.test.ts +37 -0
- package/src/openapi/openapi.ts +293 -0
- package/src/openapi/types.ts +430 -0
- package/src/openapi-to-postmanv2.d.ts +10 -0
- package/src/options.ts +20 -0
- package/src/plugin-content-docs-types.d.ts +42 -0
- package/src/plugin-openapi.d.ts +87 -0
- package/src/sidebars/index.ts +121 -0
- package/src/types.ts +97 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/* ============================================================================
|
|
2
|
+
* Copyright (c) Palo Alto Networks
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
* ========================================================================== */
|
|
7
|
+
|
|
8
|
+
import { escape } from "lodash";
|
|
9
|
+
|
|
10
|
+
import { ApiItem } from "../types";
|
|
11
|
+
import { createDescription } from "./createDescription";
|
|
12
|
+
import { createFullWidthTable } from "./createFullWidthTable";
|
|
13
|
+
import { getQualifierMessage, getSchemaName } from "./schema";
|
|
14
|
+
import { create, guard } from "./utils";
|
|
15
|
+
|
|
16
|
+
interface Props {
|
|
17
|
+
parameters: ApiItem["parameters"];
|
|
18
|
+
type: "path" | "query" | "header" | "cookie";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createParamsTable({ parameters, type }: Props) {
|
|
22
|
+
if (parameters === undefined) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const params = parameters.filter((param) => param?.in === type);
|
|
26
|
+
if (params.length === 0) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return createFullWidthTable({
|
|
31
|
+
children: [
|
|
32
|
+
create("thead", {
|
|
33
|
+
children: create("tr", {
|
|
34
|
+
children: create("th", {
|
|
35
|
+
style: { textAlign: "left" },
|
|
36
|
+
children: `${
|
|
37
|
+
type.charAt(0).toUpperCase() + type.slice(1)
|
|
38
|
+
} Parameters`,
|
|
39
|
+
}),
|
|
40
|
+
}),
|
|
41
|
+
}),
|
|
42
|
+
create("tbody", {
|
|
43
|
+
children: params.map((param) =>
|
|
44
|
+
create("tr", {
|
|
45
|
+
children: create("td", {
|
|
46
|
+
children: [
|
|
47
|
+
create("code", { children: param.name }),
|
|
48
|
+
guard(param.schema, (schema) =>
|
|
49
|
+
create("span", {
|
|
50
|
+
style: { opacity: "0.6" },
|
|
51
|
+
children: ` ${getSchemaName(schema)}`,
|
|
52
|
+
})
|
|
53
|
+
),
|
|
54
|
+
guard(param.required, () => [
|
|
55
|
+
create("span", {
|
|
56
|
+
style: { opacity: "0.6" },
|
|
57
|
+
children: " — ",
|
|
58
|
+
}),
|
|
59
|
+
create("strong", {
|
|
60
|
+
style: {
|
|
61
|
+
fontSize: "var(--ifm-code-font-size)",
|
|
62
|
+
color: "var(--openapi-required)",
|
|
63
|
+
},
|
|
64
|
+
children: " REQUIRED",
|
|
65
|
+
}),
|
|
66
|
+
]),
|
|
67
|
+
guard(getQualifierMessage(param.schema), (message) =>
|
|
68
|
+
create("div", {
|
|
69
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
70
|
+
children: createDescription(message),
|
|
71
|
+
})
|
|
72
|
+
),
|
|
73
|
+
guard(param.description, (description) =>
|
|
74
|
+
create("div", {
|
|
75
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
76
|
+
children: createDescription(description),
|
|
77
|
+
})
|
|
78
|
+
),
|
|
79
|
+
guard(param.example, (example) =>
|
|
80
|
+
create("div", {
|
|
81
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
82
|
+
children: escape(`Example: ${example}`),
|
|
83
|
+
})
|
|
84
|
+
),
|
|
85
|
+
guard(param.examples, (examples) =>
|
|
86
|
+
create("div", {
|
|
87
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
88
|
+
children: Object.entries(examples).map(([k, v]) =>
|
|
89
|
+
create("div", {
|
|
90
|
+
children: escape(`Example (${k}): ${v.value}`),
|
|
91
|
+
})
|
|
92
|
+
),
|
|
93
|
+
})
|
|
94
|
+
),
|
|
95
|
+
],
|
|
96
|
+
}),
|
|
97
|
+
})
|
|
98
|
+
),
|
|
99
|
+
}),
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* ============================================================================
|
|
2
|
+
* Copyright (c) Palo Alto Networks
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
* ========================================================================== */
|
|
7
|
+
|
|
8
|
+
import { createSchemaDetails } from "./createSchemaDetails";
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
title: string;
|
|
12
|
+
body: any;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createRequestBodyDetails({ title, body }: Props) {
|
|
16
|
+
return createSchemaDetails({ title, body });
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* ============================================================================
|
|
2
|
+
* Copyright (c) Palo Alto Networks
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
* ========================================================================== */
|
|
7
|
+
|
|
8
|
+
import { createSchemaTable } from "./createSchemaTable";
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
title: string;
|
|
12
|
+
body: any;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createRequestBodyTable({ title, body }: Props) {
|
|
16
|
+
return createSchemaTable({ title, body });
|
|
17
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/* ============================================================================
|
|
2
|
+
* Copyright (c) Palo Alto Networks
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
* ========================================================================== */
|
|
7
|
+
|
|
8
|
+
import { MediaTypeObject, SchemaObject } from "../openapi/types";
|
|
9
|
+
import { createDescription } from "./createDescription";
|
|
10
|
+
import { createDetails } from "./createDetails";
|
|
11
|
+
import { createDetailsSummary } from "./createDetailsSummary";
|
|
12
|
+
import { getQualifierMessage, getSchemaName } from "./schema";
|
|
13
|
+
import { create, guard } from "./utils";
|
|
14
|
+
|
|
15
|
+
const mergeAllOf = require("json-schema-merge-allof");
|
|
16
|
+
|
|
17
|
+
function resolveAllOf(allOf: SchemaObject[]) {
|
|
18
|
+
// Use external library to resolve and merge nested allOf schemas
|
|
19
|
+
let properties: SchemaObject = {};
|
|
20
|
+
const mergedSchemas = mergeAllOf(allOf, {
|
|
21
|
+
resolvers: {
|
|
22
|
+
readOnly: function () {
|
|
23
|
+
return true;
|
|
24
|
+
},
|
|
25
|
+
example: function () {
|
|
26
|
+
return true;
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (mergedSchemas.properties) {
|
|
32
|
+
properties = mergedSchemas.properties;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const required = allOf.reduce((acc, cur) => {
|
|
36
|
+
if (Array.isArray(cur.required)) {
|
|
37
|
+
const next = [...acc, ...cur.required];
|
|
38
|
+
return next;
|
|
39
|
+
}
|
|
40
|
+
return acc;
|
|
41
|
+
}, [] as string[]);
|
|
42
|
+
|
|
43
|
+
return { properties, required };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface RowProps {
|
|
47
|
+
name: string;
|
|
48
|
+
schema: SchemaObject;
|
|
49
|
+
required: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createRow({ name, schema, required }: RowProps) {
|
|
53
|
+
const schemaName = getSchemaName(schema, true);
|
|
54
|
+
if (schemaName && (schemaName === "object" || schemaName === "object[]")) {
|
|
55
|
+
return create("SchemaItem", {
|
|
56
|
+
collapsible: true,
|
|
57
|
+
className: "schemaItem",
|
|
58
|
+
children: [
|
|
59
|
+
createDetails({
|
|
60
|
+
children: [
|
|
61
|
+
createDetailsSummary({
|
|
62
|
+
children: [
|
|
63
|
+
create("strong", { children: name }),
|
|
64
|
+
create("span", {
|
|
65
|
+
style: { opacity: "0.6" },
|
|
66
|
+
children: ` ${schemaName}`,
|
|
67
|
+
}),
|
|
68
|
+
guard(required, () => [
|
|
69
|
+
create("strong", {
|
|
70
|
+
style: {
|
|
71
|
+
fontSize: "var(--ifm-code-font-size)",
|
|
72
|
+
color: "var(--openapi-required)",
|
|
73
|
+
},
|
|
74
|
+
children: " required",
|
|
75
|
+
}),
|
|
76
|
+
]),
|
|
77
|
+
],
|
|
78
|
+
}),
|
|
79
|
+
create("div", {
|
|
80
|
+
style: { marginLeft: "1rem" },
|
|
81
|
+
children: [
|
|
82
|
+
guard(getQualifierMessage(schema), (message) =>
|
|
83
|
+
create("div", {
|
|
84
|
+
style: { marginTop: ".5rem", marginBottom: ".5rem" },
|
|
85
|
+
children: createDescription(message),
|
|
86
|
+
})
|
|
87
|
+
),
|
|
88
|
+
guard(schema.description, (description) =>
|
|
89
|
+
create("div", {
|
|
90
|
+
style: { marginTop: ".5rem", marginBottom: ".5rem" },
|
|
91
|
+
children: createDescription(description),
|
|
92
|
+
})
|
|
93
|
+
),
|
|
94
|
+
createRows({ schema: schema }),
|
|
95
|
+
],
|
|
96
|
+
}),
|
|
97
|
+
],
|
|
98
|
+
}),
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return create("SchemaItem", {
|
|
104
|
+
collapsible: false,
|
|
105
|
+
name,
|
|
106
|
+
required,
|
|
107
|
+
schemaDescription: schema.description,
|
|
108
|
+
schemaName: getSchemaName(schema, true),
|
|
109
|
+
qualifierMessage: getQualifierMessage(schema),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface RowsProps {
|
|
114
|
+
schema: SchemaObject;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function createRows({ schema }: RowsProps): string | undefined {
|
|
118
|
+
// object
|
|
119
|
+
if (schema.properties !== undefined) {
|
|
120
|
+
return create("ul", {
|
|
121
|
+
children: Object.entries(schema.properties).map(([key, val]) =>
|
|
122
|
+
createRow({
|
|
123
|
+
name: key,
|
|
124
|
+
schema: val,
|
|
125
|
+
required: Array.isArray(schema.required)
|
|
126
|
+
? schema.required.includes(key)
|
|
127
|
+
: false,
|
|
128
|
+
})
|
|
129
|
+
),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// TODO: This can be a bit complicated types can be missmatched and there can be nested allOfs which need to be resolved before merging properties
|
|
134
|
+
if (schema.allOf !== undefined) {
|
|
135
|
+
const { properties, required } = resolveAllOf(schema.allOf);
|
|
136
|
+
return create("div", {
|
|
137
|
+
children: [
|
|
138
|
+
create("span", {
|
|
139
|
+
className: "badge badge--info",
|
|
140
|
+
style: { marginBottom: "1rem" },
|
|
141
|
+
children: "allOf",
|
|
142
|
+
}),
|
|
143
|
+
create("ul", {
|
|
144
|
+
className: "allOf",
|
|
145
|
+
children: Object.entries(properties).map(([key, val]) =>
|
|
146
|
+
createRow({
|
|
147
|
+
name: key,
|
|
148
|
+
schema: val,
|
|
149
|
+
required: Array.isArray(required)
|
|
150
|
+
? required.includes(key)
|
|
151
|
+
: false,
|
|
152
|
+
})
|
|
153
|
+
),
|
|
154
|
+
}),
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// array
|
|
160
|
+
if (schema.items !== undefined) {
|
|
161
|
+
return createRows({ schema: schema.items });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// primitive
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
interface RowsRootProps {
|
|
169
|
+
schema: SchemaObject;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function createRowsRoot({ schema }: RowsRootProps) {
|
|
173
|
+
// object
|
|
174
|
+
if (schema.properties !== undefined) {
|
|
175
|
+
return Object.entries(schema.properties).map(([key, val]) =>
|
|
176
|
+
createRow({
|
|
177
|
+
name: key,
|
|
178
|
+
schema: val,
|
|
179
|
+
required: Array.isArray(schema.required)
|
|
180
|
+
? schema.required.includes(key)
|
|
181
|
+
: false,
|
|
182
|
+
})
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// TODO: This can be a bit complicated types can be missmatched and there can be nested allOfs which need to be resolved before merging properties
|
|
187
|
+
if (schema.allOf !== undefined) {
|
|
188
|
+
const { properties, required } = resolveAllOf(schema.allOf);
|
|
189
|
+
return Object.entries(properties).map(([key, val]) =>
|
|
190
|
+
createRow({
|
|
191
|
+
name: key,
|
|
192
|
+
schema: val,
|
|
193
|
+
required: Array.isArray(required) ? required.includes(key) : false,
|
|
194
|
+
})
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// array
|
|
199
|
+
if (schema.items !== undefined) {
|
|
200
|
+
return create("li", {
|
|
201
|
+
children: create("div", {
|
|
202
|
+
children: [createRows({ schema: schema.items })],
|
|
203
|
+
}),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// primitive
|
|
208
|
+
return create("li", {
|
|
209
|
+
children: create("div", {
|
|
210
|
+
children: [
|
|
211
|
+
create("span", {
|
|
212
|
+
style: { opacity: "0.6" },
|
|
213
|
+
children: ` ${schema.type}`,
|
|
214
|
+
}),
|
|
215
|
+
guard(getQualifierMessage(schema), (message) =>
|
|
216
|
+
create("div", {
|
|
217
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
218
|
+
children: createDescription(message),
|
|
219
|
+
})
|
|
220
|
+
),
|
|
221
|
+
guard(schema.description, (description) =>
|
|
222
|
+
create("div", {
|
|
223
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
224
|
+
children: createDescription(description),
|
|
225
|
+
})
|
|
226
|
+
),
|
|
227
|
+
],
|
|
228
|
+
}),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
interface Props {
|
|
233
|
+
style?: any;
|
|
234
|
+
title: string;
|
|
235
|
+
body: {
|
|
236
|
+
content?: {
|
|
237
|
+
[key: string]: MediaTypeObject;
|
|
238
|
+
};
|
|
239
|
+
description?: string;
|
|
240
|
+
required?: boolean;
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function createSchemaDetails({ title, body, ...rest }: Props) {
|
|
245
|
+
if (body === undefined || body.content === undefined) {
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// TODO:
|
|
250
|
+
// NOTE: We just pick a random content-type.
|
|
251
|
+
// How common is it to have multiple?
|
|
252
|
+
const randomFirstKey = Object.keys(body.content)[0];
|
|
253
|
+
|
|
254
|
+
const firstBody = body.content[randomFirstKey].schema;
|
|
255
|
+
|
|
256
|
+
if (firstBody === undefined) {
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// we don't show the table if there is no properties to show
|
|
261
|
+
if (firstBody.properties !== undefined) {
|
|
262
|
+
if (Object.keys(firstBody.properties).length === 0) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return createDetails({
|
|
268
|
+
...rest,
|
|
269
|
+
children: [
|
|
270
|
+
createDetailsSummary({
|
|
271
|
+
style: { textAlign: "left" },
|
|
272
|
+
children: [
|
|
273
|
+
create("strong", { children: `${title}` }),
|
|
274
|
+
guard(body.required, () => [
|
|
275
|
+
create("strong", {
|
|
276
|
+
style: {
|
|
277
|
+
fontSize: "var(--ifm-code-font-size)",
|
|
278
|
+
color: "var(--openapi-required)",
|
|
279
|
+
},
|
|
280
|
+
children: " required",
|
|
281
|
+
}),
|
|
282
|
+
]),
|
|
283
|
+
],
|
|
284
|
+
}),
|
|
285
|
+
create("div", {
|
|
286
|
+
style: { textAlign: "left", marginLeft: "1rem" },
|
|
287
|
+
children: [
|
|
288
|
+
guard(body.description, () => [
|
|
289
|
+
create("div", {
|
|
290
|
+
style: { marginTop: "1rem", marginBottom: "1rem" },
|
|
291
|
+
children: createDescription(body.description),
|
|
292
|
+
}),
|
|
293
|
+
]),
|
|
294
|
+
],
|
|
295
|
+
}),
|
|
296
|
+
create("ul", {
|
|
297
|
+
style: { marginLeft: "1rem" },
|
|
298
|
+
children: createRowsRoot({ schema: firstBody }),
|
|
299
|
+
}),
|
|
300
|
+
],
|
|
301
|
+
});
|
|
302
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/* ============================================================================
|
|
2
|
+
* Copyright (c) Palo Alto Networks
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
* ========================================================================== */
|
|
7
|
+
|
|
8
|
+
import { MediaTypeObject, SchemaObject } from "../openapi/types";
|
|
9
|
+
import { createDescription } from "./createDescription";
|
|
10
|
+
import { createFullWidthTable } from "./createFullWidthTable";
|
|
11
|
+
import { getQualifierMessage, getSchemaName } from "./schema";
|
|
12
|
+
import { create, guard } from "./utils";
|
|
13
|
+
|
|
14
|
+
function resolveAllOf(allOf: SchemaObject[]) {
|
|
15
|
+
// TODO: naive implementation (only supports objects, no directly nested allOf)
|
|
16
|
+
const properties = allOf.reduce((acc, cur) => {
|
|
17
|
+
if (cur.properties !== undefined) {
|
|
18
|
+
const next = { ...acc, ...cur.properties };
|
|
19
|
+
return next;
|
|
20
|
+
}
|
|
21
|
+
return acc;
|
|
22
|
+
}, {});
|
|
23
|
+
|
|
24
|
+
const required = allOf.reduce((acc, cur) => {
|
|
25
|
+
if (Array.isArray(cur.required)) {
|
|
26
|
+
const next = [...acc, ...cur.required];
|
|
27
|
+
return next;
|
|
28
|
+
}
|
|
29
|
+
return acc;
|
|
30
|
+
}, [] as string[]);
|
|
31
|
+
|
|
32
|
+
return { properties, required };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface RowProps {
|
|
36
|
+
name: string;
|
|
37
|
+
schema: SchemaObject;
|
|
38
|
+
required: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function createRow({ name, schema, required }: RowProps) {
|
|
42
|
+
return create("tr", {
|
|
43
|
+
children: create("td", {
|
|
44
|
+
children: [
|
|
45
|
+
create("code", { children: name }),
|
|
46
|
+
create("span", {
|
|
47
|
+
style: { opacity: "0.6" },
|
|
48
|
+
children: ` ${getSchemaName(schema, true)}`,
|
|
49
|
+
}),
|
|
50
|
+
guard(required, () => [
|
|
51
|
+
create("span", {
|
|
52
|
+
style: { opacity: "0.6" },
|
|
53
|
+
children: " — ",
|
|
54
|
+
}),
|
|
55
|
+
create("strong", {
|
|
56
|
+
style: {
|
|
57
|
+
fontSize: "var(--ifm-code-font-size)",
|
|
58
|
+
color: "var(--openapi-required)",
|
|
59
|
+
},
|
|
60
|
+
children: " REQUIRED",
|
|
61
|
+
}),
|
|
62
|
+
]),
|
|
63
|
+
guard(getQualifierMessage(schema), (message) =>
|
|
64
|
+
create("div", {
|
|
65
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
66
|
+
children: createDescription(message),
|
|
67
|
+
})
|
|
68
|
+
),
|
|
69
|
+
guard(schema.description, (description) =>
|
|
70
|
+
create("div", {
|
|
71
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
72
|
+
children: createDescription(description),
|
|
73
|
+
})
|
|
74
|
+
),
|
|
75
|
+
createRows({ schema: schema }),
|
|
76
|
+
],
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface RowsProps {
|
|
82
|
+
schema: SchemaObject;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function createRows({ schema }: RowsProps): string | undefined {
|
|
86
|
+
// object
|
|
87
|
+
if (schema.properties !== undefined) {
|
|
88
|
+
return createFullWidthTable({
|
|
89
|
+
style: {
|
|
90
|
+
marginTop: "var(--ifm-table-cell-padding)",
|
|
91
|
+
marginBottom: "0px",
|
|
92
|
+
},
|
|
93
|
+
children: create("tbody", {
|
|
94
|
+
children: Object.entries(schema.properties).map(([key, val]) =>
|
|
95
|
+
createRow({
|
|
96
|
+
name: key,
|
|
97
|
+
schema: val,
|
|
98
|
+
required: Array.isArray(schema.required)
|
|
99
|
+
? schema.required.includes(key)
|
|
100
|
+
: false,
|
|
101
|
+
})
|
|
102
|
+
),
|
|
103
|
+
}),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// TODO: This can be a bit complicated types can be missmatched and there can be nested allOfs which need to be resolved before merging properties
|
|
108
|
+
if (schema.allOf !== undefined) {
|
|
109
|
+
const { properties, required } = resolveAllOf(schema.allOf);
|
|
110
|
+
return createFullWidthTable({
|
|
111
|
+
style: {
|
|
112
|
+
marginTop: "var(--ifm-table-cell-padding)",
|
|
113
|
+
marginBottom: "0px",
|
|
114
|
+
},
|
|
115
|
+
children: create("tbody", {
|
|
116
|
+
children: Object.entries(properties).map(([key, val]) =>
|
|
117
|
+
createRow({
|
|
118
|
+
name: key,
|
|
119
|
+
schema: val,
|
|
120
|
+
required: Array.isArray(required) ? required.includes(key) : false,
|
|
121
|
+
})
|
|
122
|
+
),
|
|
123
|
+
}),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// array
|
|
128
|
+
if (schema.items !== undefined) {
|
|
129
|
+
return createRows({ schema: schema.items });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// primitive
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
interface RowsRootProps {
|
|
137
|
+
schema: SchemaObject;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function createRowsRoot({ schema }: RowsRootProps) {
|
|
141
|
+
// object
|
|
142
|
+
if (schema.properties !== undefined) {
|
|
143
|
+
return Object.entries(schema.properties).map(([key, val]) =>
|
|
144
|
+
createRow({
|
|
145
|
+
name: key,
|
|
146
|
+
schema: val,
|
|
147
|
+
required: Array.isArray(schema.required)
|
|
148
|
+
? schema.required.includes(key)
|
|
149
|
+
: false,
|
|
150
|
+
})
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// TODO: This can be a bit complicated types can be missmatched and there can be nested allOfs which need to be resolved before merging properties
|
|
155
|
+
if (schema.allOf !== undefined) {
|
|
156
|
+
const { properties, required } = resolveAllOf(schema.allOf);
|
|
157
|
+
return Object.entries(properties).map(([key, val]) =>
|
|
158
|
+
createRow({
|
|
159
|
+
name: key,
|
|
160
|
+
schema: val,
|
|
161
|
+
required: Array.isArray(required) ? required.includes(key) : false,
|
|
162
|
+
})
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// array
|
|
167
|
+
if (schema.items !== undefined) {
|
|
168
|
+
return create("tr", {
|
|
169
|
+
children: create("td", {
|
|
170
|
+
children: [
|
|
171
|
+
create("span", {
|
|
172
|
+
style: { opacity: "0.6" },
|
|
173
|
+
children: ` ${getSchemaName(schema, true)}`,
|
|
174
|
+
}),
|
|
175
|
+
createRows({ schema: schema.items }),
|
|
176
|
+
],
|
|
177
|
+
}),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// primitive
|
|
182
|
+
return create("tr", {
|
|
183
|
+
children: create("td", {
|
|
184
|
+
children: [
|
|
185
|
+
create("span", {
|
|
186
|
+
style: { opacity: "0.6" },
|
|
187
|
+
children: ` ${schema.type}`,
|
|
188
|
+
}),
|
|
189
|
+
guard(getQualifierMessage(schema), (message) =>
|
|
190
|
+
create("div", {
|
|
191
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
192
|
+
children: createDescription(message),
|
|
193
|
+
})
|
|
194
|
+
),
|
|
195
|
+
guard(schema.description, (description) =>
|
|
196
|
+
create("div", {
|
|
197
|
+
style: { marginTop: "var(--ifm-table-cell-padding)" },
|
|
198
|
+
children: createDescription(description),
|
|
199
|
+
})
|
|
200
|
+
),
|
|
201
|
+
],
|
|
202
|
+
}),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface Props {
|
|
207
|
+
style?: any;
|
|
208
|
+
title: string;
|
|
209
|
+
body: {
|
|
210
|
+
content?: {
|
|
211
|
+
[key: string]: MediaTypeObject;
|
|
212
|
+
};
|
|
213
|
+
description?: string;
|
|
214
|
+
required?: boolean;
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function createSchemaTable({ title, body, ...rest }: Props) {
|
|
219
|
+
if (body === undefined || body.content === undefined) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// TODO:
|
|
224
|
+
// NOTE: We just pick a random content-type.
|
|
225
|
+
// How common is it to have multiple?
|
|
226
|
+
const randomFirstKey = Object.keys(body.content)[0];
|
|
227
|
+
|
|
228
|
+
const firstBody = body.content[randomFirstKey].schema;
|
|
229
|
+
|
|
230
|
+
if (firstBody === undefined) {
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// we don't show the table if there is no properties to show
|
|
235
|
+
if (firstBody.properties !== undefined) {
|
|
236
|
+
if (Object.keys(firstBody.properties).length === 0) {
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return createFullWidthTable({
|
|
242
|
+
...rest,
|
|
243
|
+
children: [
|
|
244
|
+
create("thead", {
|
|
245
|
+
children: create("tr", {
|
|
246
|
+
children: create("th", {
|
|
247
|
+
style: { textAlign: "left" },
|
|
248
|
+
children: [
|
|
249
|
+
`${title} `,
|
|
250
|
+
guard(body.required, () => [
|
|
251
|
+
create("span", {
|
|
252
|
+
style: { opacity: "0.6" },
|
|
253
|
+
children: " — ",
|
|
254
|
+
}),
|
|
255
|
+
create("strong", {
|
|
256
|
+
style: {
|
|
257
|
+
fontSize: "var(--ifm-code-font-size)",
|
|
258
|
+
color: "var(--openapi-required)",
|
|
259
|
+
},
|
|
260
|
+
children: " REQUIRED",
|
|
261
|
+
}),
|
|
262
|
+
]),
|
|
263
|
+
create("div", {
|
|
264
|
+
children: createDescription(body.description),
|
|
265
|
+
}),
|
|
266
|
+
],
|
|
267
|
+
}),
|
|
268
|
+
}),
|
|
269
|
+
}),
|
|
270
|
+
create("tbody", {
|
|
271
|
+
children: createRowsRoot({ schema: firstBody }),
|
|
272
|
+
}),
|
|
273
|
+
],
|
|
274
|
+
});
|
|
275
|
+
}
|