@sdk-it/spec 0.22.0 → 0.23.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/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -2629
- package/dist/index.js.map +4 -4
- package/dist/lib/find-polymorphic-varients.d.ts +1 -1
- package/dist/lib/find-polymorphic-varients.d.ts.map +1 -1
- package/dist/lib/find-polymorphic-varients.js +291 -0
- package/dist/lib/find-polymorphic-varients.js.map +7 -0
- package/dist/lib/find-polymorphic-varients.test.js +615 -0
- package/dist/lib/find-polymorphic-varients.test.js.map +7 -0
- package/dist/lib/find-unique-schema-name.js +16 -0
- package/dist/lib/find-unique-schema-name.js.map +7 -0
- package/dist/lib/for-each-operation.d.ts +3 -0
- package/dist/lib/for-each-operation.d.ts.map +1 -0
- package/dist/lib/for-each-operation.js +30 -0
- package/dist/lib/for-each-operation.js.map +7 -0
- package/dist/lib/format-name.js +86 -0
- package/dist/lib/format-name.js.map +7 -0
- package/dist/lib/get-ref-usage.d.ts +1 -1
- package/dist/lib/get-ref-usage.d.ts.map +1 -1
- package/dist/lib/get-ref-usage.js +52 -0
- package/dist/lib/get-ref-usage.js.map +7 -0
- package/dist/lib/is-primitive-schema.js +12 -0
- package/dist/lib/is-primitive-schema.js.map +7 -0
- package/dist/lib/is.d.ts +14 -0
- package/dist/lib/is.d.ts.map +1 -0
- package/dist/lib/is.js +71 -0
- package/dist/lib/is.js.map +7 -0
- package/dist/lib/loaders/load-spec.js +25 -0
- package/dist/lib/loaders/load-spec.js.map +7 -0
- package/dist/lib/loaders/local-loader.js +20 -0
- package/dist/lib/loaders/local-loader.js.map +7 -0
- package/dist/lib/loaders/postman/postman-converter.js +483 -0
- package/dist/lib/loaders/postman/postman-converter.js.map +7 -0
- package/dist/lib/loaders/postman/spec-types.js +1 -0
- package/dist/lib/loaders/postman/spec-types.js.map +7 -0
- package/dist/lib/loaders/remote-loader.js +29 -0
- package/dist/lib/loaders/remote-loader.js.map +7 -0
- package/dist/lib/metadata.js +51 -0
- package/dist/lib/metadata.js.map +7 -0
- package/dist/lib/operation.d.ts +4 -75
- package/dist/lib/operation.d.ts.map +1 -1
- package/dist/lib/operation.js +402 -0
- package/dist/lib/operation.js.map +7 -0
- package/dist/lib/operation.test.js +261 -0
- package/dist/lib/operation.test.js.map +7 -0
- package/dist/lib/options.d.ts +30 -0
- package/dist/lib/options.d.ts.map +1 -0
- package/dist/lib/options.js +54 -0
- package/dist/lib/options.js.map +7 -0
- package/dist/lib/overview-docs.d.ts +4 -0
- package/dist/lib/overview-docs.d.ts.map +1 -0
- package/dist/lib/overview-docs.js +276 -0
- package/dist/lib/overview-docs.js.map +7 -0
- package/dist/lib/pagination/pagination-result.js +235 -0
- package/dist/lib/pagination/pagination-result.js.map +7 -0
- package/dist/lib/pagination/pagination-result.test.js +548 -0
- package/dist/lib/pagination/pagination-result.test.js.map +7 -0
- package/dist/lib/pagination/pagination.d.ts +1 -1
- package/dist/lib/pagination/pagination.d.ts.map +1 -1
- package/dist/lib/pagination/pagination.js +222 -0
- package/dist/lib/pagination/pagination.js.map +7 -0
- package/dist/lib/pagination/pagination.test.js +378 -0
- package/dist/lib/pagination/pagination.test.js.map +7 -0
- package/dist/lib/reserved-keywords.d.ts +6 -0
- package/dist/lib/reserved-keywords.d.ts.map +1 -0
- package/dist/lib/reserved-keywords.js +71 -0
- package/dist/lib/reserved-keywords.js.map +7 -0
- package/dist/lib/security.d.ts +6 -4
- package/dist/lib/security.d.ts.map +1 -1
- package/dist/lib/security.js +75 -0
- package/dist/lib/security.js.map +7 -0
- package/dist/lib/sidebar.d.ts +5 -3
- package/dist/lib/sidebar.d.ts.map +1 -1
- package/dist/lib/sidebar.js +79 -0
- package/dist/lib/sidebar.js.map +7 -0
- package/dist/lib/tag.d.ts +19 -0
- package/dist/lib/tag.d.ts.map +1 -0
- package/dist/lib/tag.js +132 -0
- package/dist/lib/tag.js.map +7 -0
- package/dist/lib/tune.d.ts +1 -1
- package/dist/lib/tune.d.ts.map +1 -1
- package/dist/lib/tune.js +291 -0
- package/dist/lib/tune.js.map +7 -0
- package/dist/lib/tune.test.js +60 -0
- package/dist/lib/tune.test.js.map +7 -0
- package/dist/lib/types.d.ts +37 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +1 -0
- package/dist/lib/types.js.map +7 -0
- package/package.json +4 -3
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { groupBy, uniqBy } from "lodash-es";
|
|
2
|
+
import { camelcase, isEmpty, isRef, resolveRef } from "@sdk-it/core";
|
|
3
|
+
import { coerceTypes } from "./tune.js";
|
|
4
|
+
const groupSchemasByType = (spec, schemas) => {
|
|
5
|
+
const groups = schemas.reduce((acc, schema, index) => {
|
|
6
|
+
if (isRef(schema)) {
|
|
7
|
+
const referenced = resolveRef(spec, schema);
|
|
8
|
+
const [type2] = coerceTypes(referenced, false);
|
|
9
|
+
acc[type2] ??= [];
|
|
10
|
+
acc[type2].push({ schema: referenced, position: index });
|
|
11
|
+
return acc;
|
|
12
|
+
}
|
|
13
|
+
if (isRef(schema.items)) {
|
|
14
|
+
const referenced = resolveRef(spec, schema.items);
|
|
15
|
+
acc.array ??= [];
|
|
16
|
+
acc.array.push({
|
|
17
|
+
schema: { ...schema, items: referenced },
|
|
18
|
+
position: index
|
|
19
|
+
});
|
|
20
|
+
return acc;
|
|
21
|
+
}
|
|
22
|
+
if (schema.oneOf) {
|
|
23
|
+
acc["oneOf"] ??= [];
|
|
24
|
+
acc["oneOf"].push({ schema: schema.oneOf, position: index });
|
|
25
|
+
return acc;
|
|
26
|
+
}
|
|
27
|
+
if (schema.anyOf) {
|
|
28
|
+
acc["oneOf"] ??= [];
|
|
29
|
+
acc["oneOf"].push({ schema: schema.anyOf, position: index });
|
|
30
|
+
return acc;
|
|
31
|
+
}
|
|
32
|
+
if (schema.const) {
|
|
33
|
+
switch (typeof schema.const) {
|
|
34
|
+
case "string":
|
|
35
|
+
acc.string ??= [];
|
|
36
|
+
acc.string.push({ schema, position: index });
|
|
37
|
+
return acc;
|
|
38
|
+
case "number":
|
|
39
|
+
acc.number ??= [];
|
|
40
|
+
acc.number.push({ schema, position: index });
|
|
41
|
+
return acc;
|
|
42
|
+
case "boolean":
|
|
43
|
+
acc.boolean ??= [];
|
|
44
|
+
acc.boolean.push({ schema, position: index });
|
|
45
|
+
return acc;
|
|
46
|
+
default:
|
|
47
|
+
throw new Error(
|
|
48
|
+
`Unsupported const type: ${typeof schema.const} for ${schema.const}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const [type] = coerceTypes(schema, false);
|
|
53
|
+
acc[type] ??= [];
|
|
54
|
+
acc[type].push({ schema, position: index });
|
|
55
|
+
return acc;
|
|
56
|
+
}, {});
|
|
57
|
+
return groups;
|
|
58
|
+
};
|
|
59
|
+
function findVarients(spec, schemas) {
|
|
60
|
+
let varients = [];
|
|
61
|
+
const schemasByType = groupSchemasByType(spec, schemas);
|
|
62
|
+
if (!isEmpty(schemasByType.string)) {
|
|
63
|
+
for (const { schema, position } of schemasByType.string) {
|
|
64
|
+
if (schema.const !== void 0) {
|
|
65
|
+
varients.push({
|
|
66
|
+
name: schema.const || "empty",
|
|
67
|
+
type: "string",
|
|
68
|
+
position,
|
|
69
|
+
priority: 100 - varients.length
|
|
70
|
+
});
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (schema.format) {
|
|
74
|
+
varients.push({
|
|
75
|
+
name: camelcase(schema.format),
|
|
76
|
+
type: "string",
|
|
77
|
+
position,
|
|
78
|
+
priority: 90 - varients.length
|
|
79
|
+
});
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
varients.push({ name: "textContent", type: "string", position });
|
|
83
|
+
}
|
|
84
|
+
varients = uniqBy(varients, (it) => it.name);
|
|
85
|
+
}
|
|
86
|
+
if (!isEmpty(schemasByType.number) || !isEmpty(schemasByType.integer)) {
|
|
87
|
+
const schemas2 = [
|
|
88
|
+
...schemasByType.number ?? [],
|
|
89
|
+
...schemasByType.integer ?? []
|
|
90
|
+
];
|
|
91
|
+
for (const { schema, position } of schemas2) {
|
|
92
|
+
if (schema.format === "int64") {
|
|
93
|
+
varients.push({
|
|
94
|
+
name: "integer",
|
|
95
|
+
type: "number",
|
|
96
|
+
position,
|
|
97
|
+
priority: 90 - varients.length
|
|
98
|
+
});
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (schema.format === "float") {
|
|
102
|
+
varients.push({
|
|
103
|
+
name: "float",
|
|
104
|
+
type: "number",
|
|
105
|
+
position,
|
|
106
|
+
priority: 90 - varients.length
|
|
107
|
+
});
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (schema.format === "double") {
|
|
111
|
+
varients.push({
|
|
112
|
+
name: "double",
|
|
113
|
+
type: "number",
|
|
114
|
+
position,
|
|
115
|
+
priority: 90 - varients.length
|
|
116
|
+
});
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
varients.push({ name: "number", type: "number", position });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (!isEmpty(schemasByType.array)) {
|
|
123
|
+
for (const { schema, position } of schemasByType.array) {
|
|
124
|
+
const items = schema.items;
|
|
125
|
+
if (!items) {
|
|
126
|
+
varients.push({ name: "any", type: "array", position });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const [type] = coerceTypes(items);
|
|
130
|
+
if (type === "string") {
|
|
131
|
+
varients.push({
|
|
132
|
+
name: "textList",
|
|
133
|
+
type: "array",
|
|
134
|
+
subtype: "string",
|
|
135
|
+
position
|
|
136
|
+
});
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (type === "number") {
|
|
140
|
+
varients.push({
|
|
141
|
+
name: "numList",
|
|
142
|
+
type: "array",
|
|
143
|
+
subtype: "number",
|
|
144
|
+
position
|
|
145
|
+
});
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (type === "integer") {
|
|
149
|
+
varients.push({
|
|
150
|
+
name: "intList",
|
|
151
|
+
type: "array",
|
|
152
|
+
subtype: "integer",
|
|
153
|
+
position
|
|
154
|
+
});
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (type === "object") {
|
|
158
|
+
const subvarients = findVarients(spec, [items]);
|
|
159
|
+
for (const subvarient of subvarients) {
|
|
160
|
+
varients.push({
|
|
161
|
+
...subvarient,
|
|
162
|
+
type: "array",
|
|
163
|
+
position
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (type === "array") {
|
|
169
|
+
const subvarients = findVarients(spec, [items]);
|
|
170
|
+
for (const subvarient of subvarients) {
|
|
171
|
+
varients.push({
|
|
172
|
+
...subvarient,
|
|
173
|
+
name: `${subvarient.name}Matrix`,
|
|
174
|
+
type: "array",
|
|
175
|
+
position
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
varients.push({ name: "list", type: "array", position });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (!isEmpty(schemasByType.$ref)) {
|
|
184
|
+
const subvarients = findVarients(
|
|
185
|
+
spec,
|
|
186
|
+
schemasByType.$ref.map((it) => resolveRef(spec, it.schema))
|
|
187
|
+
);
|
|
188
|
+
varients.push(
|
|
189
|
+
...subvarients.map((it) => ({
|
|
190
|
+
...it
|
|
191
|
+
}))
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
if (!isEmpty(schemasByType.oneOf)) {
|
|
195
|
+
for (const { schema, position } of schemasByType.oneOf) {
|
|
196
|
+
const subvarients = findVarients(spec, schema);
|
|
197
|
+
varients.push(
|
|
198
|
+
...subvarients.map((it) => ({
|
|
199
|
+
...it,
|
|
200
|
+
position
|
|
201
|
+
}))
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const matrix = [];
|
|
206
|
+
for (const { schema, position } of schemasByType.object ?? []) {
|
|
207
|
+
if (schema.additionalProperties || isEmpty({ ...schema.properties, ...schema["x-properties"] })) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
for (const key of ["properties", "x-properties"]) {
|
|
211
|
+
if (!schema[key]) continue;
|
|
212
|
+
const list = Object.entries(schema[key]).map(([name, schemaOrRef]) => {
|
|
213
|
+
const schema2 = resolveRef(spec, schemaOrRef);
|
|
214
|
+
name = schema2.const ?? schema2.enum?.[0] ?? name;
|
|
215
|
+
if (schema2.type === "string") {
|
|
216
|
+
return {
|
|
217
|
+
priority: schema2.const !== void 0 || schema2.enum?.[0] !== void 0 ? 100 - matrix.length : void 0,
|
|
218
|
+
static: true,
|
|
219
|
+
subtype: "string",
|
|
220
|
+
source: name,
|
|
221
|
+
name,
|
|
222
|
+
type: "object",
|
|
223
|
+
position
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
subtype: "string",
|
|
228
|
+
source: name,
|
|
229
|
+
name,
|
|
230
|
+
type: "object",
|
|
231
|
+
position
|
|
232
|
+
};
|
|
233
|
+
});
|
|
234
|
+
matrix.push([...new Set(list)].sort((a) => a.static ? -1 : 1));
|
|
235
|
+
}
|
|
236
|
+
if (matrix.length === 0) {
|
|
237
|
+
throw new Error(
|
|
238
|
+
"No valid objects found. Please check your OpenAPI spec."
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
let discriminatorProp;
|
|
242
|
+
const firstRow = matrix[0];
|
|
243
|
+
for (const prop of firstRow) {
|
|
244
|
+
const existsCrossAllRows = matrix.every((row) => row.includes(prop));
|
|
245
|
+
if (existsCrossAllRows) {
|
|
246
|
+
discriminatorProp = prop;
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const row of matrix) {
|
|
252
|
+
for (const prop of row) {
|
|
253
|
+
const isUnique = matrix.every(
|
|
254
|
+
(it) => it === row ? true : !it.some((p) => p.name === prop.name)
|
|
255
|
+
);
|
|
256
|
+
if (isUnique) {
|
|
257
|
+
varients.push(prop);
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return varients.sort((a, b) => {
|
|
263
|
+
const aHasPriority = a.priority !== void 0;
|
|
264
|
+
const bHasPriority = b.priority !== void 0;
|
|
265
|
+
if (aHasPriority && bHasPriority) {
|
|
266
|
+
if (a.priority !== b.priority) {
|
|
267
|
+
return b.priority - a.priority;
|
|
268
|
+
}
|
|
269
|
+
return a.position - b.position;
|
|
270
|
+
} else if (aHasPriority) {
|
|
271
|
+
return -1;
|
|
272
|
+
} else if (bHasPriority) {
|
|
273
|
+
return 1;
|
|
274
|
+
} else {
|
|
275
|
+
return 0;
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function findPolymorphicVarients(spec, schemas) {
|
|
280
|
+
const varients = findVarients(spec, schemas);
|
|
281
|
+
return Object.values(groupBy(varients, (it) => "-" + it.position)).map(
|
|
282
|
+
(group) => {
|
|
283
|
+
return (group ?? [])[0];
|
|
284
|
+
}
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
export {
|
|
288
|
+
findPolymorphicVarients,
|
|
289
|
+
findVarients
|
|
290
|
+
};
|
|
291
|
+
//# sourceMappingURL=find-polymorphic-varients.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/lib/find-polymorphic-varients.ts"],
|
|
4
|
+
"sourcesContent": ["import { groupBy, uniqBy } from 'lodash-es';\nimport type {\n ReferenceObject,\n SchemaObject,\n SchemaObjectType,\n} from 'openapi3-ts/oas31';\n\nimport { camelcase, isEmpty, isRef, resolveRef } from '@sdk-it/core';\n\nimport { coerceTypes } from './tune.js';\nimport type { OurOpenAPIObject } from './types.js';\n\nexport type Varient = {\n name: string;\n type: string;\n position: number;\n priority?: number;\n description?: string;\n source?: string;\n static?: boolean;\n subtype?: SchemaObjectType;\n};\n\nconst groupSchemasByType = (\n spec: OurOpenAPIObject,\n schemas: (SchemaObject | ReferenceObject)[],\n) => {\n const groups = schemas.reduce<\n Partial<{\n [K in\n | SchemaObjectType\n | '$ref'\n | 'oneOf'\n | 'anyOf']: K extends SchemaObjectType\n ? { schema: SchemaObject; position: number }[]\n : K extends 'oneOf' | 'anyOf'\n ? { schema: (SchemaObject | ReferenceObject)[]; position: number }[]\n : { schema: ReferenceObject; position: number }[];\n }>\n >((acc, schema, index) => {\n if (isRef(schema)) {\n const referenced = resolveRef<SchemaObject>(spec, schema);\n const [type] = coerceTypes(referenced, false);\n acc[type] ??= [];\n acc[type].push({ schema: referenced, position: index });\n return acc;\n }\n\n if (isRef(schema.items)) {\n const referenced = resolveRef<SchemaObject>(spec, schema.items);\n acc.array ??= [];\n acc.array.push({\n schema: { ...schema, items: referenced },\n position: index,\n });\n return acc;\n }\n if (schema.oneOf) {\n acc['oneOf'] ??= [];\n acc['oneOf'].push({ schema: schema.oneOf, position: index });\n return acc;\n }\n if (schema.anyOf) {\n acc['oneOf'] ??= [];\n acc['oneOf'].push({ schema: schema.anyOf, position: index });\n return acc;\n }\n if (schema.const) {\n switch (typeof schema.const) {\n case 'string':\n acc.string ??= [];\n acc.string.push({ schema, position: index });\n return acc;\n case 'number':\n acc.number ??= [];\n acc.number.push({ schema, position: index });\n return acc;\n case 'boolean':\n acc.boolean ??= [];\n acc.boolean.push({ schema, position: index });\n return acc;\n default:\n throw new Error(\n `Unsupported const type: ${typeof schema.const} for ${schema.const}`,\n );\n }\n }\n\n const [type] = coerceTypes(schema, false);\n acc[type] ??= [];\n acc[type].push({ schema, position: index });\n return acc;\n }, {});\n\n return groups;\n};\n\nexport function findVarients(\n spec: OurOpenAPIObject,\n schemas: (SchemaObject | ReferenceObject)[],\n): Varient[] {\n let varients: Varient[] = [];\n const schemasByType = groupSchemasByType(spec, schemas);\n if (!isEmpty(schemasByType.string)) {\n for (const { schema, position } of schemasByType.string) {\n if (schema.const !== undefined) {\n varients.push({\n name: schema.const || 'empty',\n type: 'string',\n position,\n priority: 100 - varients.length,\n });\n continue;\n }\n\n if (schema.format) {\n varients.push({\n name: camelcase(schema.format),\n type: 'string',\n position,\n priority: 90 - varients.length,\n });\n continue;\n }\n\n // if (!isEmpty(schema.enum)) {\n // for (const enumValue of schema.enum) {\n // if (enumValue === '') {\n // varients.push({\n // name: 'empty',\n // type: 'string',\n // position,\n // priority: 80 - varients.length,\n // });\n // continue;\n // }\n // varients.push({\n // name: enumValue,\n // type: 'string',\n // position,\n // priority: 80 - varients.length,\n // });\n // }\n // continue;\n // }\n\n varients.push({ name: 'textContent', type: 'string', position });\n }\n varients = uniqBy(varients, (it) => it.name);\n }\n\n if (!isEmpty(schemasByType.number) || !isEmpty(schemasByType.integer)) {\n const schemas = [\n ...(schemasByType.number ?? []),\n ...(schemasByType.integer ?? []),\n ];\n for (const { schema, position } of schemas) {\n if (schema.format === 'int64') {\n varients.push({\n name: 'integer',\n type: 'number',\n position,\n priority: 90 - varients.length,\n });\n continue;\n }\n if (schema.format === 'float') {\n varients.push({\n name: 'float',\n type: 'number',\n position,\n priority: 90 - varients.length,\n });\n continue;\n }\n if (schema.format === 'double') {\n varients.push({\n name: 'double',\n type: 'number',\n position,\n priority: 90 - varients.length,\n });\n continue;\n }\n varients.push({ name: 'number', type: 'number', position });\n }\n }\n\n if (!isEmpty(schemasByType.array)) {\n for (const { schema, position } of schemasByType.array) {\n const items = schema.items;\n if (!items) {\n varients.push({ name: 'any', type: 'array', position });\n continue;\n }\n const [type] = coerceTypes(items as SchemaObject);\n if (type === 'string') {\n varients.push({\n name: 'textList',\n type: 'array',\n subtype: 'string',\n position,\n });\n continue;\n }\n if (type === 'number') {\n varients.push({\n name: 'numList',\n type: 'array',\n subtype: 'number',\n position,\n });\n continue;\n }\n if (type === 'integer') {\n varients.push({\n name: 'intList',\n type: 'array',\n subtype: 'integer',\n position,\n });\n continue;\n }\n if (type === 'object') {\n const subvarients = findVarients(spec, [items]);\n for (const subvarient of subvarients) {\n varients.push({\n ...subvarient,\n type: 'array',\n position,\n });\n }\n continue;\n }\n if (type === 'array') {\n const subvarients = findVarients(spec, [items]);\n for (const subvarient of subvarients) {\n varients.push({\n ...subvarient,\n name: `${subvarient.name}Matrix`,\n type: 'array',\n position,\n });\n }\n continue;\n }\n varients.push({ name: 'list', type: 'array', position });\n }\n }\n\n if (!isEmpty(schemasByType.$ref)) {\n const subvarients = findVarients(\n spec,\n schemasByType.$ref.map((it) => resolveRef(spec, it.schema)),\n );\n varients.push(\n ...subvarients.map((it) => ({\n ...it,\n })),\n );\n }\n\n if (!isEmpty(schemasByType.oneOf)) {\n for (const { schema, position } of schemasByType.oneOf) {\n const subvarients = findVarients(spec, schema);\n varients.push(\n ...subvarients.map((it) => ({\n ...it,\n position,\n })),\n );\n }\n }\n\n const matrix: Varient[][] = [];\n\n for (const { schema, position } of schemasByType.object ?? []) {\n if (\n schema.additionalProperties ||\n isEmpty({ ...schema.properties, ...schema['x-properties'] })\n ) {\n continue;\n }\n\n for (const key of ['properties', 'x-properties'] as const) {\n if (!schema[key]) continue;\n const list = (\n Object.entries(schema[key]) as [\n string,\n SchemaObject | ReferenceObject,\n ][]\n ).map(([name, schemaOrRef]) => {\n const schema = resolveRef<SchemaObject>(spec, schemaOrRef);\n name = schema.const ?? schema.enum?.[0] ?? name;\n if (schema.type === 'string') {\n return {\n priority:\n schema.const !== undefined || schema.enum?.[0] !== undefined\n ? 100 - matrix.length\n : undefined,\n static: true,\n subtype: 'string',\n source: name,\n name: name,\n type: 'object',\n position,\n } satisfies Varient;\n }\n return {\n subtype: 'string',\n source: name,\n name: name,\n type: 'object',\n position,\n } satisfies Varient;\n });\n matrix.push([...new Set(list)].sort((a) => (a.static ? -1 : 1)));\n }\n if (matrix.length === 0) {\n throw new Error(\n 'No valid objects found. Please check your OpenAPI spec.',\n );\n }\n let discriminatorProp: Varient | undefined;\n const firstRow = matrix[0];\n for (const prop of firstRow) {\n // check if this prop is exists across all rows\n const existsCrossAllRows = matrix.every((row) => row.includes(prop));\n if (existsCrossAllRows) {\n discriminatorProp = prop;\n break;\n }\n }\n }\n\n for (const row of matrix) {\n for (const prop of row) {\n // check if this prop is unique across all rows\n const isUnique = matrix.every((it) =>\n it === row ? true : !it.some((p) => p.name === prop.name),\n );\n if (isUnique) {\n varients.push(prop);\n break;\n }\n }\n }\n\n // Sort all variants by priority (highest first), then by original position\n return varients.sort((a, b) => {\n const aHasPriority = a.priority !== undefined;\n const bHasPriority = b.priority !== undefined;\n\n if (aHasPriority && bHasPriority) {\n // Both have priority\n if (a.priority !== b.priority) {\n return b.priority! - a.priority!; // Higher priority first\n }\n // Priorities are equal, sort by original position as a tie-breaker\n return a.position - b.position;\n } else if (aHasPriority) {\n return -1; // 'a' comes first\n } else if (bHasPriority) {\n return 1; // 'b' comes first\n } else {\n // Neither has priority. Keep their relative order from before this sort.\n // This relies on a stable sort (standard in ES2019+).\n // Returning 0 preserves the order in which they were added to the 'varients' array.\n return 0;\n }\n });\n}\n\nexport function findPolymorphicVarients(\n spec: OurOpenAPIObject,\n schemas: (SchemaObject | ReferenceObject)[],\n): Varient[] {\n const varients = findVarients(spec, schemas);\n // prepend '-' to prevent key sortings\n return Object.values(groupBy(varients, (it) => '-' + it.position)).map(\n (group) => {\n return (group ?? [])[0];\n },\n );\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,SAAS,cAAc;AAOhC,SAAS,WAAW,SAAS,OAAO,kBAAkB;AAEtD,SAAS,mBAAmB;AAc5B,MAAM,qBAAqB,CACzB,MACA,YACG;AACH,QAAM,SAAS,QAAQ,OAYrB,CAAC,KAAK,QAAQ,UAAU;AACxB,QAAI,MAAM,MAAM,GAAG;AACjB,YAAM,aAAa,WAAyB,MAAM,MAAM;AACxD,YAAM,CAACA,KAAI,IAAI,YAAY,YAAY,KAAK;AAC5C,UAAI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