@kubb/adapter-oas 5.0.0-alpha.47 → 5.0.0-alpha.49

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/package.json CHANGED
@@ -1,25 +1,35 @@
1
1
  {
2
2
  "name": "@kubb/adapter-oas",
3
- "version": "5.0.0-alpha.47",
3
+ "version": "5.0.0-alpha.49",
4
4
  "description": "OpenAPI / Swagger adapter for Kubb converts OAS input into a @kubb/ast RootNode.",
5
5
  "keywords": [
6
- "openapi",
7
- "swagger",
8
- "oas",
9
6
  "adapter",
10
- "kubb",
11
7
  "codegen",
8
+ "kubb",
9
+ "oas",
10
+ "openapi",
11
+ "swagger",
12
12
  "typescript"
13
13
  ],
14
+ "license": "MIT",
15
+ "author": "stijnvanhulle",
14
16
  "repository": {
15
17
  "type": "git",
16
18
  "url": "git+https://github.com/kubb-labs/kubb.git",
17
19
  "directory": "packages/adapter-oas"
18
20
  },
19
- "license": "MIT",
20
- "author": "stijnvanhulle",
21
- "sideEffects": false,
21
+ "files": [
22
+ "src",
23
+ "dist",
24
+ "!/**/**.test.**",
25
+ "!/**/__tests__/**",
26
+ "!/**/__snapshots__/**"
27
+ ],
22
28
  "type": "module",
29
+ "sideEffects": false,
30
+ "main": "./dist/index.cjs",
31
+ "module": "./dist/index.js",
32
+ "types": "./dist/index.d.ts",
23
33
  "exports": {
24
34
  ".": {
25
35
  "import": "./dist/index.js",
@@ -27,20 +37,16 @@
27
37
  },
28
38
  "./package.json": "./package.json"
29
39
  },
30
- "types": "./dist/index.d.ts",
31
- "files": [
32
- "src",
33
- "dist",
34
- "!/**/**.test.**",
35
- "!/**/__tests__/**",
36
- "!/**/__snapshots__/**"
37
- ],
40
+ "publishConfig": {
41
+ "access": "public",
42
+ "registry": "https://registry.npmjs.org/"
43
+ },
38
44
  "dependencies": {
39
45
  "@redocly/openapi-core": "^2.28.1",
40
46
  "oas": "^32.1.14",
41
47
  "oas-normalize": "^16.0.4",
42
48
  "swagger2openapi": "^7.0.8",
43
- "@kubb/core": "5.0.0-alpha.47"
49
+ "@kubb/core": "5.0.0-alpha.49"
44
50
  },
45
51
  "devDependencies": {
46
52
  "@types/swagger2openapi": "^7.0.4",
@@ -50,20 +56,14 @@
50
56
  "engines": {
51
57
  "node": ">=22"
52
58
  },
53
- "publishConfig": {
54
- "access": "public",
55
- "registry": "https://registry.npmjs.org/"
56
- },
57
- "main": "./dist/index.cjs",
58
- "module": "./dist/index.js",
59
59
  "inlinedDependencies": {
60
60
  "openapi-types": "12.1.3"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "tsdown",
64
64
  "clean": "npx rimraf ./dist",
65
- "lint": "bun biome lint .",
66
- "lint:fix": "bun biome lint --fix --unsafe .",
65
+ "lint": "oxlint .",
66
+ "lint:fix": "oxlint --fix .",
67
67
  "release": "pnpm publish --no-git-check",
68
68
  "release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
69
69
  "start": "tsdown --watch",
@@ -1,6 +1,9 @@
1
1
  import { ast } from '@kubb/core'
2
2
 
3
- type DiscriminatorTarget = { propertyName: string; enumValues: Array<string | number | boolean> }
3
+ type DiscriminatorTarget = {
4
+ propertyName: string
5
+ enumValues: Array<string | number | boolean>
6
+ }
4
7
 
5
8
  /**
6
9
  * Injects discriminator enum values into child schemas so they know which value identifies them.
@@ -68,7 +71,10 @@ export function applyDiscriminatorInheritance(root: ast.InputNode): ast.InputNod
68
71
  if (existing) {
69
72
  existing.enumValues.push(...enumValues)
70
73
  } else {
71
- childMap.set(refNode.name, { propertyName: discriminatorPropertyName, enumValues: [...enumValues] })
74
+ childMap.set(refNode.name, {
75
+ propertyName: discriminatorPropertyName,
76
+ enumValues: [...enumValues],
77
+ })
72
78
  }
73
79
  }
74
80
  }
@@ -87,7 +93,11 @@ export function applyDiscriminatorInheritance(root: ast.InputNode): ast.InputNod
87
93
 
88
94
  const { propertyName, enumValues } = entry
89
95
  const enumSchema = ast.createSchema({ type: 'enum', enumValues })
90
- const newProp = ast.createProperty({ name: propertyName, required: true, schema: enumSchema })
96
+ const newProp = ast.createProperty({
97
+ name: propertyName,
98
+ required: true,
99
+ schema: enumSchema,
100
+ })
91
101
 
92
102
  const existingIdx = objectNode.properties.findIndex((p) => p.name === propertyName)
93
103
  const newProperties = existingIdx >= 0 ? objectNode.properties.map((p, i) => (i === existingIdx ? newProp : p)) : [...objectNode.properties, newProp]
package/src/factory.ts CHANGED
@@ -33,9 +33,16 @@ export type ValidateDocumentOptions = {
33
33
  export async function parseDocument(pathOrApi: string | Document, { canBundle = true, enablePaths = true }: ParseOptions = {}): Promise<Document> {
34
34
  if (typeof pathOrApi === 'string' && canBundle) {
35
35
  const config = await loadConfig()
36
- const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })
36
+ const bundleResults = await bundle({
37
+ ref: pathOrApi,
38
+ config,
39
+ base: pathOrApi,
40
+ })
37
41
 
38
- return parseDocument(bundleResults.bundle.parsed as string, { canBundle, enablePaths })
42
+ return parseDocument(bundleResults.bundle.parsed as string, {
43
+ canBundle,
44
+ enablePaths,
45
+ })
39
46
  }
40
47
 
41
48
  const oasNormalize = new OASNormalize(pathOrApi, {
package/src/parser.ts CHANGED
@@ -53,7 +53,10 @@ type SchemaContext = {
53
53
  */
54
54
  function normalizeArrayEnum(schema: SchemaObject): SchemaObject {
55
55
  const isItemsObject = typeof schema.items === 'object' && !Array.isArray(schema.items)
56
- const normalizedItems: SchemaObject = { ...(isItemsObject ? (schema.items as SchemaObject) : {}), enum: schema.enum }
56
+ const normalizedItems: SchemaObject = {
57
+ ...(isItemsObject ? (schema.items as SchemaObject) : {}),
58
+ enum: schema.enum,
59
+ }
57
60
  const { enum: _enum, ...schemaWithoutEnum } = schema
58
61
 
59
62
  return { ...schemaWithoutEnum, items: normalizedItems } as SchemaObject
@@ -140,7 +143,10 @@ function createSchemaParser(ctx: OasParserContext) {
140
143
  } as ast.DistributiveOmit<ast.SchemaNode, 'kind'>)
141
144
  }
142
145
 
143
- const filteredDiscriminantValues: Array<{ propertyName: string; value: string }> = []
146
+ const filteredDiscriminantValues: Array<{
147
+ propertyName: string
148
+ value: string
149
+ }> = []
144
150
  const allOfMembers: Array<ast.SchemaNode> = (schema.allOf as Array<SchemaObject | ReferenceObject>)
145
151
  .filter((item) => {
146
152
  if (!isReference(item) || !name) return true
@@ -154,7 +160,10 @@ function createSchemaParser(ctx: OasParserContext) {
154
160
  if (inOneOf || inMapping) {
155
161
  const discriminatorValue = ast.findDiscriminator(deref.discriminator.mapping, childRef)
156
162
  if (discriminatorValue) {
157
- filteredDiscriminantValues.push({ propertyName: deref.discriminator.propertyName, value: discriminatorValue })
163
+ filteredDiscriminantValues.push({
164
+ propertyName: deref.discriminator.propertyName,
165
+ value: discriminatorValue,
166
+ })
158
167
  }
159
168
  return false
160
169
  }
@@ -178,7 +187,17 @@ function createSchemaParser(ctx: OasParserContext) {
178
187
  for (const key of missingRequired) {
179
188
  for (const resolved of resolvedMembers) {
180
189
  if (resolved.properties?.[key]) {
181
- allOfMembers.push(parseSchema({ schema: { properties: { [key]: resolved.properties[key] }, required: [key] } as SchemaObject }, rawOptions))
190
+ allOfMembers.push(
191
+ parseSchema(
192
+ {
193
+ schema: {
194
+ properties: { [key]: resolved.properties[key] },
195
+ required: [key],
196
+ } as SchemaObject,
197
+ },
198
+ rawOptions,
199
+ ),
200
+ )
182
201
  break
183
202
  }
184
203
  }
@@ -262,7 +281,11 @@ function createSchemaParser(ctx: OasParserContext) {
262
281
  type: 'intersection',
263
282
  members: [
264
283
  memberNode,
265
- narrowedDiscriminatorNode ?? ast.createDiscriminantNode({ propertyName: discriminator.propertyName, value: discriminatorValue }),
284
+ narrowedDiscriminatorNode ??
285
+ ast.createDiscriminantNode({
286
+ propertyName: discriminator.propertyName,
287
+ value: discriminatorValue,
288
+ }),
266
289
  ],
267
290
  })
268
291
  })
@@ -341,9 +364,20 @@ function createSchemaParser(ctx: OasParserContext) {
341
364
  if (!dateType) return null
342
365
 
343
366
  if (dateType.type === 'datetime') {
344
- return ast.createSchema({ ...base, primitive: 'string' as const, type: 'datetime', offset: dateType.offset, local: dateType.local })
367
+ return ast.createSchema({
368
+ ...base,
369
+ primitive: 'string' as const,
370
+ type: 'datetime',
371
+ offset: dateType.offset,
372
+ local: dateType.local,
373
+ })
345
374
  }
346
- return ast.createSchema({ ...base, primitive: 'string' as const, type: dateType.type, representation: dateType.representation })
375
+ return ast.createSchema({
376
+ ...base,
377
+ primitive: 'string' as const,
378
+ type: dateType.type,
379
+ representation: dateType.representation,
380
+ })
347
381
  }
348
382
 
349
383
  const specialType = getSchemaType(schema.format!)
@@ -352,22 +386,50 @@ function createSchemaParser(ctx: OasParserContext) {
352
386
  const specialPrimitive: ast.PrimitiveSchemaType = specialType === 'number' || specialType === 'integer' || specialType === 'bigint' ? specialType : 'string'
353
387
 
354
388
  if (specialType === 'number' || specialType === 'integer' || specialType === 'bigint') {
355
- return ast.createSchema({ ...base, primitive: specialPrimitive, type: specialType })
389
+ return ast.createSchema({
390
+ ...base,
391
+ primitive: specialPrimitive,
392
+ type: specialType,
393
+ })
356
394
  }
357
395
  if (specialType === 'url') {
358
- return ast.createSchema({ ...base, primitive: 'string' as const, type: 'url', min: schema.minLength, max: schema.maxLength })
396
+ return ast.createSchema({
397
+ ...base,
398
+ primitive: 'string' as const,
399
+ type: 'url',
400
+ min: schema.minLength,
401
+ max: schema.maxLength,
402
+ })
359
403
  }
360
404
  if (specialType === 'ipv4') {
361
- return ast.createSchema({ ...base, primitive: 'string' as const, type: 'ipv4' })
405
+ return ast.createSchema({
406
+ ...base,
407
+ primitive: 'string' as const,
408
+ type: 'ipv4',
409
+ })
362
410
  }
363
411
  if (specialType === 'ipv6') {
364
- return ast.createSchema({ ...base, primitive: 'string' as const, type: 'ipv6' })
412
+ return ast.createSchema({
413
+ ...base,
414
+ primitive: 'string' as const,
415
+ type: 'ipv6',
416
+ })
365
417
  }
366
418
  if (specialType === 'uuid' || specialType === 'email') {
367
- return ast.createSchema({ ...base, primitive: 'string' as const, type: specialType, min: schema.minLength, max: schema.maxLength })
419
+ return ast.createSchema({
420
+ ...base,
421
+ primitive: 'string' as const,
422
+ type: specialType,
423
+ min: schema.minLength,
424
+ max: schema.maxLength,
425
+ })
368
426
  }
369
427
 
370
- return ast.createSchema({ ...base, primitive: specialPrimitive, type: specialType as ast.ScalarSchemaType })
428
+ return ast.createSchema({
429
+ ...base,
430
+ primitive: specialPrimitive,
431
+ type: specialType as ast.ScalarSchemaType,
432
+ })
371
433
  }
372
434
 
373
435
  /**
@@ -473,7 +535,9 @@ function createSchemaParser(ctx: OasParserContext) {
473
535
  } else if (additionalProperties === false) {
474
536
  additionalPropertiesNode = false
475
537
  } else if (additionalProperties) {
476
- additionalPropertiesNode = ast.createSchema({ type: typeOptionMap.get(options.unknownType)! })
538
+ additionalPropertiesNode = ast.createSchema({
539
+ type: typeOptionMap.get(options.unknownType)!,
540
+ })
477
541
  }
478
542
 
479
543
  const rawPatternProperties = 'patternProperties' in schema ? schema.patternProperties : undefined
@@ -483,7 +547,9 @@ function createSchemaParser(ctx: OasParserContext) {
483
547
  Object.entries(rawPatternProperties).map(([pattern, patternSchema]) => [
484
548
  pattern,
485
549
  patternSchema === true || (typeof patternSchema === 'object' && Object.keys(patternSchema).length === 0)
486
- ? ast.createSchema({ type: typeOptionMap.get(options.unknownType)! })
550
+ ? ast.createSchema({
551
+ type: typeOptionMap.get(options.unknownType)!,
552
+ })
487
553
  : parseSchema({ schema: patternSchema as SchemaObject }, rawOptions),
488
554
  ]),
489
555
  )
@@ -504,7 +570,12 @@ function createSchemaParser(ctx: OasParserContext) {
504
570
  const discPropName = schema.discriminator.propertyName
505
571
  const values = Object.keys(schema.discriminator.mapping)
506
572
  const enumName = name ? ast.enumPropName(name, discPropName, options.enumSuffix) : undefined
507
- return ast.setDiscriminatorEnum({ node: objectNode, propertyName: discPropName, values, enumName })
573
+ return ast.setDiscriminatorEnum({
574
+ node: objectNode,
575
+ propertyName: discPropName,
576
+ values,
577
+ enumName,
578
+ })
508
579
  }
509
580
 
510
581
  return objectNode
@@ -611,7 +682,10 @@ function createSchemaParser(ctx: OasParserContext) {
611
682
  * → empty-schema fallback (`emptySchemaType` option).
612
683
  */
613
684
  function parseSchema({ schema, name }: { schema: SchemaObject; name?: string | null }, rawOptions?: Partial<ast.ParserOptions>): ast.SchemaNode {
614
- const options: ast.ParserOptions = { ...DEFAULT_PARSER_OPTIONS, ...rawOptions }
685
+ const options: ast.ParserOptions = {
686
+ ...DEFAULT_PARSER_OPTIONS,
687
+ ...rawOptions,
688
+ }
615
689
  const flattenedSchema = flattenSchema(schema)
616
690
  if (flattenedSchema && flattenedSchema !== schema) {
617
691
  return parseSchema({ schema: flattenedSchema, name }, rawOptions)
@@ -621,7 +695,15 @@ function createSchemaParser(ctx: OasParserContext) {
621
695
  const defaultValue = schema.default === null && nullable ? undefined : schema.default
622
696
  const type = Array.isArray(schema.type) ? schema.type[0] : schema.type
623
697
 
624
- const ctx: SchemaContext = { schema, name, nullable, defaultValue, type, rawOptions, options }
698
+ const ctx: SchemaContext = {
699
+ schema,
700
+ name,
701
+ nullable,
702
+ defaultValue,
703
+ type,
704
+ rawOptions,
705
+ options,
706
+ }
625
707
 
626
708
  if (isReference(schema)) return convertRef(ctx)
627
709
 
@@ -637,7 +719,11 @@ function createSchemaParser(ctx: OasParserContext) {
637
719
  }
638
720
 
639
721
  if (schema.type === 'string' && schema.contentMediaType === 'application/octet-stream') {
640
- return ast.createSchema({ type: 'blob', primitive: 'string', ...buildSchemaNode(schema, name, nullable, defaultValue) })
722
+ return ast.createSchema({
723
+ type: 'blob',
724
+ primitive: 'string',
725
+ ...buildSchemaNode(schema, name, nullable, defaultValue),
726
+ })
641
727
  }
642
728
 
643
729
  if (Array.isArray(schema.type) && schema.type.length > 1) {
@@ -673,7 +759,12 @@ function createSchemaParser(ctx: OasParserContext) {
673
759
  if (type === 'null') return convertNull(ctx)
674
760
 
675
761
  const emptyType = typeOptionMap.get(options.emptySchemaType)!
676
- return ast.createSchema({ type: emptyType as ast.ScalarSchemaType, name, title: schema.title, description: schema.description })
762
+ return ast.createSchema({
763
+ type: emptyType as ast.ScalarSchemaType,
764
+ name,
765
+ title: schema.title,
766
+ description: schema.description,
767
+ })
677
768
  }
678
769
 
679
770
  /**
@@ -701,11 +792,19 @@ function createSchemaParser(ctx: OasParserContext) {
701
792
  * Reads the inline `requestBody` metadata (description / required / contentType) that OAS exposes
702
793
  * outside the schema itself. Returns an empty object when the request body is missing or a `$ref`.
703
794
  */
704
- function getRequestBodyMeta(operation: Operation): { description?: string; required: boolean; contentType?: string } {
795
+ function getRequestBodyMeta(operation: Operation): {
796
+ description?: string
797
+ required: boolean
798
+ contentType?: string
799
+ } {
705
800
  const body = operation.schema.requestBody
706
801
  if (!body || isReference(body)) return { required: false }
707
802
 
708
- const inline = body as { description?: string; required?: boolean; content?: Record<string, unknown> }
803
+ const inline = body as {
804
+ description?: string
805
+ required?: boolean
806
+ content?: Record<string, unknown>
807
+ }
709
808
  return {
710
809
  description: inline.description,
711
810
  required: inline.required === true,
@@ -716,10 +815,16 @@ function createSchemaParser(ctx: OasParserContext) {
716
815
  /**
717
816
  * Reads the inline response object (not a `$ref`) and returns its description plus its `content` map.
718
817
  */
719
- function getResponseMeta(responseObj: unknown): { description?: string; content?: Record<string, unknown> } {
818
+ function getResponseMeta(responseObj: unknown): {
819
+ description?: string
820
+ content?: Record<string, unknown>
821
+ } {
720
822
  if (typeof responseObj !== 'object' || responseObj === null || Array.isArray(responseObj)) return {}
721
823
 
722
- const inline = responseObj as { description?: string; content?: Record<string, unknown> }
824
+ const inline = responseObj as {
825
+ description?: string
826
+ content?: Record<string, unknown>
827
+ }
723
828
  return { description: inline.description, content: inline.content }
724
829
  }
725
830
 
@@ -748,7 +853,9 @@ function createSchemaParser(ctx: OasParserContext) {
748
853
  parseParameter(options, param as unknown as Record<string, unknown>),
749
854
  )
750
855
 
751
- const requestBodySchema = getRequestSchema(document, operation, { contentType: ctx.contentType })
856
+ const requestBodySchema = getRequestSchema(document, operation, {
857
+ contentType: ctx.contentType,
858
+ })
752
859
  const requestBodySchemaNode = requestBodySchema ? parseSchema({ schema: requestBodySchema }, options) : undefined
753
860
  const requestBodyMeta = getRequestBodyMeta(operation)
754
861
 
@@ -769,7 +876,9 @@ function createSchemaParser(ctx: OasParserContext) {
769
876
  const schema =
770
877
  responseSchema && Object.keys(responseSchema).length > 0
771
878
  ? parseSchema({ schema: responseSchema }, options)
772
- : ast.createSchema({ type: typeOptionMap.get(options.emptySchemaType)! })
879
+ : ast.createSchema({
880
+ type: typeOptionMap.get(options.emptySchemaType)!,
881
+ })
773
882
 
774
883
  const { description, content } = getResponseMeta(responseObj)
775
884
  const mediaType = content ? getMediaType(Object.keys(content)[0] ?? '') : getMediaType(operation.contentType ?? '')
@@ -837,9 +946,14 @@ export function parseOas(
837
946
  options: Partial<ast.ParserOptions> & { contentType?: ContentType } = {},
838
947
  ): { root: ast.InputNode; nameMapping: Map<string, string> } {
839
948
  const { contentType, ...parserOptions } = options
840
- const mergedOptions: ast.ParserOptions = { ...DEFAULT_PARSER_OPTIONS, ...parserOptions }
949
+ const mergedOptions: ast.ParserOptions = {
950
+ ...DEFAULT_PARSER_OPTIONS,
951
+ ...parserOptions,
952
+ }
841
953
 
842
- const { schemas: schemaObjects, nameMapping } = getSchemas(document, { contentType })
954
+ const { schemas: schemaObjects, nameMapping } = getSchemas(document, {
955
+ contentType,
956
+ })
843
957
  const { parseSchema: _parseSchema, parseOperation: _parseOperation } = createSchemaParser({ document, contentType })
844
958
 
845
959
  const schemas: Array<ast.SchemaNode> = Object.entries(schemaObjects).map(([name, schema]) => _parseSchema({ schema, name }, mergedOptions))
package/src/resolvers.ts CHANGED
@@ -421,7 +421,15 @@ export function getSchemas(document: Document, { contentType }: GetSchemasOption
421
421
  ...(['responses', 'requestBodies'] as const).flatMap((source) =>
422
422
  Object.entries(components?.[source] ?? {}).flatMap(([name, item]) => {
423
423
  const schema = extractSchemaFromContent((item as { content?: Record<string, unknown> }).content, contentType)
424
- return schema ? [{ schema: resolveSchemaRef(document, schema), source, originalName: name }] : []
424
+ return schema
425
+ ? [
426
+ {
427
+ schema: resolveSchemaRef(document, schema),
428
+ source,
429
+ originalName: name,
430
+ },
431
+ ]
432
+ : []
425
433
  }),
426
434
  ),
427
435
  ]
@@ -487,11 +495,17 @@ export function getDateType(
487
495
  }
488
496
 
489
497
  if (format === 'date') {
490
- return { type: 'date', representation: options.dateType === 'date' ? 'date' : 'string' }
498
+ return {
499
+ type: 'date',
500
+ representation: options.dateType === 'date' ? 'date' : 'string',
501
+ }
491
502
  }
492
503
 
493
504
  // time
494
- return { type: 'time', representation: options.dateType === 'date' ? 'date' : 'string' }
505
+ return {
506
+ type: 'time',
507
+ representation: options.dateType === 'date' ? 'date' : 'string',
508
+ }
495
509
  }
496
510
 
497
511
  /**