@kubb/plugin-oas 3.0.0-alpha.8 → 3.0.0-beta.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/README.md +14 -5
- package/dist/OperationGenerator-BgQeGRzk.d.cts +550 -0
- package/dist/OperationGenerator-BgQeGRzk.d.ts +550 -0
- package/dist/{Schema-B1vcPGiK.d.ts → Schema-1iM2I0dK.d.ts} +1 -12
- package/dist/{Schema-DoSFh7Qd.d.cts → Schema-5o-c5UOy.d.cts} +1 -12
- package/dist/chunk-ABOQ73FL.cjs +36 -0
- package/dist/chunk-ABOQ73FL.cjs.map +1 -0
- package/dist/chunk-BG77DP54.js +30 -0
- package/dist/chunk-BG77DP54.js.map +1 -0
- package/dist/chunk-GF26SDHQ.js +28 -0
- package/dist/chunk-GF26SDHQ.js.map +1 -0
- package/dist/chunk-PADR76WZ.cjs +4 -0
- package/dist/chunk-PADR76WZ.cjs.map +1 -0
- package/dist/chunk-QAFBZLJA.cjs +48 -0
- package/dist/chunk-QAFBZLJA.cjs.map +1 -0
- package/dist/chunk-QT6ZFRNJ.cjs +752 -0
- package/dist/chunk-QT6ZFRNJ.cjs.map +1 -0
- package/dist/chunk-QWO5NQGQ.js +88 -0
- package/dist/chunk-QWO5NQGQ.js.map +1 -0
- package/dist/chunk-R47XMJ32.js +3 -0
- package/dist/chunk-R47XMJ32.js.map +1 -0
- package/dist/chunk-SR63CBLH.cjs +92 -0
- package/dist/chunk-SR63CBLH.cjs.map +1 -0
- package/dist/chunk-V2JO6RHI.js +744 -0
- package/dist/chunk-V2JO6RHI.js.map +1 -0
- package/dist/chunk-XNCEFOE6.js +45 -0
- package/dist/chunk-XNCEFOE6.js.map +1 -0
- package/dist/chunk-ZWHQ54JM.cjs +32 -0
- package/dist/chunk-ZWHQ54JM.cjs.map +1 -0
- package/dist/components.cjs +20 -12
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +3 -6
- package/dist/components.d.ts +3 -6
- package/dist/components.js +3 -12
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +14 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +9 -0
- package/dist/generators.d.ts +9 -0
- package/dist/generators.js +5 -0
- package/dist/generators.js.map +1 -0
- package/dist/hooks.cjs +102 -57
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +41 -10
- package/dist/hooks.d.ts +41 -10
- package/dist/hooks.js +91 -53
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +137 -187
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -5
- package/dist/index.d.ts +2 -5
- package/dist/index.js +76 -153
- package/dist/index.js.map +1 -1
- package/dist/utils.cjs +32 -41
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +5 -13
- package/dist/utils.d.ts +5 -13
- package/dist/utils.js +8 -40
- package/dist/utils.js.map +1 -1
- package/package.json +20 -14
- package/src/OperationGenerator.ts +23 -30
- package/src/SchemaGenerator.ts +79 -22
- package/src/SchemaMapper.ts +6 -4
- package/src/components/Schema.tsx +1 -99
- package/src/{parser.tsx → generator.tsx} +30 -22
- package/src/generators/index.ts +1 -0
- package/src/generators/jsonGenerator.ts +32 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useOperationManager.ts +77 -30
- package/src/hooks/useSchemaManager.ts +77 -0
- package/src/index.ts +3 -3
- package/src/plugin.ts +69 -56
- package/src/types.ts +24 -18
- package/src/utils/getParams.ts +1 -1
- package/src/utils/getSchemaFactory.ts +1 -1
- package/src/utils/index.ts +2 -1
- package/src/utils/parseFromConfig.ts +7 -7
- package/dist/OperationGenerator-DeXrfGDC.d.ts +0 -158
- package/dist/OperationGenerator-DhJ0MBKc.d.cts +0 -158
- package/dist/SchemaMapper-sGcY1xL5.d.cts +0 -247
- package/dist/SchemaMapper-sGcY1xL5.d.ts +0 -247
- package/dist/chunk-75BIOXB7.cjs +0 -7
- package/dist/chunk-75BIOXB7.cjs.map +0 -1
- package/dist/chunk-MUI5DWM3.js +0 -3966
- package/dist/chunk-MUI5DWM3.js.map +0 -1
- package/dist/chunk-N7EEVJA6.js +0 -35
- package/dist/chunk-N7EEVJA6.js.map +0 -1
- package/dist/chunk-NU4F7G47.cjs +0 -89
- package/dist/chunk-NU4F7G47.cjs.map +0 -1
- package/dist/chunk-O76YQFZB.cjs +0 -35
- package/dist/chunk-O76YQFZB.cjs.map +0 -1
- package/dist/chunk-SQ64ESS4.js +0 -7
- package/dist/chunk-SQ64ESS4.js.map +0 -1
- package/dist/chunk-SZDO532A.js +0 -89
- package/dist/chunk-SZDO532A.js.map +0 -1
- package/dist/chunk-VSVVTCQB.cjs +0 -3966
- package/dist/chunk-VSVVTCQB.cjs.map +0 -1
- package/dist/types-CZTUCaE5.d.cts +0 -145
- package/dist/types-CZTUCaE5.d.ts +0 -145
- package/src/utils/refSorter.ts +0 -13
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
import { getSchemaFactory, getSchemas } from './chunk-XNCEFOE6.js';
|
|
2
|
+
import { BaseGenerator } from '@kubb/core';
|
|
3
|
+
import transformers, { pascalCase } from '@kubb/core/transformers';
|
|
4
|
+
import { getUniqueName } from '@kubb/core/utils';
|
|
5
|
+
import { isReference } from '@kubb/oas';
|
|
6
|
+
import { uniqueWith, isDeepEqual, isNumber } from 'remeda';
|
|
7
|
+
|
|
8
|
+
// src/SchemaMapper.ts
|
|
9
|
+
var schemaKeywords = {
|
|
10
|
+
any: "any",
|
|
11
|
+
strict: "strict",
|
|
12
|
+
unknown: "unknown",
|
|
13
|
+
number: "number",
|
|
14
|
+
integer: "integer",
|
|
15
|
+
string: "string",
|
|
16
|
+
boolean: "boolean",
|
|
17
|
+
undefined: "undefined",
|
|
18
|
+
nullable: "nullable",
|
|
19
|
+
null: "null",
|
|
20
|
+
nullish: "nullish",
|
|
21
|
+
array: "array",
|
|
22
|
+
tuple: "tuple",
|
|
23
|
+
enum: "enum",
|
|
24
|
+
union: "union",
|
|
25
|
+
datetime: "datetime",
|
|
26
|
+
date: "date",
|
|
27
|
+
email: "email",
|
|
28
|
+
uuid: "uuid",
|
|
29
|
+
url: "url",
|
|
30
|
+
/* intersection */
|
|
31
|
+
default: "default",
|
|
32
|
+
const: "const",
|
|
33
|
+
and: "and",
|
|
34
|
+
describe: "describe",
|
|
35
|
+
min: "min",
|
|
36
|
+
max: "max",
|
|
37
|
+
optional: "optional",
|
|
38
|
+
readOnly: "readOnly",
|
|
39
|
+
writeOnly: "writeOnly",
|
|
40
|
+
// custom ones
|
|
41
|
+
object: "object",
|
|
42
|
+
ref: "ref",
|
|
43
|
+
matches: "matches",
|
|
44
|
+
firstName: "firstName",
|
|
45
|
+
lastName: "lastName",
|
|
46
|
+
password: "password",
|
|
47
|
+
phone: "phone",
|
|
48
|
+
blob: "blob",
|
|
49
|
+
deprecated: "deprecated",
|
|
50
|
+
example: "example",
|
|
51
|
+
schema: "schema",
|
|
52
|
+
catchall: "catchall",
|
|
53
|
+
time: "time",
|
|
54
|
+
name: "name"
|
|
55
|
+
};
|
|
56
|
+
function isKeyword(meta, keyword) {
|
|
57
|
+
return meta.keyword === keyword;
|
|
58
|
+
}
|
|
59
|
+
var SchemaGenerator = class _SchemaGenerator extends BaseGenerator {
|
|
60
|
+
// Collect the types of all referenced schemas, so we can export them later
|
|
61
|
+
refs = {};
|
|
62
|
+
// Keep track of already used type aliases
|
|
63
|
+
#usedAliasNames = {};
|
|
64
|
+
/**
|
|
65
|
+
* Creates a type node from a given schema.
|
|
66
|
+
* Delegates to getBaseTypeFromSchema internally and
|
|
67
|
+
* optionally adds a union with null.
|
|
68
|
+
*/
|
|
69
|
+
parse(props) {
|
|
70
|
+
const options = this.#getOptions(props);
|
|
71
|
+
const defaultSchemas = this.#parseSchemaObject(props);
|
|
72
|
+
const schemas = options.transformers?.schema?.(props, defaultSchemas) || defaultSchemas || [];
|
|
73
|
+
return uniqueWith(schemas, isDeepEqual);
|
|
74
|
+
}
|
|
75
|
+
deepSearch(tree, keyword) {
|
|
76
|
+
return _SchemaGenerator.deepSearch(tree, keyword);
|
|
77
|
+
}
|
|
78
|
+
find(tree, keyword) {
|
|
79
|
+
return _SchemaGenerator.find(tree, keyword);
|
|
80
|
+
}
|
|
81
|
+
static deepSearch(tree, keyword) {
|
|
82
|
+
const foundItems = [];
|
|
83
|
+
tree?.forEach((schema) => {
|
|
84
|
+
if (schema.keyword === keyword) {
|
|
85
|
+
foundItems.push(schema);
|
|
86
|
+
}
|
|
87
|
+
if (schema.keyword === schemaKeywords.object) {
|
|
88
|
+
const subItem = schema;
|
|
89
|
+
Object.values(subItem.args?.properties || {}).forEach((entrySchema) => {
|
|
90
|
+
foundItems.push(..._SchemaGenerator.deepSearch(entrySchema, keyword));
|
|
91
|
+
});
|
|
92
|
+
Object.values(subItem.args?.additionalProperties || {}).forEach((entrySchema) => {
|
|
93
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (schema.keyword === schemaKeywords.array) {
|
|
97
|
+
const subItem = schema;
|
|
98
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
99
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (schema.keyword === schemaKeywords.and) {
|
|
103
|
+
const subItem = schema;
|
|
104
|
+
subItem.args.forEach((entrySchema) => {
|
|
105
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (schema.keyword === schemaKeywords.tuple) {
|
|
109
|
+
const subItem = schema;
|
|
110
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
111
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (schema.keyword === schemaKeywords.union) {
|
|
115
|
+
const subItem = schema;
|
|
116
|
+
subItem.args.forEach((entrySchema) => {
|
|
117
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return foundItems;
|
|
122
|
+
}
|
|
123
|
+
static findInObject(tree, keyword) {
|
|
124
|
+
let foundItem = void 0;
|
|
125
|
+
tree?.forEach((schema) => {
|
|
126
|
+
if (!foundItem && schema.keyword === keyword) {
|
|
127
|
+
foundItem = schema;
|
|
128
|
+
}
|
|
129
|
+
if (schema.keyword === schemaKeywords.object) {
|
|
130
|
+
const subItem = schema;
|
|
131
|
+
Object.values(subItem.args?.properties || {}).forEach((entrySchema) => {
|
|
132
|
+
if (!foundItem) {
|
|
133
|
+
foundItem = _SchemaGenerator.find(entrySchema, keyword);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
Object.values(subItem.args?.additionalProperties || {}).forEach((entrySchema) => {
|
|
137
|
+
if (!foundItem) {
|
|
138
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return foundItem;
|
|
144
|
+
}
|
|
145
|
+
static find(tree, keyword) {
|
|
146
|
+
let foundItem = void 0;
|
|
147
|
+
tree?.forEach((schema) => {
|
|
148
|
+
if (!foundItem && schema.keyword === keyword) {
|
|
149
|
+
foundItem = schema;
|
|
150
|
+
}
|
|
151
|
+
if (schema.keyword === schemaKeywords.array) {
|
|
152
|
+
const subItem = schema;
|
|
153
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
154
|
+
if (!foundItem) {
|
|
155
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (schema.keyword === schemaKeywords.and) {
|
|
160
|
+
const subItem = schema;
|
|
161
|
+
subItem.args.forEach((entrySchema) => {
|
|
162
|
+
if (!foundItem) {
|
|
163
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
if (schema.keyword === schemaKeywords.tuple) {
|
|
168
|
+
const subItem = schema;
|
|
169
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
170
|
+
if (!foundItem) {
|
|
171
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (schema.keyword === schemaKeywords.union) {
|
|
176
|
+
const subItem = schema;
|
|
177
|
+
subItem.args.forEach((entrySchema) => {
|
|
178
|
+
if (!foundItem) {
|
|
179
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return foundItem;
|
|
185
|
+
}
|
|
186
|
+
#getUsedEnumNames(props) {
|
|
187
|
+
const options = this.#getOptions(props);
|
|
188
|
+
return options.usedEnumNames || {};
|
|
189
|
+
}
|
|
190
|
+
#getOptions({ name }) {
|
|
191
|
+
const { override = [] } = this.context;
|
|
192
|
+
return {
|
|
193
|
+
...this.options,
|
|
194
|
+
...override.find(({ pattern, type }) => {
|
|
195
|
+
if (name && type === "schemaName") {
|
|
196
|
+
return !!name.match(pattern);
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
})?.options || {}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
#getUnknownReturn(props) {
|
|
203
|
+
const options = this.#getOptions(props);
|
|
204
|
+
if (options.unknownType === "any") {
|
|
205
|
+
return schemaKeywords.any;
|
|
206
|
+
}
|
|
207
|
+
return schemaKeywords.unknown;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Recursively creates a type literal with the given props.
|
|
211
|
+
*/
|
|
212
|
+
#parseProperties({ schema, name }) {
|
|
213
|
+
const properties = schema?.properties || {};
|
|
214
|
+
const additionalProperties = schema?.additionalProperties;
|
|
215
|
+
const required = schema?.required;
|
|
216
|
+
const propertiesSchemas = Object.keys(properties).map((propertyName) => {
|
|
217
|
+
const validationFunctions = [];
|
|
218
|
+
const propertySchema = properties[propertyName];
|
|
219
|
+
const isRequired = Array.isArray(required) ? required?.includes(propertyName) : !!required;
|
|
220
|
+
const nullable = propertySchema.nullable ?? propertySchema["x-nullable"] ?? false;
|
|
221
|
+
validationFunctions.push(...this.parse({ schema: propertySchema, name: propertyName, parentName: name }));
|
|
222
|
+
validationFunctions.push({
|
|
223
|
+
keyword: schemaKeywords.name,
|
|
224
|
+
args: propertyName
|
|
225
|
+
});
|
|
226
|
+
if (!isRequired && nullable) {
|
|
227
|
+
validationFunctions.push({ keyword: schemaKeywords.nullish });
|
|
228
|
+
} else if (!isRequired) {
|
|
229
|
+
validationFunctions.push({ keyword: schemaKeywords.optional });
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
[propertyName]: validationFunctions
|
|
233
|
+
};
|
|
234
|
+
}).reduce((acc, curr) => ({ ...acc, ...curr }), {});
|
|
235
|
+
let additionalPropertiesSchemas = [];
|
|
236
|
+
if (additionalProperties) {
|
|
237
|
+
additionalPropertiesSchemas = additionalProperties === true || !Object.keys(additionalProperties).length ? [{ keyword: this.#getUnknownReturn({ schema, name }) }] : this.parse({ schema: additionalProperties, parentName: name });
|
|
238
|
+
}
|
|
239
|
+
return [
|
|
240
|
+
{
|
|
241
|
+
keyword: schemaKeywords.object,
|
|
242
|
+
args: {
|
|
243
|
+
properties: propertiesSchemas,
|
|
244
|
+
additionalProperties: additionalPropertiesSchemas
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Create a type alias for the schema referenced by the given ReferenceObject
|
|
251
|
+
*/
|
|
252
|
+
#getRefAlias(obj) {
|
|
253
|
+
const { $ref } = obj;
|
|
254
|
+
let ref = this.refs[$ref];
|
|
255
|
+
const originalName = getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames);
|
|
256
|
+
const propertyName = this.context.pluginManager.resolveName({
|
|
257
|
+
name: originalName,
|
|
258
|
+
pluginKey: this.context.plugin.key,
|
|
259
|
+
type: "function"
|
|
260
|
+
});
|
|
261
|
+
if (ref) {
|
|
262
|
+
return [
|
|
263
|
+
{
|
|
264
|
+
keyword: schemaKeywords.ref,
|
|
265
|
+
args: { name: ref.propertyName, path: ref.path }
|
|
266
|
+
}
|
|
267
|
+
];
|
|
268
|
+
}
|
|
269
|
+
const fileName = this.context.pluginManager.resolveName({
|
|
270
|
+
name: originalName,
|
|
271
|
+
pluginKey: this.context.plugin.key,
|
|
272
|
+
type: "file"
|
|
273
|
+
});
|
|
274
|
+
const file = this.context.pluginManager.getFile({
|
|
275
|
+
name: fileName,
|
|
276
|
+
pluginKey: this.context.plugin.key,
|
|
277
|
+
extname: ".ts"
|
|
278
|
+
});
|
|
279
|
+
ref = this.refs[$ref] = {
|
|
280
|
+
propertyName,
|
|
281
|
+
originalName,
|
|
282
|
+
path: file.path
|
|
283
|
+
};
|
|
284
|
+
return [
|
|
285
|
+
{
|
|
286
|
+
keyword: schemaKeywords.ref,
|
|
287
|
+
args: { name: ref.propertyName, path: ref?.path }
|
|
288
|
+
}
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
#getParsedSchemaObject(schema) {
|
|
292
|
+
const parsedSchema = getSchemaFactory(this.context.oas)(schema);
|
|
293
|
+
return parsedSchema;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* This is the very core of the OpenAPI to TS conversion - it takes a
|
|
297
|
+
* schema and returns the appropriate type.
|
|
298
|
+
*/
|
|
299
|
+
#parseSchemaObject({ schema: _schema, name, parentName }) {
|
|
300
|
+
const options = this.#getOptions({ schema: _schema, name });
|
|
301
|
+
const unknownReturn = this.#getUnknownReturn({ schema: _schema, name });
|
|
302
|
+
const { schema, version } = this.#getParsedSchemaObject(_schema);
|
|
303
|
+
if (!schema) {
|
|
304
|
+
return [{ keyword: unknownReturn }];
|
|
305
|
+
}
|
|
306
|
+
const baseItems = [
|
|
307
|
+
{
|
|
308
|
+
keyword: schemaKeywords.schema,
|
|
309
|
+
args: {
|
|
310
|
+
type: schema.type,
|
|
311
|
+
format: schema.format
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
];
|
|
315
|
+
const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
|
|
316
|
+
const max = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
|
|
317
|
+
const nullable = schema.nullable ?? schema["x-nullable"] ?? false;
|
|
318
|
+
if (schema.default !== void 0 && !Array.isArray(schema.default)) {
|
|
319
|
+
if (typeof schema.default === "string") {
|
|
320
|
+
baseItems.push({
|
|
321
|
+
keyword: schemaKeywords.default,
|
|
322
|
+
args: transformers.stringify(schema.default)
|
|
323
|
+
});
|
|
324
|
+
} else if (typeof schema.default === "boolean") {
|
|
325
|
+
baseItems.push({
|
|
326
|
+
keyword: schemaKeywords.default,
|
|
327
|
+
args: schema.default ?? false
|
|
328
|
+
});
|
|
329
|
+
} else {
|
|
330
|
+
baseItems.push({
|
|
331
|
+
keyword: schemaKeywords.default,
|
|
332
|
+
args: schema.default
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (schema.description) {
|
|
337
|
+
baseItems.push({
|
|
338
|
+
keyword: schemaKeywords.describe,
|
|
339
|
+
args: schema.description
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
if (schema.pattern) {
|
|
343
|
+
baseItems.unshift({
|
|
344
|
+
keyword: schemaKeywords.matches,
|
|
345
|
+
args: schema.pattern
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
if (max !== void 0) {
|
|
349
|
+
baseItems.unshift({ keyword: schemaKeywords.max, args: max });
|
|
350
|
+
}
|
|
351
|
+
if (min !== void 0) {
|
|
352
|
+
baseItems.unshift({ keyword: schemaKeywords.min, args: min });
|
|
353
|
+
}
|
|
354
|
+
if (nullable) {
|
|
355
|
+
baseItems.push({ keyword: schemaKeywords.nullable });
|
|
356
|
+
}
|
|
357
|
+
if (schema.type && Array.isArray(schema.type)) {
|
|
358
|
+
const [_schema2, nullable2] = schema.type;
|
|
359
|
+
if (nullable2 === "null") {
|
|
360
|
+
baseItems.push({ keyword: schemaKeywords.nullable });
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (schema.readOnly) {
|
|
364
|
+
baseItems.push({ keyword: schemaKeywords.readOnly });
|
|
365
|
+
}
|
|
366
|
+
if (schema.writeOnly) {
|
|
367
|
+
baseItems.push({ keyword: schemaKeywords.writeOnly });
|
|
368
|
+
}
|
|
369
|
+
if (isReference(schema)) {
|
|
370
|
+
return [
|
|
371
|
+
...this.#getRefAlias(schema),
|
|
372
|
+
nullable && { keyword: schemaKeywords.nullable },
|
|
373
|
+
schema.readOnly && { keyword: schemaKeywords.readOnly },
|
|
374
|
+
schema.writeOnly && { keyword: schemaKeywords.writeOnly },
|
|
375
|
+
{
|
|
376
|
+
keyword: schemaKeywords.schema,
|
|
377
|
+
args: {
|
|
378
|
+
type: schema.type,
|
|
379
|
+
format: schema.format
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
].filter(Boolean);
|
|
383
|
+
}
|
|
384
|
+
if (schema.oneOf) {
|
|
385
|
+
const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
|
|
386
|
+
const union = {
|
|
387
|
+
keyword: schemaKeywords.union,
|
|
388
|
+
args: schema.oneOf.map((item) => {
|
|
389
|
+
return item && this.parse({ schema: item, name, parentName })[0];
|
|
390
|
+
}).filter(Boolean).filter((item) => {
|
|
391
|
+
return item && item.keyword !== unknownReturn;
|
|
392
|
+
})
|
|
393
|
+
};
|
|
394
|
+
if (schemaWithoutOneOf.properties) {
|
|
395
|
+
const propertySchemas = this.parse({ schema: schemaWithoutOneOf, name, parentName });
|
|
396
|
+
return [
|
|
397
|
+
{
|
|
398
|
+
...union,
|
|
399
|
+
args: union.args.map((arg) => {
|
|
400
|
+
return {
|
|
401
|
+
keyword: schemaKeywords.and,
|
|
402
|
+
args: [arg, ...propertySchemas]
|
|
403
|
+
};
|
|
404
|
+
})
|
|
405
|
+
},
|
|
406
|
+
...baseItems
|
|
407
|
+
];
|
|
408
|
+
}
|
|
409
|
+
return [union, ...baseItems];
|
|
410
|
+
}
|
|
411
|
+
if (schema.anyOf) {
|
|
412
|
+
const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
|
|
413
|
+
const union = {
|
|
414
|
+
keyword: schemaKeywords.union,
|
|
415
|
+
args: schema.anyOf.map((item) => {
|
|
416
|
+
return item && this.parse({ schema: item, name, parentName })[0];
|
|
417
|
+
}).filter(Boolean).filter((item) => {
|
|
418
|
+
return item && item.keyword !== unknownReturn;
|
|
419
|
+
}).map((item) => {
|
|
420
|
+
if (isKeyword(item, schemaKeywords.object)) {
|
|
421
|
+
return {
|
|
422
|
+
...item,
|
|
423
|
+
args: {
|
|
424
|
+
...item.args,
|
|
425
|
+
strict: true
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return item;
|
|
430
|
+
})
|
|
431
|
+
};
|
|
432
|
+
if (schemaWithoutAnyOf.properties) {
|
|
433
|
+
return [...this.parse({ schema: schemaWithoutAnyOf, name, parentName }), union, ...baseItems];
|
|
434
|
+
}
|
|
435
|
+
return [union, ...baseItems];
|
|
436
|
+
}
|
|
437
|
+
if (schema.allOf) {
|
|
438
|
+
const schemaWithoutAllOf = { ...schema, allOf: void 0 };
|
|
439
|
+
const and = {
|
|
440
|
+
keyword: schemaKeywords.and,
|
|
441
|
+
args: schema.allOf.map((item) => {
|
|
442
|
+
return item && this.parse({ schema: item, name, parentName })[0];
|
|
443
|
+
}).filter(Boolean).filter((item) => {
|
|
444
|
+
return item && item.keyword !== unknownReturn;
|
|
445
|
+
})
|
|
446
|
+
};
|
|
447
|
+
if (schemaWithoutAllOf.properties) {
|
|
448
|
+
return [
|
|
449
|
+
{
|
|
450
|
+
...and,
|
|
451
|
+
args: [...and.args || [], ...this.parse({ schema: schemaWithoutAllOf, name, parentName })]
|
|
452
|
+
},
|
|
453
|
+
...baseItems
|
|
454
|
+
];
|
|
455
|
+
}
|
|
456
|
+
return [and, ...baseItems];
|
|
457
|
+
}
|
|
458
|
+
if (schema.enum) {
|
|
459
|
+
const enumName = getUniqueName(pascalCase([parentName, name, options.enumSuffix].join(" ")), this.#getUsedEnumNames({ schema, name }));
|
|
460
|
+
const typeName = this.context.pluginManager.resolveName({
|
|
461
|
+
name: enumName,
|
|
462
|
+
pluginKey: this.context.plugin.key,
|
|
463
|
+
type: "type"
|
|
464
|
+
});
|
|
465
|
+
const nullableEnum = schema.enum.includes(null);
|
|
466
|
+
if (nullableEnum) {
|
|
467
|
+
baseItems.push({ keyword: schemaKeywords.nullable });
|
|
468
|
+
}
|
|
469
|
+
const filteredValues = schema.enum.filter((value) => value !== null);
|
|
470
|
+
const extensionEnums = ["x-enumNames", "x-enum-varnames"].filter((extensionKey) => extensionKey in schema).map((extensionKey) => {
|
|
471
|
+
return [
|
|
472
|
+
{
|
|
473
|
+
keyword: schemaKeywords.enum,
|
|
474
|
+
args: {
|
|
475
|
+
name,
|
|
476
|
+
typeName,
|
|
477
|
+
asConst: false,
|
|
478
|
+
items: [...new Set(schema[extensionKey])].map((name2, index) => ({
|
|
479
|
+
name: transformers.stringify(name2),
|
|
480
|
+
value: schema.enum?.[index],
|
|
481
|
+
format: isNumber(schema.enum?.[index]) ? "number" : "string"
|
|
482
|
+
}))
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
...baseItems.filter(
|
|
486
|
+
(item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches
|
|
487
|
+
)
|
|
488
|
+
];
|
|
489
|
+
});
|
|
490
|
+
if (schema.type === "number" || schema.type === "integer") {
|
|
491
|
+
const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
|
|
492
|
+
return [
|
|
493
|
+
{
|
|
494
|
+
keyword: schemaKeywords.enum,
|
|
495
|
+
args: {
|
|
496
|
+
name: enumName,
|
|
497
|
+
typeName,
|
|
498
|
+
asConst: true,
|
|
499
|
+
items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({
|
|
500
|
+
name: name2,
|
|
501
|
+
value,
|
|
502
|
+
format: "number"
|
|
503
|
+
})) : [...new Set(filteredValues)].map((value) => {
|
|
504
|
+
return {
|
|
505
|
+
name: value,
|
|
506
|
+
value,
|
|
507
|
+
format: "number"
|
|
508
|
+
};
|
|
509
|
+
})
|
|
510
|
+
}
|
|
511
|
+
},
|
|
512
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
|
|
513
|
+
];
|
|
514
|
+
}
|
|
515
|
+
if (schema.type === "boolean") {
|
|
516
|
+
const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
|
|
517
|
+
return [
|
|
518
|
+
{
|
|
519
|
+
keyword: schemaKeywords.enum,
|
|
520
|
+
args: {
|
|
521
|
+
name: enumName,
|
|
522
|
+
typeName,
|
|
523
|
+
asConst: true,
|
|
524
|
+
items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({
|
|
525
|
+
name: name2,
|
|
526
|
+
value,
|
|
527
|
+
format: "boolean"
|
|
528
|
+
})) : [...new Set(filteredValues)].map((value) => {
|
|
529
|
+
return {
|
|
530
|
+
name: value,
|
|
531
|
+
value,
|
|
532
|
+
format: "boolean"
|
|
533
|
+
};
|
|
534
|
+
})
|
|
535
|
+
}
|
|
536
|
+
},
|
|
537
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.matches)
|
|
538
|
+
];
|
|
539
|
+
}
|
|
540
|
+
if (extensionEnums.length > 0 && extensionEnums[0]) {
|
|
541
|
+
return extensionEnums[0];
|
|
542
|
+
}
|
|
543
|
+
return [
|
|
544
|
+
{
|
|
545
|
+
keyword: schemaKeywords.enum,
|
|
546
|
+
args: {
|
|
547
|
+
name: enumName,
|
|
548
|
+
typeName,
|
|
549
|
+
asConst: false,
|
|
550
|
+
items: [...new Set(filteredValues)].map((value) => ({
|
|
551
|
+
name: transformers.stringify(value),
|
|
552
|
+
value,
|
|
553
|
+
format: isNumber(value) ? "number" : "string"
|
|
554
|
+
}))
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
|
|
558
|
+
];
|
|
559
|
+
}
|
|
560
|
+
if ("prefixItems" in schema) {
|
|
561
|
+
const prefixItems = schema.prefixItems;
|
|
562
|
+
const min2 = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
|
|
563
|
+
const max2 = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
|
|
564
|
+
return [
|
|
565
|
+
{
|
|
566
|
+
keyword: schemaKeywords.tuple,
|
|
567
|
+
args: {
|
|
568
|
+
min: min2,
|
|
569
|
+
max: max2,
|
|
570
|
+
items: prefixItems.map((item) => {
|
|
571
|
+
return this.parse({ schema: item, name, parentName })[0];
|
|
572
|
+
}).filter(Boolean)
|
|
573
|
+
}
|
|
574
|
+
},
|
|
575
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
|
|
576
|
+
];
|
|
577
|
+
}
|
|
578
|
+
if (version === "3.1" && "const" in schema) {
|
|
579
|
+
if (schema["const"]) {
|
|
580
|
+
return [
|
|
581
|
+
{
|
|
582
|
+
keyword: schemaKeywords.const,
|
|
583
|
+
args: {
|
|
584
|
+
name: schema["const"],
|
|
585
|
+
format: typeof schema["const"] === "number" ? "number" : "string",
|
|
586
|
+
value: schema["const"]
|
|
587
|
+
}
|
|
588
|
+
},
|
|
589
|
+
...baseItems
|
|
590
|
+
];
|
|
591
|
+
}
|
|
592
|
+
return [{ keyword: schemaKeywords.null }];
|
|
593
|
+
}
|
|
594
|
+
if (schema.format) {
|
|
595
|
+
switch (schema.format) {
|
|
596
|
+
case "binary":
|
|
597
|
+
baseItems.push({ keyword: schemaKeywords.blob });
|
|
598
|
+
return baseItems;
|
|
599
|
+
case "date-time":
|
|
600
|
+
if (options.dateType) {
|
|
601
|
+
if (options.dateType === "date") {
|
|
602
|
+
baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
|
|
603
|
+
return baseItems;
|
|
604
|
+
}
|
|
605
|
+
if (options.dateType === "stringOffset") {
|
|
606
|
+
baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: true } });
|
|
607
|
+
return baseItems;
|
|
608
|
+
}
|
|
609
|
+
if (options.dateType === "stringLocal") {
|
|
610
|
+
baseItems.unshift({ keyword: schemaKeywords.datetime, args: { local: true } });
|
|
611
|
+
return baseItems;
|
|
612
|
+
}
|
|
613
|
+
baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: false } });
|
|
614
|
+
return baseItems;
|
|
615
|
+
}
|
|
616
|
+
break;
|
|
617
|
+
case "date":
|
|
618
|
+
if (options.dateType) {
|
|
619
|
+
if (options.dateType === "date") {
|
|
620
|
+
baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
|
|
621
|
+
return baseItems;
|
|
622
|
+
}
|
|
623
|
+
baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "string" } });
|
|
624
|
+
return baseItems;
|
|
625
|
+
}
|
|
626
|
+
break;
|
|
627
|
+
case "time":
|
|
628
|
+
if (options.dateType) {
|
|
629
|
+
if (options.dateType === "date") {
|
|
630
|
+
baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "date" } });
|
|
631
|
+
return baseItems;
|
|
632
|
+
}
|
|
633
|
+
baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "string" } });
|
|
634
|
+
return baseItems;
|
|
635
|
+
}
|
|
636
|
+
break;
|
|
637
|
+
case "uuid":
|
|
638
|
+
baseItems.unshift({ keyword: schemaKeywords.uuid });
|
|
639
|
+
break;
|
|
640
|
+
case "email":
|
|
641
|
+
case "idn-email":
|
|
642
|
+
baseItems.unshift({ keyword: schemaKeywords.email });
|
|
643
|
+
break;
|
|
644
|
+
case "uri":
|
|
645
|
+
case "ipv4":
|
|
646
|
+
case "ipv6":
|
|
647
|
+
case "uri-reference":
|
|
648
|
+
case "hostname":
|
|
649
|
+
case "idn-hostname":
|
|
650
|
+
baseItems.unshift({ keyword: schemaKeywords.url });
|
|
651
|
+
break;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
if ("items" in schema || schema.type === "array") {
|
|
655
|
+
const min2 = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
|
|
656
|
+
const max2 = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
|
|
657
|
+
const items = this.parse({ schema: "items" in schema ? schema.items : [], name, parentName });
|
|
658
|
+
return [
|
|
659
|
+
{
|
|
660
|
+
keyword: schemaKeywords.array,
|
|
661
|
+
args: {
|
|
662
|
+
items,
|
|
663
|
+
min: min2,
|
|
664
|
+
max: max2
|
|
665
|
+
}
|
|
666
|
+
},
|
|
667
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
|
|
668
|
+
];
|
|
669
|
+
}
|
|
670
|
+
if (schema.properties || schema.additionalProperties) {
|
|
671
|
+
return [...this.#parseProperties({ schema, name }), ...baseItems];
|
|
672
|
+
}
|
|
673
|
+
if (schema.type) {
|
|
674
|
+
if (Array.isArray(schema.type)) {
|
|
675
|
+
const [type] = schema.type;
|
|
676
|
+
return [
|
|
677
|
+
...this.parse({
|
|
678
|
+
schema: {
|
|
679
|
+
...schema,
|
|
680
|
+
type
|
|
681
|
+
},
|
|
682
|
+
name,
|
|
683
|
+
parentName
|
|
684
|
+
}),
|
|
685
|
+
...baseItems
|
|
686
|
+
].filter(Boolean);
|
|
687
|
+
}
|
|
688
|
+
if (!["boolean", "object", "number", "string", "integer", "null"].includes(schema.type)) {
|
|
689
|
+
this.context.pluginManager.logger.emit("warning", `Schema type '${schema.type}' is not valid for schema ${parentName}.${name}`);
|
|
690
|
+
}
|
|
691
|
+
return [{ keyword: schema.type }, ...baseItems];
|
|
692
|
+
}
|
|
693
|
+
return [{ keyword: unknownReturn }];
|
|
694
|
+
}
|
|
695
|
+
async build(...generators) {
|
|
696
|
+
const { oas, contentType, include } = this.context;
|
|
697
|
+
oas.resolveDiscriminators();
|
|
698
|
+
const schemas = getSchemas({ oas, contentType, includes: include });
|
|
699
|
+
const promises = Object.entries(schemas).reduce((acc, [name, value]) => {
|
|
700
|
+
if (!value) {
|
|
701
|
+
return acc;
|
|
702
|
+
}
|
|
703
|
+
const options = this.#getOptions({ name });
|
|
704
|
+
const promiseOperation = this.schema.call(this, name, value, {
|
|
705
|
+
...this.options,
|
|
706
|
+
...options
|
|
707
|
+
});
|
|
708
|
+
if (promiseOperation) {
|
|
709
|
+
acc.push(promiseOperation);
|
|
710
|
+
}
|
|
711
|
+
generators?.forEach((generator) => {
|
|
712
|
+
const tree = this.parse({ schema: value, name });
|
|
713
|
+
const promise = generator.schema?.({
|
|
714
|
+
instance: this,
|
|
715
|
+
schema: {
|
|
716
|
+
name,
|
|
717
|
+
value,
|
|
718
|
+
tree
|
|
719
|
+
},
|
|
720
|
+
options: {
|
|
721
|
+
...this.options,
|
|
722
|
+
...options
|
|
723
|
+
}
|
|
724
|
+
});
|
|
725
|
+
if (promise) {
|
|
726
|
+
acc.push(promise);
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
return acc;
|
|
730
|
+
}, []);
|
|
731
|
+
const files = await Promise.all(promises);
|
|
732
|
+
return files.flat().filter(Boolean);
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Schema
|
|
736
|
+
*/
|
|
737
|
+
async schema(name, object, options) {
|
|
738
|
+
return [];
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
export { SchemaGenerator, isKeyword, schemaKeywords };
|
|
743
|
+
//# sourceMappingURL=chunk-V2JO6RHI.js.map
|
|
744
|
+
//# sourceMappingURL=chunk-V2JO6RHI.js.map
|