@kubb/plugin-ts 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.
@@ -1,16 +1,14 @@
1
- import type { PrinterFactoryOptions } from '@kubb/ast'
2
- import { createPrinterFactory } from '@kubb/ast'
3
- import type { FunctionNodeType, FunctionParameterNode, FunctionParametersNode, FunctionParamNode, ParameterGroupNode, ParamsTypeNode } from '@kubb/ast/types'
1
+ import { ast } from '@kubb/core'
4
2
  import { PARAM_RANK } from '../constants.ts'
5
3
 
6
4
  /**
7
5
  * Maps each function-printer handler key to its concrete node type.
8
6
  */
9
7
  export type FunctionNodeByType = {
10
- functionParameter: FunctionParameterNode
11
- parameterGroup: ParameterGroupNode
12
- functionParameters: FunctionParametersNode
13
- paramsType: ParamsTypeNode
8
+ functionParameter: ast.FunctionParameterNode
9
+ parameterGroup: ast.ParameterGroupNode
10
+ functionParameters: ast.FunctionParametersNode
11
+ paramsType: ast.ParamsTypeNode
14
12
  }
15
13
 
16
14
  const kindToHandlerKey = {
@@ -18,7 +16,7 @@ const kindToHandlerKey = {
18
16
  ParameterGroup: 'parameterGroup',
19
17
  FunctionParameters: 'functionParameters',
20
18
  ParamsType: 'paramsType',
21
- } satisfies Record<string, FunctionNodeType>
19
+ } satisfies Record<string, ast.FunctionNodeType>
22
20
 
23
21
  /**
24
22
  * Creates a function-parameter printer factory.
@@ -26,7 +24,9 @@ const kindToHandlerKey = {
26
24
  * Uses `createPrinterFactory` and dispatches handlers by `node.kind`
27
25
  * (for function nodes) rather than by `node.type` (for schema nodes).
28
26
  */
29
- export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionNodeType, FunctionNodeByType>((node) => kindToHandlerKey[node.kind])
27
+ export const defineFunctionPrinter = ast.createPrinterFactory<ast.FunctionParamNode, ast.FunctionNodeType, FunctionNodeByType>(
28
+ (node) => kindToHandlerKey[node.kind],
29
+ )
30
30
 
