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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +194 -0
  3. package/lib/index.d.ts +3 -0
  4. package/lib/index.js +194 -0
  5. package/lib/markdown/createDeprecationNotice.d.ts +6 -0
  6. package/lib/markdown/createDeprecationNotice.js +19 -0
  7. package/lib/markdown/createDescription.d.ts +1 -0
  8. package/lib/markdown/createDescription.js +16 -0
  9. package/lib/markdown/createDetails.d.ts +2 -0
  10. package/lib/markdown/createDetails.js +18 -0
  11. package/lib/markdown/createDetailsSummary.d.ts +2 -0
  12. package/lib/markdown/createDetailsSummary.js +18 -0
  13. package/lib/markdown/createFullWidthTable.d.ts +2 -0
  14. package/lib/markdown/createFullWidthTable.js +18 -0
  15. package/lib/markdown/createParamsDetails.d.ts +7 -0
  16. package/lib/markdown/createParamsDetails.js +44 -0
  17. package/lib/markdown/createParamsTable.d.ts +7 -0
  18. package/lib/markdown/createParamsTable.js +80 -0
  19. package/lib/markdown/createRequestBodyDetails.d.ts +6 -0
  20. package/lib/markdown/createRequestBodyDetails.js +14 -0
  21. package/lib/markdown/createRequestBodyTable.d.ts +6 -0
  22. package/lib/markdown/createRequestBodyTable.js +14 -0
  23. package/lib/markdown/createSchemaDetails.d.ts +14 -0
  24. package/lib/markdown/createSchemaDetails.js +241 -0
  25. package/lib/markdown/createSchemaTable.d.ts +14 -0
  26. package/lib/markdown/createSchemaTable.js +217 -0
  27. package/lib/markdown/createStatusCodes.d.ts +6 -0
  28. package/lib/markdown/createStatusCodes.js +47 -0
  29. package/lib/markdown/createVersionBadge.d.ts +1 -0
  30. package/lib/markdown/createVersionBadge.js +20 -0
  31. package/lib/markdown/index.d.ts +3 -0
  32. package/lib/markdown/index.js +43 -0
  33. package/lib/markdown/schema.d.ts +3 -0
  34. package/lib/markdown/schema.js +100 -0
  35. package/lib/markdown/schema.test.d.ts +1 -0
  36. package/lib/markdown/schema.test.js +171 -0
  37. package/lib/markdown/utils.d.ts +7 -0
  38. package/lib/markdown/utils.js +33 -0
  39. package/lib/openapi/createExample.d.ts +2 -0
  40. package/lib/openapi/createExample.js +113 -0
  41. package/lib/openapi/index.d.ts +1 -0
  42. package/lib/openapi/index.js +12 -0
  43. package/lib/openapi/openapi.d.ts +11 -0
  44. package/lib/openapi/openapi.js +233 -0
  45. package/lib/openapi/openapi.test.d.ts +1 -0
  46. package/lib/openapi/openapi.test.js +33 -0
  47. package/lib/openapi/types.d.ts +331 -0
  48. package/lib/openapi/types.js +8 -0
  49. package/lib/options.d.ts +4 -0
  50. package/lib/options.js +18 -0
  51. package/lib/sidebars/index.d.ts +3 -0
  52. package/lib/sidebars/index.js +89 -0
  53. package/lib/types.d.ts +68 -0
  54. package/lib/types.js +8 -0
  55. package/package.json +58 -0
  56. package/src/index.ts +244 -0
  57. package/src/markdown/createDeprecationNotice.ts +30 -0
  58. package/src/markdown/createDescription.ts +13 -0
  59. package/src/markdown/createDetails.ts +16 -0
  60. package/src/markdown/createDetailsSummary.ts +16 -0
  61. package/src/markdown/createFullWidthTable.ts +16 -0
  62. package/src/markdown/createParamsDetails.ts +53 -0
  63. package/src/markdown/createParamsTable.ts +102 -0
  64. package/src/markdown/createRequestBodyDetails.ts +17 -0
  65. package/src/markdown/createRequestBodyTable.ts +17 -0
  66. package/src/markdown/createSchemaDetails.ts +302 -0
  67. package/src/markdown/createSchemaTable.ts +275 -0
  68. package/src/markdown/createStatusCodes.ts +52 -0
  69. package/src/markdown/createVersionBadge.ts +18 -0
  70. package/src/markdown/index.ts +55 -0
  71. package/src/markdown/schema.test.ts +196 -0
  72. package/src/markdown/schema.ts +115 -0
  73. package/src/markdown/utils.ts +39 -0
  74. package/src/openapi/__fixtures__/examples/openapi.yaml +13 -0
  75. package/src/openapi/__fixtures__/examples/yogurtstore/_category_.json +4 -0
  76. package/src/openapi/__fixtures__/examples/yogurtstore/froyo.yaml +13 -0
  77. package/src/openapi/__fixtures__/examples/yogurtstore/nested/nested.yaml +13 -0
  78. package/src/openapi/createExample.ts +143 -0
  79. package/src/openapi/index.ts +8 -0
  80. package/src/openapi/openapi.test.ts +37 -0
  81. package/src/openapi/openapi.ts +293 -0
  82. package/src/openapi/types.ts +430 -0
  83. package/src/openapi-to-postmanv2.d.ts +10 -0
  84. package/src/options.ts +20 -0
  85. package/src/plugin-content-docs-types.d.ts +42 -0
  86. package/src/plugin-openapi.d.ts +87 -0
  87. package/src/sidebars/index.ts +121 -0
  88. package/src/types.ts +97 -0
  89. package/tsconfig.json +7 -0
