@kubb/adapter-oas 5.0.0-beta.18 → 5.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -194,11 +194,14 @@ function buildDiscriminatorChildMap(schemas) {
194
194
  const enumValues = enumNode.enumValues.filter((v) => v !== null);
195
195
  if (!enumValues.length) continue;
196
196
  const existing = childMap.get(refNode.name);
197
- if (existing) existing.enumValues.push(...enumValues);
198
- else childMap.set(refNode.name, {
199
- propertyName: discriminatorPropertyName,
200
- enumValues: [...enumValues]
201
- });
197
+ if (!existing) {
198
+ childMap.set(refNode.name, {
199
+ propertyName: discriminatorPropertyName,
200
+ enumValues: [...enumValues]
201
+ });
202
+ continue;
203
+ }
204
+ existing.enumValues.push(...enumValues);
202
205
  }
203
206
  }
204
207
  return childMap;
@@ -781,8 +784,8 @@ function resolveRef(document, $ref) {
781
784
  const origRef = $ref;
782
785
  $ref = $ref.trim();
783
786
  if ($ref === "") return null;
784
- if ($ref.startsWith("#")) $ref = globalThis.decodeURIComponent($ref.substring(1));
785
- else return null;
787
+ if (!$ref.startsWith("#")) return null;
788
+ $ref = globalThis.decodeURIComponent($ref.substring(1));
786
789
  let docCache = _refCache.get(document);
787
790
  if (!docCache) {
788
791
  docCache = /* @__PURE__ */ new Map();
@@ -1013,12 +1016,14 @@ function* collectRefs(schema) {
1013
1016
  }
1014
1017
  if (schema && typeof schema === "object") for (const key in schema) {
1015
1018
  const value = schema[key];
1016
- if (key === "$ref" && typeof value === "string") {
1017
- if (value.startsWith("#/components/schemas/")) {
1018
- const name = value.slice(21);
1019
- if (name) yield name;
1020
- }
1021
- } else yield* collectRefs(value);
1019
+ if (!(key === "$ref" && typeof value === "string")) {
1020
+ yield* collectRefs(value);
1021
+ continue;
1022
+ }
1023
+ if (value.startsWith("#/components/schemas/")) {
1024
+ const name = value.slice(21);
1025
+ if (name) yield name;
1026
+ }
1022
1027
  }
1023
1028
  }
1024
1029
  /**
@@ -1254,17 +1259,19 @@ function createSchemaParser(ctx) {
1254
1259
  function convertRef({ schema, name, nullable, defaultValue, rawOptions }) {
1255
1260
  let resolvedSchema;
1256
1261
  const refPath = schema.$ref;
1257
- if (refPath && !resolvingRefs.has(refPath)) if (resolvedRefCache.has(refPath)) resolvedSchema = resolvedRefCache.get(refPath);
1258
- else {
1259
- try {
1260
- const referenced = resolveRef(document, refPath);
1261
- if (referenced) {
1262
- resolvingRefs.add(refPath);
1263
- resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1264
- resolvingRefs.delete(refPath);
1265
- }
1266
- } catch {}
1267
- resolvedRefCache.set(refPath, resolvedSchema);
1262
+ if (refPath && !resolvingRefs.has(refPath)) {
1263
+ if (!resolvedRefCache.has(refPath)) {
1264
+ try {
1265
+ const referenced = resolveRef(document, refPath);
1266
+ if (referenced) {
1267
+ resolvingRefs.add(refPath);
1268
+ resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1269
+ resolvingRefs.delete(refPath);
1270
+ }
1271
+ } catch {}
1272
+ resolvedRefCache.set(refPath, resolvedSchema);
1273
+ }
1274
+ resolvedSchema = resolvedRefCache.get(refPath);
1268
1275
  }
1269
1276
  return _kubb_core.ast.createSchema({
1270
1277
  ...buildSchemaNode(schema, name, nullable, defaultValue),
@@ -1576,15 +1583,18 @@ function createSchemaParser(ctx) {
1576
1583
  schema: resolvedPropSchema,
1577
1584
  name: _kubb_core.ast.childName(name, propName)
1578
1585
  }, rawOptions);
1579
- let schemaNode = _kubb_core.ast.setEnumName(propNode, name, propName, options.enumSuffix);
1580
- const tupleNode = _kubb_core.ast.narrowSchema(schemaNode, "tuple");
1581
- if (tupleNode?.items) {
1582
- const namedItems = tupleNode.items.map((item) => _kubb_core.ast.setEnumName(item, name, propName, options.enumSuffix));
1583
- if (namedItems.some((item, i) => item !== tupleNode.items[i])) schemaNode = {
1584
- ...tupleNode,
1585
- items: namedItems
1586
- };
1587
- }
1586
+ const schemaNode = (() => {
1587
+ const node = _kubb_core.ast.setEnumName(propNode, name, propName, options.enumSuffix);
1588
+ const tupleNode = _kubb_core.ast.narrowSchema(node, "tuple");
1589
+ if (tupleNode?.items) {
1590
+ const namedItems = tupleNode.items.map((item) => _kubb_core.ast.setEnumName(item, name, propName, options.enumSuffix));
1591
+ if (namedItems.some((item, i) => item !== tupleNode.items[i])) return {
1592
+ ...tupleNode,
1593
+ items: namedItems
1594
+ };
1595
+ }
1596
+ return node;
1597
+ })();
1588
1598
  return _kubb_core.ast.createProperty({
1589
1599
  name: propName,
1590
1600
  schema: {
@@ -1595,11 +1605,12 @@ function createSchemaParser(ctx) {
1595
1605
  });
1596
1606
  }) : [];
1597
1607
  const additionalProperties = schema.additionalProperties;
1598
- let additionalPropertiesNode;
1599
- if (additionalProperties === true) additionalPropertiesNode = true;
1600
- else if (additionalProperties && Object.keys(additionalProperties).length > 0) additionalPropertiesNode = parseSchema({ schema: additionalProperties }, rawOptions);
1601
- else if (additionalProperties === false) additionalPropertiesNode = false;
1602
- else if (additionalProperties) additionalPropertiesNode = _kubb_core.ast.createSchema({ type: typeOptionMap.get(options.unknownType) });
1608
+ const additionalPropertiesNode = (() => {
1609
+ if (additionalProperties === true) return true;
1610
+ if (additionalProperties === false) return false;
1611
+ if (additionalProperties && Object.keys(additionalProperties).length > 0) return parseSchema({ schema: additionalProperties }, rawOptions);
1612
+ if (additionalProperties) return _kubb_core.ast.createSchema({ type: typeOptionMap.get(options.unknownType) });
1613
+ })();
1603
1614
  const rawPatternProperties = "patternProperties" in schema ? schema.patternProperties : void 0;
1604
1615
  const patternProperties = rawPatternProperties ? Object.fromEntries(Object.entries(rawPatternProperties).map(([pattern, patternSchema]) => [pattern, patternSchema === true || typeof patternSchema === "object" && Object.keys(patternSchema).length === 0 ? _kubb_core.ast.createSchema({ type: typeOptionMap.get(options.unknownType) }) : parseSchema({ schema: patternSchema }, rawOptions)])) : void 0;
1605
1616
  const objectNode = _kubb_core.ast.createSchema({
@@ -1980,6 +1991,8 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
1980
1991
  let nameMapping = /* @__PURE__ */ new Map();
1981
1992
  let parsedDocument = null;
1982
1993
  let schemaObjects = null;
1994
+ let baseOasInstance = null;
1995
+ let schemaParserInstance = null;
1983
1996
  function resolveBaseURL(document) {
1984
1997
  const server = serverIndex !== void 0 ? document.servers?.at(serverIndex) : void 0;
1985
1998
  return server?.url ? resolveServerUrl(server, serverVariables) : void 0;
@@ -1999,6 +2012,17 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
1999
2012
  }
2000
2013
  return schemaObjects;
2001
2014
  }
