@kubb/ast 5.0.0-beta.6 → 5.0.0-beta.61

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.
Files changed (102) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +51 -27
  3. package/dist/chunk-CNktS9qV.js +17 -0
  4. package/dist/defineMacro-BLIR6k-j.d.ts +475 -0
  5. package/dist/defineMacro-BTXvS8nI.js +106 -0
  6. package/dist/defineMacro-BTXvS8nI.js.map +1 -0
  7. package/dist/defineMacro-Bv9R_9a2.cjs +123 -0
  8. package/dist/defineMacro-Bv9R_9a2.cjs.map +1 -0
  9. package/dist/extractStringsFromNodes-Cja-xxx5.js +29 -0
  10. package/dist/extractStringsFromNodes-Cja-xxx5.js.map +1 -0
  11. package/dist/extractStringsFromNodes-DKgDjFO0.cjs +34 -0
  12. package/dist/extractStringsFromNodes-DKgDjFO0.cjs.map +1 -0
  13. package/dist/extractStringsFromNodes-p4mX1TQD.d.ts +14 -0
  14. package/dist/factory-CZNOGI-N.js +283 -0
  15. package/dist/factory-CZNOGI-N.js.map +1 -0
  16. package/dist/factory-DG1CVkEb.cjs +300 -0
  17. package/dist/factory-DG1CVkEb.cjs.map +1 -0
  18. package/dist/factory.cjs +29 -0
  19. package/dist/factory.d.ts +62 -0
  20. package/dist/factory.js +3 -0
  21. package/dist/index-BzjwdK2M.d.ts +2433 -0
  22. package/dist/index.cjs +444 -2180
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +94 -3408
  25. package/dist/index.js +395 -2101
  26. package/dist/index.js.map +1 -1
  27. package/dist/macros.cjs +117 -0
  28. package/dist/macros.cjs.map +1 -0
  29. package/dist/macros.d.ts +59 -0
  30. package/dist/macros.js +115 -0
  31. package/dist/macros.js.map +1 -0
  32. package/dist/operationParams-BZ07xDm0.d.ts +204 -0
  33. package/dist/response-KUdWiDWw.js +658 -0
  34. package/dist/response-KUdWiDWw.js.map +1 -0
  35. package/dist/response-hnSw2NKE.cjs +1027 -0
  36. package/dist/response-hnSw2NKE.cjs.map +1 -0
  37. package/dist/types-DyDzizSf.d.ts +364 -0
  38. package/dist/types.cjs +0 -0
  39. package/dist/types.d.ts +6 -0
  40. package/dist/types.js +1 -0
  41. package/dist/utils-BLJwyza-.cjs +912 -0
  42. package/dist/utils-BLJwyza-.cjs.map +1 -0
  43. package/dist/utils-CF_-Pn_c.js +770 -0
  44. package/dist/utils-CF_-Pn_c.js.map +1 -0
  45. package/dist/utils.cjs +36 -0
  46. package/dist/utils.d.ts +358 -0
  47. package/dist/utils.js +4 -0
  48. package/dist/visitor-DJ6ZEJvq.js +548 -0
  49. package/dist/visitor-DJ6ZEJvq.js.map +1 -0
  50. package/dist/visitor-DpKZ9Tk0.cjs +654 -0
  51. package/dist/visitor-DpKZ9Tk0.cjs.map +1 -0
  52. package/package.json +21 -6
  53. package/src/constants.ts +19 -64
  54. package/src/dedupe.ts +239 -0
  55. package/src/defineMacro.ts +132 -0
  56. package/src/dialect.ts +53 -0
  57. package/src/factory.ts +67 -678
  58. package/src/guards.ts +10 -92
  59. package/src/index.ts +13 -44
  60. package/src/infer.ts +16 -14
  61. package/src/macros/index.ts +3 -0
  62. package/src/macros/macroDiscriminatorEnum.ts +44 -0
  63. package/src/macros/macroEnumName.ts +25 -0
  64. package/src/macros/macroSimplifyUnion.ts +50 -0
  65. package/src/mocks.ts +7 -127
  66. package/src/node.ts +128 -0
  67. package/src/nodes/base.ts +5 -12
  68. package/src/nodes/code.ts +165 -74
  69. package/src/nodes/content.ts +56 -0
  70. package/src/nodes/file.ts +97 -36
  71. package/src/nodes/function.ts +216 -156
  72. package/src/nodes/http.ts +1 -35
  73. package/src/nodes/index.ts +23 -15
  74. package/src/nodes/input.ts +140 -0
  75. package/src/nodes/operation.ts +122 -68
  76. package/src/nodes/output.ts +23 -0
  77. package/src/nodes/parameter.ts +33 -3
  78. package/src/nodes/property.ts +36 -3
  79. package/src/nodes/requestBody.ts +61 -0
  80. package/src/nodes/response.ts +58 -13
  81. package/src/nodes/schema.ts +93 -17
  82. package/src/printer.ts +48 -42
  83. package/src/registry.ts +75 -0
  84. package/src/signature.ts +207 -0
  85. package/src/types.ts +8 -68
  86. package/src/utils/codegen.ts +104 -0
  87. package/src/utils/extractStringsFromNodes.ts +34 -0
  88. package/src/utils/fileMerge.ts +184 -0
  89. package/src/utils/index.ts +11 -0
  90. package/src/utils/operationParams.ts +353 -0
  91. package/src/utils/refs.ts +138 -0
  92. package/src/utils/schemaGraph.ts +169 -0
  93. package/src/utils/schemaMerge.ts +34 -0
  94. package/src/utils/schemaTraversal.ts +86 -0
  95. package/src/utils/strings.ts +139 -0
  96. package/src/visitor.ts +227 -289
  97. package/dist/chunk--u3MIqq1.js +0 -8
  98. package/src/nodes/root.ts +0 -64
  99. package/src/refs.ts +0 -67
  100. package/src/resolvers.ts +0 -45
  101. package/src/transformers.ts +0 -159
  102. package/src/utils.ts +0 -915
