@kubb/adapter-oas 5.18.5 → 5.18.6

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
@@ -1,5 +1,5 @@
1
1
  import { defineExtension } from "@kubb/core";
2
- import { Diagnostics, ast, childName, enumPrimitive, enumPropName, extractRefName, macroDedupeUnion, macroDiscriminatorEnum, macroEnumName, mergeAdjacentObjectsLazy } from "@kubb/kit";
2
+ import { Diagnostics, ast, childName, enumPrimitive, enumPropName, macroDedupeUnion, macroDiscriminatorEnum, macroEnumName, mergeAdjacentObjectsLazy } from "@kubb/kit";
3
3
  import { dirname, join, resolve } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { access, readFile } from "node:fs/promises";
@@ -20,7 +20,7 @@ var compatibility = {
20
20
  "node": ">=22.22"
21
21
  };
22
22
  // package.json
23
- var version = "5.18.5";
23
+ var version = "5.18.6";
24
24
  // src/constants.ts
25
25
  /**
26
26
  * Default parser options applied when no explicit options are provided.
@@ -60,17 +60,6 @@ const SUPPORTED_METHODS = /* @__PURE__ */ new Set([
60
60
  "query"
61
61
  ]);
62
62
  /**
63
- * Media types OpenAPI 3.2 reads as a stream of items (its "sequential media types"), each described by `itemSchema`.
64
- */
65
- const STREAM_MEDIA_TYPES = /* @__PURE__ */ new Set([
66
- "application/jsonl",
67
- "application/x-ndjson",
68
- "application/json-seq",
69
- "application/geo+json-seq",
70
- "text/event-stream",
71
- "multipart/mixed"
72
- ]);
73
- /**
74
63
  * Set of JSON Schema keywords that prevent a schema fragment from being inlined during `allOf` flattening.
75
64
  *
76
65
  * A fragment that contains any of these keys carries structural meaning of its own and must stay as a separate
@@ -167,21 +156,21 @@ const enumDescriptionKeys = ["x-enumDescriptions", "x-enum-descriptions"];
167
156
  function buildDiscriminatorChildMap(schemas) {
168
157
  const childMap = /* @__PURE__ */ new Map();