31
31
  export type FunctionPrinterOptions = {
32
32
  /**
@@ -50,9 +50,9 @@ export type FunctionPrinterOptions = {
50
50
  transformType?: (type: string) => string
51
51
  }
52
52
 
53
- type DefaultPrinter = PrinterFactoryOptions<'functionParameters', FunctionPrinterOptions, string>
53
+ type DefaultPrinter = ast.PrinterFactoryOptions<'functionParameters', FunctionPrinterOptions, string>
54
54
 
55
- function rank(param: FunctionParameterNode | ParameterGroupNode): number {
55
+ function rank(param: ast.FunctionParameterNode | ast.ParameterGroupNode): number {
56
56
  if (param.kind === 'ParameterGroup') {
57
57
  if (param.default) return PARAM_RANK.withDefault
58
58
  const isOptional = param.optional ?? param.properties.every((p) => p.optional || p.default !== undefined)
@@ -63,11 +63,11 @@ function rank(param: FunctionParameterNode | ParameterGroupNode): number {
63
63
  return param.optional ? PARAM_RANK.optional : PARAM_RANK.required
64
64
  }
65
65
 
66
- function sortParams(params: ReadonlyArray<FunctionParameterNode | ParameterGroupNode>): Array<FunctionParameterNode | ParameterGroupNode> {
66
+ function sortParams(params: ReadonlyArray<ast.FunctionParameterNode | ast.ParameterGroupNode>): Array<ast.FunctionParameterNode | ast.ParameterGroupNode> {
67
67
  return [...params].sort((a, b) => rank(a) - rank(b))
68
68
  }
69
69
 
70
- function sortChildParams(params: Array<FunctionParameterNode>): Array<FunctionParameterNode> {
70
+ function sortChildParams(params: Array<ast.FunctionParameterNode>): Array<ast.FunctionParameterNode> {
71
71
  return [...params].sort((a, b) => rank(a) - rank(b))
72
72
  }
73
73
 
@@ -1,6 +1,5 @@
1
- import { extractRefName, isStringType, narrowSchema, schemaTypes, syncSchemaRef } from '@kubb/ast'
2
1
  import type { PrinterFactoryOptions, PrinterPartial } from '@kubb/core'
3
- import { definePrinter } from '@kubb/core'
2
+ import { ast, definePrinter } from '@kubb/core'
4
3
  import { safePrint } from '@kubb/parser-ts'
5
4
  import type ts from 'typescript'
6
5
  import { ENUM_TYPES_WITH_KEY_SUFFIX, OPTIONAL_ADDS_QUESTION_TOKEN, OPTIONAL_ADDS_UNDEFINED } from '../constants.ts'
@@ -154,7 +153,7 @@ export const printerTs = definePrinter<PrinterTs>((options) => {
154
153
  // Use the canonical name from the $ref path — node.name may have been overridden
155
154
  // (e.g. by single-member allOf flatten using the property-derived child name).
156
155
  // Inline refs (without $ref) from utils already carry resolved type names.
157
- const refName = node.ref ? (extractRefName(node.ref) ?? node.name) : node.name
156
+ const refName = node.ref ? (ast.extractRefName(node.ref) ?? node.name) : node.name
158
157
 
159
158
  // When a Key suffix is configured, enum refs must use the suffixed name (e.g. `StatusKey`)
160
159
  // so the reference matches what the enum file actually exports.
@@ -192,15 +191,15 @@ export const printerTs = definePrinter<PrinterTs>((options) => {
192
191
  const members = node.members ?? []
193
192
 
194
193
  const hasStringLiteral = members.some((m) => {
195
- const enumNode = narrowSchema(m, schemaTypes.enum)
194
+ const enumNode = ast.narrowSchema(m, ast.schemaTypes.enum)
196
195
  return enumNode?.primitive === 'string'
197
196
  })
198
- const hasPlainString = members.some((m) => isStringType(m))
197
+ const hasPlainString = members.some((m) => ast.isStringType(m))
199
198
 
200
199
  if (hasStringLiteral && hasPlainString) {
201
200
  const memberNodes = members
202
201
  .map((m) => {
203
- if (isStringType(m)) {
202
+ if (ast.isStringType(m)) {
204
203
  return factory.createIntersectionDeclaration({
205
204
  nodes: [factory.keywordTypeNodes.string, factory.createTypeLiteralNode([])],
206
205
  withParentheses: true,
@@ -235,7 +234,7 @@ export const printerTs = definePrinter<PrinterTs>((options) => {
235
234
  const propertyNodes: Array<ts.TypeElement> = node.properties.map((prop) => {
236
235
  const baseType = transform(prop.schema) ?? factory.keywordTypeNodes.unknown
237
236
  const type = factory.buildPropertyType(prop.schema, baseType, options.optionalType)
238
- const propMeta = syncSchemaRef(prop.schema)
237
+ const propMeta = ast.syncSchemaRef(prop.schema)
239
238
 
240
239
  const propertyNode = factory.createPropertySignature({
241
240
  questionToken: prop.schema.optional || prop.schema.nullish ? addsQuestionToken : false,
@@ -264,7 +263,7 @@ export const printerTs = definePrinter<PrinterTs>((options) => {
264
263
  if (!base) return null
265
264
 
266
265
  // For ref nodes, structural metadata lives on node.schema rather than the ref node itself.
267
- const meta = syncSchemaRef(node)
266
+ const meta = ast.syncSchemaRef(node)
268
267
 
269
268
  // Without name, apply modifiers inline and return.
270
269
  if (!name) {
package/src/types.ts CHANGED
@@ -1,6 +1,5 @@
1
- import type { OperationParamsResolver } from '@kubb/ast'
2
- import type { OperationNode, ParameterNode, StatusCode, Visitor } from '@kubb/ast/types'
3
1
  import type {
2
+ ast,
4
3
  CompatibilityPreset,
5
4
  Exclude,
6
5
  Generator,
@@ -20,7 +19,7 @@ import type { PrinterTsNodes } from './printers/printerTs.ts'
20
19
  * plugin-specific naming helpers for operations, parameters, responses, and schemas.
21
20
  */
22
21
  export type ResolverTs = Resolver &
23
- OperationParamsResolver & {
22
+ ast.OperationParamsResolver & {
24
23
  /**
25
24
  * Resolves the name for a given raw name (equivalent to `default(name, 'function')`).
26
25
  * Since TypeScript only emits types, this is the canonical naming method.
@@ -39,7 +38,7 @@ export type ResolverTs = Resolver &
39
38
  /**
40
39
  * Resolves the request body type name for an operation (required on ResolverTs).
41
40
  */
42
- resolveDataName(node: OperationNode): string
41
+ resolveDataName(node: ast.OperationNode): string
43
42
 
44
43
  /**
45
44
  * Resolves the name for an operation response by status code.
@@ -48,28 +47,28 @@ export type ResolverTs = Resolver &
48
47
  * @example
49
48
  * resolver.resolveResponseStatusName(node, 200) // → 'ListPetsStatus200'
50
49
  */
51
- resolveResponseStatusName(node: OperationNode, statusCode: StatusCode): string
50
+ resolveResponseStatusName(node: ast.OperationNode, statusCode: ast.StatusCode): string
52
51
  /**
53
52
  * Resolves the name for an operation's request config (`RequestConfig`).
54
53
  *
55
54
  * @example
56
55
  * resolver.resolveRequestConfigName(node) // → 'ListPetsRequestConfig'
57
56
  */
58
- resolveRequestConfigName(node: OperationNode): string
57
+ resolveRequestConfigName(node: ast.OperationNode): string
59
58
  /**
60
59
  * Resolves the name for the collection of all operation responses (`Responses`).
61
60
  *
62
61
  * @example
63
62
  * resolver.resolveResponsesName(node) // → 'ListPetsResponses'
64
63
  */
65
- resolveResponsesName(node: OperationNode): string
64
+ resolveResponsesName(node: ast.OperationNode): string
66
65
  /**
67
66
  * Resolves the name for the union of all operation responses (`Response`).
68
67
  *
69
68
  * @example
70
69
  * resolver.resolveResponseName(node) // → 'ListPetsResponse'
71
70
  */
72
- resolveResponseName(node: OperationNode): string
71
+ resolveResponseName(node: ast.OperationNode): string
73
72
  /**
74
73
  * Resolves the TypeScript type alias name for an enum schema's key variant.
75
74
  * Appends `enumTypeSuffix` (default `'Key'`) after applying the default naming convention.
@@ -86,21 +85,21 @@ export type ResolverTs = Resolver &
86
85
  * @example
87
86
  * resolver.resolvePathParamsName(node, param) // → 'GetPetByIdPathParams'
88
87
  */
89
- resolvePathParamsName(node: OperationNode, param: ParameterNode): string
88
+ resolvePathParamsName(node: ast.OperationNode, param: ast.ParameterNode): string
90
89
  /**
91
90
  * Resolves the name for an operation's grouped query parameters type.
92
91
  *
93
92
  * @example
94
93
  * resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'
95
94
  */
96
- resolveQueryParamsName(node: OperationNode, param: ParameterNode): string
95
+ resolveQueryParamsName(node: ast.OperationNode, param: ast.ParameterNode): string
97
96
  /**
98
97
  * Resolves the name for an operation's grouped header parameters type.
99
98
  *
100
99
  * @example
101
100
  * resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'
102
101
  */
103
- resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string
102
+ resolveHeaderParamsName(node: ast.OperationNode, param: ast.ParameterNode): string
104
103
  }
105
104
 
106
105
  type EnumKeyCasing = 'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none'
@@ -274,7 +273,7 @@ export type Options = {
274
273
  * }
275
274
  * ```
276
275
  */
277
- transformer?: Visitor
276
+ transformer?: ast.Visitor
278
277
  /**
279
278
  * Override individual printer node handlers to customize rendering of specific schema types.
280
279
  *
package/src/utils.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { jsStringEscape, stringify } from '@internals/utils'
2
- import { createProperty, createSchema, syncSchemaRef } from '@kubb/ast'
3
- import type { OperationNode, ParameterNode, SchemaNode } from '@kubb/ast/types'
2
+ import { ast } from '@kubb/core'
4
3
  import type { ResolverTs } from './types.ts'
5
4
 
6
5
  /**
@@ -10,8 +9,8 @@ import type { ResolverTs } from './types.ts'
10
9
  * Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
11
10
 
12
11
  */
13
- export function buildPropertyJSDocComments(schema: SchemaNode): Array<string | undefined> {
14
- const meta = syncSchemaRef(schema)
12
+ export function buildPropertyJSDocComments(schema: ast.SchemaNode): Array<string | undefined> {
13
+ const meta = ast.syncSchemaRef(schema)
15
14
 
16
15
  const isArray = meta?.primitive === 'array'
17
16
 
@@ -33,7 +32,7 @@ export function buildPropertyJSDocComments(schema: SchemaNode): Array<string | u
33
32
  }
34
33
 
35
34
  type BuildParamsSchemaOptions = {
36
- params: Array<ParameterNode>
35
+ params: Array<ast.ParameterNode>
37
36
  resolver: ResolverTs
38
37
  }
39
38
 
@@ -41,14 +40,14 @@ type BuildOperationSchemaOptions = {
41
40
  resolver: ResolverTs
42
41
  }
43
42
 
44
- export function buildParams(node: OperationNode, { params, resolver }: BuildParamsSchemaOptions): SchemaNode {
45
- return createSchema({
43
+ export function buildParams(node: ast.OperationNode, { params, resolver }: BuildParamsSchemaOptions): ast.SchemaNode {
44
+ return ast.createSchema({
46
45
  type: 'object',
47
46
  properties: params.map((param) =>
48
- createProperty({
47
+ ast.createProperty({
49
48
  name: param.name,
50
49
  required: param.required,
51
- schema: createSchema({
50
+ schema: ast.createSchema({
52
51
  type: 'ref',
53
52
  name: resolver.resolveParamName(node, param),
54
53
  }),
@@ -57,75 +56,75 @@ export function buildParams(node: OperationNode, { params, resolver }: BuildPara
57
56
  })
58
57
  }
59
58
 
60
- export function buildData(node: OperationNode, { resolver }: BuildOperationSchemaOptions): SchemaNode {
59
+ export function buildData(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode {
61
60
  const pathParams = node.parameters.filter((p) => p.in === 'path')
62
61
  const queryParams = node.parameters.filter((p) => p.in === 'query')
63
62
  const headerParams = node.parameters.filter((p) => p.in === 'header')
64
63
 
65
- return createSchema({
64
+ return ast.createSchema({
66
65
  type: 'object',
67
66
  deprecated: node.deprecated,
68
67
  properties: [
69
- createProperty({
68
+ ast.createProperty({
70
69
  name: 'data',
71
70
  schema: node.requestBody?.schema
72
- ? createSchema({ type: 'ref', name: resolver.resolveDataName(node), optional: true })
73
- : createSchema({ type: 'never', primitive: undefined, optional: true }),
71
+ ? ast.createSchema({ type: 'ref', name: resolver.resolveDataName(node), optional: true })
72
+ : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
74
73
  }),
75
- createProperty({
74
+ ast.createProperty({
76
75
  name: 'pathParams',
77
76
  required: pathParams.length > 0,
78
- schema: pathParams.length > 0 ? buildParams(node, { params: pathParams, resolver }) : createSchema({ type: 'never', primitive: undefined }),
77
+ schema: pathParams.length > 0 ? buildParams(node, { params: pathParams, resolver }) : ast.createSchema({ type: 'never', primitive: undefined }),
79
78
  }),
80
- createProperty({
79
+ ast.createProperty({
81
80
  name: 'queryParams',
82
81
  schema:
83
82
  queryParams.length > 0
84
- ? createSchema({ ...buildParams(node, { params: queryParams, resolver }), optional: true })
85
- : createSchema({ type: 'never', primitive: undefined, optional: true }),
83
+ ? ast.createSchema({ ...buildParams(node, { params: queryParams, resolver }), optional: true })
84
+ : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
86
85
  }),
87
- createProperty({
86
+ ast.createProperty({
88
87
  name: 'headerParams',
89
88
  schema:
90
89
  headerParams.length > 0
91
- ? createSchema({ ...buildParams(node, { params: headerParams, resolver }), optional: true })
92
- : createSchema({ type: 'never', primitive: undefined, optional: true }),
90
+ ? ast.createSchema({ ...buildParams(node, { params: headerParams, resolver }), optional: true })
91
+ : ast.createSchema({ type: 'never', primitive: undefined, optional: true }),
93
92
  }),
94
- createProperty({
93
+ ast.createProperty({
95
94
  name: 'url',
96
95
  required: true,
97
- schema: createSchema({ type: 'url', path: node.path }),
96
+ schema: ast.createSchema({ type: 'url', path: node.path }),
98
97
  }),
99
98
  ],
100
99
  })
101
100
  }
102
101
 
103
- export function buildResponses(node: OperationNode, { resolver }: BuildOperationSchemaOptions): SchemaNode | null {
102
+ export function buildResponses(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode | null {
104
103
  if (node.responses.length === 0) {
105
104
  return null
106
105
  }
107
106
 
108
- return createSchema({
107
+ return ast.createSchema({
109
108
  type: 'object',
110
109
  properties: node.responses.map((res) =>
111
- createProperty({
110
+ ast.createProperty({
112
111
  name: String(res.statusCode),
113
112
  required: true,
114
- schema: createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) }),
113
+ schema: ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) }),
115
114
  }),
116
115
  ),
117
116
  })
118
117
  }
119
118
 
120
- export function buildResponseUnion(node: OperationNode, { resolver }: BuildOperationSchemaOptions): SchemaNode | null {
119
+ export function buildResponseUnion(node: ast.OperationNode, { resolver }: BuildOperationSchemaOptions): ast.SchemaNode | null {
121
120
  const responsesWithSchema = node.responses.filter((res) => res.schema)
122
121
 
123
122
  if (responsesWithSchema.length === 0) {
124
123
  return null
125
124
  }
126
125
 
127
- return createSchema({
126
+ return ast.createSchema({
128
127
  type: 'union',
129
- members: responsesWithSchema.map((res) => createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) })),
128
+ members: responsesWithSchema.map((res) => ast.createSchema({ type: 'ref', name: resolver.resolveResponseStatusName(node, res.statusCode) })),
130
129
  })
131
130
  }
package/src/presets.ts DELETED
@@ -1,28 +0,0 @@
1
- import { definePresets } from '@kubb/core'
2
- import { typeGenerator } from './generators/typeGenerator.tsx'
3
- import { typeGeneratorLegacy } from './generators/typeGeneratorLegacy.tsx'
4
- import { printerTs } from './printers/printerTs.ts'
5
- import { resolverTs } from './resolvers/resolverTs.ts'
6
- import { resolverTsLegacy } from './resolvers/resolverTsLegacy.ts'
7
- import type { ResolverTs } from './types.ts'
8
-
9
- /**
10
- * Built-in preset registry for `@kubb/plugin-ts`.
11
- *
12
- * - `default` — uses `resolverTs` and `typeGenerator` (current naming conventions).
13
- * - `kubbV4` — uses `resolverTsLegacy` and `typeGeneratorLegacy` (Kubb v4 naming conventions).
14
- */
15
- export const presets = definePresets<ResolverTs>({
16
- default: {
17
- name: 'default',
18
- resolver: resolverTs,
19
- generators: [typeGenerator],
20
- printer: printerTs,
21
- },
22
- kubbV4: {
23
- name: 'kubbV4',
24
- resolver: resolverTsLegacy,
25
- generators: [typeGeneratorLegacy],
26
- printer: printerTs,
27
- },
28
- })