@kubb/plugin-oas 3.16.2 → 3.16.4
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/Oas-CuqAnIw-.js +60 -0
- package/dist/Oas-CuqAnIw-.js.map +1 -0
- package/dist/Oas-Cv_pyXRM.cjs +78 -0
- package/dist/Oas-Cv_pyXRM.cjs.map +1 -0
- package/dist/OperationGenerator-CgbXErhW.d.cts +900 -0
- package/dist/OperationGenerator-OtLcGk2y.d.ts +900 -0
- package/dist/Schema-DJshqHto.d.cts +38 -0
- package/dist/Schema-DOXGUW1m.d.ts +38 -0
- package/dist/SchemaGenerator-8205BXCp.cjs +942 -0
- package/dist/SchemaGenerator-8205BXCp.cjs.map +1 -0
- package/dist/SchemaGenerator-B8Io6kky.js +930 -0
- package/dist/SchemaGenerator-B8Io6kky.js.map +1 -0
- package/dist/SchemaMapper-BaZQKrQB.js +54 -0
- package/dist/SchemaMapper-BaZQKrQB.js.map +1 -0
- package/dist/SchemaMapper-BiagBQN4.d.cts +388 -0
- package/dist/SchemaMapper-C2J2d3o4.d.ts +388 -0
- package/dist/SchemaMapper-D8J0V9Pj.cjs +66 -0
- package/dist/SchemaMapper-D8J0V9Pj.cjs.map +1 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/components.cjs +4 -19
- package/dist/components.d.cts +42 -35
- package/dist/components.d.ts +42 -35
- package/dist/components.js +3 -3
- package/dist/generators-BhLMlRNk.js +137 -0
- package/dist/generators-BhLMlRNk.js.map +1 -0
- package/dist/generators-DLH2kvlh.cjs +155 -0
- package/dist/generators-DLH2kvlh.cjs.map +1 -0
- package/dist/generators.cjs +4 -13
- package/dist/generators.d.cts +5 -7
- package/dist/generators.d.ts +5 -7
- package/dist/generators.js +5 -5
- package/dist/getFooter-BChY2kC1.cjs +43 -0
- package/dist/getFooter-BChY2kC1.cjs.map +1 -0
- package/dist/getFooter-T7_pZ6f8.js +31 -0
- package/dist/getFooter-T7_pZ6f8.js.map +1 -0
- package/dist/getSchemas-D8mP129c.cjs +67 -0
- package/dist/getSchemas-D8mP129c.cjs.map +1 -0
- package/dist/getSchemas-WoSBIxG8.js +55 -0
- package/dist/getSchemas-WoSBIxG8.js.map +1 -0
- package/dist/hooks.cjs +213 -195
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +91 -86
- package/dist/hooks.d.ts +91 -86
- package/dist/hooks.js +212 -193
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +280 -342
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -10
- package/dist/index.d.ts +7 -10
- package/dist/index.js +274 -318
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +802 -737
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.cts +12 -11
- package/dist/mocks.d.ts +12 -11
- package/dist/mocks.js +802 -735
- package/dist/mocks.js.map +1 -1
- package/dist/parseFromConfig-CMNGsef2.cjs +33 -0
- package/dist/parseFromConfig-CMNGsef2.cjs.map +1 -0
- package/dist/parseFromConfig-yAiFK03V.js +27 -0
- package/dist/parseFromConfig-yAiFK03V.js.map +1 -0
- package/dist/utils.cjs +58 -74
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +96 -42
- package/dist/utils.d.ts +96 -42
- package/dist/utils.js +47 -46
- package/dist/utils.js.map +1 -1
- package/package.json +26 -34
- package/src/components/Operation.tsx +1 -2
- package/src/mocks/schemas.ts +39 -0
- package/src/utils/getBanner.ts +5 -5
- package/src/utils/getFooter.ts +4 -4
- package/dist/OperationGenerator-C7NPZtOM.d.ts +0 -288
- package/dist/OperationGenerator-DJE2XQ83.d.cts +0 -288
- package/dist/Schema-AjebpeVY.d.ts +0 -32
- package/dist/Schema-M6983FL1.d.cts +0 -32
- package/dist/SchemaMapper-B21ZmWag.d.cts +0 -275
- package/dist/SchemaMapper-B21ZmWag.d.ts +0 -275
- package/dist/chunk-6S7YFY6F.js +0 -33
- package/dist/chunk-6S7YFY6F.js.map +0 -1
- package/dist/chunk-76E35IZ2.js +0 -56
- package/dist/chunk-76E35IZ2.js.map +0 -1
- package/dist/chunk-7RFNM43R.cjs +0 -41
- package/dist/chunk-7RFNM43R.cjs.map +0 -1
- package/dist/chunk-DTD4ZUDA.cjs +0 -36
- package/dist/chunk-DTD4ZUDA.cjs.map +0 -1
- package/dist/chunk-G3FDK7YW.js +0 -35
- package/dist/chunk-G3FDK7YW.js.map +0 -1
- package/dist/chunk-JNN4JPWK.js +0 -28
- package/dist/chunk-JNN4JPWK.js.map +0 -1
- package/dist/chunk-MBX66JAT.js +0 -93
- package/dist/chunk-MBX66JAT.js.map +0 -1
- package/dist/chunk-NC5PWNEF.js +0 -866
- package/dist/chunk-NC5PWNEF.js.map +0 -1
- package/dist/chunk-PORSNYI5.js +0 -47
- package/dist/chunk-PORSNYI5.js.map +0 -1
- package/dist/chunk-QJMOOF2A.cjs +0 -97
- package/dist/chunk-QJMOOF2A.cjs.map +0 -1
- package/dist/chunk-RKHCI3WI.cjs +0 -1002
- package/dist/chunk-RKHCI3WI.cjs.map +0 -1
- package/dist/chunk-YWMMI3MO.cjs +0 -59
- package/dist/chunk-YWMMI3MO.cjs.map +0 -1
- package/dist/chunk-Z2NREI4X.cjs +0 -32
- package/dist/chunk-Z2NREI4X.cjs.map +0 -1
- package/dist/chunk-ZVFL3NXX.cjs +0 -50
- package/dist/chunk-ZVFL3NXX.cjs.map +0 -1
- package/dist/components.cjs.map +0 -1
- package/dist/components.js.map +0 -1
- package/dist/generators.cjs.map +0 -1
- package/dist/generators.js.map +0 -1
|
@@ -0,0 +1,930 @@
|
|
|
1
|
+
import { isKeyword, schemaKeywords } from "./SchemaMapper-BaZQKrQB.js";
|
|
2
|
+
import { getSchemaFactory, getSchemas } from "./getSchemas-WoSBIxG8.js";
|
|
3
|
+
import { BaseGenerator } from "@kubb/core";
|
|
4
|
+
import transformers, { pascalCase } from "@kubb/core/transformers";
|
|
5
|
+
import { isDeepEqual, isNumber, uniqueWith } from "remeda";
|
|
6
|
+
import { getUniqueName } from "@kubb/core/utils";
|
|
7
|
+
import { isDiscriminator, isNullable, isReference } from "@kubb/oas";
|
|
8
|
+
|
|
9
|
+
//#region ../../node_modules/.pnpm/yocto-queue@1.2.1/node_modules/yocto-queue/index.js
|
|
10
|
+
var Node = class {
|
|
11
|
+
value;
|
|
12
|
+
next;
|
|
13
|
+
constructor(value) {
|
|
14
|
+
this.value = value;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var Queue = class {
|
|
18
|
+
#head;
|
|
19
|
+
#tail;
|
|
20
|
+
#size;
|
|
21
|
+
constructor() {
|
|
22
|
+
this.clear();
|
|
23
|
+
}
|
|
24
|
+
enqueue(value) {
|
|
25
|
+
const node = new Node(value);
|
|
26
|
+
if (this.#head) {
|
|
27
|
+
this.#tail.next = node;
|
|
28
|
+
this.#tail = node;
|
|
29
|
+
} else {
|
|
30
|
+
this.#head = node;
|
|
31
|
+
this.#tail = node;
|
|
32
|
+
}
|
|
33
|
+
this.#size++;
|
|
34
|
+
}
|
|
35
|
+
dequeue() {
|
|
36
|
+
const current = this.#head;
|
|
37
|
+
if (!current) return;
|
|
38
|
+
this.#head = this.#head.next;
|
|
39
|
+
this.#size--;
|
|
40
|
+
return current.value;
|
|
41
|
+
}
|
|
42
|
+
peek() {
|
|
43
|
+
if (!this.#head) return;
|
|
44
|
+
return this.#head.value;
|
|
45
|
+
}
|
|
46
|
+
clear() {
|
|
47
|
+
this.#head = void 0;
|
|
48
|
+
this.#tail = void 0;
|
|
49
|
+
this.#size = 0;
|
|
50
|
+
}
|
|
51
|
+
get size() {
|
|
52
|
+
return this.#size;
|
|
53
|
+
}
|
|
54
|
+
*[Symbol.iterator]() {
|
|
55
|
+
let current = this.#head;
|
|
56
|
+
while (current) {
|
|
57
|
+
yield current.value;
|
|
58
|
+
current = current.next;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
*drain() {
|
|
62
|
+
while (this.#head) yield this.dequeue();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region ../../node_modules/.pnpm/p-limit@7.0.0/node_modules/p-limit/index.js
|
|
68
|
+
function pLimit(concurrency) {
|
|
69
|
+
validateConcurrency(concurrency);
|
|
70
|
+
const queue = new Queue();
|
|
71
|
+
let activeCount = 0;
|
|
72
|
+
const resumeNext = () => {
|
|
73
|
+
if (activeCount < concurrency && queue.size > 0) {
|
|
74
|
+
activeCount++;
|
|
75
|
+
queue.dequeue()();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const next = () => {
|
|
79
|
+
activeCount--;
|
|
80
|
+
resumeNext();
|
|
81
|
+
};
|
|
82
|
+
const run = async (function_, resolve, arguments_) => {
|
|
83
|
+
const result = (async () => function_(...arguments_))();
|
|
84
|
+
resolve(result);
|
|
85
|
+
try {
|
|
86
|
+
await result;
|
|
87
|
+
} catch {}
|
|
88
|
+
next();
|
|
89
|
+
};
|
|
90
|
+
const enqueue = (function_, resolve, arguments_) => {
|
|
91
|
+
new Promise((internalResolve) => {
|
|
92
|
+
queue.enqueue(internalResolve);
|
|
93
|
+
}).then(run.bind(void 0, function_, resolve, arguments_));
|
|
94
|
+
if (activeCount < concurrency) resumeNext();
|
|
95
|
+
};
|
|
96
|
+
const generator = (function_, ...arguments_) => new Promise((resolve) => {
|
|
97
|
+
enqueue(function_, resolve, arguments_);
|
|
98
|
+
});
|
|
99
|
+
Object.defineProperties(generator, {
|
|
100
|
+
activeCount: { get: () => activeCount },
|
|
101
|
+
pendingCount: { get: () => queue.size },
|
|
102
|
+
clearQueue: { value() {
|
|
103
|
+
queue.clear();
|
|
104
|
+
} },
|
|
105
|
+
concurrency: {
|
|
106
|
+
get: () => concurrency,
|
|
107
|
+
set(newConcurrency) {
|
|
108
|
+
validateConcurrency(newConcurrency);
|
|
109
|
+
concurrency = newConcurrency;
|
|
110
|
+
queueMicrotask(() => {
|
|
111
|
+
while (activeCount < concurrency && queue.size > 0) resumeNext();
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
map: { async value(array, function_) {
|
|
116
|
+
const promises = array.map((value) => this(function_, value));
|
|
117
|
+
return Promise.all(promises);
|
|
118
|
+
} }
|
|
119
|
+
});
|
|
120
|
+
return generator;
|
|
121
|
+
}
|
|
122
|
+
function validateConcurrency(concurrency) {
|
|
123
|
+
if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) throw new TypeError("Expected `concurrency` to be a number from 1 and up");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/SchemaGenerator.ts
|
|
128
|
+
var SchemaGenerator = class SchemaGenerator extends BaseGenerator {
|
|
129
|
+
refs = {};
|
|
130
|
+
#usedAliasNames = {};
|
|
131
|
+
/**
|
|
132
|
+
* Creates a type node from a given schema.
|
|
133
|
+
* Delegates to getBaseTypeFromSchema internally and
|
|
134
|
+
* optionally adds a union with null.
|
|
135
|
+
*/
|
|
136
|
+
parse(props) {
|
|
137
|
+
const options = this.#getOptions(props);
|
|
138
|
+
const defaultSchemas = this.#parseSchemaObject(props);
|
|
139
|
+
const schemas = options.transformers?.schema?.(props, defaultSchemas) || defaultSchemas || [];
|
|
140
|
+
return uniqueWith(schemas, isDeepEqual);
|
|
141
|
+
}
|
|
142
|
+
deepSearch(tree, keyword) {
|
|
143
|
+
return SchemaGenerator.deepSearch(tree, keyword);
|
|
144
|
+
}
|
|
145
|
+
find(tree, keyword) {
|
|
146
|
+
return SchemaGenerator.find(tree, keyword);
|
|
147
|
+
}
|
|
148
|
+
static deepSearch(tree, keyword) {
|
|
149
|
+
const foundItems = [];
|
|
150
|
+
tree?.forEach((schema) => {
|
|
151
|
+
if (schema.keyword === keyword) foundItems.push(schema);
|
|
152
|
+
if (isKeyword(schema, schemaKeywords.object)) {
|
|
153
|
+
Object.values(schema.args?.properties || {}).forEach((entrySchema) => {
|
|
154
|
+
foundItems.push(...SchemaGenerator.deepSearch(entrySchema, keyword));
|
|
155
|
+
});
|
|
156
|
+
Object.values(schema.args?.additionalProperties || {}).forEach((entrySchema) => {
|
|
157
|
+
foundItems.push(...SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (isKeyword(schema, schemaKeywords.array)) schema.args.items.forEach((entrySchema) => {
|
|
161
|
+
foundItems.push(...SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
162
|
+
});
|
|
163
|
+
if (isKeyword(schema, schemaKeywords.and)) schema.args.forEach((entrySchema) => {
|
|
164
|
+
foundItems.push(...SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
165
|
+
});
|
|
166
|
+
if (isKeyword(schema, schemaKeywords.tuple)) schema.args.items.forEach((entrySchema) => {
|
|
167
|
+
foundItems.push(...SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
168
|
+
});
|
|
169
|
+
if (isKeyword(schema, schemaKeywords.union)) schema.args.forEach((entrySchema) => {
|
|
170
|
+
foundItems.push(...SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
return foundItems;
|
|
174
|
+
}
|
|
175
|
+
static findInObject(tree, keyword) {
|
|
176
|
+
let foundItem;
|
|
177
|
+
tree?.forEach((schema) => {
|
|
178
|
+
if (!foundItem && schema.keyword === keyword) foundItem = schema;
|
|
179
|
+
if (isKeyword(schema, schemaKeywords.object)) {
|
|
180
|
+
Object.values(schema.args?.properties || {}).forEach((entrySchema) => {
|
|
181
|
+
if (!foundItem) foundItem = SchemaGenerator.find(entrySchema, keyword);
|
|
182
|
+
});
|
|
183
|
+
Object.values(schema.args?.additionalProperties || {}).forEach((entrySchema) => {
|
|
184
|
+
if (!foundItem) foundItem = SchemaGenerator.find([entrySchema], keyword);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
return foundItem;
|
|
189
|
+
}
|
|
190
|
+
static find(tree, keyword) {
|
|
191
|
+
let foundItem;
|
|
192
|
+
tree?.forEach((schema) => {
|
|
193
|
+
if (!foundItem && schema.keyword === keyword) foundItem = schema;
|
|
194
|
+
if (isKeyword(schema, schemaKeywords.array)) schema.args.items.forEach((entrySchema) => {
|
|
195
|
+
if (!foundItem) foundItem = SchemaGenerator.find([entrySchema], keyword);
|
|
196
|
+
});
|
|
197
|
+
if (isKeyword(schema, schemaKeywords.and)) schema.args.forEach((entrySchema) => {
|
|
198
|
+
if (!foundItem) foundItem = SchemaGenerator.find([entrySchema], keyword);
|
|
199
|
+
});
|
|
200
|
+
if (isKeyword(schema, schemaKeywords.tuple)) schema.args.items.forEach((entrySchema) => {
|
|
201
|
+
if (!foundItem) foundItem = SchemaGenerator.find([entrySchema], keyword);
|
|
202
|
+
});
|
|
203
|
+
if (isKeyword(schema, schemaKeywords.union)) schema.args.forEach((entrySchema) => {
|
|
204
|
+
if (!foundItem) foundItem = SchemaGenerator.find([entrySchema], keyword);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
return foundItem;
|
|
208
|
+
}
|
|
209
|
+
static combineObjects(tree) {
|
|
210
|
+
if (!tree) return [];
|
|
211
|
+
return tree.map((schema) => {
|
|
212
|
+
if (!isKeyword(schema, schemaKeywords.and)) return schema;
|
|
213
|
+
let mergedProperties = null;
|
|
214
|
+
let mergedAdditionalProps = [];
|
|
215
|
+
const newArgs = [];
|
|
216
|
+
for (const subSchema of schema.args) if (isKeyword(subSchema, schemaKeywords.object)) {
|
|
217
|
+
const { properties = {}, additionalProperties = [] } = subSchema.args ?? {};
|
|
218
|
+
if (!mergedProperties) mergedProperties = {};
|
|
219
|
+
for (const [key, value] of Object.entries(properties)) mergedProperties[key] = value;
|
|
220
|
+
if (additionalProperties.length > 0) mergedAdditionalProps = additionalProperties;
|
|
221
|
+
} else newArgs.push(subSchema);
|
|
222
|
+
if (mergedProperties) newArgs.push({
|
|
223
|
+
keyword: schemaKeywords.object,
|
|
224
|
+
args: {
|
|
225
|
+
properties: mergedProperties,
|
|
226
|
+
additionalProperties: mergedAdditionalProps
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
return {
|
|
230
|
+
keyword: schemaKeywords.and,
|
|
231
|
+
args: newArgs
|
|
232
|
+
};
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
#getUsedEnumNames(props) {
|
|
236
|
+
const options = this.#getOptions(props);
|
|
237
|
+
return options.usedEnumNames || {};
|
|
238
|
+
}
|
|
239
|
+
#getOptions({ name }) {
|
|
240
|
+
const { override = [] } = this.context;
|
|
241
|
+
return {
|
|
242
|
+
...this.options,
|
|
243
|
+
...override.find(({ pattern, type }) => {
|
|
244
|
+
if (name && type === "schemaName") return !!name.match(pattern);
|
|
245
|
+
return false;
|
|
246
|
+
})?.options || {}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
#getUnknownType(props) {
|
|
250
|
+
const options = this.#getOptions(props);
|
|
251
|
+
if (options.unknownType === "any") return schemaKeywords.any;
|
|
252
|
+
if (options.unknownType === "void") return schemaKeywords.void;
|
|
253
|
+
return schemaKeywords.unknown;
|
|
254
|
+
}
|
|
255
|
+
#getEmptyType(props) {
|
|
256
|
+
const options = this.#getOptions(props);
|
|
257
|
+
if (options.emptySchemaType === "any") return schemaKeywords.any;
|
|
258
|
+
if (options.emptySchemaType === "void") return schemaKeywords.void;
|
|
259
|
+
return schemaKeywords.unknown;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Recursively creates a type literal with the given props.
|
|
263
|
+
*/
|
|
264
|
+
#parseProperties({ schemaObject, name }) {
|
|
265
|
+
const properties = schemaObject?.properties || {};
|
|
266
|
+
const additionalProperties = schemaObject?.additionalProperties;
|
|
267
|
+
const required = schemaObject?.required;
|
|
268
|
+
const propertiesSchemas = Object.keys(properties).map((propertyName) => {
|
|
269
|
+
const validationFunctions = [];
|
|
270
|
+
const propertySchema = properties[propertyName];
|
|
271
|
+
const isRequired = Array.isArray(required) ? required?.includes(propertyName) : !!required;
|
|
272
|
+
const nullable = propertySchema.nullable ?? propertySchema["x-nullable"] ?? false;
|
|
273
|
+
validationFunctions.push(...this.parse({
|
|
274
|
+
schemaObject: propertySchema,
|
|
275
|
+
name: propertyName,
|
|
276
|
+
parentName: name
|
|
277
|
+
}));
|
|
278
|
+
validationFunctions.push({
|
|
279
|
+
keyword: schemaKeywords.name,
|
|
280
|
+
args: propertyName
|
|
281
|
+
});
|
|
282
|
+
if (!isRequired && nullable) validationFunctions.push({ keyword: schemaKeywords.nullish });
|
|
283
|
+
else if (!isRequired) validationFunctions.push({ keyword: schemaKeywords.optional });
|
|
284
|
+
return { [propertyName]: validationFunctions };
|
|
285
|
+
}).reduce((acc, curr) => ({
|
|
286
|
+
...acc,
|
|
287
|
+
...curr
|
|
288
|
+
}), {});
|
|
289
|
+
let additionalPropertiesSchemas = [];
|
|
290
|
+
if (additionalProperties) additionalPropertiesSchemas = additionalProperties === true || !Object.keys(additionalProperties).length ? [{ keyword: this.#getUnknownType({
|
|
291
|
+
schemaObject,
|
|
292
|
+
name
|
|
293
|
+
}) }] : this.parse({
|
|
294
|
+
schemaObject: additionalProperties,
|
|
295
|
+
parentName: name
|
|
296
|
+
});
|
|
297
|
+
return [{
|
|
298
|
+
keyword: schemaKeywords.object,
|
|
299
|
+
args: {
|
|
300
|
+
properties: propertiesSchemas,
|
|
301
|
+
additionalProperties: additionalPropertiesSchemas
|
|
302
|
+
}
|
|
303
|
+
}];
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Create a type alias for the schema referenced by the given ReferenceObject
|
|
307
|
+
*/
|
|
308
|
+
#getRefAlias(schemaObject, name) {
|
|
309
|
+
const { $ref } = schemaObject;
|
|
310
|
+
const ref = this.refs[$ref];
|
|
311
|
+
if (ref) {
|
|
312
|
+
const dereferencedSchema = this.context.oas.dereferenceWithRef(schemaObject);
|
|
313
|
+
if (dereferencedSchema && isDiscriminator(dereferencedSchema)) {
|
|
314
|
+
const [key] = Object.entries(dereferencedSchema.discriminator.mapping || {}).find(([_key, value]) => value.replace(/.+\//, "") === name) || [];
|
|
315
|
+
if (key) return [{
|
|
316
|
+
keyword: schemaKeywords.and,
|
|
317
|
+
args: [{
|
|
318
|
+
keyword: schemaKeywords.ref,
|
|
319
|
+
args: {
|
|
320
|
+
name: ref.propertyName,
|
|
321
|
+
$ref,
|
|
322
|
+
path: ref.path,
|
|
323
|
+
isImportable: !!this.context.oas.get($ref)
|
|
324
|
+
}
|
|
325
|
+
}, {
|
|
326
|
+
keyword: schemaKeywords.object,
|
|
327
|
+
args: { properties: { [dereferencedSchema.discriminator.propertyName]: [{
|
|
328
|
+
keyword: schemaKeywords.const,
|
|
329
|
+
args: {
|
|
330
|
+
name: key,
|
|
331
|
+
format: "string",
|
|
332
|
+
value: key
|
|
333
|
+
}
|
|
334
|
+
}] } }
|
|
335
|
+
}]
|
|
336
|
+
}];
|
|
337
|
+
}
|
|
338
|
+
return [{
|
|
339
|
+
keyword: schemaKeywords.ref,
|
|
340
|
+
args: {
|
|
341
|
+
name: ref.propertyName,
|
|
342
|
+
$ref,
|
|
343
|
+
path: ref.path,
|
|
344
|
+
isImportable: !!this.context.oas.get($ref)
|
|
345
|
+
}
|
|
346
|
+
}];
|
|
347
|
+
}
|
|
348
|
+
const originalName = getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames);
|
|
349
|
+
const propertyName = this.context.pluginManager.resolveName({
|
|
350
|
+
name: originalName,
|
|
351
|
+
pluginKey: this.context.plugin.key,
|
|
352
|
+
type: "function"
|
|
353
|
+
});
|
|
354
|
+
const fileName = this.context.pluginManager.resolveName({
|
|
355
|
+
name: originalName,
|
|
356
|
+
pluginKey: this.context.plugin.key,
|
|
357
|
+
type: "file"
|
|
358
|
+
});
|
|
359
|
+
const file = this.context.pluginManager.getFile({
|
|
360
|
+
name: fileName,
|
|
361
|
+
pluginKey: this.context.plugin.key,
|
|
362
|
+
extname: ".ts"
|
|
363
|
+
});
|
|
364
|
+
this.refs[$ref] = {
|
|
365
|
+
propertyName,
|
|
366
|
+
originalName,
|
|
367
|
+
path: file.path
|
|
368
|
+
};
|
|
369
|
+
return this.#getRefAlias(schemaObject, name);
|
|
370
|
+
}
|
|
371
|
+
#getParsedSchemaObject(schema) {
|
|
372
|
+
return getSchemaFactory(this.context.oas)(schema);
|
|
373
|
+
}
|
|
374
|
+
#addDiscriminatorToSchema({ schema, schemaObject, discriminator }) {
|
|
375
|
+
if (!isKeyword(schema, schemaKeywords.union)) return schema;
|
|
376
|
+
const objectPropertySchema = SchemaGenerator.find(this.parse({ schemaObject }), schemaKeywords.object);
|
|
377
|
+
return {
|
|
378
|
+
...schema,
|
|
379
|
+
args: Object.entries(discriminator.mapping || {}).map(([key, value]) => {
|
|
380
|
+
const arg = schema.args.find((item) => isKeyword(item, schemaKeywords.ref) && item.args.$ref === value);
|
|
381
|
+
return {
|
|
382
|
+
keyword: schemaKeywords.and,
|
|
383
|
+
args: [arg, {
|
|
384
|
+
keyword: schemaKeywords.object,
|
|
385
|
+
args: { properties: {
|
|
386
|
+
...objectPropertySchema?.args?.properties || {},
|
|
387
|
+
[discriminator.propertyName]: [{
|
|
388
|
+
keyword: schemaKeywords.const,
|
|
389
|
+
args: {
|
|
390
|
+
name: key,
|
|
391
|
+
format: "string",
|
|
392
|
+
value: key
|
|
393
|
+
}
|
|
394
|
+
}, ...objectPropertySchema?.args?.properties[discriminator.propertyName] || []].filter((item) => !isKeyword(item, schemaKeywords.enum))
|
|
395
|
+
} }
|
|
396
|
+
}]
|
|
397
|
+
};
|
|
398
|
+
})
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* This is the very core of the OpenAPI to TS conversion - it takes a
|
|
403
|
+
* schema and returns the appropriate type.
|
|
404
|
+
*/
|
|
405
|
+
#parseSchemaObject({ schemaObject: _schemaObject, name, parentName }) {
|
|
406
|
+
const { schemaObject, version } = this.#getParsedSchemaObject(_schemaObject);
|
|
407
|
+
const options = this.#getOptions({
|
|
408
|
+
schemaObject,
|
|
409
|
+
name
|
|
410
|
+
});
|
|
411
|
+
const emptyType = this.#getEmptyType({
|
|
412
|
+
schemaObject,
|
|
413
|
+
name
|
|
414
|
+
});
|
|
415
|
+
if (!schemaObject) return [{ keyword: emptyType }];
|
|
416
|
+
const baseItems = [{
|
|
417
|
+
keyword: schemaKeywords.schema,
|
|
418
|
+
args: {
|
|
419
|
+
type: schemaObject.type,
|
|
420
|
+
format: schemaObject.format
|
|
421
|
+
}
|
|
422
|
+
}];
|
|
423
|
+
const min = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0;
|
|
424
|
+
const max = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0;
|
|
425
|
+
const nullable = isNullable(schemaObject);
|
|
426
|
+
const defaultNullAndNullable = schemaObject.default === null && nullable;
|
|
427
|
+
if (schemaObject.default !== void 0 && !defaultNullAndNullable && !Array.isArray(schemaObject.default)) if (typeof schemaObject.default === "string") baseItems.push({
|
|
428
|
+
keyword: schemaKeywords.default,
|
|
429
|
+
args: transformers.stringify(schemaObject.default)
|
|
430
|
+
});
|
|
431
|
+
else if (typeof schemaObject.default === "boolean") baseItems.push({
|
|
432
|
+
keyword: schemaKeywords.default,
|
|
433
|
+
args: schemaObject.default ?? false
|
|
434
|
+
});
|
|
435
|
+
else baseItems.push({
|
|
436
|
+
keyword: schemaKeywords.default,
|
|
437
|
+
args: schemaObject.default
|
|
438
|
+
});
|
|
439
|
+
if (schemaObject.deprecated) baseItems.push({ keyword: schemaKeywords.deprecated });
|
|
440
|
+
if (schemaObject.description) baseItems.push({
|
|
441
|
+
keyword: schemaKeywords.describe,
|
|
442
|
+
args: schemaObject.description
|
|
443
|
+
});
|
|
444
|
+
if (max !== void 0) baseItems.unshift({
|
|
445
|
+
keyword: schemaKeywords.max,
|
|
446
|
+
args: max
|
|
447
|
+
});
|
|
448
|
+
if (min !== void 0) baseItems.unshift({
|
|
449
|
+
keyword: schemaKeywords.min,
|
|
450
|
+
args: min
|
|
451
|
+
});
|
|
452
|
+
if (nullable) baseItems.push({ keyword: schemaKeywords.nullable });
|
|
453
|
+
if (schemaObject.type && Array.isArray(schemaObject.type)) {
|
|
454
|
+
const items = schemaObject.type.filter((value) => value !== "null");
|
|
455
|
+
const hasNull = schemaObject.type.includes("null");
|
|
456
|
+
if (hasNull && !nullable) baseItems.push({ keyword: schemaKeywords.nullable });
|
|
457
|
+
if (items.length > 1) {
|
|
458
|
+
const parsedItems = [{
|
|
459
|
+
keyword: schemaKeywords.union,
|
|
460
|
+
args: items.map((item) => this.parse({
|
|
461
|
+
schemaObject: {
|
|
462
|
+
...schemaObject,
|
|
463
|
+
type: item
|
|
464
|
+
},
|
|
465
|
+
name,
|
|
466
|
+
parentName
|
|
467
|
+
})[0]).filter(Boolean).filter((item) => !isKeyword(item, schemaKeywords.unknown)).map((item) => isKeyword(item, schemaKeywords.object) ? {
|
|
468
|
+
...item,
|
|
469
|
+
args: {
|
|
470
|
+
...item.args,
|
|
471
|
+
strict: true
|
|
472
|
+
}
|
|
473
|
+
} : item)
|
|
474
|
+
}];
|
|
475
|
+
return [...parsedItems, ...baseItems].filter(Boolean);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (schemaObject.readOnly) baseItems.push({ keyword: schemaKeywords.readOnly });
|
|
479
|
+
if (schemaObject.writeOnly) baseItems.push({ keyword: schemaKeywords.writeOnly });
|
|
480
|
+
if (isReference(schemaObject)) return [
|
|
481
|
+
...this.#getRefAlias(schemaObject, name),
|
|
482
|
+
schemaObject.description && {
|
|
483
|
+
keyword: schemaKeywords.describe,
|
|
484
|
+
args: schemaObject.description
|
|
485
|
+
},
|
|
486
|
+
schemaObject.pattern && schemaObject.type === "string" && {
|
|
487
|
+
keyword: schemaKeywords.matches,
|
|
488
|
+
args: schemaObject.pattern
|
|
489
|
+
},
|
|
490
|
+
nullable && { keyword: schemaKeywords.nullable },
|
|
491
|
+
schemaObject.readOnly && { keyword: schemaKeywords.readOnly },
|
|
492
|
+
schemaObject.writeOnly && { keyword: schemaKeywords.writeOnly },
|
|
493
|
+
{
|
|
494
|
+
keyword: schemaKeywords.schema,
|
|
495
|
+
args: {
|
|
496
|
+
type: schemaObject.type,
|
|
497
|
+
format: schemaObject.format
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
].filter(Boolean);
|
|
501
|
+
if (schemaObject.oneOf || schemaObject.anyOf) {
|
|
502
|
+
const schemaWithoutOneOf = {
|
|
503
|
+
...schemaObject,
|
|
504
|
+
oneOf: void 0,
|
|
505
|
+
anyOf: void 0
|
|
506
|
+
};
|
|
507
|
+
const discriminator = this.context.oas.getDiscriminator(schemaObject);
|
|
508
|
+
const union = {
|
|
509
|
+
keyword: schemaKeywords.union,
|
|
510
|
+
args: (schemaObject.oneOf || schemaObject.anyOf).map((item) => {
|
|
511
|
+
return item && this.parse({
|
|
512
|
+
schemaObject: item,
|
|
513
|
+
name,
|
|
514
|
+
parentName
|
|
515
|
+
})[0];
|
|
516
|
+
}).filter(Boolean).filter((item) => !isKeyword(item, schemaKeywords.unknown))
|
|
517
|
+
};
|
|
518
|
+
if (discriminator) {
|
|
519
|
+
if (this.context) return [this.#addDiscriminatorToSchema({
|
|
520
|
+
schemaObject: schemaWithoutOneOf,
|
|
521
|
+
schema: union,
|
|
522
|
+
discriminator
|
|
523
|
+
}), ...baseItems];
|
|
524
|
+
}
|
|
525
|
+
if (schemaWithoutOneOf.properties) {
|
|
526
|
+
const propertySchemas = this.parse({
|
|
527
|
+
schemaObject: schemaWithoutOneOf,
|
|
528
|
+
name,
|
|
529
|
+
parentName
|
|
530
|
+
});
|
|
531
|
+
union.args = [...union.args.map((arg) => {
|
|
532
|
+
return {
|
|
533
|
+
keyword: schemaKeywords.and,
|
|
534
|
+
args: [arg, ...propertySchemas]
|
|
535
|
+
};
|
|
536
|
+
})];
|
|
537
|
+
return [union, ...baseItems];
|
|
538
|
+
}
|
|
539
|
+
return [union, ...baseItems];
|
|
540
|
+
}
|
|
541
|
+
if (schemaObject.allOf) {
|
|
542
|
+
const schemaWithoutAllOf = {
|
|
543
|
+
...schemaObject,
|
|
544
|
+
allOf: void 0
|
|
545
|
+
};
|
|
546
|
+
const and = {
|
|
547
|
+
keyword: schemaKeywords.and,
|
|
548
|
+
args: schemaObject.allOf.map((item) => {
|
|
549
|
+
return item && this.parse({
|
|
550
|
+
schemaObject: item,
|
|
551
|
+
name,
|
|
552
|
+
parentName
|
|
553
|
+
})[0];
|
|
554
|
+
}).filter(Boolean).filter((item) => !isKeyword(item, schemaKeywords.unknown))
|
|
555
|
+
};
|
|
556
|
+
if (schemaWithoutAllOf.required?.length) {
|
|
557
|
+
const allOfItems = schemaObject.allOf;
|
|
558
|
+
const resolvedSchemas = [];
|
|
559
|
+
for (const item of allOfItems) {
|
|
560
|
+
const resolved = isReference(item) ? this.context.oas.get(item.$ref) : item;
|
|
561
|
+
if (resolved) resolvedSchemas.push(resolved);
|
|
562
|
+
}
|
|
563
|
+
const existingKeys = schemaWithoutAllOf.properties ? new Set(Object.keys(schemaWithoutAllOf.properties)) : null;
|
|
564
|
+
const parsedItems = [];
|
|
565
|
+
for (const key of schemaWithoutAllOf.required) {
|
|
566
|
+
if (existingKeys?.has(key)) continue;
|
|
567
|
+
for (const schema of resolvedSchemas) if (schema.properties?.[key]) {
|
|
568
|
+
parsedItems.push({
|
|
569
|
+
properties: { [key]: schema.properties[key] },
|
|
570
|
+
required: [key]
|
|
571
|
+
});
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
for (const item of parsedItems) {
|
|
576
|
+
const parsed = this.parse({
|
|
577
|
+
schemaObject: item,
|
|
578
|
+
name,
|
|
579
|
+
parentName
|
|
580
|
+
});
|
|
581
|
+
if (Array.isArray(parsed)) and.args = and.args ? and.args.concat(parsed) : parsed;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
if (schemaWithoutAllOf.properties) and.args = [...and.args || [], ...this.parse({
|
|
585
|
+
schemaObject: schemaWithoutAllOf,
|
|
586
|
+
name,
|
|
587
|
+
parentName
|
|
588
|
+
})];
|
|
589
|
+
return SchemaGenerator.combineObjects([and, ...baseItems]);
|
|
590
|
+
}
|
|
591
|
+
if (schemaObject.enum) {
|
|
592
|
+
if (options.enumSuffix === "") this.context.pluginManager.logger.emit("info", "EnumSuffix set to an empty string does not work");
|
|
593
|
+
const enumName = getUniqueName(pascalCase([
|
|
594
|
+
parentName,
|
|
595
|
+
name,
|
|
596
|
+
options.enumSuffix
|
|
597
|
+
].join(" ")), this.#getUsedEnumNames({
|
|
598
|
+
schemaObject,
|
|
599
|
+
name
|
|
600
|
+
}));
|
|
601
|
+
const typeName = this.context.pluginManager.resolveName({
|
|
602
|
+
name: enumName,
|
|
603
|
+
pluginKey: this.context.plugin.key,
|
|
604
|
+
type: "type"
|
|
605
|
+
});
|
|
606
|
+
const nullableEnum = schemaObject.enum.includes(null);
|
|
607
|
+
if (nullableEnum) baseItems.push({ keyword: schemaKeywords.nullable });
|
|
608
|
+
const filteredValues = schemaObject.enum.filter((value) => value !== null);
|
|
609
|
+
const extensionEnums = ["x-enumNames", "x-enum-varnames"].filter((extensionKey) => extensionKey in schemaObject).map((extensionKey) => {
|
|
610
|
+
return [{
|
|
611
|
+
keyword: schemaKeywords.enum,
|
|
612
|
+
args: {
|
|
613
|
+
name,
|
|
614
|
+
typeName,
|
|
615
|
+
asConst: false,
|
|
616
|
+
items: [...new Set(schemaObject[extensionKey])].map((name$1, index) => ({
|
|
617
|
+
name: transformers.stringify(name$1),
|
|
618
|
+
value: schemaObject.enum?.[index],
|
|
619
|
+
format: isNumber(schemaObject.enum?.[index]) ? "number" : "string"
|
|
620
|
+
}))
|
|
621
|
+
}
|
|
622
|
+
}, ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)];
|
|
623
|
+
});
|
|
624
|
+
if (schemaObject.type === "number" || schemaObject.type === "integer") {
|
|
625
|
+
const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
|
|
626
|
+
return [{
|
|
627
|
+
keyword: schemaKeywords.enum,
|
|
628
|
+
args: {
|
|
629
|
+
name: enumName,
|
|
630
|
+
typeName,
|
|
631
|
+
asConst: true,
|
|
632
|
+
items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name$1, value }) => ({
|
|
633
|
+
name: name$1,
|
|
634
|
+
value,
|
|
635
|
+
format: "number"
|
|
636
|
+
})) : [...new Set(filteredValues)].map((value) => {
|
|
637
|
+
return {
|
|
638
|
+
name: value,
|
|
639
|
+
value,
|
|
640
|
+
format: "number"
|
|
641
|
+
};
|
|
642
|
+
})
|
|
643
|
+
}
|
|
644
|
+
}, ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)];
|
|
645
|
+
}
|
|
646
|
+
if (schemaObject.type === "boolean") {
|
|
647
|
+
const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
|
|
648
|
+
return [{
|
|
649
|
+
keyword: schemaKeywords.enum,
|
|
650
|
+
args: {
|
|
651
|
+
name: enumName,
|
|
652
|
+
typeName,
|
|
653
|
+
asConst: true,
|
|
654
|
+
items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name$1, value }) => ({
|
|
655
|
+
name: name$1,
|
|
656
|
+
value,
|
|
657
|
+
format: "boolean"
|
|
658
|
+
})) : [...new Set(filteredValues)].map((value) => {
|
|
659
|
+
return {
|
|
660
|
+
name: value,
|
|
661
|
+
value,
|
|
662
|
+
format: "boolean"
|
|
663
|
+
};
|
|
664
|
+
})
|
|
665
|
+
}
|
|
666
|
+
}, ...baseItems.filter((item) => item.keyword !== schemaKeywords.matches)];
|
|
667
|
+
}
|
|
668
|
+
if (extensionEnums.length > 0 && extensionEnums[0]) return extensionEnums[0];
|
|
669
|
+
return [{
|
|
670
|
+
keyword: schemaKeywords.enum,
|
|
671
|
+
args: {
|
|
672
|
+
name: enumName,
|
|
673
|
+
typeName,
|
|
674
|
+
asConst: false,
|
|
675
|
+
items: [...new Set(filteredValues)].map((value) => ({
|
|
676
|
+
name: transformers.stringify(value),
|
|
677
|
+
value,
|
|
678
|
+
format: isNumber(value) ? "number" : "string"
|
|
679
|
+
}))
|
|
680
|
+
}
|
|
681
|
+
}, ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)];
|
|
682
|
+
}
|
|
683
|
+
if ("prefixItems" in schemaObject) {
|
|
684
|
+
const prefixItems = schemaObject.prefixItems;
|
|
685
|
+
const items = "items" in schemaObject ? schemaObject.items : [];
|
|
686
|
+
const min$1 = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0;
|
|
687
|
+
const max$1 = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0;
|
|
688
|
+
return [{
|
|
689
|
+
keyword: schemaKeywords.tuple,
|
|
690
|
+
args: {
|
|
691
|
+
min: min$1,
|
|
692
|
+
max: max$1,
|
|
693
|
+
items: prefixItems.map((item) => {
|
|
694
|
+
return this.parse({
|
|
695
|
+
schemaObject: item,
|
|
696
|
+
name,
|
|
697
|
+
parentName
|
|
698
|
+
})[0];
|
|
699
|
+
}).filter(Boolean),
|
|
700
|
+
rest: this.parse({
|
|
701
|
+
schemaObject: items,
|
|
702
|
+
name,
|
|
703
|
+
parentName
|
|
704
|
+
})[0]
|
|
705
|
+
}
|
|
706
|
+
}, ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)];
|
|
707
|
+
}
|
|
708
|
+
if (version === "3.1" && "const" in schemaObject) {
|
|
709
|
+
if (schemaObject["const"] === null) return [{ keyword: schemaKeywords.null }];
|
|
710
|
+
if (schemaObject["const"] === void 0) return [{ keyword: schemaKeywords.undefined }];
|
|
711
|
+
let format = typeof schemaObject["const"];
|
|
712
|
+
if (format !== "number" && format !== "boolean") format = "string";
|
|
713
|
+
return [{
|
|
714
|
+
keyword: schemaKeywords.const,
|
|
715
|
+
args: {
|
|
716
|
+
name: schemaObject["const"],
|
|
717
|
+
format,
|
|
718
|
+
value: schemaObject["const"]
|
|
719
|
+
}
|
|
720
|
+
}, ...baseItems];
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* > Structural validation alone may be insufficient to allow an application to correctly utilize certain values. The "format"
|
|
724
|
+
* > annotation keyword is defined to allow schema authors to convey semantic information for a fixed subset of values which are
|
|
725
|
+
* > accurately described by authoritative resources, be they RFCs or other external specifications.
|
|
726
|
+
*
|
|
727
|
+
* In other words: format is more specific than type alone, hence it should override the type value, if possible.
|
|
728
|
+
*
|
|
729
|
+
* see also https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.7
|
|
730
|
+
*/
|
|
731
|
+
if (schemaObject.format) {
|
|
732
|
+
if (schemaObject.type === "integer" && (schemaObject.format === "int32" || schemaObject.format === "int64")) {
|
|
733
|
+
baseItems.unshift({ keyword: schemaKeywords.integer });
|
|
734
|
+
return baseItems;
|
|
735
|
+
}
|
|
736
|
+
if (schemaObject.type === "number" && (schemaObject.format === "float" || schemaObject.format === "double")) {
|
|
737
|
+
baseItems.unshift({ keyword: schemaKeywords.number });
|
|
738
|
+
return baseItems;
|
|
739
|
+
}
|
|
740
|
+
switch (schemaObject.format) {
|
|
741
|
+
case "binary":
|
|
742
|
+
baseItems.push({ keyword: schemaKeywords.blob });
|
|
743
|
+
return baseItems;
|
|
744
|
+
case "date-time":
|
|
745
|
+
if (options.dateType) {
|
|
746
|
+
if (options.dateType === "date") {
|
|
747
|
+
baseItems.unshift({
|
|
748
|
+
keyword: schemaKeywords.date,
|
|
749
|
+
args: { type: "date" }
|
|
750
|
+
});
|
|
751
|
+
return baseItems;
|
|
752
|
+
}
|
|
753
|
+
if (options.dateType === "stringOffset") {
|
|
754
|
+
baseItems.unshift({
|
|
755
|
+
keyword: schemaKeywords.datetime,
|
|
756
|
+
args: { offset: true }
|
|
757
|
+
});
|
|
758
|
+
return baseItems;
|
|
759
|
+
}
|
|
760
|
+
if (options.dateType === "stringLocal") {
|
|
761
|
+
baseItems.unshift({
|
|
762
|
+
keyword: schemaKeywords.datetime,
|
|
763
|
+
args: { local: true }
|
|
764
|
+
});
|
|
765
|
+
return baseItems;
|
|
766
|
+
}
|
|
767
|
+
baseItems.unshift({
|
|
768
|
+
keyword: schemaKeywords.datetime,
|
|
769
|
+
args: { offset: false }
|
|
770
|
+
});
|
|
771
|
+
return baseItems;
|
|
772
|
+
}
|
|
773
|
+
break;
|
|
774
|
+
case "date":
|
|
775
|
+
if (options.dateType) {
|
|
776
|
+
if (options.dateType === "date") {
|
|
777
|
+
baseItems.unshift({
|
|
778
|
+
keyword: schemaKeywords.date,
|
|
779
|
+
args: { type: "date" }
|
|
780
|
+
});
|
|
781
|
+
return baseItems;
|
|
782
|
+
}
|
|
783
|
+
baseItems.unshift({
|
|
784
|
+
keyword: schemaKeywords.date,
|
|
785
|
+
args: { type: "string" }
|
|
786
|
+
});
|
|
787
|
+
return baseItems;
|
|
788
|
+
}
|
|
789
|
+
break;
|
|
790
|
+
case "time":
|
|
791
|
+
if (options.dateType) {
|
|
792
|
+
if (options.dateType === "date") {
|
|
793
|
+
baseItems.unshift({
|
|
794
|
+
keyword: schemaKeywords.time,
|
|
795
|
+
args: { type: "date" }
|
|
796
|
+
});
|
|
797
|
+
return baseItems;
|
|
798
|
+
}
|
|
799
|
+
baseItems.unshift({
|
|
800
|
+
keyword: schemaKeywords.time,
|
|
801
|
+
args: { type: "string" }
|
|
802
|
+
});
|
|
803
|
+
return baseItems;
|
|
804
|
+
}
|
|
805
|
+
break;
|
|
806
|
+
case "uuid":
|
|
807
|
+
baseItems.unshift({ keyword: schemaKeywords.uuid });
|
|
808
|
+
return baseItems;
|
|
809
|
+
case "email":
|
|
810
|
+
case "idn-email":
|
|
811
|
+
baseItems.unshift({ keyword: schemaKeywords.email });
|
|
812
|
+
return baseItems;
|
|
813
|
+
case "uri":
|
|
814
|
+
case "ipv4":
|
|
815
|
+
case "ipv6":
|
|
816
|
+
case "uri-reference":
|
|
817
|
+
case "hostname":
|
|
818
|
+
case "idn-hostname":
|
|
819
|
+
baseItems.unshift({ keyword: schemaKeywords.url });
|
|
820
|
+
return baseItems;
|
|
821
|
+
default: break;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
if (schemaObject.pattern && schemaObject.type === "string") {
|
|
825
|
+
baseItems.unshift({
|
|
826
|
+
keyword: schemaKeywords.matches,
|
|
827
|
+
args: schemaObject.pattern
|
|
828
|
+
});
|
|
829
|
+
return baseItems;
|
|
830
|
+
}
|
|
831
|
+
if ("items" in schemaObject || schemaObject.type === "array") {
|
|
832
|
+
const min$1 = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0;
|
|
833
|
+
const max$1 = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0;
|
|
834
|
+
const items = this.parse({
|
|
835
|
+
schemaObject: "items" in schemaObject ? schemaObject.items : [],
|
|
836
|
+
name,
|
|
837
|
+
parentName
|
|
838
|
+
});
|
|
839
|
+
const unique = !!schemaObject.uniqueItems;
|
|
840
|
+
return [{
|
|
841
|
+
keyword: schemaKeywords.array,
|
|
842
|
+
args: {
|
|
843
|
+
items,
|
|
844
|
+
min: min$1,
|
|
845
|
+
max: max$1,
|
|
846
|
+
unique
|
|
847
|
+
}
|
|
848
|
+
}, ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)];
|
|
849
|
+
}
|
|
850
|
+
if (schemaObject.properties || schemaObject.additionalProperties) {
|
|
851
|
+
if (isDiscriminator(schemaObject)) {
|
|
852
|
+
const schemaObjectOverriden = Object.keys(schemaObject.properties || {}).reduce((acc, propertyName) => {
|
|
853
|
+
if (acc.properties?.[propertyName] && propertyName === schemaObject.discriminator.propertyName) return {
|
|
854
|
+
...acc,
|
|
855
|
+
properties: {
|
|
856
|
+
...acc.properties,
|
|
857
|
+
[propertyName]: {
|
|
858
|
+
...acc.properties[propertyName] || {},
|
|
859
|
+
enum: schemaObject.discriminator.mapping ? Object.keys(schemaObject.discriminator.mapping) : void 0
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
return acc;
|
|
864
|
+
}, schemaObject || {});
|
|
865
|
+
return [...this.#parseProperties({
|
|
866
|
+
schemaObject: schemaObjectOverriden,
|
|
867
|
+
name
|
|
868
|
+
}), ...baseItems];
|
|
869
|
+
}
|
|
870
|
+
return [...this.#parseProperties({
|
|
871
|
+
schemaObject,
|
|
872
|
+
name
|
|
873
|
+
}), ...baseItems];
|
|
874
|
+
}
|
|
875
|
+
if (schemaObject.type) {
|
|
876
|
+
const type = Array.isArray(schemaObject.type) ? schemaObject.type.filter((item) => item !== "null")[0] : schemaObject.type;
|
|
877
|
+
if (![
|
|
878
|
+
"boolean",
|
|
879
|
+
"object",
|
|
880
|
+
"number",
|
|
881
|
+
"string",
|
|
882
|
+
"integer",
|
|
883
|
+
"null"
|
|
884
|
+
].includes(type)) this.context.pluginManager.logger.emit("warning", `Schema type '${schemaObject.type}' is not valid for schema ${parentName}.${name}`);
|
|
885
|
+
return [{ keyword: type }, ...baseItems];
|
|
886
|
+
}
|
|
887
|
+
return [{ keyword: emptyType }];
|
|
888
|
+
}
|
|
889
|
+
async build(...generators) {
|
|
890
|
+
const { oas, contentType, include } = this.context;
|
|
891
|
+
const schemas = getSchemas({
|
|
892
|
+
oas,
|
|
893
|
+
contentType,
|
|
894
|
+
includes: include
|
|
895
|
+
});
|
|
896
|
+
const schemaEntries = Object.entries(schemas);
|
|
897
|
+
const generatorLimit = pLimit(1);
|
|
898
|
+
const schemaLimit = pLimit(10);
|
|
899
|
+
const writeTasks = generators.map((generator) => generatorLimit(async () => {
|
|
900
|
+
const schemaTasks = schemaEntries.map(([name, schemaObject]) => schemaLimit(async () => {
|
|
901
|
+
const options = this.#getOptions({ name });
|
|
902
|
+
const tree = this.parse({
|
|
903
|
+
name,
|
|
904
|
+
schemaObject
|
|
905
|
+
});
|
|
906
|
+
const result = await generator.schema?.({
|
|
907
|
+
instance: this,
|
|
908
|
+
schema: {
|
|
909
|
+
name,
|
|
910
|
+
value: schemaObject,
|
|
911
|
+
tree
|
|
912
|
+
},
|
|
913
|
+
options: {
|
|
914
|
+
...this.options,
|
|
915
|
+
...options
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
return result ?? [];
|
|
919
|
+
}));
|
|
920
|
+
const schemaResults = await Promise.all(schemaTasks);
|
|
921
|
+
return schemaResults.flat();
|
|
922
|
+
}));
|
|
923
|
+
const nestedResults = await Promise.all(writeTasks);
|
|
924
|
+
return nestedResults.flat();
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
//#endregion
|
|
929
|
+
export { SchemaGenerator, pLimit };
|
|
930
|
+
//# sourceMappingURL=SchemaGenerator-B8Io6kky.js.map
|