@kubb/adapter-oas 5.0.0-alpha.34 → 5.0.0-alpha.35
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 +109 -99
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +104 -94
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/adapter.ts +5 -7
- package/src/constants.ts +7 -8
- package/src/discriminator.ts +15 -16
- package/src/parser.ts +105 -131
- package/src/resolvers.ts +6 -7
- package/src/types.ts +6 -7
package/src/adapter.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { InputNode } from '@kubb/ast/types'
|
|
3
|
-
import { createAdapter } from '@kubb/core'
|
|
1
|
+
import { ast, createAdapter } from '@kubb/core'
|
|
4
2
|
import { DEFAULT_PARSER_OPTIONS } from './constants.ts'
|
|
5
3
|
import { applyDiscriminatorInheritance } from './discriminator.ts'
|
|
6
4
|
import { parseFromConfig, validateDocument } from './factory.ts'
|
|
@@ -49,7 +47,7 @@ export const adapterOas = createAdapter<AdapterOas>((options) => {
|
|
|
49
47
|
// Let-binding so parse() can replace it with a simple reassignment (no clear+loop).
|
|
50
48
|
let nameMapping = new Map<string, string>()
|
|
51
49
|
let parsedDocument: Document | null
|
|
52
|
-
let inputNode: InputNode | null
|
|
50
|
+
let inputNode: ast.InputNode | null
|
|
53
51
|
|
|
54
52
|
return {
|
|
55
53
|
name: adapterOasName,
|
|
@@ -75,14 +73,14 @@ export const adapterOas = createAdapter<AdapterOas>((options) => {
|
|
|
75
73
|
return inputNode
|
|
76
74
|
},
|
|
77
75
|
getImports(node, resolve) {
|
|
78
|
-
return collectImports({
|
|
76
|
+
return ast.collectImports({
|
|
79
77
|
node,
|
|
80
78
|
nameMapping,
|
|
81
79
|
resolve: (schemaName) => {
|
|
82
80
|
const result = resolve(schemaName)
|
|
83
81
|
if (!result) return
|
|
84
82
|
|
|
85
|
-
return createImport({ name: [result.name], path: result.path })
|
|
83
|
+
return ast.createImport({ name: [result.name], path: result.path })
|
|
86
84
|
},
|
|
87
85
|
})
|
|
88
86
|
},
|
|
@@ -112,7 +110,7 @@ export const adapterOas = createAdapter<AdapterOas>((options) => {
|
|
|
112
110
|
// Expose the raw document so consumers (e.g. plugin-redoc) can access it.
|
|
113
111
|
parsedDocument = document
|
|
114
112
|
|
|
115
|
-
inputNode = createInput({
|
|
113
|
+
inputNode = ast.createInput({
|
|
116
114
|
...node,
|
|
117
115
|
meta: {
|
|
118
116
|
title: document.info?.title,
|
package/src/constants.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { ParserOptions, ScalarSchemaType, SchemaType } from '@kubb/ast/types'
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -18,7 +17,7 @@ export const DEFAULT_PARSER_OPTIONS = {
|
|
|
18
17
|
unknownType: 'any',
|
|
19
18
|
emptySchemaType: 'any',
|
|
20
19
|
enumSuffix: 'enum',
|
|
21
|
-
} as const satisfies ParserOptions
|
|
20
|
+
} as const satisfies ast.ParserOptions
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
23
|
* OpenAPI version string written into the stub document created during multi-spec merges.
|
|
@@ -86,7 +85,7 @@ export const formatMap = {
|
|
|
86
85
|
int32: 'integer',
|
|
87
86
|
float: 'number',
|
|
88
87
|
double: 'number',
|
|
89
|
-
} as const satisfies Record<string, SchemaType>
|
|
88
|
+
} as const satisfies Record<string, ast.SchemaType>
|
|
90
89
|
|
|
91
90
|
/**
|
|
92
91
|
* Vendor extension keys that attach human-readable labels to enum values, checked in priority order.
|
|
@@ -104,8 +103,8 @@ export const enumExtensionKeys = ['x-enumNames', 'x-enum-varnames'] as const
|
|
|
104
103
|
* Maps `'any' | 'unknown' | 'void'` option strings to their `ScalarSchemaType` constant.
|
|
105
104
|
* Replaces a plain object lookup with a `Map` for explicit key membership testing via `.has()`.
|
|
106
105
|
*/
|
|
107
|
-
export const typeOptionMap = new Map<'any' | 'unknown' | 'void', ScalarSchemaType>([
|
|
108
|
-
['any', schemaTypes.any],
|
|
109
|
-
['unknown', schemaTypes.unknown],
|
|
110
|
-
['void', schemaTypes.void],
|
|
106
|
+
export const typeOptionMap = new Map<'any' | 'unknown' | 'void', ast.ScalarSchemaType>([
|
|
107
|
+
['any', ast.schemaTypes.any],
|
|
108
|
+
['unknown', ast.schemaTypes.unknown],
|
|
109
|
+
['void', ast.schemaTypes.void],
|
|
111
110
|
])
|
package/src/discriminator.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { InputNode } from '@kubb/ast/types'
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
3
2
|
|
|
4
3
|
type DiscriminatorTarget = { propertyName: string; enumValues: Array<string | number | boolean> }
|
|
5
4
|
|
|
@@ -18,19 +17,19 @@ type DiscriminatorTarget = { propertyName: string; enumValues: Array<string | nu
|
|
|
18
17
|
* const next = applyDiscriminatorInheritance(root)
|
|
19
18
|
* ```
|
|
20
19
|
*/
|
|
21
|
-
export function applyDiscriminatorInheritance(root: InputNode): InputNode {
|
|
20
|
+
export function applyDiscriminatorInheritance(root: ast.InputNode): ast.InputNode {
|
|
22
21
|
const childMap = new Map<string, DiscriminatorTarget>()
|
|
23
22
|
|
|
24
23
|
for (const schema of root.schemas) {
|
|
25
24
|
// Case 1: top-level schema is a union (oneOf/anyOf with discriminator)
|
|
26
25
|
// Case 2: top-level schema is an intersection wrapping a union (oneOf/anyOf + shared properties)
|
|
27
|
-
let unionNode = narrowSchema(schema, 'union')
|
|
26
|
+
let unionNode = ast.narrowSchema(schema, 'union')
|
|
28
27
|
|
|
29
28
|
if (!unionNode) {
|
|
30
|
-
const intersectionMembers = narrowSchema(schema, 'intersection')?.members
|
|
29
|
+
const intersectionMembers = ast.narrowSchema(schema, 'intersection')?.members
|
|
31
30
|
if (intersectionMembers) {
|
|
32
31
|
for (const m of intersectionMembers) {
|
|
33
|
-
const u = narrowSchema(m, 'union')
|
|
32
|
+
const u = ast.narrowSchema(m, 'union')
|
|
34
33
|
if (u) {
|
|
35
34
|
unionNode = u
|
|
36
35
|
break
|
|
@@ -45,21 +44,21 @@ export function applyDiscriminatorInheritance(root: InputNode): InputNode {
|
|
|
45
44
|
|
|
46
45
|
for (const member of members) {
|
|
47
46
|
// Members with a discriminant value are intersections: [RefSchemaNode, ObjectSchemaNode]
|
|
48
|
-
const intersectionNode = narrowSchema(member, 'intersection')
|
|
47
|
+
const intersectionNode = ast.narrowSchema(member, 'intersection')
|
|
49
48
|
if (!intersectionNode?.members) continue
|
|
50
49
|
|
|
51
|
-
let refNode: ReturnType<typeof narrowSchema<'ref'>> | undefined
|
|
52
|
-
let objNode: ReturnType<typeof narrowSchema<'object'>> | undefined
|
|
50
|
+
let refNode: ReturnType<typeof ast.narrowSchema<'ref'>> | undefined
|
|
51
|
+
let objNode: ReturnType<typeof ast.narrowSchema<'object'>> | undefined
|
|
53
52
|
|
|
54
53
|
for (const m of intersectionNode.members) {
|
|
55
|
-
refNode ??= narrowSchema(m, 'ref')
|
|
56
|
-
objNode ??= narrowSchema(m, 'object')
|
|
54
|
+
refNode ??= ast.narrowSchema(m, 'ref')
|
|
55
|
+
objNode ??= ast.narrowSchema(m, 'object')
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
if (!refNode?.name || !objNode) continue
|
|
60
59
|
|
|
61
60
|
const prop = objNode.properties.find((p) => p.name === discriminatorPropertyName)
|
|
62
|
-
const enumNode = prop ? narrowSchema(prop.schema, 'enum') : undefined
|
|
61
|
+
const enumNode = prop ? ast.narrowSchema(prop.schema, 'enum') : undefined
|
|
63
62
|
if (!enumNode?.enumValues?.length) continue
|
|
64
63
|
|
|
65
64
|
const enumValues = enumNode.enumValues.filter((v): v is string | number | boolean => v !== null)
|
|
@@ -76,19 +75,19 @@ export function applyDiscriminatorInheritance(root: InputNode): InputNode {
|
|
|
76
75
|
|
|
77
76
|
if (childMap.size === 0) return root
|
|
78
77
|
|
|
79
|
-
return transform(root, {
|
|
78
|
+
return ast.transform(root, {
|
|
80
79
|
schema(node, { parent }) {
|
|
81
80
|
if (parent?.kind !== 'Input' || !node.name) return
|
|
82
81
|
|
|
83
82
|
const entry = childMap.get(node.name)
|
|
84
83
|
if (!entry) return
|
|
85
84
|
|
|
86
|
-
const objectNode = narrowSchema(node, 'object')
|
|
85
|
+
const objectNode = ast.narrowSchema(node, 'object')
|
|
87
86
|
if (!objectNode) return
|
|
88
87
|
|
|
89
88
|
const { propertyName, enumValues } = entry
|
|
90
|
-
const enumSchema = createSchema({ type: 'enum', enumValues })
|
|
91
|
-
const newProp = createProperty({ name: propertyName, required: true, schema: enumSchema })
|
|
89
|
+
const enumSchema = ast.createSchema({ type: 'enum', enumValues })
|
|
90
|
+
const newProp = ast.createProperty({ name: propertyName, required: true, schema: enumSchema })
|
|
92
91
|
|
|
93
92
|
const existingIdx = objectNode.properties.findIndex((p) => p.name === propertyName)
|
|
94
93
|
const newProperties = existingIdx >= 0 ? objectNode.properties.map((p, i) => (i === existingIdx ? newProp : p)) : [...objectNode.properties, newProp]
|