@kubb/adapter-oas 5.0.0-beta.17 → 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
  /**
@@ -1144,7 +1149,8 @@ function buildSchemaNode(schema, name, nullable, defaultValue) {
1144
1149
  readOnly: schema.readOnly,
1145
1150
  writeOnly: schema.writeOnly,
1146
1151
  default: defaultValue,
1147
- example: schema.example
1152
+ example: schema.example,
1153
+ format: schema.format
1148
1154
  };
1149
1155
  }
1150
1156
  /**
@@ -1227,17 +1233,19 @@ function createSchemaParser(ctx) {
1227
1233
  function convertRef({ schema, name, nullable, defaultValue, rawOptions }) {
1228
1234
  let resolvedSchema;
1229
1235
  const refPath = schema.$ref;
1230
- if (refPath && !resolvingRefs.has(refPath)) if (resolvedRefCache.has(refPath)) resolvedSchema = resolvedRefCache.get(refPath);
1231
- else {
1232
- try {
1233
- const referenced = resolveRef(document, refPath);
1234
- if (referenced) {
1235
- resolvingRefs.add(refPath);
1236
- resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1237
- resolvingRefs.delete(refPath);
1238
- }
1239
- } catch {}
1240
- 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);
1241
1249
  }
1242
1250
  return ast.createSchema({
1243
1251
  ...buildSchemaNode(schema, name, nullable, defaultValue),
@@ -1271,7 +1279,8 @@ function createSchemaParser(ctx) {
1271
1279
  writeOnly: schema.writeOnly ?? memberNode.writeOnly,
1272
1280
  default: mergedDefault,
1273
1281
  example: schema.example ?? memberNode.example,
1274
- pattern: schema.pattern ?? ("pattern" in memberNode ? memberNode.pattern : void 0)
1282
+ pattern: schema.pattern ?? ("pattern" in memberNode ? memberNode.pattern : void 0),
1283
+ format: schema.format ?? memberNode.format
1275
1284
  });
1276
1285
  }
1277
1286
  const filteredDiscriminantValues = [];
@@ -1403,7 +1412,8 @@ function createSchemaParser(ctx) {
1403
1412
  name,
1404
1413
  title: schema.title,
1405
1414
  description: schema.description,
1406
- deprecated: schema.deprecated
1415
+ deprecated: schema.deprecated,
1416
+ format: schema.format
1407
1417
  });
1408
1418
  const constPrimitive = getPrimitiveType(typeof constValue === "number" ? "number" : typeof constValue === "boolean" ? "boolean" : "string");
1409
1419
  return ast.createSchema({
@@ -1507,7 +1517,8 @@ function createSchemaParser(ctx) {
1507
1517
  readOnly: schema.readOnly,
1508
1518
  writeOnly: schema.writeOnly,
1509
1519
  default: enumDefault,
1510
- example: schema.example
1520
+ example: schema.example,
1521
+ format: schema.format
1511
1522
  };
1512
1523
  const extensionKey = enumExtensionKeys.find((key) => key in schema);
1513
1524
  if (extensionKey || enumPrimitive === "number" || enumPrimitive === "integer" || enumPrimitive === "boolean") {
@@ -1546,15 +1557,18 @@ function createSchemaParser(ctx) {
1546
1557
  schema: resolvedPropSchema,
1547
1558
  name: ast.childName(name, propName)
1548
1559
  }, rawOptions);
1549
- let schemaNode = ast.setEnumName(propNode, name, propName, options.enumSuffix);
1550
- const tupleNode = ast.narrowSchema(schemaNode, "tuple");
1551
- if (tupleNode?.items) {
1552
- const namedItems = tupleNode.items.map((item) => ast.setEnumName(item, name, propName, options.enumSuffix));
1553
- if (namedItems.some((item, i) => item !== tupleNode.items[i])) schemaNode = {
1554
- ...tupleNode,
1555
- items: namedItems
1556
- };
1557
- }
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
+ })();
1558
1572
  return ast.createProperty({
1559
1573
  name: propName,
1560
1574
  schema: {
@@ -1565,11 +1579,12 @@ function createSchemaParser(ctx) {
1565
1579
  });
1566
1580
  }) : [];
1567
1581
  const additionalProperties = schema.additionalProperties;
1568
- let additionalPropertiesNode;
1569
- if (additionalProperties === true) additionalPropertiesNode = true;
1570
- else if (additionalProperties && Object.keys(additionalProperties).length > 0) additionalPropertiesNode = parseSchema({ schema: additionalProperties }, rawOptions);
1571
- else if (additionalProperties === false) additionalPropertiesNode = false;
1572
- 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
+ })();
1573
1588
  const rawPatternProperties = "patternProperties" in schema ? schema.patternProperties : void 0;
1574
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;
1575
1590
  const objectNode = ast.createSchema({
@@ -1680,7 +1695,8 @@ function createSchemaParser(ctx) {
1680
1695
  title: schema.title,
1681
1696
  description: schema.description,
1682
1697
  deprecated: schema.deprecated,
1683
- nullable
1698
+ nullable,
1699
+ format: schema.format
1684
1700
  });
1685
1701
  }
1686
1702
  /**
@@ -1758,7 +1774,8 @@ function createSchemaParser(ctx) {
1758
1774
  type: emptyType,
1759
1775
  name,
1760
1776
  title: schema.title,
1761
- description: schema.description
1777
+ description: schema.description,
1778
+ format: schema.format
1762
1779
  });
1763
1780
  }
1764
1781
  /**
@@ -1948,6 +1965,8 @@ const adapterOas = createAdapter((options) => {
1948
1965
  let nameMapping = /* @__PURE__ */ new Map();