169
158
  for (const schema of schemas) {
170
- const unionNode = ast.narrowSchema(schema, "union") ?? ast.narrowSchema(schema, "intersection")?.members?.map((member) => ast.narrowSchema(member, "union")).find((union) => union);
159
+ const unionNode = ast.schema.narrow(schema, "union") ?? ast.schema.narrow(schema, "intersection")?.members?.map((member) => ast.schema.narrow(member, "union")).find((union) => union);
171
160
  if (!unionNode?.discriminatorPropertyName || !unionNode.members) continue;
172
161
  const { discriminatorPropertyName, members } = unionNode;
173
162
  for (const member of members) {
174
- const intersectionNode = ast.narrowSchema(member, "intersection");
163
+ const intersectionNode = ast.schema.narrow(member, "intersection");
175
164
  if (!intersectionNode?.members) continue;
176
165
  let refNode = null;
177
166
  let objNode = null;
178
167
  for (const m of intersectionNode.members) {
179
- refNode ??= ast.narrowSchema(m, "ref");
180
- objNode ??= ast.narrowSchema(m, "object");
168
+ refNode ??= ast.schema.narrow(m, "ref");
169
+ objNode ??= ast.schema.narrow(m, "object");
181
170
  }
182
171
  if (!refNode?.name || !objNode) continue;
183
172
  const prop = objNode.properties.find((p) => p.name === discriminatorPropertyName);
184
- const enumNode = prop ? ast.narrowSchema(prop.schema, "enum") : null;
173
+ const enumNode = prop ? ast.schema.narrow(prop.schema, "enum") : null;
185
174
  if (!enumNode?.enumValues?.length) continue;
186
175
  const enumValues = enumNode.enumValues.filter((v) => v !== null);
187
176
  if (!enumValues.length) continue;
@@ -216,7 +205,7 @@ function patchDiscriminatorNode(node, entry) {
216
205
  required: true,
217
206
  schema: enumSchema
218
207
  });
219
- const objectNode = ast.narrowSchema(node, "object");
208
+ const objectNode = ast.schema.narrow(node, "object");
220
209
  if (objectNode) {
221
210
  const existingIdx = objectNode.properties.findIndex((p) => p.name === propertyName);
222
211
  const newProperties = existingIdx >= 0 ? objectNode.properties.map((p, i) => i === existingIdx ? newProp : p) : [...objectNode.properties, newProp];
@@ -225,14 +214,14 @@ function patchDiscriminatorNode(node, entry) {
225
214
  properties: newProperties
226
215
  };
227
216
  }
228
- const intersectionNode = ast.narrowSchema(node, "intersection");
217
+ const intersectionNode = ast.schema.narrow(node, "intersection");
229
218
  if (!intersectionNode?.members) return node;
230
219
  const discriminantNode = ast.factory.createSchema({
231
220
  type: "object",
232
221
  primitive: "object",
233
222
  properties: [newProp]
234
223
  });
235
- const patchedIdx = intersectionNode.members.findIndex((member) => ast.narrowSchema(member, "object")?.properties.some((p) => p.name === propertyName));
224
+ const patchedIdx = intersectionNode.members.findIndex((member) => ast.schema.narrow(member, "object")?.properties.some((p) => p.name === propertyName));
236
225
  const newMembers = patchedIdx >= 0 ? intersectionNode.members.map((member, i) => i === patchedIdx ? patchDiscriminatorNode(member, entry) : member) : [...intersectionNode.members, discriminantNode];
237
226
  return {
238
227
  ...intersectionNode,
@@ -1455,22 +1444,16 @@ function toInputTags(tags) {
1455
1444
  }
1456
1445
  // src/model/mediaType.ts
1457
1446
  /**
1458
- * The media type without its parameters, so `text/event-stream; charset=utf-8` matches the bare name.
1459
- */
1460
- function baseMediaType(contentType) {
1461
- return contentType.split(";")[0].trim().toLowerCase();
1462
- }
1463
- /**
1464
- * Returns `true` for a media type read as a stream of items: one that OpenAPI 3.2 lists, or any that declares `itemSchema`.
1447
+ * Returns `true` for a media type read as a stream of items: one `ast.mediaType.isStream` accepts, or any that declares `itemSchema`.
1465
1448
  */
1466
1449
  function isStream(contentType, mediaType) {
1467
- return STREAM_MEDIA_TYPES.has(baseMediaType(contentType)) || mediaType?.itemSchema !== void 0;
1450
+ return ast.mediaType.isStream(contentType) || mediaType?.itemSchema !== void 0;
1468
1451
  }
1469
1452
  /**
1470
1453
  * Whether the media type is `text/event-stream`, with or without parameters.
1471
1454
  */
1472
1455
  function isEventStream(contentType) {
1473
- return baseMediaType(contentType) === "text/event-stream";
1456
+ return ast.mediaType.getBase(contentType) === "text/event-stream";
1474
1457
  }
1475
1458
  /**
1476
1459
  * Replaces a `data` string that holds JSON with its `contentSchema`, so the event reads as parsed.
@@ -1760,10 +1743,10 @@ function memberValues(node, propertyName, seen = /* @__PURE__ */ new Set()) {
1760
1743
  seen.add(node);
1761
1744
  const target = node.type === "ref" && node.schema ? node.schema : node;
1762
1745
  if (target.type === "union") return [...new Set(target.members?.flatMap((member) => memberValues(member, propertyName, seen)))];
1763
- return intersect(ast.resolveSchemaProperties({
1746
+ return intersect(ast.schema.findProperties({
1764
1747
  node,
1765
1748
  propertyName
1766
- }).map((property) => [...ast.getSchemaLiteralValues(property.schema)]));
1749
+ }).map((property) => [...ast.schema.getLiterals(property.schema)]));
1767
1750
  }
1768
1751
  function intersect(slices) {
1769
1752
  const pinned = slices.filter((values) => values.length > 0);
@@ -2031,6 +2014,25 @@ function parseParameter({ param, pointer, parentName, options, parseSchema, refs
2031
2014
  ...param.allowEmptyValue && { allowEmptyValue: true }
2032
2015
  });
2033
2016
  }
2017
+ function spreadQuerystring(param) {
2018
+ if (param.in !== "querystring") return [param];
2019
+ const schema = ast.ref.getSchema(param.schema);
2020
+ const formEncoded = !param.contentType || ast.mediaType.getBase(param.contentType) === "application/x-www-form-urlencoded";
2021
+ if (schema.type !== "object" || !formEncoded) return [param];
2022
+ return schema.properties.map((property) => {
2023
+ const { style, explode, allowReserved } = param.encoding?.[property.name] ?? {};
2024
+ return ast.factory.createParameter({
2025
+ ...param.pointer ? { pointer: param.pointer } : {},
2026
+ name: property.name,
2027
+ in: "query",
2028
+ schema: property.schema,
2029
+ required: param.required && property.required,
2030
+ ...style !== void 0 && { style },
2031
+ ...explode !== void 0 && { explode },
2032
+ ...allowReserved !== void 0 && { allowReserved }
2033
+ });
2034
+ });
2035
+ }
2034
2036
  /**
2035
2037
  * Converts an OAS `Operation` into an `OperationNode`.
2036
2038
  */
@@ -2048,7 +2050,7 @@ function parseOperation({ operation, ...ctx }) {
2048
2050
  refs,
2049
2051
  ...entry,
2050
2052
  parentName: operationName
2051
- }));
2053
+ })).flatMap(spreadQuerystring);
2052
2054
  const body = getRequestBody({
2053
2055
  operation,
2054
2056
  refs
@@ -2068,7 +2070,7 @@ function parseOperation({ operation, ...ctx }) {
2068
2070
  contentType: ct,
2069
2071
  refs
2070
2072
  });
2071
- const node = ast.optionality(parseSchema({
2073
+ const node = ast.schema.applyOptionality(parseSchema({
2072
2074
  schema,
2073
2075
  name: requestBodyName
2074
2076
  }), bodyRequired);
@@ -2361,7 +2363,7 @@ function isObjectLike(member, refs) {
2361
2363
  */
2362
2364
  function narrowUnionMembers({ unionMembers, discriminator, sharedPropertiesNode, parentType, parse, name, refs }) {
2363
2365
  function pickDiscriminatorPropertyNode(node, propertyName) {
2364
- const discriminatorProperty = ast.narrowSchema(node, "object")?.properties?.find((property) => property.name === propertyName);
2366
+ const discriminatorProperty = ast.schema.narrow(node, "object")?.properties?.find((property) => property.name === propertyName);
2365
2367
  if (!discriminatorProperty) return null;
2366
2368
  return ast.factory.createSchema({
2367
2369
  type: "object",
@@ -2371,7 +2373,7 @@ function narrowUnionMembers({ unionMembers, discriminator, sharedPropertiesNode,
2371
2373
  }
2372
2374
  function implicitDiscriminantValue(member) {
2373
2375
  if (!discriminator || discriminator.mapping || !isReference(member)) return null;
2374
- const value = extractRefName(member.$ref);
2376
+ const value = ast.ref.getName(member.$ref);
2375
2377
  if (!value) return null;
2376
2378
  const variant = refs.resolve(member.$ref, { report: false });
2377
2379
  if (!variant) return null;
@@ -2419,7 +2421,7 @@ function narrowUnionMembers({ unionMembers, discriminator, sharedPropertiesNode,
2419
2421
  name
2420
2422
  });
2421
2423
  if (!discriminatorValues.length || !discriminator || !isObjectLike(s, refs)) return memberNode;
2422
- const narrowedDiscriminatorNode = sharedPropertiesNode ? pickDiscriminatorPropertyNode(ast.applyMacros(sharedPropertiesNode, [macroDiscriminatorEnum({
2424
+ const narrowedDiscriminatorNode = sharedPropertiesNode ? pickDiscriminatorPropertyNode(ast.macro.apply(sharedPropertiesNode, [macroDiscriminatorEnum({
2423
2425
  propertyName: discriminator.propertyName,
2424
2426
  values: discriminatorValues
2425
2427
  })], { depth: "shallow" }), discriminator.propertyName) : void 0;
@@ -2611,7 +2613,7 @@ function resolveUnionSchema(schema, siblingProperties) {
2611
2613
  *
2612
2614
  * The resolved schema is stored in `node.schema`. Usage-site sibling fields
2613
2615
  * (description, readOnly, nullable, etc.) are stored directly on the ref node.
2614
- * Use `syncSchemaRef(node)` in printers to get a merged view of both.
2616
+ * Use `ast.ref.mergeSchema(node)` in printers to get a merged view of both.
2615
2617
  * Circular refs are detected in `refs.resolveNode` and leave `schema` as `null`.
2616
2618
  */
2617
2619
  function convertRef({ schema, name, nullable, defaultValue, document, parse, refs, renames }) {
@@ -2623,7 +2625,7 @@ function convertRef({ schema, name, nullable, defaultValue, document, parse, ref
2623
2625
  nullable,
2624
2626
  defaultValue
2625
2627
  };
2626
- if (refPath && document.components && !refs.resolve(refPath, { report: false })) return createNode(ctx, { type: ast.schemaTypes.unknown });
2628
+ if (refPath && document.components && !refs.resolve(refPath, { report: false })) return createNode(ctx, { type: ast.schema.types.unknown });
2627
2629
  if (refPath && !refPath.startsWith("#/components/schemas/") && resolvedSchema) return ast.factory.createSchema({
2628
2630
  ...resolvedSchema,
2629
2631
  title: schema.title ?? resolvedSchema.title,
@@ -2637,8 +2639,8 @@ function convertRef({ schema, name, nullable, defaultValue, document, parse, ref
2637
2639
  });
2638
2640
  const targetName = renames?.get(schema.$ref);
2639
2641
  return createNode(ctx, {
2640
- type: ast.schemaTypes.ref,
2641
- name: extractRefName(schema.$ref),
2642
+ type: ast.schema.types.ref,
2643
+ name: ast.ref.getName(schema.$ref),
2642
2644
  ref: schema.$ref,
2643
2645
  ...targetName ? { targetName } : {},
2644
2646
  schema: resolvedSchema
@@ -2649,7 +2651,7 @@ function convertNot({ schema, name, nullable, defaultValue, parse }) {
2649
2651
  const { not, ...rest } = schema;
2650
2652
  const [subschema] = resolveUnionMembers([not], rest.properties ?? {});
2651
2653
  const notNode = ast.factory.createSchema({
2652
- type: ast.schemaTypes.not,
2654
+ type: ast.schema.types.not,
2653
2655
  members: [parse({ schema: subschema })]
2654
2656
  });
2655
2657
  return createNode({
@@ -2658,7 +2660,7 @@ function convertNot({ schema, name, nullable, defaultValue, parse }) {
2658
2660
  nullable,
2659
2661
  defaultValue
2660
2662
  }, {
2661
- type: ast.schemaTypes.intersection,
2663
+ type: ast.schema.types.intersection,
2662
2664
  members: [parse({
2663
2665
  schema: rest,
2664
2666
  name
@@ -2786,7 +2788,7 @@ function convertAllOf({ schema: original, name, nullable, defaultValue, parse, r
2786
2788
  nullable,
2787
2789
  defaultValue
2788
2790
  }, {
2789
- type: ast.schemaTypes.intersection,
2791
+ type: ast.schema.types.intersection,
2790
2792
  members: [...mergeAdjacentObjectsLazy(allOfMembers.slice(0, syntheticStart)), ...mergeAdjacentObjectsLazy(allOfMembers.slice(syntheticStart))]
2791
2793
  });
2792
2794
  }
@@ -2830,24 +2832,24 @@ function convertUnion(context) {
2830
2832
  refs
2831
2833
  });
2832
2834
  const unionNode = createNode(ctx, {
2833
- type: ast.schemaTypes.union,
2835
+ type: ast.schema.types.union,
2834
2836
  strategy,
2835
2837
  members,
2836
- discriminatorPropertyName: explicitDiscriminatorPropertyName ?? ast.inferDiscriminatorPropertyName(members)
2838
+ discriminatorPropertyName: explicitDiscriminatorPropertyName ?? ast.schema.inferDiscriminator(members)
2837
2839
  });
2838
2840
  const requiredProperties = missingRequired.map((name) => ast.factory.createProperty({
2839
2841
  name,
2840
2842
  required: true,
2841
- schema: ast.factory.createSchema({ type: ast.schemaTypes.unknown })
2843
+ schema: ast.factory.createSchema({ type: ast.schema.types.unknown })
2842
2844
  }));
2843
2845
  const siblings = sharedPropertiesNode ? [sharedPropertiesNode] : [];
2844
2846
  if (requiredProperties.length) siblings.push(ast.factory.createSchema({
2845
- type: ast.schemaTypes.object,
2847
+ type: ast.schema.types.object,
2846
2848
  properties: requiredProperties
2847
2849
  }));
2848
2850
  if (!siblings.length) return unionNode;
2849
2851
  return createNode(ctx, {
2850
- type: ast.schemaTypes.intersection,
2852
+ type: ast.schema.types.intersection,
2851
2853
  members: [unionNode, ...siblings]
2852
2854
  });
2853
2855
  }
@@ -2856,12 +2858,12 @@ function convertUnion(context) {
2856
2858
  name
2857
2859
  }));
2858
2860
  const unionNode = createNode(ctx, {
2859
- type: ast.schemaTypes.union,
2861
+ type: ast.schema.types.union,
2860
2862
  strategy,
2861
2863
  members,
2862
- discriminatorPropertyName: explicitDiscriminatorPropertyName ?? ast.inferDiscriminatorPropertyName(members)
2864
+ discriminatorPropertyName: explicitDiscriminatorPropertyName ?? ast.schema.inferDiscriminator(members)
2863
2865
  });
2864
- return ast.applyMacros(unionNode, [macroDedupeUnion], { depth: "shallow" });
2866
+ return ast.macro.apply(unionNode, [macroDedupeUnion], { depth: "shallow" });
2865
2867
  }
2866
2868
  /**
2867
2869
  * Converts an OAS 3.1 multi-type array (e.g. `type: ['string', 'number']`) into a `UnionSchemaNode`.
@@ -2878,7 +2880,7 @@ function convertMultiType({ schema, name, nullable, defaultValue, parse }) {
2878
2880
  nullable: types.includes("null") || nullable || void 0,
2879
2881
  defaultValue
2880
2882
  }, {
2881
- type: ast.schemaTypes.union,
2883
+ type: ast.schema.types.union,
2882
2884
  members: nonNullTypes.map((t) => {
2883
2885
  return parse({
2884
2886
  schema: {
@@ -3157,10 +3159,10 @@ function resolveMapSchema(mapSchema, { options, parse }) {
3157
3159
  */
3158
3160
  function nameEnums(node, options) {
3159
3161
  const macro = macroEnumName(options);
3160
- const named = ast.applyMacros(node, [macro], { depth: "shallow" });
3161
- const tupleNode = ast.narrowSchema(named, "tuple");
3162
+ const named = ast.macro.apply(node, [macro], { depth: "shallow" });
3163
+ const tupleNode = ast.schema.narrow(named, "tuple");
3162
3164
  if (tupleNode?.items) {
3163
- const namedItems = tupleNode.items.map((item) => ast.applyMacros(item, [macro], { depth: "shallow" }));
3165
+ const namedItems = tupleNode.items.map((item) => ast.macro.apply(item, [macro], { depth: "shallow" }));
3164
3166
  if (namedItems.some((item, i) => item !== tupleNode.items[i])) return {
3165
3167
  ...tupleNode,
3166
3168
  items: namedItems
@@ -3243,7 +3245,7 @@ function convertObject({ schema, name, nullable, defaultValue, options, parse, r
3243
3245
  })
3244
3246
  });
3245
3247
  const enumName = name ? enumPropName(name, discPropName, options.enumSuffix) : void 0;
3246
- return ast.applyMacros(objectNode, [macroDiscriminatorEnum({
3248
+ return ast.macro.apply(objectNode, [macroDiscriminatorEnum({
3247
3249
  propertyName: discPropName,
3248
3250
  values,
3249
3251
  enumName
@@ -3487,9 +3489,9 @@ function createSchemaParser(ctx) {
3487
3489
  }
3488
3490
  const presenceKeys = /* @__PURE__ */ new WeakMap();
3489
3491
  function requireKeys(keys) {
3490
- const unknown = ast.factory.createSchema({ type: ast.schemaTypes.unknown });
3492
+ const unknown = ast.factory.createSchema({ type: ast.schema.types.unknown });
3491
3493
  const node = ast.factory.createSchema({
3492
- type: ast.schemaTypes.object,
3494
+ type: ast.schema.types.object,
3493
3495
  primitive: "object",
3494
3496
  properties: keys.map((name) => ast.factory.createProperty({
3495
3497
  name,
@@ -3569,8 +3571,8 @@ function createSchemaParser(ctx) {
3569
3571
  function collectInlineEnums(roots, topLevelNames) {
3570
3572
  const promoted = /* @__PURE__ */ new Map();
3571
3573
  for (const root of roots) {
3572
- const isSchemaRoot = root.kind === ast.nodeKinds.schema;
3573
- for (const node of ast.collect(root, { schema: (schemaNode) => schemaNode })) {
3574
+ const isSchemaRoot = root.kind === ast.node.kinds.schema;
3575
+ for (const node of ast.node.collect(root, { schema: (schemaNode) => schemaNode })) {
3574
3576
  if (node.type !== "enum" || !node.name) continue;
3575
3577
  if (isSchemaRoot && node === root) continue;
3576
3578
  if (topLevelNames.has(node.name)) continue;
@@ -3589,7 +3591,7 @@ function collectInlineEnums(roots, topLevelNames) {
3589
3591
  */
3590
3592
  function refPromotedEnums(node, promoted) {
3591
3593
  if (promoted.size === 0) return node;
3592
- return ast.transform(node, { schema(schemaNode) {
3594
+ return ast.node.transform(node, { schema(schemaNode) {
3593
3595
  if (schemaNode.type !== "enum" || !schemaNode.name || !promoted.has(schemaNode.name)) return void 0;
3594
3596
  return ast.factory.createSchema({
3595
3597
  type: "ref",
@@ -3852,7 +3854,7 @@ const adapterOas = defineExtension({
3852
3854
  const promotedEnums = enums === "root" ? collectInlineEnums([...parsedByName.values(), ...operationNodes], new Set(Object.keys(schemas))) : null;
3853
3855
  const aliasTargetOf = (name) => {
3854
3856
  const node = refAliasMap.get(name);
3855
- return node?.ref ? ast.refName(node.ref) : node?.name;
3857
+ return node?.ref ? ast.ref.getName(node.ref) : node?.name;
3856
3858
  };
3857
3859
  function isAliasCycle(name) {
3858
3860
  const seen = /* @__PURE__ */ new Set([name]);