react-query-lightbase-codegen 1.4.0 → 1.4.1
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/dist/utils.d.ts +5 -2
- package/dist/utils.js +53 -48
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/utils.ts +156 -134
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject } from
|
|
1
|
+
import type { ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject } from "openapi3-ts";
|
|
2
2
|
export declare const getDocs: (data: ReferenceObject | {
|
|
3
3
|
description?: string;
|
|
4
4
|
}) => string;
|
|
@@ -21,4 +21,7 @@ export declare const formatDescription: (description?: string) => string;
|
|
|
21
21
|
/**
|
|
22
22
|
* Extract responses / request types from open-api specs
|
|
23
23
|
*/
|
|
24
|
-
export declare const getResReqTypes: (responsesOrRequests: Array<[
|
|
24
|
+
export declare const getResReqTypes: (responsesOrRequests: Array<[
|
|
25
|
+
string,
|
|
26
|
+
ResponseObject | ReferenceObject | RequestBodyObject
|
|
27
|
+
]>) => string;
|
package/dist/utils.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import lodash from
|
|
1
|
+
import lodash from "lodash";
|
|
2
2
|
const { uniq, isEmpty } = lodash;
|
|
3
|
-
import pasCase from
|
|
3
|
+
import pasCase from "case";
|
|
4
|
+
import chalk from "chalk";
|
|
4
5
|
const { pascal } = pasCase;
|
|
5
|
-
export const getDocs = (data) => isReference(data) ?
|
|
6
|
+
export const getDocs = (data) => isReference(data) ? "" : formatDescription(data.description);
|
|
6
7
|
/**
|
|
7
8
|
* Discriminator helper for `ReferenceObject`
|
|
8
9
|
*/
|
|
@@ -13,18 +14,19 @@ export const isReference = (property) => {
|
|
|
13
14
|
* Return the typescript equivalent of open-api data type
|
|
14
15
|
*/
|
|
15
16
|
export const getScalar = (item) => {
|
|
16
|
-
const nullable = item.nullable ?
|
|
17
|
+
const nullable = item.nullable ? " | null" : "";
|
|
17
18
|
switch (item.type) {
|
|
18
|
-
case
|
|
19
|
-
case
|
|
20
|
-
return
|
|
21
|
-
case
|
|
22
|
-
return
|
|
23
|
-
case
|
|
19
|
+
case "number":
|
|
20
|
+
case "integer":
|
|
21
|
+
return "number" + nullable;
|
|
22
|
+
case "boolean":
|
|
23
|
+
return "boolean" + nullable;
|
|
24
|
+
case "array":
|
|
24
25
|
return getArray(item) + nullable;
|
|
25
|
-
case
|
|
26
|
-
return (item.enum ? `"${item.enum.join(`" | "`)}"` :
|
|
27
|
-
case
|
|
26
|
+
case "string":
|
|
27
|
+
return (item.enum ? `"${item.enum.join(`" | "`)}"` : "string") + nullable;
|
|
28
|
+
case "object":
|
|
29
|
+
return getObject(item) + nullable;
|
|
28
30
|
default:
|
|
29
31
|
return getObject(item) + nullable;
|
|
30
32
|
}
|
|
@@ -33,20 +35,20 @@ export const getScalar = (item) => {
|
|
|
33
35
|
* Return the output type from the $ref
|
|
34
36
|
*/
|
|
35
37
|
const getRef = ($ref) => {
|
|
36
|
-
if ($ref.startsWith(
|
|
37
|
-
return pascal($ref.replace(
|
|
38
|
+
if ($ref.startsWith("#/components/schemas")) {
|
|
39
|
+
return pascal($ref.replace("#/components/schemas/", ""));
|
|
38
40
|
}
|
|
39
|
-
else if ($ref.startsWith(
|
|
40
|
-
return pascal($ref.replace(
|
|
41
|
+
else if ($ref.startsWith("#/components/responses")) {
|
|
42
|
+
return pascal($ref.replace("#/components/responses/", "")) + "Response";
|
|
41
43
|
}
|
|
42
|
-
else if ($ref.startsWith(
|
|
43
|
-
return pascal($ref.replace(
|
|
44
|
+
else if ($ref.startsWith("#/components/parameters")) {
|
|
45
|
+
return pascal($ref.replace("#/components/parameters/", "")) + "Parameter";
|
|
44
46
|
}
|
|
45
|
-
else if ($ref.startsWith(
|
|
46
|
-
return pascal($ref.replace(
|
|
47
|
+
else if ($ref.startsWith("#/components/requestBodies")) {
|
|
48
|
+
return (pascal($ref.replace("#/components/requestBodies/", "")) + "RequestBody");
|
|
47
49
|
}
|
|
48
50
|
else {
|
|
49
|
-
throw new Error(
|
|
51
|
+
throw new Error("This library only resolve $ref that are include into `#/components/*` for now");
|
|
50
52
|
}
|
|
51
53
|
};
|
|
52
54
|
/**
|
|
@@ -54,16 +56,15 @@ const getRef = ($ref) => {
|
|
|
54
56
|
*/
|
|
55
57
|
const getArray = (item) => {
|
|
56
58
|
if (item.items) {
|
|
57
|
-
if (!isReference(item.items) &&
|
|
59
|
+
if (!isReference(item.items) &&
|
|
60
|
+
(item.items.oneOf || item.items.allOf || item.items.enum)) {
|
|
58
61
|
return `(${resolveValue(item.items)})[]`;
|
|
59
62
|
}
|
|
60
63
|
else {
|
|
61
64
|
return `${resolveValue(item.items)}[]`;
|
|
62
65
|
}
|
|
63
66
|
}
|
|
64
|
-
|
|
65
|
-
throw new Error('All arrays must have an `items` key define');
|
|
66
|
-
}
|
|
67
|
+
console.log(chalk.red("Skipped: All arrays must have an `items` key define"));
|
|
67
68
|
};
|
|
68
69
|
/**
|
|
69
70
|
* Return the output type from an object
|
|
@@ -73,46 +74,50 @@ const getObject = (item) => {
|
|
|
73
74
|
return getRef(item.$ref);
|
|
74
75
|
}
|
|
75
76
|
if (item.allOf) {
|
|
76
|
-
return item.allOf.map(resolveValue).join(
|
|
77
|
+
return item.allOf.map(resolveValue).join(" & ");
|
|
77
78
|
}
|
|
78
79
|
if (item.oneOf) {
|
|
79
|
-
return item.oneOf.map(resolveValue).join(
|
|
80
|
+
return item.oneOf.map(resolveValue).join(" | ");
|
|
80
81
|
}
|
|
81
82
|
if (!item.type && !item.properties && !item.additionalProperties) {
|
|
82
|
-
return
|
|
83
|
+
return "{}";
|
|
83
84
|
}
|
|
84
85
|
// Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
|
|
85
|
-
if (item.type ===
|
|
86
|
+
if (item.type === "object" &&
|
|
86
87
|
!item.properties &&
|
|
87
|
-
(!item.additionalProperties ||
|
|
88
|
-
|
|
88
|
+
(!item.additionalProperties ||
|
|
89
|
+
item.additionalProperties === true ||
|
|
90
|
+
isEmpty(item.additionalProperties))) {
|
|
91
|
+
return "{[key: string]: any}";
|
|
89
92
|
}
|
|
90
93
|
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
91
94
|
// Consolidation of item.properties & item.additionalProperties
|
|
92
|
-
let output =
|
|
95
|
+
let output = "{";
|
|
93
96
|
if (item.properties) {
|
|
94
97
|
output += Object.entries(item.properties)
|
|
95
98
|
.map(([key, prop]) => {
|
|
96
99
|
const doc = getDocs(prop);
|
|
97
100
|
const isRequired = (item.required || []).includes(key);
|
|
98
101
|
const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
|
|
99
|
-
return `${doc}\n${processedKey}${isRequired ?
|
|
102
|
+
return `${doc}\n${processedKey}${isRequired ? "" : "?"}: ${resolveValue(prop)};`;
|
|
100
103
|
})
|
|
101
|
-
.join(
|
|
104
|
+
.join("");
|
|
102
105
|
}
|
|
103
106
|
if (item.additionalProperties) {
|
|
104
107
|
if (item.properties) {
|
|
105
|
-
output +=
|
|
108
|
+
output += "\n";
|
|
106
109
|
}
|
|
107
|
-
output += `} & { [key: string]: ${item.additionalProperties === true
|
|
110
|
+
output += `} & { [key: string]: ${item.additionalProperties === true
|
|
111
|
+
? "any"
|
|
112
|
+
: resolveValue(item.additionalProperties)}`;
|
|
108
113
|
}
|
|
109
114
|
if (item.properties || item.additionalProperties) {
|
|
110
|
-
if (output ===
|
|
111
|
-
return
|
|
115
|
+
if (output === "{\n") {
|
|
116
|
+
return "{}";
|
|
112
117
|
}
|
|
113
|
-
return output +
|
|
118
|
+
return output + "\n}";
|
|
114
119
|
}
|
|
115
|
-
return item.type ===
|
|
120
|
+
return item.type === "object" ? "{[key: string]: any}" : "any";
|
|
116
121
|
};
|
|
117
122
|
/**
|
|
118
123
|
* Resolve the value of a schema object to a proper type definition.
|
|
@@ -123,12 +128,12 @@ export const resolveValue = (schema) => isReference(schema) ? getRef(schema.$ref
|
|
|
123
128
|
*/
|
|
124
129
|
export const formatDescription = (description) => {
|
|
125
130
|
if (!description) {
|
|
126
|
-
return
|
|
131
|
+
return "";
|
|
127
132
|
}
|
|
128
133
|
return `\n/**\n${description
|
|
129
|
-
.split(
|
|
134
|
+
.split("\n")
|
|
130
135
|
.map((i) => `* ${i}`)
|
|
131
|
-
.join(
|
|
136
|
+
.join("\n")}\n */`;
|
|
132
137
|
};
|
|
133
138
|
/**
|
|
134
139
|
* Extract responses / request types from open-api specs
|
|
@@ -142,9 +147,9 @@ export const getResReqTypes = (responsesOrRequests) => {
|
|
|
142
147
|
return getRef(res.$ref);
|
|
143
148
|
}
|
|
144
149
|
if (res.content) {
|
|
145
|
-
for (
|
|
146
|
-
if (contentType.startsWith(
|
|
147
|
-
contentType.startsWith(
|
|
150
|
+
for (const contentType of Object.keys(res.content)) {
|
|
151
|
+
if (contentType.startsWith("application/json") ||
|
|
152
|
+
contentType.startsWith("application/octet-stream")) {
|
|
148
153
|
const schema = res.content[contentType].schema;
|
|
149
154
|
return resolveValue(schema);
|
|
150
155
|
}
|
|
@@ -152,6 +157,6 @@ export const getResReqTypes = (responsesOrRequests) => {
|
|
|
152
157
|
return;
|
|
153
158
|
}
|
|
154
159
|
return;
|
|
155
|
-
})).join(
|
|
160
|
+
})).join(" | ");
|
|
156
161
|
};
|
|
157
162
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;AACjC,OAAO,OAAO,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts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O,CAAC,WAAW,CAAC,CAAC,MAAO,CAAC;oBAEhD,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC;gBAC7B,CAAC;YACF,CAAC;YACD,OAAO;QACR,CAAC;QAED,OAAO;IACR,CAAC,CAAC,CACF,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACf,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/utils.ts
CHANGED
|
@@ -1,190 +1,212 @@
|
|
|
1
|
-
import lodash from
|
|
1
|
+
import lodash from "lodash";
|
|
2
2
|
const { uniq, isEmpty } = lodash;
|
|
3
|
-
import pasCase from
|
|
3
|
+
import pasCase from "case";
|
|
4
|
+
import chalk from "chalk";
|
|
4
5
|
|
|
5
6
|
const { pascal } = pasCase;
|
|
6
7
|
|
|
7
|
-
import {
|
|
8
|
+
import type {
|
|
9
|
+
ReferenceObject,
|
|
10
|
+
RequestBodyObject,
|
|
11
|
+
ResponseObject,
|
|
12
|
+
SchemaObject,
|
|
13
|
+
} from "openapi3-ts";
|
|
8
14
|
|
|
9
15
|
export const getDocs = (data: ReferenceObject | { description?: string }) =>
|
|
10
|
-
|
|
16
|
+
isReference(data) ? "" : formatDescription(data.description);
|
|
11
17
|
/**
|
|
12
18
|
* Discriminator helper for `ReferenceObject`
|
|
13
19
|
*/
|
|
14
20
|
export const isReference = (property: any): property is ReferenceObject => {
|
|
15
|
-
|
|
21
|
+
return Boolean(property.$ref);
|
|
16
22
|
};
|
|
17
23
|
|
|
18
24
|
/**
|
|
19
25
|
* Return the typescript equivalent of open-api data type
|
|
20
26
|
*/
|
|
21
27
|
export const getScalar = (item: SchemaObject) => {
|
|
22
|
-
|
|
28
|
+
const nullable = item.nullable ? " | null" : "";
|
|
23
29
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
switch (item.type) {
|
|
31
|
+
case "number":
|
|
32
|
+
case "integer":
|
|
33
|
+
return "number" + nullable;
|
|
28
34
|
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
case "boolean":
|
|
36
|
+
return "boolean" + nullable;
|
|
31
37
|
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
case "array":
|
|
39
|
+
return getArray(item) + nullable;
|
|
34
40
|
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
case "string":
|
|
42
|
+
return (item.enum ? `"${item.enum.join(`" | "`)}"` : "string") + nullable;
|
|
37
43
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
case "object":
|
|
45
|
+
return getObject(item) + nullable;
|
|
46
|
+
|
|
47
|
+
default:
|
|
48
|
+
return getObject(item) + nullable;
|
|
49
|
+
}
|
|
42
50
|
};
|
|
43
51
|
|
|
44
52
|
/**
|
|
45
53
|
* Return the output type from the $ref
|
|
46
54
|
*/
|
|
47
|
-
const getRef = ($ref: ReferenceObject[
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
const getRef = ($ref: ReferenceObject["$ref"]) => {
|
|
56
|
+
if ($ref.startsWith("#/components/schemas")) {
|
|
57
|
+
return pascal($ref.replace("#/components/schemas/", ""));
|
|
58
|
+
} else if ($ref.startsWith("#/components/responses")) {
|
|
59
|
+
return pascal($ref.replace("#/components/responses/", "")) + "Response";
|
|
60
|
+
} else if ($ref.startsWith("#/components/parameters")) {
|
|
61
|
+
return pascal($ref.replace("#/components/parameters/", "")) + "Parameter";
|
|
62
|
+
} else if ($ref.startsWith("#/components/requestBodies")) {
|
|
63
|
+
return (
|
|
64
|
+
pascal($ref.replace("#/components/requestBodies/", "")) + "RequestBody"
|
|
65
|
+
);
|
|
66
|
+
} else {
|
|
67
|
+
throw new Error(
|
|
68
|
+
"This library only resolve $ref that are include into `#/components/*` for now",
|
|
69
|
+
);
|
|
70
|
+
}
|
|
59
71
|
};
|
|
60
72
|
|
|
61
73
|
/**
|
|
62
74
|
* Return the output type from an array
|
|
63
75
|
*/
|
|
64
|
-
const getArray = (item: SchemaObject): string => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
const getArray = (item: SchemaObject): string | undefined => {
|
|
77
|
+
if (item.items) {
|
|
78
|
+
if (
|
|
79
|
+
!isReference(item.items) &&
|
|
80
|
+
(item.items.oneOf || item.items.allOf || item.items.enum)
|
|
81
|
+
) {
|
|
82
|
+
return `(${resolveValue(item.items)})[]`;
|
|
83
|
+
} else {
|
|
84
|
+
return `${resolveValue(item.items)}[]`;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
console.log(chalk.red("Skipped: All arrays must have an `items` key define"));
|
|
74
88
|
};
|
|
75
89
|
|
|
76
90
|
/**
|
|
77
91
|
* Return the output type from an object
|
|
78
92
|
*/
|
|
79
93
|
const getObject = (item: SchemaObject): string => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
94
|
+
if (isReference(item)) {
|
|
95
|
+
return getRef(item.$ref);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (item.allOf) {
|
|
99
|
+
return item.allOf.map(resolveValue).join(" & ");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (item.oneOf) {
|
|
103
|
+
return item.oneOf.map(resolveValue).join(" | ");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!item.type && !item.properties && !item.additionalProperties) {
|
|
107
|
+
return "{}";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
|
|
111
|
+
if (
|
|
112
|
+
item.type === "object" &&
|
|
113
|
+
!item.properties &&
|
|
114
|
+
(!item.additionalProperties ||
|
|
115
|
+
item.additionalProperties === true ||
|
|
116
|
+
isEmpty(item.additionalProperties))
|
|
117
|
+
) {
|
|
118
|
+
return "{[key: string]: any}";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
122
|
+
// Consolidation of item.properties & item.additionalProperties
|
|
123
|
+
let output = "{";
|
|
124
|
+
if (item.properties) {
|
|
125
|
+
output += Object.entries(item.properties)
|
|
126
|
+
.map(([key, prop]: [string, ReferenceObject | SchemaObject]) => {
|
|
127
|
+
const doc = getDocs(prop);
|
|
128
|
+
const isRequired = (item.required || []).includes(key);
|
|
129
|
+
const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
|
|
130
|
+
return `${doc}\n${processedKey}${isRequired ? "" : "?"}: ${resolveValue(
|
|
131
|
+
prop,
|
|
132
|
+
)};`;
|
|
133
|
+
})
|
|
134
|
+
.join("");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (item.additionalProperties) {
|
|
138
|
+
if (item.properties) {
|
|
139
|
+
output += "\n";
|
|
140
|
+
}
|
|
141
|
+
output += `} & { [key: string]: ${
|
|
142
|
+
item.additionalProperties === true
|
|
143
|
+
? "any"
|
|
144
|
+
: resolveValue(item.additionalProperties)
|
|
145
|
+
}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (item.properties || item.additionalProperties) {
|
|
149
|
+
if (output === "{\n") {
|
|
150
|
+
return "{}";
|
|
151
|
+
}
|
|
152
|
+
return output + "\n}";
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return item.type === "object" ? "{[key: string]: any}" : "any";
|
|
136
156
|
};
|
|
137
157
|
|
|
138
158
|
/**
|
|
139
159
|
* Resolve the value of a schema object to a proper type definition.
|
|
140
160
|
*/
|
|
141
161
|
export const resolveValue = (schema: SchemaObject) =>
|
|
142
|
-
|
|
162
|
+
isReference(schema) ? getRef(schema.$ref) : getScalar(schema);
|
|
143
163
|
|
|
144
164
|
/**
|
|
145
165
|
* Format a description to code documentation.
|
|
146
166
|
*/
|
|
147
167
|
export const formatDescription = (description?: string) => {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
168
|
+
if (!description) {
|
|
169
|
+
return "";
|
|
170
|
+
}
|
|
171
|
+
return `\n/**\n${description
|
|
172
|
+
.split("\n")
|
|
173
|
+
.map((i) => `* ${i}`)
|
|
174
|
+
.join("\n")}\n */`;
|
|
155
175
|
};
|
|
156
176
|
|
|
157
177
|
/**
|
|
158
178
|
* Extract responses / request types from open-api specs
|
|
159
179
|
*/
|
|
160
180
|
export const getResReqTypes = (
|
|
161
|
-
|
|
181
|
+
responsesOrRequests: Array<
|
|
182
|
+
[string, ResponseObject | ReferenceObject | RequestBodyObject]
|
|
183
|
+
>,
|
|
162
184
|
) => {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
185
|
+
return uniq(
|
|
186
|
+
responsesOrRequests.map(([_, res]) => {
|
|
187
|
+
if (!res) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (isReference(res)) {
|
|
192
|
+
return getRef(res.$ref);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (res.content) {
|
|
196
|
+
for (const contentType of Object.keys(res.content)) {
|
|
197
|
+
if (
|
|
198
|
+
contentType.startsWith("application/json") ||
|
|
199
|
+
contentType.startsWith("application/octet-stream")
|
|
200
|
+
) {
|
|
201
|
+
const schema = res.content[contentType].schema!;
|
|
202
|
+
|
|
203
|
+
return resolveValue(schema);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return;
|
|
210
|
+
}),
|
|
211
|
+
).join(" | ");
|
|
190
212
|
};
|