1949
1966
  let parsedDocument = null;
1950
1967
  let schemaObjects = null;
1968
+ let baseOasInstance = null;
1969
+ let schemaParserInstance = null;
1951
1970
  function resolveBaseURL(document) {
1952
1971
  const server = serverIndex !== void 0 ? document.servers?.at(serverIndex) : void 0;
1953
1972
  return server?.url ? resolveServerUrl(server, serverVariables) : void 0;
@@ -1967,6 +1986,17 @@ const adapterOas = createAdapter((options) => {
1967
1986
  }
1968
1987
  return schemaObjects;
1969
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
+ }
1970
2000
  return {
1971
2001
  name: "oas",
1972
2002
  get options() {
@@ -2029,7 +2059,7 @@ const adapterOas = createAdapter((options) => {
2029
2059
  async count(source) {
2030
2060
  const document = await ensureDocument(source);
2031
2061
  const schemas = await ensureSchemas(document);
2032
- const baseOas = new BaseOas(document);
2062
+ const baseOas = ensureBaseOas(document);
2033
2063
  const operationCount = Object.values(baseOas.getPaths()).flatMap(Object.values).filter(Boolean).length;
2034
2064
  return {
2035
2065
  schemas: Object.keys(schemas).length,
@@ -2039,43 +2069,33 @@ const adapterOas = createAdapter((options) => {
2039
2069
  async stream(source) {
2040
2070
  const document = await ensureDocument(source);
2041
2071
  const schemas = await ensureSchemas(document);
2042
- let discriminatorChildMap = null;
2043
- if (discriminator === "inherit") {
2044
- const { parseSchema: _preParser } = createSchemaParser({
2045
- document,
2046
- contentType
2047
- });
2072
+ const discriminatorChildMap = (() => {
2073
+ if (discriminator !== "inherit") return null;
2074
+ const { parseSchema: _preParser } = ensureSchemaParser(document);
2048
2075
  const parentNodes = [];
2049
2076
  for (const [name, schema] of Object.entries(schemas)) if ((schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) parentNodes.push(_preParser({
2050
2077
  schema,
2051
2078
  name
2052
2079
  }, parserOptions));
2053
- if (parentNodes.length > 0) discriminatorChildMap = buildDiscriminatorChildMap(parentNodes);
2054
- }
2080
+ return parentNodes.length > 0 ? buildDiscriminatorChildMap(parentNodes) : null;
2081
+ })();
2055
2082
  const schemasIterable = { [Symbol.asyncIterator]() {
2056
2083
  return (async function* () {
2057
- const { parseSchema: _parseSchema } = createSchemaParser({
2058
- document,
2059
- contentType
2060
- });
2084
+ const { parseSchema: _parseSchema } = ensureSchemaParser(document);
2061
2085
  for (const [name, schema] of Object.entries(schemas)) {
2062
- let node = _parseSchema({
2086
+ const parsedNode = _parseSchema({
2063
2087
  schema,
2064
2088
  name
2065
2089
  }, parserOptions);
2066
2090
  const entry = discriminatorChildMap?.get(name);
2067
- if (entry) node = patchDiscriminatorNode(node, entry);
2068
- yield node;
2091
+ yield entry ? patchDiscriminatorNode(parsedNode, entry) : parsedNode;
2069
2092
  }
2070
2093
  })();
2071
2094
  } };
2072
2095
  const operationsIterable = { [Symbol.asyncIterator]() {
2073
2096
  return (async function* () {
2074
- const { parseOperation: _parseOperation } = createSchemaParser({
2075
- document,
2076
- contentType
2077
- });
2078
- const paths = new BaseOas(document).getPaths();
2097
+ const { parseOperation: _parseOperation } = ensureSchemaParser(document);
2098
+ const paths = ensureBaseOas(document).getPaths();
2079
2099
  for (const methods of Object.values(paths)) for (const operation of Object.values(methods)) {
2080
2100
  if (!operation) continue;
2081
2101
  const node = _parseOperation(parserOptions, operation);