@@ -0,0 +1,52 @@
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 { ApiItem } from "../types";
9
+ import { createDescription } from "./createDescription";
10
+ import { createSchemaDetails } from "./createSchemaDetails";
11
+ import { create } from "./utils";
12
+
13
+ interface Props {
14
+ responses: ApiItem["responses"];
15
+ }
16
+
17
+ export function createStatusCodes({ responses }: Props) {
18
+ if (responses === undefined) {
19
+ return undefined;
20
+ }
21
+
22
+ const codes = Object.keys(responses);
23
+ if (codes.length === 0) {
24
+ return undefined;
25
+ }
26
+
27
+ return create("div", {
28
+ children: [
29
+ create("Tabs", {
30
+ children: codes.map((code) => {
31
+ return create("TabItem", {
32
+ label: code,
33
+ value: code,
34
+ children: [
35
+ create("div", {
36
+ children: createDescription(responses[code].description),
37
+ }),
38
+ create("div", {
39
+ children: createSchemaDetails({
40
+ title: "Schema",
41
+ body: {
42
+ content: responses[code].content,
43
+ },
44
+ }),
45
+ }),
46
+ ],
47
+ });
48
+ }),
49
+ }),
50
+ ],
51
+ });
52
+ }
@@ -0,0 +1,18 @@
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 { create, guard } from "./utils";
9
+
10
+ export function createVersionBadge(version: string | undefined) {
11
+ return guard(version, (version) => [
12
+ create("span", {
13
+ className: "theme-doc-version-badge badge badge--secondary",
14
+ children: `Version: ${escape(version)}`,
15
+ }),
16
+ `\n\n`,
17
+ ]);
18
+ }
@@ -0,0 +1,55 @@
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 { ApiPageMetadata, InfoPageMetadata } from "../types";
11
+ import { createDeprecationNotice } from "./createDeprecationNotice";
12
+ import { createDescription } from "./createDescription";
13
+ import { createParamsDetails } from "./createParamsDetails";
14
+ import { createRequestBodyDetails } from "./createRequestBodyDetails";
15
+ import { createStatusCodes } from "./createStatusCodes";
16
+ import { createVersionBadge } from "./createVersionBadge";
17
+ import { render } from "./utils";
18
+
19
+ export function createApiPageMD({
20
+ title,
21
+ api: {
22
+ deprecated,
23
+ "x-deprecated-description": deprecatedDescription,
24
+ description,
25
+ parameters,
26
+ requestBody,
27
+ responses,
28
+ },
29
+ }: ApiPageMetadata) {
30
+ return render([
31
+ `import ParamsItem from "@theme/ParamsItem";\n`,
32
+ `import SchemaItem from "@theme/SchemaItem"\n`,
33
+ `import Tabs from "@theme/Tabs";\n`,
34
+ `import TabItem from "@theme/TabItem";\n\n`,
35
+ `## ${escape(title)}\n\n`,
36
+ createDeprecationNotice({ deprecated, description: deprecatedDescription }),
37
+ createDescription(escape(description)),
38
+ createParamsDetails({ parameters, type: "path" }),
39
+ createParamsDetails({ parameters, type: "query" }),
40
+ createParamsDetails({ parameters, type: "header" }),
41
+ createParamsDetails({ parameters, type: "cookie" }),
42
+ createRequestBodyDetails({ title: "Request Body", body: requestBody }),
43
+ createStatusCodes({ responses }),
44
+ ]);
45
+ }
46
+
47
+ export function createInfoPageMD({
48
+ info: { title, version, description },
49
+ }: InfoPageMetadata) {
50
+ return render([
51
+ createVersionBadge(version),
52
+ `# ${escape(title)}\n\n`,
53
+ createDescription(description),
54
+ ]);
55
+ }
@@ -0,0 +1,196 @@
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 { getQualifierMessage } from "./schema";
9
+
10
+ describe("getQualifierMessage", () => {
11
+ it("should render nothing", () => {
12
+ const actual = getQualifierMessage({});
13
+ expect(actual).toBeUndefined();
14
+ });
15
+
16
+ //
17
+ // minLength + maxLength
18
+ //
19
+ it("should render minLength", () => {
20
+ const expected = "**Possible values:** 1 ≤ length";
21
+ const actual = getQualifierMessage({ minLength: 1 });
22
+ expect(actual).toBe(expected);
23
+ });
24
+
25
+ it("should render maxLength", () => {
26
+ const expected = "**Possible values:** length ≤ 40";
27
+ const actual = getQualifierMessage({ maxLength: 40 });
28
+ expect(actual).toBe(expected);
29
+ });
30
+
31
+ it("should render minLength and maxLength", () => {
32
+ const expected = "**Possible values:** 1 ≤ length ≤ 40";
33
+ const actual = getQualifierMessage({ minLength: 1, maxLength: 40 });
34
+ expect(actual).toBe(expected);
35
+ });
36
+
37
+ //
38
+ // pattern
39
+ //
40
+ it("should render pattern", () => {
41
+ const expected =
42
+ "**Possible values:** Value must match regular expression `^[a-zA-Z0-9_-]*$`";
43
+ const actual = getQualifierMessage({ pattern: "^[a-zA-Z0-9_-]*$" });
44
+ expect(actual).toBe(expected);
45
+ });
46
+
47
+ it("should render multiple string qualifiers", () => {
48
+ const expected =
49
+ "**Possible values:** 1 ≤ length ≤ 40, Value must match regular expression `^[a-zA-Z0-9_-]*$`";
50
+ const actual = getQualifierMessage({
51
+ minLength: 1,
52
+ maxLength: 40,
53
+ pattern: "^[a-zA-Z0-9_-]*$",
54
+ });
55
+ expect(actual).toBe(expected);
56
+ });
57
+
58
+ //
59
+ // enum
60
+ //
61
+ it("should render enum", () => {
62
+ const expected = "**Possible values:** [`cat`, `dog`, `mouse`]";
63
+ const actual = getQualifierMessage({ enum: ["cat", "dog", "mouse"] });
64
+ expect(actual).toBe(expected);
65
+ });
66
+
67
+ //
68
+ // minimum + maximum + exclusiveMinimum + exclusiveMaximum
69
+ //
70
+ it("should render minimum", () => {
71
+ const expected = "**Possible values:** 1 ≤ value";
72
+ const actual = getQualifierMessage({ minimum: 1 });
73
+ expect(actual).toBe(expected);
74
+ });
75
+
76
+ it("should render maximum", () => {
77
+ const expected = "**Possible values:** value ≤ 40";
78
+ const actual = getQualifierMessage({ maximum: 40 });
79
+ expect(actual).toBe(expected);
80
+ });
81
+
82
+ it("should render numeric exclusiveMinimum", () => {
83
+ const expected = "**Possible values:** 1 < value";
84
+ const actual = getQualifierMessage({ exclusiveMinimum: 1 });
85
+ expect(actual).toBe(expected);
86
+ });
87
+
88
+ it("should render numeric exclusiveMaximum", () => {
89
+ const expected = "**Possible values:** value < 40";
90
+ const actual = getQualifierMessage({ exclusiveMaximum: 40 });
91
+ expect(actual).toBe(expected);
92
+ });
93
+
94
+ it("should render boolean exclusiveMinimum", () => {
95
+ const expected = "**Possible values:** 1 < value";
96
+ const actual = getQualifierMessage({ minimum: 1, exclusiveMinimum: true });
97
+ expect(actual).toBe(expected);
98
+ });
99
+
100
+ it("should render boolean exclusiveMaximum", () => {
101
+ const expected = "**Possible values:** value < 40";
102
+ const actual = getQualifierMessage({ maximum: 40, exclusiveMaximum: true });
103
+ expect(actual).toBe(expected);
104
+ });
105
+
106
+ it("should render minimum when exclusiveMinimum is false", () => {
107
+ const expected = "**Possible values:** 1 ≤ value";
108
+ const actual = getQualifierMessage({ minimum: 1, exclusiveMinimum: false });
109
+ expect(actual).toBe(expected);
110
+ });
111
+
112
+ it("should render maximum when exclusiveMaximum is false", () => {
113
+ const expected = "**Possible values:** value ≤ 40";
114
+ const actual = getQualifierMessage({
115
+ maximum: 40,
116
+ exclusiveMaximum: false,
117
+ });
118
+ expect(actual).toBe(expected);
119
+ });
120
+
121
+ it("should render minimum and maximum", () => {
122
+ const expected = "**Possible values:** 1 ≤ value ≤ 40";
123
+ const actual = getQualifierMessage({ minimum: 1, maximum: 40 });
124
+ expect(actual).toBe(expected);
125
+ });
126
+
127
+ it("should render boolean exclusiveMinimum and maximum", () => {
128
+ const expected = "**Possible values:** 1 < value ≤ 40";
129
+ const actual = getQualifierMessage({
130
+ minimum: 1,
131
+ maximum: 40,
132
+ exclusiveMinimum: true,
133
+ });
134
+ expect(actual).toBe(expected);
135
+ });
136
+
137
+ it("should render minimum and boolean exclusiveMaximum", () => {
138
+ const expected = "**Possible values:** 1 ≤ value < 40";
139
+ const actual = getQualifierMessage({
140
+ minimum: 1,
141
+ maximum: 40,
142
+ exclusiveMaximum: true,
143
+ });
144
+ expect(actual).toBe(expected);
145
+ });
146
+
147
+ it("should render numeric exclusiveMinimum and maximum", () => {
148
+ const expected = "**Possible values:** 1 < value ≤ 40";
149
+ const actual = getQualifierMessage({
150
+ exclusiveMinimum: 1,
151
+ maximum: 40,
152
+ });
153
+ expect(actual).toBe(expected);
154
+ });
155
+
156
+ it("should render minimum and numeric exclusiveMaximum", () => {
157
+ const expected = "**Possible values:** 1 ≤ value < 40";
158
+ const actual = getQualifierMessage({
159
+ minimum: 1,
160
+ exclusiveMaximum: 40,
161
+ });
162
+ expect(actual).toBe(expected);
163
+ });
164
+
165
+ it("should render numeric exclusiveMinimum and boolean exclusiveMaximum", () => {
166
+ const expected = "**Possible values:** 1 < value < 40";
167
+ const actual = getQualifierMessage({
168
+ exclusiveMinimum: 1,
169
+ maximum: 40,
170
+ exclusiveMaximum: true,
171
+ });
172
+ expect(actual).toBe(expected);
173
+ });
174
+
175
+ it("should render nothing with empty boolean exclusiveMinimum", () => {
176
+ const actual = getQualifierMessage({
177
+ exclusiveMinimum: true,
178
+ });
179
+ expect(actual).toBeUndefined();
180
+ });
181
+
182
+ it("should render nothing with empty boolean exclusiveMaximum", () => {
183
+ const actual = getQualifierMessage({
184
+ exclusiveMaximum: true,
185
+ });
186
+ expect(actual).toBeUndefined();
187
+ });
188
+
189
+ it("should render nothing with empty boolean exclusiveMinimum and exclusiveMaximum", () => {
190
+ const actual = getQualifierMessage({
191
+ exclusiveMinimum: true,
192
+ exclusiveMaximum: true,
193
+ });
194
+ expect(actual).toBeUndefined();
195
+ });
196
+ });
@@ -0,0 +1,115 @@
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 { SchemaObject } from "../openapi/types";
9
+
10
+ function prettyName(schema: SchemaObject, circular?: boolean) {
11
+ if (schema.$ref) {
12
+ return schema.$ref.replace("#/components/schemas/", "") + circular
13
+ ? " (circular)"
14
+ : "";
15
+ }
16
+
17
+ if (schema.format) {
18
+ return schema.format;
19
+ }
20
+
21
+ if (schema.allOf) {
22
+ return "object";
23
+ }
24
+
25
+ if (schema.type === "object") {
26
+ return schema.xml?.name ?? schema.type;
27
+ }
28
+
29
+ return schema.title ?? schema.type;
30
+ }
31
+
32
+ export function getSchemaName(
33
+ schema: SchemaObject,
34
+ circular?: boolean
35
+ ): string {
36
+ if (schema.items) {
37
+ return prettyName(schema.items, circular) + "[]";
38
+ }
39
+
40
+ return prettyName(schema, circular) ?? "";
41
+ }
42
+
43
+ export function getQualifierMessage(schema?: SchemaObject): string | undefined {
44
+ // TODO:
45
+ // - maxItems
46
+ // - minItems
47
+ // - uniqueItems
48
+ // - maxProperties
49
+ // - minProperties
50
+ // - multipleOf
51
+ if (!schema) {
52
+ return undefined;
53
+ }
54
+
55
+ if (schema.items) {
56
+ return getQualifierMessage(schema.items);
57
+ }
58
+
59
+ let message = "**Possible values:** ";
60
+
61
+ let qualifierGroups = [];
62
+
63
+ if (schema.minLength || schema.maxLength) {
64
+ let lengthQualifier = "";
65
+ if (schema.minLength) {
66
+ lengthQualifier += `${schema.minLength} ≤ `;
67
+ }
68
+ lengthQualifier += "length";
69
+ if (schema.maxLength) {
70
+ lengthQualifier += ` ≤ ${schema.maxLength}`;
71
+ }
72
+ qualifierGroups.push(lengthQualifier);
73
+ }
74
+
75
+ if (
76
+ schema.minimum ||
77
+ schema.maximum ||
78
+ typeof schema.exclusiveMinimum === "number" ||
79
+ typeof schema.exclusiveMaximum === "number"
80
+ ) {
81
+ let minmaxQualifier = "";
82
+ if (typeof schema.exclusiveMinimum === "number") {
83
+ minmaxQualifier += `${schema.exclusiveMinimum} < `;
84
+ } else if (schema.minimum && !schema.exclusiveMinimum) {
85
+ minmaxQualifier += `${schema.minimum} ≤ `;
86
+ } else if (schema.minimum && schema.exclusiveMinimum === true) {
87
+ minmaxQualifier += `${schema.minimum} < `;
88
+ }
89
+ minmaxQualifier += "value";
90
+ if (typeof schema.exclusiveMaximum === "number") {
91
+ minmaxQualifier += ` < ${schema.exclusiveMaximum}`;
92
+ } else if (schema.maximum && !schema.exclusiveMaximum) {
93
+ minmaxQualifier += ` ≤ ${schema.maximum}`;
94
+ } else if (schema.maximum && schema.exclusiveMaximum === true) {
95
+ minmaxQualifier += ` < ${schema.maximum}`;
96
+ }
97
+ qualifierGroups.push(minmaxQualifier);
98
+ }
99
+
100
+ if (schema.pattern) {
101
+ qualifierGroups.push(
102
+ `Value must match regular expression \`${schema.pattern}\``
103
+ );
104
+ }
105
+
106
+ if (schema.enum) {
107
+ qualifierGroups.push(`[${schema.enum.map((e) => `\`${e}\``).join(", ")}]`);
108
+ }
109
+
110
+ if (qualifierGroups.length === 0) {
111
+ return undefined;
112
+ }
113
+
114
+ return message + qualifierGroups.join(", ");
115
+ }
@@ -0,0 +1,39 @@
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
+ export type Children = string | undefined | (string | undefined)[];
9
+
10
+ export type Props = Record<string, any> & { children?: Children };
11
+
12
+ export function create(tag: string, props: Props): string {
13
+ const { children, ...rest } = props;
14
+
15
+ let propString = "";
16
+ for (const [key, value] of Object.entries(rest)) {
17
+ propString += ` ${key}={${JSON.stringify(value)}}`;
18
+ }
19
+
20
+ return `<${tag}${propString}>${render(children)}</${tag}>`;
21
+ }
22
+
23
+ export function guard<T>(
24
+ value: T | undefined,
25
+ cb: (value: T) => Children
26
+ ): string {
27
+ if (value) {
28
+ const children = cb(value);
29
+ return render(children);
30
+ }
31
+ return "";
32
+ }
33
+
34
+ export function render(children: Children): string {
35
+ if (Array.isArray(children)) {
36
+ return children.filter((c) => c !== undefined).join("");
37
+ }
38
+ return children ?? "";
39
+ }
@@ -0,0 +1,13 @@
1
+ openapi: 3.0.3
2
+ info:
3
+ title: YAML Example
4
+ version: 1.0.0
5
+ description: Sample description.
6
+ paths:
7
+ /hello:
8
+ get:
9
+ summary: Hello World
10
+ description: Example OpenApi definition with YAML.
11
+ responses:
12
+ 200:
13
+ description: OK
@@ -0,0 +1,4 @@
1
+ {
2
+ "label": "Foods",
3
+ "collapsed": true
4
+ }
@@ -0,0 +1,13 @@
1
+ openapi: 3.0.3
2
+ info:
3
+ title: Frozen Yogurt Example
4
+ version: 1.0.0
5
+ description: Sample description.
6
+ paths:
7
+ /flavors:
8
+ get:
9
+ summary: List All Flavors
10
+ description: Froyo's the best!
11
+ responses:
12
+ 200:
13
+ description: OK
@@ -0,0 +1,13 @@
1
+ openapi: 3.0.3
2
+ info:
3
+ title: Nested Folder Example
4
+ version: 1.0.0
5
+ description: Sample description.
6
+ paths:
7
+ /flavors:
8
+ get:
9
+ summary: List All
10
+ description: All
11
+ responses:
12
+ 200:
13
+ description: OK
@@ -0,0 +1,143 @@
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 { SchemaObject } from "./types";
9
+
10
+ interface OASTypeToTypeMap {
11
+ string: string;
12
+ number: number;
13
+ integer: number;
14
+ boolean: boolean;
15
+ object: any;
16
+ array: any[];
17
+ }
18
+
19
+ type Primitives = {
20
+ [OASType in keyof OASTypeToTypeMap]: {
21
+ [format: string]: (schema: SchemaObject) => OASTypeToTypeMap[OASType];
22
+ };
23
+ };
24
+
25
+ const primitives: Primitives = {
26
+ string: {
27
+ default: () => "string",
28
+ email: () => "user@example.com",
29
+ date: () => new Date().toISOString().substring(0, 10),
30
+ uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
31
+ hostname: () => "example.com",
32
+ ipv4: () => "198.51.100.42",
33
+ ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
34
+ },
35
+ number: {
36
+ default: () => 0,
37
+ float: () => 0.0,
38
+ },
39
+ integer: {
40
+ default: () => 0,
41
+ },
42
+ boolean: {
43
+ default: (schema) =>
44
+ typeof schema.default === "boolean" ? schema.default : true,
45
+ },
46
+ object: {},
47
+ array: {},
48
+ };
49
+
50
+ export const sampleFromSchema = (schema: SchemaObject = {}): any => {
51
+ let { type, example, allOf, properties, items } = schema;
52
+
53
+ if (example !== undefined) {
54
+ return example;
55
+ }
56
+
57
+ if (allOf) {
58
+ // TODO: We are just assuming it will always be an object for now
59
+ let obj: SchemaObject = {
60
+ type: "object",
61
+ properties: {},
62
+ required: [], // NOTE: We shouldn't need to worry about required
63
+ };
64
+ for (let item of allOf) {
65
+ if (item.properties) {
66
+ obj.properties = {
67
+ ...obj.properties,
68
+ ...item.properties,
69
+ };
70
+ }
71
+ }
72
+ return sampleFromSchema(obj);
73
+ }
74
+
75
+ if (!type) {
76
+ if (properties) {
77
+ type = "object";
78
+ } else if (items) {
79
+ type = "array";
80
+ } else {
81
+ return;
82
+ }
83
+ }
84
+
85
+ if (type === "object") {
86
+ let obj: any = {};
87
+ for (let [name, prop] of Object.entries(properties ?? {})) {
88
+ if (prop.deprecated) {
89
+ continue;
90
+ }
91
+ obj[name] = sampleFromSchema(prop);
92
+ }
93
+ return obj;
94
+ }
95
+
96
+ if (type === "array") {
97
+ if (Array.isArray(items?.anyOf)) {
98
+ return items?.anyOf.map((item) => sampleFromSchema(item));
99
+ }
100
+
101
+ if (Array.isArray(items?.oneOf)) {
102
+ return items?.oneOf.map((item) => sampleFromSchema(item));
103
+ }
104
+
105
+ return [sampleFromSchema(items)];
106
+ }
107
+
108
+ if (schema.enum) {
109
+ if (schema.default) {
110
+ return schema.default;
111
+ }
112
+ return normalizeArray(schema.enum)[0];
113
+ }
114
+
115
+ return primitive(schema);
116
+ };
117
+
118
+ function primitive(schema: SchemaObject = {}) {
119
+ let { type, format } = schema;
120
+
121
+ if (type === undefined) {
122
+ return;
123
+ }
124
+
125
+ let fn = primitives[type].default;
126
+
127
+ if (format !== undefined) {
128
+ fn = primitives[type][format] || fn;
129
+ }
130
+
131
+ if (fn) {
132
+ return fn(schema);
133
+ }
134
+
135
+ return "Unknown Type: " + schema.type;
136
+ }
137
+
138
+ function normalizeArray(arr: any) {
139
+ if (Array.isArray(arr)) {
140
+ return arr;
141
+ }
142
+ return [arr];
143
+ }
@@ -0,0 +1,8 @@
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
+ export { readOpenapiFiles, processOpenapiFiles } from "./openapi";