@kubb/plugin-ts 5.0.0-beta.42 → 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.
@@ -1,83 +0,0 @@
1
- import { camelCase, trimQuotes } from '@internals/utils'
2
- import type { ast } from '@kubb/core'
3
- import { parserTs } from '@kubb/parser-ts'
4
- import { File } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import { ENUM_TYPES_WITH_KEY_SUFFIX, ENUM_TYPES_WITH_RUNTIME_VALUE, ENUM_TYPES_WITH_TYPE_ONLY } from '../constants.ts'
7
- import * as factory from '../factory.ts'
8
- import type { PluginTs, ResolverTs } from '../types.ts'
9
-
10
- type Props = {
11
- node: ast.EnumSchemaNode
12
- enumType: PluginTs['resolvedOptions']['enumType']
13
- enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix']
14
- enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
15
- resolver: ResolverTs
16
- key?: string | number | null
17
- }
18
-
19
- /**
20
- * Resolves the runtime identifier name and the TypeScript type name for an enum schema node.
21
- *
22
- * The raw `node.name` may be a YAML key such as `"enumNames.Type"` which is not a
23
- * valid TypeScript identifier. The resolver normalizes it; for inline enum
24
- * properties the adapter already emits a PascalCase+suffix name so resolution is typically a no-op.
25
- */
26
- export function getEnumNames({
27
- node,
28
- enumType,
29
- enumTypeSuffix,
30
- resolver,
31
- }: {
32
- node: ast.EnumSchemaNode
33
- enumType: PluginTs['resolvedOptions']['enumType']
34
- enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix']
35
- resolver: ResolverTs
36
- }): {
37
- enumName: string
38
- typeName: string
39
- } {
40
- const resolved = resolver.default(node.name!, 'type')
41
- const enumName = enumType === 'asPascalConst' ? resolved : camelCase(node.name!)
42
- const typeName = ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) ? resolver.resolveEnumKeyName(node, enumTypeSuffix) : resolved
43
-
44
- return { enumName, typeName }
45
- }
46
-
47
- /**
48
- * Renders the enum declaration(s) for a single named `EnumSchemaNode`.
49
- *
50
- * Depending on `enumType` this may emit:
51
- * - A runtime object (`asConst` / `asPascalConst`) plus a `typeof` type alias
52
- * - A `const enum` or plain `enum` declaration (`constEnum` / `enum`)
53
- * - A union literal type alias (`literal`)
54
- *
55
- * The emitted `File.Source` nodes carry the resolved names so that the barrel
56
- * index picks up the correct export identifiers.
57
- */
58
- export function Enum({ node, enumType, enumTypeSuffix, enumKeyCasing, resolver }: Props): KubbReactNode {
59
- const { enumName, typeName } = getEnumNames({ node, enumType, enumTypeSuffix, resolver })
60
-
61
- const [nameNode, typeNode] = factory.createEnumDeclaration({
62
- name: enumName,
63
- typeName,
64
- enums: (node.namedEnumValues?.map((v) => [trimQuotes(v.name.toString()), v.value]) ??
65
- node.enumValues?.filter((v): v is NonNullable<typeof v> => v !== null && v !== undefined).map((v) => [trimQuotes(v.toString()), v]) ??
66
- []) as Array<[string | number, string | number | boolean]>,
67
- type: enumType,
68
- enumKeyCasing,
69
- })
70
-
71
- return (
72
- <>
73
- {nameNode && (
74
- <File.Source name={enumName} isExportable isIndexable isTypeOnly={false}>
75
- {parserTs.print(nameNode)}
76
- </File.Source>
77
- )}
78
- <File.Source name={typeName} isIndexable isExportable={ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumType)} isTypeOnly={ENUM_TYPES_WITH_TYPE_ONLY.has(enumType)}>
79
- {parserTs.print(typeNode)}
80
- </File.Source>
81
- </>
82
- )
83
- }
@@ -1,60 +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
- enumType: PluginTs['resolvedOptions']['enumType']
17
- enumTypeSuffix: PluginTs['resolvedOptions']['enumTypeSuffix']
18
- enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
19
- resolver: ResolverTs
20
- }
21
-
22
- export function Type({ name, node, printer, enumType, enumTypeSuffix, enumKeyCasing, resolver }: Props): KubbReactNode {
23
- const enumSchemaNodes = ast.collect<ast.EnumSchemaNode>(node, {
24
- schema(n): ast.EnumSchemaNode | undefined {
25
- const enumNode = ast.narrowSchema(n, ast.schemaTypes.enum)
26
- if (enumNode?.name) return enumNode
27
- },
28
- })
29
-
30
- const output = printer.print(node)
31
-
32
- if (!output) {
33
- return
34
- }
35
-
36
- const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
37
- return {
38
- node,
39
- ...getEnumNames({ node, enumType, enumTypeSuffix, resolver }),
40
- }
41
- })
42
-
43
- // Skip enum exports when using inlineLiteral
44
- const shouldExportEnums = enumType !== 'inlineLiteral'
45
- const shouldExportType = enumType === 'inlineLiteral' || enums.every((item) => item.typeName !== name)
46
-
47
- return (
48
- <>
49
- {shouldExportEnums &&
50
- enums.map(({ node }) => (
51
- <Enum key={node.name} node={node} enumType={enumType} enumTypeSuffix={enumTypeSuffix} enumKeyCasing={enumKeyCasing} resolver={resolver} />
52
- ))}
53
- {shouldExportType && (
54
- <File.Source name={name} isTypeOnly isExportable isIndexable>
55
- {output}
56
- </File.Source>
57
- )}
58
- </>
59
- )
60
- }
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']['enumType']
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
- * `enumType` values that append a `Key` suffix to the generated enum type alias.
18
- */
19
- export const ENUM_TYPES_WITH_KEY_SUFFIX = new Set<EnumType>(['asConst', 'asPascalConst'] as const)
20
-
21
- /**
22
- * `enumType` 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', 'asPascalConst', 'constEnum', 'literal', undefined] as const)
25
-
26
- /**
27
- * `enumType` 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', 'asPascalConst', '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