@kubb/plugin-mcp 5.0.0-beta.3 → 5.0.0-beta.30

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/src/utils.ts CHANGED
@@ -1,19 +1,5 @@
1
- import { camelCase } from '@internals/utils'
2
1
  import type { ast } from '@kubb/core'
3
2
 
4
- /**
5
- * Find the first 2xx response status code from an operation's responses.
6
- */
7
- export function findSuccessStatusCode(responses: Array<{ statusCode: number | string }>): ast.StatusCode | undefined {
8
- for (const res of responses) {
9
- const code = Number(res.statusCode)
10
- if (code >= 200 && code < 300) {
11
- return res.statusCode as ast.StatusCode
12
- }
13
- }
14
- return undefined
15
- }
16
-
17
3
  export type ZodParam = {
18
4
  name: string
19
5
  schemaName: string
@@ -31,83 +17,32 @@ export function zodGroupExpr(entry: string | Array<ZodParam>): string {
31
17
  return `z.object({ ${entries.join(', ')} })`
32
18
  }
33
19
 
34
- /**
35
- * Build JSDoc comment lines from an OperationNode.
36
- */
37
- export function getComments(node: ast.OperationNode): Array<string> {
38
- return [
39
- node.description && `@description ${node.description}`,
40
- node.summary && `@summary ${node.summary}`,
41
- node.deprecated && '@deprecated',
42
- `{@link ${node.path.replaceAll('{', ':').replaceAll('}', '')}}`,
43
- ].filter((x): x is string => Boolean(x))
44
- }
45
-
46
- /**
47
- * Build a mapping of original param names → camelCase names.
48
- * Returns `undefined` when no names actually change (no remapping needed).
49
- */
50
- export function getParamsMapping(params: Array<{ name: string }>): Record<string, string> | undefined {
51
- if (!params.length) {
52
- return undefined
53
- }
54
-
55
- const mapping: Record<string, string> = {}
56
- let hasDifference = false
57
-
58
- for (const p of params) {
59
- const camelName = camelCase(p.name)
60
- mapping[p.name] = camelName
61
- if (p.name !== camelName) {
62
- hasDifference = true
63
- }
64
- }
65
-
66
- return hasDifference ? mapping : undefined
67
- }
68
-
69
20
  /**
70
21
  * Convert a SchemaNode type to an inline Zod expression string.
71
22
  * Used as fallback when no named zod schema is available for a path parameter.
72
23
  */
73
24
  export function zodExprFromSchemaNode(schema: ast.SchemaNode): string {
74
- let expr: string
75
- switch (schema.type) {
76
- case 'enum': {
77
- // namedEnumValues takes priority over enumValues
25
+ const baseExpr = (() => {
26
+ if (schema.type === 'enum') {
78
27
  const rawValues: Array<string | number | boolean> = schema.namedEnumValues?.length
79
28
  ? schema.namedEnumValues.map((v) => v.value)
80
29
  : (schema.enumValues ?? []).filter((v): v is string | number | boolean => v !== null)
81
30
 
82
31
  if (rawValues.length > 0 && rawValues.every((v) => typeof v === 'string')) {
83
- expr = `z.enum([${rawValues.map((v) => JSON.stringify(v)).join(', ')}])`
84
- } else if (rawValues.length > 0) {
32
+ return `z.enum([${rawValues.map((v) => JSON.stringify(v)).join(', ')}])`
33
+ }
34
+ if (rawValues.length > 0) {
85
35
  const literals = rawValues.map((v) => `z.literal(${JSON.stringify(v)})`)
86
- expr = literals.length === 1 ? literals[0]! : `z.union([${literals.join(', ')}])`
87
- } else {
88
- expr = 'z.string()'
36
+ return literals.length === 1 ? literals[0]! : `z.union([${literals.join(', ')}])`
89
37
  }
90
- break
38
+ return 'z.string()'
91
39
  }
92
- case 'integer':
93
- expr = 'z.coerce.number()'
94
- break
95
- case 'number':
96
- expr = 'z.number()'
97
- break
98
- case 'boolean':
99
- expr = 'z.boolean()'
100
- break
101
- case 'array':
102
- expr = 'z.array(z.unknown())'
103
- break
104
- default:
105
- expr = 'z.string()'
106
- }
107
-
108
- if (schema.nullable) {
109
- expr = `${expr}.nullable()`
110
- }
111
-
112
- return expr
40
+ if (schema.type === 'integer') return 'z.coerce.number()'
41
+ if (schema.type === 'number') return 'z.number()'
42
+ if (schema.type === 'boolean') return 'z.boolean()'
43
+ if (schema.type === 'array') return 'z.array(z.unknown())'
44
+ return 'z.string()'
45
+ })()
46
+
47
+ return schema.nullable ? `${baseExpr}.nullable()` : baseExpr
113
48
  }