@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.js CHANGED
@@ -168,11 +168,14 @@ function buildDiscriminatorChildMap(schemas) {
168
168
  const enumValues = enumNode.enumValues.filter((v) => v !== null);
169
169
  if (!enumValues.length) continue;
170
170
  const existing = childMap.get(refNode.name);
171
- if (existing) existing.enumValues.push(...enumValues);
172
- else childMap.set(refNode.name, {
173
- propertyName: discriminatorPropertyName,
174
- enumValues: [...enumValues]
175
- });
171
+ if (!existing) {
172
+ childMap.set(refNode.name, {
173
+ propertyName: discriminatorPropertyName,
174
+ enumValues: [...enumValues]
175
+ });
176
+ continue;
177
+ }
178
+ existing.enumValues.push(...enumValues);
176
179
  }
177
180
  }
178
181
  return childMap;
@@ -755,8 +758,8 @@ function resolveRef(document, $ref) {
755
758
  const origRef = $ref;
756
759
  $ref = $ref.trim();
757
760
  if ($ref === "") return null;
758
- if ($ref.startsWith("#")) $ref = globalThis.decodeURIComponent($ref.substring(1));
759
- else return null;
761
+ if (!$ref.startsWith("#")) return null;
762
+ $ref = globalThis.decodeURIComponent($ref.substring(1));
760
763
  let docCache = _refCache.get(document);
761
764
  if (!docCache) {
762
765
  docCache = /* @__PURE__ */ new Map();
@@ -987,12 +990,14 @@ function* collectRefs(schema) {
987
990
  }
988
991
  if (schema && typeof schema === "object") for (const key in schema) {
989
992
  const value = schema[key];
990
- if (key === "$ref" && typeof value === "string") {
991
- if (value.startsWith("#/components/schemas/")) {
992
- const name = value.slice(21);
993
- if (name) yield name;
994
- }
995
- } else yield* collectRefs(value);
993
+ if (!(key === "$ref" && typeof value === "string")) {
994
+ yield* collectRefs(value);
995
+ continue;
996
+ }
997
+ if (value.startsWith("#/components/schemas/")) {
998
+ const name = value.slice(21);
999
+ if (name) yield name;
1000
+ }
996
1001
  }
997
1002
  }
998
1003
  /**
@@ -1228,17 +1233,19 @@ function createSchemaParser(ctx) {
1228
1233
  function convertRef({ schema, name, nullable, defaultValue, rawOptions }) {
1229
1234
  let resolvedSchema;
1230
1235
  const refPath = schema.$ref;
1231
- if (refPath && !resolvingRefs.has(refPath)) if (resolvedRefCache.has(refPath)) resolvedSchema = resolvedRefCache.get(refPath);
1232
- else {
1233
- try {
1234
- const referenced = resolveRef(document, refPath);
1235
- if (referenced) {
1236
- resolvingRefs.add(refPath);
1237
- resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1238
- resolvingRefs.delete(refPath);
1239
- }
1240
- } catch {}
1241
- resolvedRefCache.set(refPath, resolvedSchema);
1236
+ if (refPath && !resolvingRefs.has(refPath)) {
1237
+ if (!resolvedRefCache.has(refPath)) {
1238
+ try {
1239
+ const referenced = resolveRef(document, refPath);
1240
+ if (referenced) {
1241
+ resolvingRefs.add(refPath);
1242
+ resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1243
+ resolvingRefs.delete(refPath);
1244
+ }
1245
+ } catch {}
1246
+ resolvedRefCache.set(refPath, resolvedSchema);
1247
+ }
1248
+ resolvedSchema = resolvedRefCache.get(refPath);
1242
1249
  }
1243
1250
  return ast.createSchema({
1244
1251
  ...buildSchemaNode(schema, name, nullable, defaultValue),
@@ -1550,15 +1557,18 @@ function createSchemaParser(ctx) {
1550
1557
  schema: resolvedPropSchema,
1551
1558
  name: ast.childName(name, propName)
1552
1559
  }, rawOptions);
1553
- let schemaNode = ast.setEnumName(propNode, name, propName, options.enumSuffix);
1554
- const tupleNode = ast.narrowSchema(schemaNode, "tuple");
1555
- if (tupleNode?.items) {
1556
- const namedItems = tupleNode.items.map((item) => ast.setEnumName(item, name, propName, options.enumSuffix));
1557
- if (namedItems.some((item, i) => item !== tupleNode.items[i])) schemaNode = {
1558
- ...tupleNode,
1559
- items: namedItems
1560
- };
1561
- }
1560
+ const schemaNode = (() => {
1561
+ const node = ast.setEnumName(propNode, name, propName, options.enumSuffix);
1562
+ const tupleNode = ast.narrowSchema(node, "tuple");
1563
+ if (tupleNode?.items) {
1564
+ const namedItems = tupleNode.items.map((item) => ast.setEnumName(item, name, propName, options.enumSuffix));
1565
+ if (namedItems.some((item, i) => item !== tupleNode.items[i])) return {
1566
+ ...tupleNode,
1567
+ items: namedItems
1568
+ };
1569
+ }
1570
+ return node;
1571
+ })();
1562
1572
  return ast.createProperty({
1563
1573
  name: propName,
1564
1574
  schema: {
@@ -1569,11 +1579,12 @@ function createSchemaParser(ctx) {
1569
1579
  });
1570
1580
  }) : [];
1571
1581
  const additionalProperties = schema.additionalProperties;
1572
- let additionalPropertiesNode;
1573
- if (additionalProperties === true) additionalPropertiesNode = true;
1574
- else if (additionalProperties && Object.keys(additionalProperties).length > 0) additionalPropertiesNode = parseSchema({ schema: additionalProperties }, rawOptions);
1575
- else if (additionalProperties === false) additionalPropertiesNode = false;
1576
- else if (additionalProperties) additionalPropertiesNode = ast.createSchema({ type: typeOptionMap.get(options.unknownType) });
1582
+ const additionalPropertiesNode = (() => {
1583
+ if (additionalProperties === true) return true;
1584
+ if (additionalProperties === false) return false;
1585
+ if (additionalProperties && Object.keys(additionalProperties).length > 0) return parseSchema({ schema: additionalProperties }, rawOptions);
1586
+ if (additionalProperties) return ast.createSchema({ type: typeOptionMap.get(options.unknownType) });
1587
+ })();
1577
1588
  const rawPatternProperties = "patternProperties" in schema ? schema.patternProperties : void 0;
1578
1589
  const patternProperties = rawPatternProperties ? Object.fromEntries(Object.entries(rawPatternProperties).map(([pattern, patternSchema]) => [pattern, patternSchema === true || typeof patternSchema === "object" && Object.keys(patternSchema).length === 0 ? ast.createSchema({ type: typeOptionMap.get(options.unknownType) }) : parseSchema({ schema: patternSchema }, rawOptions)])) : void 0;
1579
1590
  const objectNode = ast.createSchema({
@@ -1954,6 +1965,8 @@ const adapterOas = createAdapter((options) => {
1954
1965
  let nameMapping = /* @__PURE__ */ new Map();
1955
1966
  let parsedDocument = null;
1956
1967
  let schemaObjects = null;
1968
+ let baseOasInstance = null;
1969
+ let schemaParserInstance = null;
1957
1970
  function resolveBaseURL(document) {
1958
1971
  const server = serverIndex !== void 0 ? document.servers?.at(serverIndex) : void 0;
1959
1972
  return server?.url ? resolveServerUrl(server, serverVariables) : void 0;
@@ -1973,6 +1986,17 @@ const adapterOas = createAdapter((options) => {
1973
1986
  }
1974
1987
  return schemaObjects;
1975
1988
  }
1989
+ function ensureBaseOas(document) {
1990
+ if (!baseOasInstance) baseOasInstance = new BaseOas(document);
1991
+ return baseOasInstance;
1992
+ }
1993
+ function ensureSchemaParser(document) {
1994
+ if (!schemaParserInstance) schemaParserInstance = createSchemaParser({
1995
+ document,
1996
+ contentType
1997
+ });
1998
+ return schemaParserInstance;
1999
+ }
1976
2000
  return {
1977
2001
  name: "oas",
1978
2002
  get options() {
@@ -2035,7 +2059,7 @@ const adapterOas = createAdapter((options) => {
2035
2059
  async count(source) {
2036
2060
  const document = await ensureDocument(source);
2037
2061
  const schemas = await ensureSchemas(document);
2038
- const baseOas = new BaseOas(document);
2062
+ const baseOas = ensureBaseOas(document);
2039
2063
  const operationCount = Object.values(baseOas.getPaths()).flatMap(Object.values).filter(Boolean).length;
2040
2064
  return {
2041
2065
  schemas: Object.keys(schemas).length,
@@ -2045,43 +2069,33 @@ const adapterOas = createAdapter((options) => {
2045
2069
  async stream(source) {
2046
2070
  const document = await ensureDocument(source);
2047
2071
  const schemas = await ensureSchemas(document);
2048
- let discriminatorChildMap = null;
2049
- if (discriminator === "inherit") {
2050
- const { parseSchema: _preParser } = createSchemaParser({
2051
- document,
2052
- contentType
2053
- });
2072
+ const discriminatorChildMap = (() => {
2073
+ if (discriminator !== "inherit") return null;
2074
+ const { parseSchema: _preParser } = ensureSchemaParser(document);
2054
2075
  const parentNodes = [];
2055
2076
  for (const [name, schema] of Object.entries(schemas)) if ((schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) parentNodes.push(_preParser({
2056
2077
  schema,
2057
2078
  name
2058
2079
  }, parserOptions));
2059
- if (parentNodes.length > 0) discriminatorChildMap = buildDiscriminatorChildMap(parentNodes);
2060
- }
2080
+ return parentNodes.length > 0 ? buildDiscriminatorChildMap(parentNodes) : null;
2081
+ })();
2061
2082
  const schemasIterable = { [Symbol.asyncIterator]() {
2062
2083
  return (async function* () {
2063
- const { parseSchema: _parseSchema } = createSchemaParser({
2064
- document,
2065
- contentType
2066
- });
2084
+ const { parseSchema: _parseSchema } = ensureSchemaParser(document);
2067
2085
  for (const [name, schema] of Object.entries(schemas)) {
2068
- let node = _parseSchema({
2086
+ const parsedNode = _parseSchema({
2069
2087
  schema,
2070
2088
  name
2071
2089
  }, parserOptions);
2072
2090
  const entry = discriminatorChildMap?.get(name);
2073
- if (entry) node = patchDiscriminatorNode(node, entry);
2074
- yield node;
2091
+ yield entry ? patchDiscriminatorNode(parsedNode, entry) : parsedNode;
2075
2092
  }
2076
2093
  })();
2077
2094
  } };
2078
2095
  const operationsIterable = { [Symbol.asyncIterator]() {
2079
2096
  return (async function* () {
2080
- const { parseOperation: _parseOperation } = createSchemaParser({
2081
- document,
2082
- contentType
2083
- });
2084
- const paths = new BaseOas(document).getPaths();
2097
+ const { parseOperation: _parseOperation } = ensureSchemaParser(document);
2098
+ const paths = ensureBaseOas(document).getPaths();
2085
2099
  for (const methods of Object.values(paths)) for (const operation of Object.values(methods)) {
2086
2100
  if (!operation) continue;
2087
2101
  const node = _parseOperation(parserOptions, operation);