UNPKG

@kubb/plugin-oas

Version:

OpenAPI Specification (OAS) plugin for Kubb, providing core functionality for parsing and processing OpenAPI/Swagger schemas for code generation.

869 lines (864 loc) • 34 kB
'use strict'; var chunkYWMMI3MO_cjs = require('./chunk-YWMMI3MO.cjs'); var chunkZVFL3NXX_cjs = require('./chunk-ZVFL3NXX.cjs'); var core = require('@kubb/core'); var transformers = require('@kubb/core/transformers'); var utils = require('@kubb/core/utils'); var oas = require('@kubb/oas'); var remeda = require('remeda'); var pLimit = require('p-limit'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var transformers__default = /*#__PURE__*/_interopDefault(transformers); var pLimit__default = /*#__PURE__*/_interopDefault(pLimit); var SchemaGenerator = class _SchemaGenerator extends core.BaseGenerator { // Collect the types of all referenced schemas, so we can export them later refs = {}; // Keep track of already used type aliases #usedAliasNames = {}; /** * Creates a type node from a given schema. * Delegates to getBaseTypeFromSchema internally and * optionally adds a union with null. */ parse(props) { const options = this.#getOptions(props); const defaultSchemas = this.#parseSchemaObject(props); const schemas = options.transformers?.schema?.(props, defaultSchemas) || defaultSchemas || []; return remeda.uniqueWith(schemas, remeda.isDeepEqual); } deepSearch(tree, keyword) { return _SchemaGenerator.deepSearch(tree, keyword); } find(tree, keyword) { return _SchemaGenerator.find(tree, keyword); } static deepSearch(tree, keyword) { const foundItems = []; tree?.forEach((schema) => { if (schema.keyword === keyword) { foundItems.push(schema); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.object)) { Object.values(schema.args?.properties || {}).forEach((entrySchema) => { foundItems.push(..._SchemaGenerator.deepSearch(entrySchema, keyword)); }); Object.values(schema.args?.additionalProperties || {}).forEach((entrySchema) => { foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword)); }); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.array)) { schema.args.items.forEach((entrySchema) => { foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword)); }); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.and)) { schema.args.forEach((entrySchema) => { foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword)); }); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.tuple)) { schema.args.items.forEach((entrySchema) => { foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword)); }); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.union)) { schema.args.forEach((entrySchema) => { foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword)); }); } }); return foundItems; } static findInObject(tree, keyword) { let foundItem; tree?.forEach((schema) => { if (!foundItem && schema.keyword === keyword) { foundItem = schema; } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.object)) { Object.values(schema.args?.properties || {}).forEach((entrySchema) => { if (!foundItem) { foundItem = _SchemaGenerator.find(entrySchema, keyword); } }); Object.values(schema.args?.additionalProperties || {}).forEach((entrySchema) => { if (!foundItem) { foundItem = _SchemaGenerator.find([entrySchema], keyword); } }); } }); return foundItem; } static find(tree, keyword) { let foundItem; tree?.forEach((schema) => { if (!foundItem && schema.keyword === keyword) { foundItem = schema; } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.array)) { schema.args.items.forEach((entrySchema) => { if (!foundItem) { foundItem = _SchemaGenerator.find([entrySchema], keyword); } }); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.and)) { schema.args.forEach((entrySchema) => { if (!foundItem) { foundItem = _SchemaGenerator.find([entrySchema], keyword); } }); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.tuple)) { schema.args.items.forEach((entrySchema) => { if (!foundItem) { foundItem = _SchemaGenerator.find([entrySchema], keyword); } }); } if (chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.union)) { schema.args.forEach((entrySchema) => { if (!foundItem) { foundItem = _SchemaGenerator.find([entrySchema], keyword); } }); } }); return foundItem; } static combineObjects(tree) { if (!tree) { return []; } return tree.map((schema) => { if (!chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.and)) { return schema; } let mergedProperties = null; let mergedAdditionalProps = []; const newArgs = []; for (const subSchema of schema.args) { if (chunkYWMMI3MO_cjs.isKeyword(subSchema, chunkYWMMI3MO_cjs.schemaKeywords.object)) { const { properties = {}, additionalProperties = [] } = subSchema.args ?? {}; if (!mergedProperties) { mergedProperties = {}; } for (const [key, value] of Object.entries(properties)) { mergedProperties[key] = value; } if (additionalProperties.length > 0) { mergedAdditionalProps = additionalProperties; } } else { newArgs.push(subSchema); } } if (mergedProperties) { newArgs.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.object, args: { properties: mergedProperties, additionalProperties: mergedAdditionalProps } }); } return { keyword: chunkYWMMI3MO_cjs.schemaKeywords.and, args: newArgs }; }); } #getUsedEnumNames(props) { const options = this.#getOptions(props); return options.usedEnumNames || {}; } #getOptions({ name }) { const { override = [] } = this.context; return { ...this.options, ...override.find(({ pattern, type }) => { if (name && type === "schemaName") { return !!name.match(pattern); } return false; })?.options || {} }; } #getUnknownType(props) { const options = this.#getOptions(props); if (options.unknownType === "any") { return chunkYWMMI3MO_cjs.schemaKeywords.any; } if (options.unknownType === "void") { return chunkYWMMI3MO_cjs.schemaKeywords.void; } return chunkYWMMI3MO_cjs.schemaKeywords.unknown; } #getEmptyType(props) { const options = this.#getOptions(props); if (options.emptySchemaType === "any") { return chunkYWMMI3MO_cjs.schemaKeywords.any; } if (options.emptySchemaType === "void") { return chunkYWMMI3MO_cjs.schemaKeywords.void; } return chunkYWMMI3MO_cjs.schemaKeywords.unknown; } /** * Recursively creates a type literal with the given props. */ #parseProperties({ schemaObject, name }) { const properties = schemaObject?.properties || {}; const additionalProperties = schemaObject?.additionalProperties; const required = schemaObject?.required; const propertiesSchemas = Object.keys(properties).map((propertyName) => { const validationFunctions = []; const propertySchema = properties[propertyName]; const isRequired = Array.isArray(required) ? required?.includes(propertyName) : !!required; const nullable = propertySchema.nullable ?? propertySchema["x-nullable"] ?? false; validationFunctions.push(...this.parse({ schemaObject: propertySchema, name: propertyName, parentName: name })); validationFunctions.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.name, args: propertyName }); if (!isRequired && nullable) { validationFunctions.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.nullish }); } else if (!isRequired) { validationFunctions.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.optional }); } return { [propertyName]: validationFunctions }; }).reduce((acc, curr) => ({ ...acc, ...curr }), {}); let additionalPropertiesSchemas = []; if (additionalProperties) { additionalPropertiesSchemas = additionalProperties === true || !Object.keys(additionalProperties).length ? [{ keyword: this.#getUnknownType({ schemaObject, name }) }] : this.parse({ schemaObject: additionalProperties, parentName: name }); } return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.object, args: { properties: propertiesSchemas, additionalProperties: additionalPropertiesSchemas } } ]; } /** * Create a type alias for the schema referenced by the given ReferenceObject */ #getRefAlias(schemaObject, name) { const { $ref } = schemaObject; const ref = this.refs[$ref]; if (ref) { const dereferencedSchema = this.context.oas.dereferenceWithRef(schemaObject); if (dereferencedSchema && oas.isDiscriminator(dereferencedSchema)) { const [key] = Object.entries(dereferencedSchema.discriminator.mapping || {}).find(([_key, value]) => value.replace(/.+\//, "") === name) || []; if (key) { return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.and, args: [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.ref, args: { name: ref.propertyName, $ref, path: ref.path, isImportable: !!this.context.oas.get($ref) } }, { keyword: chunkYWMMI3MO_cjs.schemaKeywords.object, args: { properties: { [dereferencedSchema.discriminator.propertyName]: [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.const, args: { name: key, format: "string", value: key } } ] } } } ] } ]; } } return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.ref, args: { name: ref.propertyName, $ref, path: ref.path, isImportable: !!this.context.oas.get($ref) } } ]; } const originalName = utils.getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames); const propertyName = this.context.pluginManager.resolveName({ name: originalName, pluginKey: this.context.plugin.key, type: "function" }); const fileName = this.context.pluginManager.resolveName({ name: originalName, pluginKey: this.context.plugin.key, type: "file" }); const file = this.context.pluginManager.getFile({ name: fileName, pluginKey: this.context.plugin.key, extname: ".ts" }); this.refs[$ref] = { propertyName, originalName, path: file.path }; return this.#getRefAlias(schemaObject, name); } #getParsedSchemaObject(schema) { return chunkZVFL3NXX_cjs.getSchemaFactory(this.context.oas)(schema); } #addDiscriminatorToSchema({ schema, schemaObject, discriminator }) { if (!chunkYWMMI3MO_cjs.isKeyword(schema, chunkYWMMI3MO_cjs.schemaKeywords.union)) { return schema; } const objectPropertySchema = _SchemaGenerator.find(this.parse({ schemaObject }), chunkYWMMI3MO_cjs.schemaKeywords.object); return { ...schema, args: Object.entries(discriminator.mapping || {}).map(([key, value]) => { const arg = schema.args.find((item) => chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.ref) && item.args.$ref === value); return { keyword: chunkYWMMI3MO_cjs.schemaKeywords.and, args: [ arg, { keyword: chunkYWMMI3MO_cjs.schemaKeywords.object, args: { properties: { ...objectPropertySchema?.args?.properties || {}, [discriminator.propertyName]: [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.const, args: { name: key, format: "string", value: key } }, //enum and literal will conflict ...objectPropertySchema?.args?.properties[discriminator.propertyName] || [] ].filter((item) => !chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.enum)) } } } ] }; }) }; } /** * This is the very core of the OpenAPI to TS conversion - it takes a * schema and returns the appropriate type. */ #parseSchemaObject({ schemaObject: _schemaObject, name, parentName }) { const { schemaObject, version } = this.#getParsedSchemaObject(_schemaObject); const options = this.#getOptions({ schemaObject, name }); const emptyType = this.#getEmptyType({ schemaObject, name }); if (!schemaObject) { return [{ keyword: emptyType }]; } const baseItems = [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.schema, args: { type: schemaObject.type, format: schemaObject.format } } ]; const min = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0; const max = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0; const nullable = oas.isNullable(schemaObject); const defaultNullAndNullable = schemaObject.default === null && nullable; if (schemaObject.default !== void 0 && !defaultNullAndNullable && !Array.isArray(schemaObject.default)) { if (typeof schemaObject.default === "string") { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.default, args: transformers__default.default.stringify(schemaObject.default) }); } else if (typeof schemaObject.default === "boolean") { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.default, args: schemaObject.default ?? false }); } else { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.default, args: schemaObject.default }); } } if (schemaObject.deprecated) { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.deprecated }); } if (schemaObject.description) { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.describe, args: schemaObject.description }); } if (max !== void 0) { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.max, args: max }); } if (min !== void 0) { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.min, args: min }); } if (nullable) { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.nullable }); } if (schemaObject.type && Array.isArray(schemaObject.type)) { const items = schemaObject.type.filter((value) => value !== "null"); const hasNull = schemaObject.type.includes("null"); if (hasNull && !nullable) { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.nullable }); } if (items.length > 1) { const parsedItems = [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.union, args: items.map( (item) => this.parse({ schemaObject: { ...schemaObject, type: item }, name, parentName })[0] ).filter(Boolean).filter((item) => !chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.unknown)).map((item) => chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.object) ? { ...item, args: { ...item.args, strict: true } } : item) } ]; return [...parsedItems, ...baseItems].filter(Boolean); } } if (schemaObject.readOnly) { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.readOnly }); } if (schemaObject.writeOnly) { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.writeOnly }); } if (oas.isReference(schemaObject)) { return [ ...this.#getRefAlias(schemaObject, name), schemaObject.description && { keyword: chunkYWMMI3MO_cjs.schemaKeywords.describe, args: schemaObject.description }, nullable && { keyword: chunkYWMMI3MO_cjs.schemaKeywords.nullable }, schemaObject.readOnly && { keyword: chunkYWMMI3MO_cjs.schemaKeywords.readOnly }, schemaObject.writeOnly && { keyword: chunkYWMMI3MO_cjs.schemaKeywords.writeOnly }, { keyword: chunkYWMMI3MO_cjs.schemaKeywords.schema, args: { type: schemaObject.type, format: schemaObject.format } } ].filter(Boolean); } if (schemaObject.oneOf || schemaObject.anyOf) { const schemaWithoutOneOf = { ...schemaObject, oneOf: void 0, anyOf: void 0 }; const discriminator = this.context.oas.getDiscriminator(schemaObject); const union = { keyword: chunkYWMMI3MO_cjs.schemaKeywords.union, args: (schemaObject.oneOf || schemaObject.anyOf).map((item) => { return item && this.parse({ schemaObject: item, name, parentName })[0]; }).filter(Boolean).filter((item) => !chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.unknown)) }; if (discriminator) { if (this.context) return [this.#addDiscriminatorToSchema({ schemaObject: schemaWithoutOneOf, schema: union, discriminator }), ...baseItems]; } if (schemaWithoutOneOf.properties) { const propertySchemas = this.parse({ schemaObject: schemaWithoutOneOf, name, parentName }); union.args = [ ...union.args.map((arg) => { return { keyword: chunkYWMMI3MO_cjs.schemaKeywords.and, args: [arg, ...propertySchemas] }; }) ]; return [union, ...baseItems]; } return [union, ...baseItems]; } if (schemaObject.allOf) { const schemaWithoutAllOf = { ...schemaObject, allOf: void 0 }; const and = { keyword: chunkYWMMI3MO_cjs.schemaKeywords.and, args: schemaObject.allOf.map((item) => { return item && this.parse({ schemaObject: item, name, parentName })[0]; }).filter(Boolean).filter((item) => !chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.unknown)) }; if (schemaWithoutAllOf.required?.length) { const allOfItems = schemaObject.allOf; const resolvedSchemas = []; for (const item of allOfItems) { const resolved = oas.isReference(item) ? this.context.oas.get(item.$ref) : item; if (resolved) { resolvedSchemas.push(resolved); } } const existingKeys = schemaWithoutAllOf.properties ? new Set(Object.keys(schemaWithoutAllOf.properties)) : null; const parsedItems = []; for (const key of schemaWithoutAllOf.required) { if (existingKeys?.has(key)) { continue; } for (const schema of resolvedSchemas) { if (schema.properties?.[key]) { parsedItems.push({ properties: { [key]: schema.properties[key] }, required: [key] }); break; } } } for (const item of parsedItems) { const parsed = this.parse({ schemaObject: item, name, parentName }); if (Array.isArray(parsed)) { and.args = and.args ? and.args.concat(parsed) : parsed; } } } if (schemaWithoutAllOf.properties) { and.args = [...and.args || [], ...this.parse({ schemaObject: schemaWithoutAllOf, name, parentName })]; } return _SchemaGenerator.combineObjects([and, ...baseItems]); } if (schemaObject.enum) { if (options.enumSuffix === "") { this.context.pluginManager.logger.emit("info", "EnumSuffix set to an empty string does not work"); } const enumName = utils.getUniqueName(transformers.pascalCase([parentName, name, options.enumSuffix].join(" ")), this.#getUsedEnumNames({ schemaObject, name })); const typeName = this.context.pluginManager.resolveName({ name: enumName, pluginKey: this.context.plugin.key, type: "type" }); const nullableEnum = schemaObject.enum.includes(null); if (nullableEnum) { baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.nullable }); } const filteredValues = schemaObject.enum.filter((value) => value !== null); const extensionEnums = ["x-enumNames", "x-enum-varnames"].filter((extensionKey) => extensionKey in schemaObject).map((extensionKey) => { return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.enum, args: { name, typeName, asConst: false, items: [...new Set(schemaObject[extensionKey])].map((name2, index) => ({ name: transformers__default.default.stringify(name2), value: schemaObject.enum?.[index], format: remeda.isNumber(schemaObject.enum?.[index]) ? "number" : "string" })) } }, ...baseItems.filter( (item) => item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.min && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.max && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.matches ) ]; }); if (schemaObject.type === "number" || schemaObject.type === "integer") { const enumNames = extensionEnums[0]?.find((item) => chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.enum)); return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.enum, args: { name: enumName, typeName, asConst: true, items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({ name: name2, value, format: "number" })) : [...new Set(filteredValues)].map((value) => { return { name: value, value, format: "number" }; }) } }, ...baseItems.filter((item) => item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.min && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.max && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.matches) ]; } if (schemaObject.type === "boolean") { const enumNames = extensionEnums[0]?.find((item) => chunkYWMMI3MO_cjs.isKeyword(item, chunkYWMMI3MO_cjs.schemaKeywords.enum)); return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.enum, args: { name: enumName, typeName, asConst: true, items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({ name: name2, value, format: "boolean" })) : [...new Set(filteredValues)].map((value) => { return { name: value, value, format: "boolean" }; }) } }, ...baseItems.filter((item) => item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.matches) ]; } if (extensionEnums.length > 0 && extensionEnums[0]) { return extensionEnums[0]; } return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.enum, args: { name: enumName, typeName, asConst: false, items: [...new Set(filteredValues)].map((value) => ({ name: transformers__default.default.stringify(value), value, format: remeda.isNumber(value) ? "number" : "string" })) } }, ...baseItems.filter((item) => item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.min && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.max && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.matches) ]; } if ("prefixItems" in schemaObject) { const prefixItems = schemaObject.prefixItems; const items = "items" in schemaObject ? schemaObject.items : []; const min2 = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0; const max2 = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0; return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.tuple, args: { min: min2, max: max2, items: prefixItems.map((item) => { return this.parse({ schemaObject: item, name, parentName })[0]; }).filter(Boolean), rest: this.parse({ schemaObject: items, name, parentName })[0] } }, ...baseItems.filter((item) => item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.min && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.max) ]; } if (version === "3.1" && "const" in schemaObject) { if (schemaObject["const"] === null) { return [{ keyword: chunkYWMMI3MO_cjs.schemaKeywords.null }]; } if (schemaObject["const"] === void 0) { return [{ keyword: chunkYWMMI3MO_cjs.schemaKeywords.undefined }]; } let format = typeof schemaObject["const"]; if (format !== "number" && format !== "boolean") { format = "string"; } return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.const, args: { name: schemaObject["const"], format, value: schemaObject["const"] } }, ...baseItems ]; } if (schemaObject.format) { if (schemaObject.type === "integer" && (schemaObject.format === "int32" || schemaObject.format === "int64")) { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.integer }); return baseItems; } if (schemaObject.type === "number" && (schemaObject.format === "float" || schemaObject.format === "double")) { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.number }); return baseItems; } switch (schemaObject.format) { case "binary": baseItems.push({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.blob }); return baseItems; case "date-time": if (options.dateType) { if (options.dateType === "date") { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.date, args: { type: "date" } }); return baseItems; } if (options.dateType === "stringOffset") { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.datetime, args: { offset: true } }); return baseItems; } if (options.dateType === "stringLocal") { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.datetime, args: { local: true } }); return baseItems; } baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.datetime, args: { offset: false } }); return baseItems; } break; case "date": if (options.dateType) { if (options.dateType === "date") { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.date, args: { type: "date" } }); return baseItems; } baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.date, args: { type: "string" } }); return baseItems; } break; case "time": if (options.dateType) { if (options.dateType === "date") { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.time, args: { type: "date" } }); return baseItems; } baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.time, args: { type: "string" } }); return baseItems; } break; case "uuid": baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.uuid }); return baseItems; case "email": case "idn-email": baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.email }); return baseItems; case "uri": case "ipv4": case "ipv6": case "uri-reference": case "hostname": case "idn-hostname": baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.url }); return baseItems; } } if (schemaObject.pattern && schemaObject.type === "string") { baseItems.unshift({ keyword: chunkYWMMI3MO_cjs.schemaKeywords.matches, args: schemaObject.pattern }); return baseItems; } if ("items" in schemaObject || schemaObject.type === "array") { const min2 = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0; const max2 = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0; const items = this.parse({ schemaObject: "items" in schemaObject ? schemaObject.items : [], name, parentName }); const unique = !!schemaObject.uniqueItems; return [ { keyword: chunkYWMMI3MO_cjs.schemaKeywords.array, args: { items, min: min2, max: max2, unique } }, ...baseItems.filter((item) => item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.min && item.keyword !== chunkYWMMI3MO_cjs.schemaKeywords.max) ]; } if (schemaObject.properties || schemaObject.additionalProperties) { if (oas.isDiscriminator(schemaObject)) { const schemaObjectOverriden = Object.keys(schemaObject.properties || {}).reduce((acc, propertyName) => { if (acc.properties?.[propertyName] && propertyName === schemaObject.discriminator.propertyName) { return { ...acc, properties: { ...acc.properties, [propertyName]: { ...acc.properties[propertyName] || {}, enum: schemaObject.discriminator.mapping ? Object.keys(schemaObject.discriminator.mapping) : void 0 } } }; } return acc; }, schemaObject || {}); return [ ...this.#parseProperties({ schemaObject: schemaObjectOverriden, name }), ...baseItems ]; } return [...this.#parseProperties({ schemaObject, name }), ...baseItems]; } if (schemaObject.type) { const type = Array.isArray(schemaObject.type) ? schemaObject.type.filter((item) => item !== "null")[0] : schemaObject.type; if (!["boolean", "object", "number", "string", "integer", "null"].includes(type)) { this.context.pluginManager.logger.emit("warning", `Schema type '${schemaObject.type}' is not valid for schema ${parentName}.${name}`); } return [{ keyword: type }, ...baseItems]; } return [{ keyword: emptyType }]; } async build(...generators) { const { oas, contentType, include } = this.context; const schemas = chunkZVFL3NXX_cjs.getSchemas({ oas, contentType, includes: include }); const schemaEntries = Object.entries(schemas); const generatorLimit = pLimit__default.default(1); const schemaLimit = pLimit__default.default(10); const writeTasks = generators.map( (generator) => generatorLimit(async () => { const schemaTasks = schemaEntries.map( ([name, schemaObject]) => schemaLimit(async () => { const options = this.#getOptions({ name }); const tree = this.parse({ name, schemaObject }); const result = await generator.schema?.({ instance: this, schema: { name, value: schemaObject, tree }, options: { ...this.options, ...options } }); return result ?? []; }) ); const schemaResults = await Promise.all(schemaTasks); return schemaResults.flat(); }) ); const nestedResults = await Promise.all(writeTasks); return nestedResults.flat(); } }; exports.SchemaGenerator = SchemaGenerator; //# sourceMappingURL=chunk-NFLZLRQS.cjs.map //# sourceMappingURL=chunk-NFLZLRQS.cjs.map