2015
+ function ensureBaseOas(document) {
2016
+ if (!baseOasInstance) baseOasInstance = new oas.default(document);
2017
+ return baseOasInstance;
2018
+ }
2019
+ function ensureSchemaParser(document) {
2020
+ if (!schemaParserInstance) schemaParserInstance = createSchemaParser({
2021
+ document,
2022
+ contentType
2023
+ });
2024
+ return schemaParserInstance;
2025
+ }
2002
2026
  return {
2003
2027
  name: "oas",
2004
2028
  get options() {
@@ -2061,7 +2085,7 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
2061
2085
  async count(source) {
2062
2086
  const document = await ensureDocument(source);
2063
2087
  const schemas = await ensureSchemas(document);
2064
- const baseOas = new oas.default(document);
2088
+ const baseOas = ensureBaseOas(document);
2065
2089
  const operationCount = Object.values(baseOas.getPaths()).flatMap(Object.values).filter(Boolean).length;
2066
2090
  return {
2067
2091
  schemas: Object.keys(schemas).length,
@@ -2071,43 +2095,33 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
2071
2095
  async stream(source) {
2072
2096
  const document = await ensureDocument(source);
2073
2097
  const schemas = await ensureSchemas(document);
2074
- let discriminatorChildMap = null;
2075
- if (discriminator === "inherit") {
2076
- const { parseSchema: _preParser } = createSchemaParser({
2077
- document,
2078
- contentType
2079
- });
2098
+ const discriminatorChildMap = (() => {
2099
+ if (discriminator !== "inherit") return null;
2100
+ const { parseSchema: _preParser } = ensureSchemaParser(document);
2080
2101
  const parentNodes = [];
2081
2102
  for (const [name, schema] of Object.entries(schemas)) if ((schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) parentNodes.push(_preParser({
2082
2103
  schema,
2083
2104
  name
2084
2105
  }, parserOptions));
2085
- if (parentNodes.length > 0) discriminatorChildMap = buildDiscriminatorChildMap(parentNodes);
2086
- }
2106
+ return parentNodes.length > 0 ? buildDiscriminatorChildMap(parentNodes) : null;
2107
+ })();
2087
2108
  const schemasIterable = { [Symbol.asyncIterator]() {
2088
2109
  return (async function* () {
2089
- const { parseSchema: _parseSchema } = createSchemaParser({
2090
- document,
2091
- contentType
2092
- });
2110
+ const { parseSchema: _parseSchema } = ensureSchemaParser(document);
2093
2111
  for (const [name, schema] of Object.entries(schemas)) {
2094
- let node = _parseSchema({
2112
+ const parsedNode = _parseSchema({
2095
2113
  schema,
2096
2114
  name
2097
2115
  }, parserOptions);
2098
2116
  const entry = discriminatorChildMap?.get(name);
2099
- if (entry) node = patchDiscriminatorNode(node, entry);
2100
- yield node;
2117
+ yield entry ? patchDiscriminatorNode(parsedNode, entry) : parsedNode;
2101
2118
  }
2102
2119
  })();
2103
2120
  } };
2104
2121
  const operationsIterable = { [Symbol.asyncIterator]() {
2105
2122
  return (async function* () {
2106
- const { parseOperation: _parseOperation } = createSchemaParser({
2107
- document,
2108
- contentType
2109
- });
2110
- const paths = new oas.default(document).getPaths();
2123
+ const { parseOperation: _parseOperation } = ensureSchemaParser(document);
2124
+ const paths = ensureBaseOas(document).getPaths();
2111
2125
  for (const methods of Object.values(paths)) for (const operation of Object.values(methods)) {
2112
2126
  if (!operation) continue;
2113
2127
  const node = _parseOperation(parserOptions, operation);