package/src/nodes/root.ts DELETED
@@ -1,64 +0,0 @@
1
- import type { BaseNode } from './base.ts'
2
- import type { OperationNode } from './operation.ts'
3
- import type { SchemaNode } from './schema.ts'
4
-
5
- /**
6
- * Basic metadata for an API document.
7
- * Adapters fill fields that exist in their source format.
8
- *
9
- * @example
10
- * ```ts
11
- * const meta: InputMeta = { title: 'Pet API', version: '1.0.0' }
12
- * ```
13
- */
14
- export type InputMeta = {
15
- /**
16
- * API title (from `info.title` in OAS/AsyncAPI).
17
- */
18
- title?: string
19
- /**
20
- * API description (from `info.description` in OAS/AsyncAPI).
21
- */
22
- description?: string
23
- /**
24
- * API version string (from `info.version` in OAS/AsyncAPI).
25
- */
26
- version?: string
27
- /**
28
- * Resolved API base URL.
29
- * For OpenAPI and AsyncAPI, this comes from the selected server URL.
30
- */
31
- baseURL?: string
32
- }
33
-
34
- /**
35
- * Input AST node that contains all schemas and operations for one API document.
36
- * Produced by the adapter and consumed by all Kubb plugins.
37
- *
38
- * @example
39
- * ```ts
40
- * const input: InputNode = {
41
- * kind: 'Input',
42
- * schemas: [],
43
- * operations: [],
44
- * }
45
- * ```
46
- */
47
- export type InputNode = BaseNode & {
48
- /**
49
- * Node kind.
50
- */
51
- kind: 'Input'
52
- /**
53
- * All schema nodes in the document.
54
- */
55
- schemas: Array<SchemaNode>
56
- /**
57
- * All operation nodes in the document.
58
- */
59
- operations: Array<OperationNode>
60
- /**
61
- * Optional document metadata populated by the adapter.
62
- */
63
- meta?: InputMeta
64
- }
package/src/refs.ts DELETED
@@ -1,67 +0,0 @@
1
- import type { InputNode } from './nodes/root.ts'
2
- import type { SchemaNode } from './nodes/schema.ts'
3
-
4
- /**
5
- * Lookup map from schema name to `SchemaNode`.
6
- */
7
- export type RefMap = Map<string, SchemaNode>
8
-
9
- /**
10
- * Returns the last path segment of a reference string.
11
- *
12
- * Example: `#/components/schemas/Pet` becomes `Pet`.
13
- *
14
- * @example
15
- * ```ts
16
- * extractRefName('#/components/schemas/Pet') // 'Pet'
17
- * ```
18
- */
19
- export function extractRefName(ref: string): string {
20
- return ref.split('/').at(-1) ?? ref
21
- }
22
-
23
- /**
24
- * Builds a `RefMap` from `input.schemas` using each schema's `name`.
25
- *
26
- * Unnamed schemas are skipped.
27
- *
28
- * @example
29
- * ```ts
30
- * const refMap = buildRefMap(input)
31
- * const pet = refMap.get('Pet')
32
- * ```
33
- */
34
- export function buildRefMap(input: InputNode): RefMap {
35
- const map: RefMap = new Map()
36
-
37
- for (const schema of input.schemas) {
38
- if (schema.name) {
39
- map.set(schema.name, schema)
40
- }
41
- }
42
- return map
43
- }
44
-
45
- /**
46
- * Resolves a schema by name from a `RefMap`.
47
- *
48
- * @example
49
- * ```ts
50
- * const petSchema = resolveRef(refMap, 'Pet')
51
- * ```
52
- */
53
- export function resolveRef(refMap: RefMap, ref: string): SchemaNode | undefined {
54
- return refMap.get(ref)
55
- }
56
-
57
- /**
58
- * Converts a `RefMap` into a plain object.
59
- *
60
- * @example
61
- * ```ts
62
- * const refsObject = refMapToObject(refMap)
63
- * ```
64
- */
65
- export function refMapToObject(refMap: RefMap): Record<string, SchemaNode> {
66
- return Object.fromEntries(refMap)
67
- }
package/src/resolvers.ts DELETED
@@ -1,45 +0,0 @@
1
- import { pascalCase } from '@internals/utils'
2
- import { narrowSchema } from './guards.ts'
3
- import type { SchemaNode } from './nodes/schema.ts'
4
- import { extractRefName } from './refs.ts'
5
- import { collect } from './visitor.ts'
6
-
7
- export function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null {
8
- if (!mapping || !ref) return null
9
- return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null
10
- }
11
-
12
- export function childName(parentName: string | null | undefined, propName: string): string | null {
13
- return parentName ? pascalCase([parentName, propName].join(' ')) : null
14
- }
15
-
16
- export function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {
17
- return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
18
- }
19
-
20
- /**
21
- * Collects import entries for all `ref` schema nodes in `node`.
22
- */
23
- export function collectImports<TImport>({
24
- node,
25
- nameMapping,
26
- resolve,
27
- }: {
28
- node: SchemaNode
29
- nameMapping: Map<string, string>
30
- resolve: (schemaName: string) => TImport | undefined
31
- }): Array<TImport> {
32
- return collect<TImport>(node, {
33
- schema(schemaNode): TImport | undefined {
34
- const schemaRef = narrowSchema(schemaNode, 'ref')
35
- if (!schemaRef?.ref) return
36
-
37
- const rawName = extractRefName(schemaRef.ref)
38
- const schemaName = nameMapping.get(rawName) ?? rawName
39
- const result = resolve(schemaName)
40
- if (!result) return
41
-
42
- return result
43
- },
44
- })
45
- }
@@ -1,159 +0,0 @@
1
- import { isScalarPrimitive } from './constants.ts'
2
- import { createProperty, createSchema } from './factory.ts'
3
- import { narrowSchema } from './guards.ts'
4
- import type { SchemaNode } from './nodes/schema.ts'
5
- import { enumPropName } from './resolvers.ts'
6
-
7
- /**
8
- * Replaces a discriminator property's schema with a string enum of allowed values.
9
- *
10
- * If `node` is not an object schema, or if the property does not exist, the input
11
- * node is returned as-is.
12
- *
13
- * @example
14
- * ```ts
15
- * const schema = createSchema({
16
- * type: 'object',
17
- * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
18
- * })
19
- * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
20
- * ```
21
- */
22
- export function setDiscriminatorEnum({
23
- node,
24
- propertyName,
25
- values,
26
- enumName,
27
- }: {
28
- node: SchemaNode
29
- propertyName: string
30
- values: Array<string>
31
- enumName?: string
32
- }): SchemaNode {
33
- const objectNode = narrowSchema(node, 'object')
34
- if (!objectNode?.properties?.length) {
35
- return node
36
- }
37
-
38
- const hasProperty = objectNode.properties.some((prop) => prop.name === propertyName)
39
- if (!hasProperty) {
40
- return node
41
- }
42
-
43
- return createSchema({
44
- ...objectNode,
45
- properties: objectNode.properties.map((prop) => {
46
- if (prop.name !== propertyName) {
47
- return prop
48
- }
49
-
50
- return createProperty({
51
- ...prop,
52
- schema: createSchema({
53
- type: 'enum',
54
- primitive: 'string',
55
- enumValues: values,
56
- name: enumName,
57
- readOnly: prop.schema.readOnly,
58
- writeOnly: prop.schema.writeOnly,
59
- }),
60
- })
61
- }),
62
- })
63
- }
64
-
65
- /**
66
- * Merges adjacent anonymous object members into a single anonymous object member.
67
- *
68
- * @example
69
- * ```ts
70
- * const merged = mergeAdjacentObjects([
71
- * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
72
- * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
73
- * ])
74
- * ```
75
- */
76
- export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
77
- return members.reduce<Array<SchemaNode>>((acc, member) => {
78
- const objectMember = narrowSchema(member, 'object')
79
- if (objectMember && !objectMember.name) {
80
- const previous = acc.at(-1)
81
- const previousObject = previous ? narrowSchema(previous, 'object') : undefined
82
-
83
- if (previousObject && !previousObject.name) {
84
- acc[acc.length - 1] = createSchema({
85
- ...previousObject,
86
- properties: [...(previousObject.properties ?? []), ...(objectMember.properties ?? [])],
87
- })
88
- return acc
89
- }
90
- }
91
-
92
- acc.push(member)
93
- return acc
94
- }, [])
95
- }
96
-
97
- /**
98
- * Removes enum members that are covered by broader scalar primitives in the same union.
99
- *
100
- * @example
101
- * ```ts
102
- * const simplified = simplifyUnion([
103
- * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
104
- * createSchema({ type: 'string' }),
105
- * ])
106
- * // keeps only string member
107
- * ```
108
- */
109
- export function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode> {
110
- const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))
111
-
112
- if (!scalarPrimitives.size) {
113
- return members
114
- }
115
-
116
- return members.filter((member) => {
117
- const enumNode = narrowSchema(member, 'enum')
118
- if (!enumNode) {
119
- return true
120
- }
121
-
122
- const primitive = enumNode.primitive
123
- if (!primitive) {
124
- return true
125
- }
126
-
127
- const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0
128
- if (enumValueCount <= 1) {
129
- return true
130
- }
131
-
132
- if (scalarPrimitives.has(primitive)) {
133
- return false
134
- }
135
-
136
- if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) {
137
- return false
138
- }
139
-
140
- return true
141
- })
142
- }
143
-
144
- export function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode {
145
- const enumNode = narrowSchema(propNode, 'enum')
146
-
147
- if (enumNode?.primitive === 'boolean') {
148
- return { ...propNode, name: undefined }
149
- }
150
-
151
- if (enumNode) {
152
- return {
153
- ...propNode,
154
- name: enumPropName(parentName, propName, enumSuffix),
155
- }
156
- }
157
-
158
- return propNode
159
- }