@kubb/plugin-ts 5.0.0-beta.56 → 5.0.0-beta.64
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 +139 -115
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +31 -19
- package/dist/index.js +140 -117
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
- package/src/components/Enum.tsx +0 -87
- package/src/components/Type.tsx +0 -55
- package/src/constants.ts +0 -39
- package/src/factory.ts +0 -970
- package/src/generators/typeGenerator.tsx +0 -274
- package/src/index.ts +0 -9
- package/src/plugin.ts +0 -95
- package/src/printers/functionPrinter.ts +0 -197
- package/src/printers/printerTs.ts +0 -315
- package/src/resolvers/resolverTs.ts +0 -69
- package/src/types.ts +0 -335
- package/src/utils.ts +0 -140
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-ts",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.64",
|
|
4
4
|
"description": "Generate TypeScript types, interfaces, and enums from your OpenAPI specification. The foundational plugin that powers type safety across the entire Kubb ecosystem.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-generation",
|
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
"directory": "packages/plugin-ts"
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
|
-
"src",
|
|
26
25
|
"dist",
|
|
27
26
|
"!/**/**.test.**",
|
|
28
27
|
"!/**/__tests__/**",
|
|
@@ -45,10 +44,10 @@
|
|
|
45
44
|
"registry": "https://registry.npmjs.org/"
|
|
46
45
|
},
|
|
47
46
|
"dependencies": {
|
|
48
|
-
"@kubb/ast": "5.0.0-beta.
|
|
49
|
-
"@kubb/core": "5.0.0-beta.
|
|
50
|
-
"@kubb/parser-ts": "5.0.0-beta.
|
|
51
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
47
|
+
"@kubb/ast": "5.0.0-beta.63",
|
|
48
|
+
"@kubb/core": "5.0.0-beta.63",
|
|
49
|
+
"@kubb/parser-ts": "5.0.0-beta.63",
|
|
50
|
+
"@kubb/renderer-jsx": "5.0.0-beta.63",
|
|
52
51
|
"typescript": "^6.0.3"
|
|
53
52
|
},
|
|
54
53
|
"devDependencies": {
|
|
@@ -56,7 +55,7 @@
|
|
|
56
55
|
"@internals/utils": "0.0.0"
|
|
57
56
|
},
|
|
58
57
|
"peerDependencies": {
|
|
59
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
58
|
+
"@kubb/renderer-jsx": "5.0.0-beta.63"
|
|
60
59
|
},
|
|
61
60
|
"engines": {
|
|
62
61
|
"node": ">=22"
|
package/src/components/Enum.tsx
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { camelCase } from '@internals/utils'
|
|
2
|
-
import { trimQuotes } from '@kubb/ast/utils'
|
|
3
|
-
import type { ast } from '@kubb/core'
|
|
4
|
-
import { parserTs } from '@kubb/parser-ts'
|
|
5
|
-
import { File } from '@kubb/renderer-jsx'
|
|
6
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
7
|
-
import { ENUM_TYPES_WITH_KEY_SUFFIX, ENUM_TYPES_WITH_RUNTIME_VALUE, ENUM_TYPES_WITH_TYPE_ONLY } from '../constants.ts'
|
|
8
|
-
import * as factory from '../factory.ts'
|
|
9
|
-
import type { PluginTs, ResolverTs } from '../types.ts'
|
|
10
|
-
|
|
11
|
-
type EnumOptions = PluginTs['resolvedOptions']['enum']
|
|
12
|
-
|
|
13
|
-
type Props = {
|
|
14
|
-
node: ast.EnumSchemaNode
|
|
15
|
-
enum: EnumOptions
|
|
16
|
-
resolver: ResolverTs
|
|
17
|
-
key?: string | number | null
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Resolves the runtime identifier name and the TypeScript type name for an enum schema node.
|
|
22
|
-
*
|
|
23
|
-
* The raw `node.name` may be a YAML key such as `"enumNames.Type"` which is not a
|
|
24
|
-
* valid TypeScript identifier. The resolver normalizes it. For inline enum properties the adapter
|
|
25
|
-
* already emits a PascalCase+suffix name, so resolution is typically a no-op.
|
|
26
|
-
*
|
|
27
|
-
* When `constCasing` is `'pascalCase'` and `typeSuffix` is empty, the const and the type
|
|
28
|
-
* resolve to the same name, which TypeScript merges into a single value+type declaration.
|
|
29
|
-
*/
|
|
30
|
-
export function getEnumNames({ node, enum: enumOptions, resolver }: { node: ast.EnumSchemaNode; enum: EnumOptions; resolver: ResolverTs }): {
|
|
31
|
-
enumName: string
|
|
32
|
-
typeName: string
|
|
33
|
-
} {
|
|
34
|
-
const resolved = resolver.default(node.name!, 'type')
|
|
35
|
-
const enumName = enumOptions.constCasing === 'pascalCase' ? resolved : camelCase(node.name!)
|
|
36
|
-
const typeName = ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) ? resolver.resolveEnumKeyName(node, enumOptions.typeSuffix) : resolved
|
|
37
|
-
|
|
38
|
-
return { enumName, typeName }
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Renders the enum declaration(s) for a single named `EnumSchemaNode`.
|
|
43
|
-
*
|
|
44
|
-
* Depending on `enum.type` this may emit:
|
|
45
|
-
* - A runtime object (`asConst`) plus a `typeof` type alias
|
|
46
|
-
* - A `const enum` or plain `enum` declaration (`constEnum` / `enum`)
|
|
47
|
-
* - A union literal type alias (`literal`)
|
|
48
|
-
*
|
|
49
|
-
* The emitted `File.Source` nodes carry the resolved names so that the barrel
|
|
50
|
-
* index picks up the correct export identifiers.
|
|
51
|
-
*/
|
|
52
|
-
export function Enum({ node, enum: enumOptions, resolver }: Props): KubbReactNode {
|
|
53
|
-
const { enumName, typeName } = getEnumNames({ node, enum: enumOptions, resolver })
|
|
54
|
-
|
|
55
|
-
const [nameNode, typeNode] = factory.createEnumDeclaration({
|
|
56
|
-
name: enumName,
|
|
57
|
-
typeName,
|
|
58
|
-
enums: (node.namedEnumValues?.map((v) => [trimQuotes(v.name.toString()), v.value]) ??
|
|
59
|
-
node.enumValues?.filter((v): v is NonNullable<typeof v> => v !== null && v !== undefined).map((v) => [trimQuotes(v.toString()), v]) ??
|
|
60
|
-
[]) as Array<[string | number, string | number | boolean]>,
|
|
61
|
-
type: enumOptions.type,
|
|
62
|
-
enumKeyCasing: enumOptions.keyCasing,
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
// When the const and the type share a name (pascalCase const + empty typeSuffix) they merge into
|
|
66
|
-
// one declaration. The const carries the barrel export, so keep the type alias in the file but out
|
|
67
|
-
// of the barrel to avoid re-exporting the name a second time as `export type { … }`.
|
|
68
|
-
const namesMerge = !!nameNode && enumName === typeName
|
|
69
|
-
|
|
70
|
-
return (
|
|
71
|
-
<>
|
|
72
|
-
{nameNode && (
|
|
73
|
-
<File.Source name={enumName} isExportable isIndexable isTypeOnly={false}>
|
|
74
|
-
{parserTs.print(nameNode)}
|
|
75
|
-
</File.Source>
|
|
76
|
-
)}
|
|
77
|
-
<File.Source
|
|
78
|
-
name={typeName}
|
|
79
|
-
isIndexable={!namesMerge}
|
|
80
|
-
isExportable={!namesMerge && ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumOptions.type)}
|
|
81
|
-
isTypeOnly={ENUM_TYPES_WITH_TYPE_ONLY.has(enumOptions.type)}
|
|
82
|
-
>
|
|
83
|
-
{parserTs.print(typeNode)}
|
|
84
|
-
</File.Source>
|
|
85
|
-
</>
|
|
86
|
-
)
|
|
87
|
-
}
|
package/src/components/Type.tsx
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
import { File } from '@kubb/renderer-jsx'
|
|
3
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
4
|
-
import type { PrinterTsFactory } from '../printers/printerTs.ts'
|
|
5
|
-
import type { PluginTs, ResolverTs } from '../types.ts'
|
|
6
|
-
import { Enum, getEnumNames } from './Enum.tsx'
|
|
7
|
-
|
|
8
|
-
type Props = {
|
|
9
|
-
name: string
|
|
10
|
-
node: ast.SchemaNode
|
|
11
|
-
/**
|
|
12
|
-
* Pre-configured printer instance created by the generator.
|
|
13
|
-
* Created with `printerTs({ ..., nodes: options.printer?.nodes })`.
|
|
14
|
-
*/
|
|
15
|
-
printer: ast.Printer<PrinterTsFactory>
|
|
16
|
-
enum: PluginTs['resolvedOptions']['enum']
|
|
17
|
-
resolver: ResolverTs
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function Type({ name, node, printer, enum: enumOptions, resolver }: Props): KubbReactNode {
|
|
21
|
-
const enumSchemaNodes = ast.collect<ast.EnumSchemaNode>(node, {
|
|
22
|
-
schema(n): ast.EnumSchemaNode | undefined {
|
|
23
|
-
const enumNode = ast.narrowSchema(n, ast.schemaTypes.enum)
|
|
24
|
-
if (enumNode?.name) return enumNode
|
|
25
|
-
},
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
const output = printer.print(node)
|
|
29
|
-
|
|
30
|
-
if (!output) {
|
|
31
|
-
return
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
35
|
-
return {
|
|
36
|
-
node,
|
|
37
|
-
...getEnumNames({ node, enum: enumOptions, resolver }),
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
// Skip enum exports when using inlineLiteral
|
|
42
|
-
const shouldExportEnums = enumOptions.type !== 'inlineLiteral'
|
|
43
|
-
const shouldExportType = enumOptions.type === 'inlineLiteral' || enums.every((item) => item.typeName !== name)
|
|
44
|
-
|
|
45
|
-
return (
|
|
46
|
-
<>
|
|
47
|
-
{shouldExportEnums && enums.map(({ node }) => <Enum key={node.name} node={node} enum={enumOptions} resolver={resolver} />)}
|
|
48
|
-
{shouldExportType && (
|
|
49
|
-
<File.Source name={name} isTypeOnly isExportable isIndexable>
|
|
50
|
-
{output}
|
|
51
|
-
</File.Source>
|
|
52
|
-
)}
|
|
53
|
-
</>
|
|
54
|
-
)
|
|
55
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { PluginTs } from './types.ts'
|
|
2
|
-
|
|
3
|
-
type OptionalType = PluginTs['resolvedOptions']['optionalType']
|
|
4
|
-
type EnumType = PluginTs['resolvedOptions']['enum']['type']
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* `optionalType` values that cause a property's type to include `| undefined`.
|
|
8
|
-
*/
|
|
9
|
-
export const OPTIONAL_ADDS_UNDEFINED = new Set<OptionalType>(['undefined', 'questionTokenAndUndefined'] as const)
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* `optionalType` values that render the property key with a `?` token.
|
|
13
|
-
*/
|
|
14
|
-
export const OPTIONAL_ADDS_QUESTION_TOKEN = new Set<OptionalType>(['questionToken', 'questionTokenAndUndefined'] as const)
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* `enum.type` values that append a `typeSuffix` to the generated enum type alias.
|
|
18
|
-
*/
|
|
19
|
-
export const ENUM_TYPES_WITH_KEY_SUFFIX = new Set<EnumType>(['asConst'] as const)
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* `enum.type` values that require a runtime value declaration (object, enum, or literal).
|
|
23
|
-
*/
|
|
24
|
-
export const ENUM_TYPES_WITH_RUNTIME_VALUE = new Set<EnumType | undefined>(['enum', 'asConst', 'constEnum', 'literal', undefined] as const)
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* `enum.type` values whose type declaration is type-only (no runtime value emitted for the type alias).
|
|
28
|
-
*/
|
|
29
|
-
export const ENUM_TYPES_WITH_TYPE_ONLY = new Set<EnumType | undefined>(['asConst', 'literal', undefined] as const)
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Ordering priority for function parameters: lower = sorted earlier.
|
|
33
|
-
*/
|
|
34
|
-
export const PARAM_RANK = {
|
|
35
|
-
required: 0,
|
|
36
|
-
optional: 1,
|
|
37
|
-
withDefault: 2,
|
|
38
|
-
rest: 3,
|
|
39
|
-
} as const
|