@kubb/plugin-ts 5.0.0-alpha.2 → 5.0.0-alpha.20
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/Type-B70QnSzH.cjs +688 -0
- package/dist/Type-B70QnSzH.cjs.map +1 -0
- package/dist/Type-CMC7L-38.js +671 -0
- package/dist/Type-CMC7L-38.js.map +1 -0
- package/dist/casing-Cp-jbC_k.js +84 -0
- package/dist/casing-Cp-jbC_k.js.map +1 -0
- package/dist/casing-D2uQKLWS.cjs +144 -0
- package/dist/casing-D2uQKLWS.cjs.map +1 -0
- package/dist/components.cjs +3 -2
- package/dist/components.d.ts +41 -11
- package/dist/components.js +2 -2
- package/dist/generators-BFkr7ecU.js +556 -0
- package/dist/generators-BFkr7ecU.js.map +1 -0
- package/dist/generators-xHWQCNd9.cjs +560 -0
- package/dist/generators-xHWQCNd9.cjs.map +1 -0
- package/dist/generators.cjs +2 -2
- package/dist/generators.d.ts +3 -491
- package/dist/generators.js +1 -1
- package/dist/index.cjs +146 -3
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +145 -1
- package/dist/index.js.map +1 -0
- package/dist/resolvers-DsKabI0F.js +184 -0
- package/dist/resolvers-DsKabI0F.js.map +1 -0
- package/dist/resolvers-YIpeP5YD.cjs +194 -0
- package/dist/resolvers-YIpeP5YD.cjs.map +1 -0
- package/dist/resolvers.cjs +4 -0
- package/dist/resolvers.d.ts +52 -0
- package/dist/resolvers.js +2 -0
- package/dist/types-zqLMbIqZ.d.ts +340 -0
- package/package.json +15 -8
- package/src/components/Enum.tsx +83 -0
- package/src/components/Type.tsx +25 -144
- package/src/components/index.ts +1 -0
- package/src/constants.ts +29 -0
- package/src/factory.ts +14 -16
- package/src/generators/typeGenerator.tsx +221 -414
- package/src/generators/utils.ts +308 -0
- package/src/index.ts +1 -1
- package/src/plugin.ts +74 -87
- package/src/presets.ts +23 -0
- package/src/printer.ts +256 -92
- package/src/resolvers/index.ts +2 -0
- package/src/resolvers/resolverTs.ts +104 -0
- package/src/resolvers/resolverTsLegacy.ts +87 -0
- package/src/types.ts +234 -63
- package/dist/components-9wydyqUx.cjs +0 -848
- package/dist/components-9wydyqUx.cjs.map +0 -1
- package/dist/components-LmqJfxMv.js +0 -721
- package/dist/components-LmqJfxMv.js.map +0 -1
- package/dist/plugin-CNkzbtpl.cjs +0 -508
- package/dist/plugin-CNkzbtpl.cjs.map +0 -1
- package/dist/plugin-DoLrDl9P.js +0 -476
- package/dist/plugin-DoLrDl9P.js.map +0 -1
- package/dist/types-BpeKGgCn.d.ts +0 -170
- package/src/parser.ts +0 -396
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
+
import { CompatibilityPreset, Group, Output, PluginFactoryOptions, Resolver } from "@kubb/core";
|
|
3
|
+
import { OperationNode, ParameterNode, SchemaNode, StatusCode, Visitor } from "@kubb/ast/types";
|
|
4
|
+
import { Oas, contentType } from "@kubb/oas";
|
|
5
|
+
import { Exclude, Include, Override, ResolvePathOptions } from "@kubb/plugin-oas";
|
|
6
|
+
import { Generator } from "@kubb/plugin-oas/generators";
|
|
7
|
+
|
|
8
|
+
//#region src/types.d.ts
|
|
9
|
+
/**
|
|
10
|
+
* The concrete resolver type for `@kubb/plugin-ts`.
|
|
11
|
+
* Extends the base `Resolver` (which provides `default` and `resolveOptions`) with
|
|
12
|
+
* plugin-specific naming helpers for operations, parameters, responses, and schemas.
|
|
13
|
+
*/
|
|
14
|
+
type ResolverTs = Resolver & {
|
|
15
|
+
/**
|
|
16
|
+
* Resolves the variable/function name for a given raw name (equivalent to `default(name, 'function')`).
|
|
17
|
+
* Use this shorthand when matching the `name` field produced by the v2 TypeGenerator,
|
|
18
|
+
* so call-sites don't need to repeat the `'function'` type literal.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* resolver.resolveName('list pets status 200') // → 'listPetsStatus200'
|
|
22
|
+
*/
|
|
23
|
+
resolveName(name: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Resolves the TypeScript type name for a given raw name (equivalent to `default(name, 'type')`).
|
|
26
|
+
* Use this shorthand when matching the `typedName` field produced by the v2 TypeGenerator,
|
|
27
|
+
* so call-sites don't need to repeat the `'type'` type literal.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* resolver.resolveTypedName('list pets status 200') // → 'ListPetsStatus200'
|
|
31
|
+
*/
|
|
32
|
+
resolveTypedName(name: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Resolves the file/path name for a given identifier using PascalCase.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* resolver.resolvePathName('list pets', 'file') // → 'ListPets'
|
|
38
|
+
*/
|
|
39
|
+
resolvePathName(name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
|
|
40
|
+
/**
|
|
41
|
+
* Resolves the variable/function name for an operation parameter.
|
|
42
|
+
* Encapsulates the `<operationId> <PascalCase(paramIn)> <paramName>` naming convention.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* resolver.resolveParamName(node, param) // → 'ListPetsQueryLimit'
|
|
46
|
+
*/
|
|
47
|
+
resolveParamName(node: OperationNode, param: ParameterNode): string;
|
|
48
|
+
/**
|
|
49
|
+
* Resolves the TypeScript type alias name for an operation parameter
|
|
50
|
+
* (equivalent to `resolveParamName` with `type: 'type'`).
|
|
51
|
+
* In the default implementation both return the same PascalCase string;
|
|
52
|
+
* the distinction is preserved so overrides can diverge the two forms.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* resolver.resolveParamTypedName(node, param) // → 'ListPetsQueryLimit'
|
|
56
|
+
*/
|
|
57
|
+
resolveParamTypedName(node: OperationNode, param: ParameterNode): string;
|
|
58
|
+
/**
|
|
59
|
+
* Resolves the variable/function name for an operation response by status code.
|
|
60
|
+
* Encapsulates the `<operationId> Status <statusCode>` template with PascalCase applied to the result.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* resolver.resolveResponseStatusName(node, 200) // → 'ListPetsStatus200'
|
|
64
|
+
*/
|
|
65
|
+
resolveResponseStatusName(node: OperationNode, statusCode: StatusCode): string;
|
|
66
|
+
/**
|
|
67
|
+
* Resolves the TypeScript type alias name for an operation response by status code.
|
|
68
|
+
* Encapsulates the `<operationId> Status <statusCode>` template with PascalCase applied to the result.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* resolver.resolveResponseStatusTypedName(node, 200) // → 'ListPetsStatus200'
|
|
72
|
+
*/
|
|
73
|
+
resolveResponseStatusTypedName(node: OperationNode, statusCode: StatusCode): string;
|
|
74
|
+
/**
|
|
75
|
+
* Resolves the variable/function name for an operation's request body (`Data`).
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* resolver.resolveDataName(node) // → 'ListPetsData'
|
|
79
|
+
*/
|
|
80
|
+
resolveDataName(node: OperationNode): string;
|
|
81
|
+
/**
|
|
82
|
+
* Resolves the TypeScript type alias name for an operation's request body (`Data`).
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* resolver.resolveDataTypedName(node) // → 'ListPetsData'
|
|
86
|
+
*/
|
|
87
|
+
resolveDataTypedName(node: OperationNode): string;
|
|
88
|
+
/**
|
|
89
|
+
* Resolves the variable/function name for an operation's request config (`RequestConfig`).
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* resolver.resolveRequestConfigName(node) // → 'ListPetsRequestConfig'
|
|
93
|
+
*/
|
|
94
|
+
resolveRequestConfigName(node: OperationNode): string;
|
|
95
|
+
/**
|
|
96
|
+
* Resolves the TypeScript type alias name for an operation's request config (`RequestConfig`).
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* resolver.resolveRequestConfigTypedName(node) // → 'ListPetsRequestConfig'
|
|
100
|
+
*/
|
|
101
|
+
resolveRequestConfigTypedName(node: OperationNode): string;
|
|
102
|
+
/**
|
|
103
|
+
* Resolves the variable/function name for the collection of all operation responses (`Responses`).
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* resolver.resolveResponsesName(node) // → 'ListPetsResponses'
|
|
107
|
+
*/
|
|
108
|
+
resolveResponsesName(node: OperationNode): string;
|
|
109
|
+
/**
|
|
110
|
+
* Resolves the TypeScript type alias name for the collection of all operation responses.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* resolver.resolveResponsesTypedName(node) // → 'ListPetsResponses'
|
|
114
|
+
*/
|
|
115
|
+
resolveResponsesTypedName(node: OperationNode): string;
|
|
116
|
+
/**
|
|
117
|
+
* Resolves the variable/function name for the union of all operation responses (`Response`).
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* resolver.resolveResponseName(node) // → 'ListPetsResponse'
|
|
121
|
+
*/
|
|
122
|
+
resolveResponseName(node: OperationNode): string;
|
|
123
|
+
/**
|
|
124
|
+
* Resolves the TypeScript type alias name for the union of all operation responses.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* resolver.resolveResponseTypedName(node) // → 'ListPetsResponse'
|
|
128
|
+
*/
|
|
129
|
+
resolveResponseTypedName(node: OperationNode): string;
|
|
130
|
+
/**
|
|
131
|
+
* Resolves the TypeScript type alias name for an enum schema's key variant.
|
|
132
|
+
* Appends the `Key` suffix after applying the default naming convention.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* resolver.resolveEnumKeyTypedName(node) // → 'PetStatusKey'
|
|
136
|
+
*/
|
|
137
|
+
resolveEnumKeyTypedName(node: SchemaNode): string;
|
|
138
|
+
/**
|
|
139
|
+
* Resolves the variable/function name for an operation's grouped path parameters type.
|
|
140
|
+
* Only available with `compatibilityPreset: 'kubbV4'`.
|
|
141
|
+
*
|
|
142
|
+
* @deprecated Kubb v4 compatibility only — use `resolveParamName` per individual parameter instead.
|
|
143
|
+
* @example
|
|
144
|
+
* resolver.resolvePathParamsName(node) // → 'GetPetByIdPathParams'
|
|
145
|
+
*/
|
|
146
|
+
resolvePathParamsName?(node: OperationNode): string;
|
|
147
|
+
/**
|
|
148
|
+
* Resolves the TypeScript type alias name for an operation's grouped path parameters type.
|
|
149
|
+
* Only available with `compatibilityPreset: 'kubbV4'`.
|
|
150
|
+
*
|
|
151
|
+
* @deprecated Kubb v4 compatibility only — use `resolveParamTypedName` per individual parameter instead.
|
|
152
|
+
* @example
|
|
153
|
+
* resolver.resolvePathParamsTypedName(node) // → 'GetPetByIdPathParams'
|
|
154
|
+
*/
|
|
155
|
+
resolvePathParamsTypedName?(node: OperationNode): string;
|
|
156
|
+
/**
|
|
157
|
+
* Resolves the variable/function name for an operation's grouped query parameters type.
|
|
158
|
+
* Only available with `compatibilityPreset: 'kubbV4'`.
|
|
159
|
+
*
|
|
160
|
+
* @deprecated Kubb v4 compatibility only — use `resolveParamName` per individual parameter instead.
|
|
161
|
+
* @example
|
|
162
|
+
* resolver.resolveQueryParamsName(node) // → 'FindPetsByStatusQueryParams'
|
|
163
|
+
*/
|
|
164
|
+
resolveQueryParamsName?(node: OperationNode): string;
|
|
165
|
+
/**
|
|
166
|
+
* Resolves the TypeScript type alias name for an operation's grouped query parameters type.
|
|
167
|
+
* Only available with `compatibilityPreset: 'kubbV4'`.
|
|
168
|
+
*
|
|
169
|
+
* @deprecated Kubb v4 compatibility only — use `resolveParamTypedName` per individual parameter instead.
|
|
170
|
+
* @example
|
|
171
|
+
* resolver.resolveQueryParamsTypedName(node) // → 'FindPetsByStatusQueryParams'
|
|
172
|
+
*/
|
|
173
|
+
resolveQueryParamsTypedName?(node: OperationNode): string;
|
|
174
|
+
/**
|
|
175
|
+
* Resolves the variable/function name for an operation's grouped header parameters type.
|
|
176
|
+
* Only available with `compatibilityPreset: 'kubbV4'`.
|
|
177
|
+
*
|
|
178
|
+
* @deprecated Kubb v4 compatibility only — use `resolveParamName` per individual parameter instead.
|
|
179
|
+
* @example
|
|
180
|
+
* resolver.resolveHeaderParamsName(node) // → 'DeletePetHeaderParams'
|
|
181
|
+
*/
|
|
182
|
+
resolveHeaderParamsName?(node: OperationNode): string;
|
|
183
|
+
/**
|
|
184
|
+
* Resolves the TypeScript type alias name for an operation's grouped header parameters type.
|
|
185
|
+
* Only available with `compatibilityPreset: 'kubbV4'`.
|
|
186
|
+
*
|
|
187
|
+
* @deprecated Kubb v4 compatibility only — use `resolveParamTypedName` per individual parameter instead.
|
|
188
|
+
* @example
|
|
189
|
+
* resolver.resolveHeaderParamsTypedName(node) // → 'DeletePetHeaderParams'
|
|
190
|
+
*/
|
|
191
|
+
resolveHeaderParamsTypedName?(node: OperationNode): string;
|
|
192
|
+
};
|
|
193
|
+
type Options = {
|
|
194
|
+
/**
|
|
195
|
+
* Specify the export location for the files and define the behavior of the output
|
|
196
|
+
* @default { path: 'types', barrelType: 'named' }
|
|
197
|
+
*/
|
|
198
|
+
output?: Output<Oas>;
|
|
199
|
+
/**
|
|
200
|
+
* Define which contentType should be used.
|
|
201
|
+
* By default, uses the first valid JSON media type.
|
|
202
|
+
*/
|
|
203
|
+
contentType?: contentType;
|
|
204
|
+
/**
|
|
205
|
+
* Group the clients based on the provided name.
|
|
206
|
+
*/
|
|
207
|
+
group?: Group;
|
|
208
|
+
/**
|
|
209
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
210
|
+
*/
|
|
211
|
+
exclude?: Array<Exclude>;
|
|
212
|
+
/**
|
|
213
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
214
|
+
*/
|
|
215
|
+
include?: Array<Include>;
|
|
216
|
+
/**
|
|
217
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
218
|
+
*/
|
|
219
|
+
override?: Array<Override<ResolvedOptions>>;
|
|
220
|
+
/**
|
|
221
|
+
* Choose to use enum, asConst, asPascalConst, constEnum, literal, or inlineLiteral for enums.
|
|
222
|
+
* - 'enum' generates TypeScript enum declarations.
|
|
223
|
+
* - 'asConst' generates const objects with camelCase names and as const assertion.
|
|
224
|
+
* - 'asPascalConst' generates const objects with PascalCase names and as const assertion.
|
|
225
|
+
* - 'constEnum' generates TypeScript const enum declarations.
|
|
226
|
+
* - 'literal' generates literal union types.
|
|
227
|
+
* - 'inlineLiteral' inline enum values directly into the type (default in v5).
|
|
228
|
+
* @default 'asConst'
|
|
229
|
+
* @note In Kubb v5, 'inlineLiteral' becomes the default.
|
|
230
|
+
*/
|
|
231
|
+
enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral';
|
|
232
|
+
/**
|
|
233
|
+
* Choose the casing for enum key names.
|
|
234
|
+
* - 'screamingSnakeCase' generates keys in SCREAMING_SNAKE_CASE format.
|
|
235
|
+
* - 'snakeCase' generates keys in snake_case format.
|
|
236
|
+
* - 'pascalCase' generates keys in PascalCase format.
|
|
237
|
+
* - 'camelCase' generates keys in camelCase format.
|
|
238
|
+
* - 'none' uses the enum value as-is without transformation.
|
|
239
|
+
* @default 'none'
|
|
240
|
+
*/
|
|
241
|
+
enumKeyCasing?: 'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none';
|
|
242
|
+
/**
|
|
243
|
+
* Switch between type or interface for creating TypeScript types.
|
|
244
|
+
* - 'type' generates type alias declarations.
|
|
245
|
+
* - 'interface' generates interface declarations.
|
|
246
|
+
* @default 'type'
|
|
247
|
+
*/
|
|
248
|
+
syntaxType?: 'type' | 'interface';
|
|
249
|
+
/**
|
|
250
|
+
* Choose what to use as mode for an optional value.
|
|
251
|
+
* - 'questionToken' marks the property as optional with ? (e.g., type?: string).
|
|
252
|
+
* - 'undefined' adds undefined to the type union (e.g., type: string | undefined).
|
|
253
|
+
* - 'questionTokenAndUndefined' combines both approaches (e.g., type?: string | undefined).
|
|
254
|
+
* @default 'questionToken'
|
|
255
|
+
*/
|
|
256
|
+
optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined';
|
|
257
|
+
/**
|
|
258
|
+
* Choose between Array<string> or string[] for array types.
|
|
259
|
+
* - 'generic' generates Array<Type> syntax.
|
|
260
|
+
* - 'array' generates Type[] syntax.
|
|
261
|
+
* @default 'array'
|
|
262
|
+
*/
|
|
263
|
+
arrayType?: 'generic' | 'array';
|
|
264
|
+
/**
|
|
265
|
+
* How to style your params, by default no casing is applied
|
|
266
|
+
* - 'camelcase' uses camelCase for pathParams, queryParams and headerParams property names
|
|
267
|
+
* @default undefined
|
|
268
|
+
* @note response types (data/body) are NOT affected by this option
|
|
269
|
+
*/
|
|
270
|
+
paramsCasing?: 'camelcase';
|
|
271
|
+
/**
|
|
272
|
+
* Define some generators next to the ts generators
|
|
273
|
+
*/
|
|
274
|
+
generators?: Array<Generator<PluginTs>>;
|
|
275
|
+
/**
|
|
276
|
+
* Unstable naming for v5
|
|
277
|
+
*/
|
|
278
|
+
UNSTABLE_NAMING?: true;
|
|
279
|
+
/**
|
|
280
|
+
* Apply a compatibility naming preset.
|
|
281
|
+
* Use `kubbV4` for strict v4 type-generation compatibility.
|
|
282
|
+
* You can further customize naming with `resolvers`.
|
|
283
|
+
* @default 'default'
|
|
284
|
+
*/
|
|
285
|
+
compatibilityPreset?: CompatibilityPreset;
|
|
286
|
+
/**
|
|
287
|
+
* Array of named resolvers that control naming conventions.
|
|
288
|
+
* Later entries override earlier ones (last wins).
|
|
289
|
+
* Built-in: `resolverTs` (default), `resolverTsLegacy`.
|
|
290
|
+
* @default [resolverTs]
|
|
291
|
+
*/
|
|
292
|
+
resolvers?: Array<ResolverTs>;
|
|
293
|
+
/**
|
|
294
|
+
* Array of AST visitors applied to each SchemaNode/OperationNode before printing.
|
|
295
|
+
* Uses `transform()` from `@kubb/ast` — visitors can modify, replace, or annotate nodes.
|
|
296
|
+
*
|
|
297
|
+
* @example Remove writeOnly properties from response types
|
|
298
|
+
* ```ts
|
|
299
|
+
* transformers: [{
|
|
300
|
+
* property(node) {
|
|
301
|
+
* if (node.schema.writeOnly) return undefined
|
|
302
|
+
* }
|
|
303
|
+
* }]
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* @example Force all dates to plain strings
|
|
307
|
+
* ```ts
|
|
308
|
+
* transformers: [{
|
|
309
|
+
* schema(node) {
|
|
310
|
+
* if (node.type === 'date') return { ...node, type: 'string' }
|
|
311
|
+
* }
|
|
312
|
+
* }]
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
transformers?: Array<Visitor>;
|
|
316
|
+
};
|
|
317
|
+
type ResolvedOptions = {
|
|
318
|
+
output: Output<Oas>;
|
|
319
|
+
group: Options['group'];
|
|
320
|
+
override: NonNullable<Options['override']>;
|
|
321
|
+
enumType: NonNullable<Options['enumType']>;
|
|
322
|
+
enumKeyCasing: NonNullable<Options['enumKeyCasing']>;
|
|
323
|
+
optionalType: NonNullable<Options['optionalType']>;
|
|
324
|
+
arrayType: NonNullable<Options['arrayType']>;
|
|
325
|
+
syntaxType: NonNullable<Options['syntaxType']>;
|
|
326
|
+
paramsCasing: Options['paramsCasing'];
|
|
327
|
+
compatibilityPreset: NonNullable<CompatibilityPreset>;
|
|
328
|
+
resolver: ResolverTs;
|
|
329
|
+
/**
|
|
330
|
+
* The resolver without user naming overrides applied.
|
|
331
|
+
* Used internally to derive stable names for unnamed enums and grouped params
|
|
332
|
+
* so that the schema tree stays consistent regardless of `transformers.name` / custom resolvers.
|
|
333
|
+
*/
|
|
334
|
+
baseResolver: ResolverTs;
|
|
335
|
+
transformers: Array<Visitor>;
|
|
336
|
+
};
|
|
337
|
+
type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions, ResolverTs>;
|
|
338
|
+
//#endregion
|
|
339
|
+
export { PluginTs as n, ResolverTs as r, Options as t };
|
|
340
|
+
//# sourceMappingURL=types-zqLMbIqZ.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-ts",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.20",
|
|
4
4
|
"description": "TypeScript code generation plugin for Kubb, transforming OpenAPI schemas into TypeScript interfaces, types, and utility functions.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -39,6 +39,10 @@
|
|
|
39
39
|
"import": "./dist/generators.js",
|
|
40
40
|
"require": "./dist/generators.cjs"
|
|
41
41
|
},
|
|
42
|
+
"./resolvers": {
|
|
43
|
+
"import": "./dist/resolvers.js",
|
|
44
|
+
"require": "./dist/resolvers.cjs"
|
|
45
|
+
},
|
|
42
46
|
"./package.json": "./package.json"
|
|
43
47
|
},
|
|
44
48
|
"types": "./dist/index.d.ts",
|
|
@@ -49,6 +53,9 @@
|
|
|
49
53
|
],
|
|
50
54
|
"generators": [
|
|
51
55
|
"./dist/generators.d.ts"
|
|
56
|
+
],
|
|
57
|
+
"resolvers": [
|
|
58
|
+
"./dist/resolvers.d.ts"
|
|
52
59
|
]
|
|
53
60
|
}
|
|
54
61
|
},
|
|
@@ -67,17 +74,17 @@
|
|
|
67
74
|
}
|
|
68
75
|
],
|
|
69
76
|
"dependencies": {
|
|
70
|
-
"@kubb/fabric-core": "0.
|
|
71
|
-
"@kubb/react-fabric": "0.
|
|
77
|
+
"@kubb/fabric-core": "0.15.1",
|
|
78
|
+
"@kubb/react-fabric": "0.15.1",
|
|
72
79
|
"remeda": "^2.33.6",
|
|
73
80
|
"typescript": "5.9.3",
|
|
74
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
75
|
-
"@kubb/core": "5.0.0-alpha.
|
|
76
|
-
"@kubb/oas": "5.0.0-alpha.
|
|
77
|
-
"@kubb/plugin-oas": "5.0.0-alpha.
|
|
81
|
+
"@kubb/ast": "5.0.0-alpha.20",
|
|
82
|
+
"@kubb/core": "5.0.0-alpha.20",
|
|
83
|
+
"@kubb/oas": "5.0.0-alpha.20",
|
|
84
|
+
"@kubb/plugin-oas": "5.0.0-alpha.20"
|
|
78
85
|
},
|
|
79
86
|
"peerDependencies": {
|
|
80
|
-
"@kubb/react-fabric": "0.
|
|
87
|
+
"@kubb/react-fabric": "0.15.1"
|
|
81
88
|
},
|
|
82
89
|
"engines": {
|
|
83
90
|
"node": ">=22"
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { camelCase, trimQuotes } from '@internals/utils'
|
|
2
|
+
import type { EnumSchemaNode } from '@kubb/ast/types'
|
|
3
|
+
import { safePrint } from '@kubb/fabric-core/parsers/typescript'
|
|
4
|
+
import { File } from '@kubb/react-fabric'
|
|
5
|
+
import type { FabricReactNode } from '@kubb/react-fabric/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: EnumSchemaNode
|
|
12
|
+
enumType: PluginTs['resolvedOptions']['enumType']
|
|
13
|
+
enumKeyCasing: PluginTs['resolvedOptions']['enumKeyCasing']
|
|
14
|
+
resolver: ResolverTs
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the runtime identifier name and the TypeScript type name for an enum schema node.
|
|
19
|
+
*
|
|
20
|
+
* The raw `node.name` may be a YAML key such as `"enumNames.Type"` which is not a
|
|
21
|
+
* valid TypeScript identifier. The resolver normalizes it; for inline enum
|
|
22
|
+
* properties the adapter already emits a PascalCase+suffix name so resolution is typically a no-op.
|
|
23
|
+
*/
|
|
24
|
+
export function getEnumNames({ node, enumType, resolver }: { node: EnumSchemaNode; enumType: PluginTs['resolvedOptions']['enumType']; resolver: ResolverTs }): {
|
|
25
|
+
enumName: string
|
|
26
|
+
typeName: string
|
|
27
|
+
/**
|
|
28
|
+
* The PascalCase name that `$ref` importers will use to reference this enum type.
|
|
29
|
+
* For `asConst`/`asPascalConst` this differs from `typeName` (which has a `Key` suffix).
|
|
30
|
+
*/
|
|
31
|
+
refName: string
|
|
32
|
+
} {
|
|
33
|
+
const resolved = resolver.default(node.name!, 'type')
|
|
34
|
+
const enumName = enumType === 'asPascalConst' ? resolved : camelCase(node.name!)
|
|
35
|
+
const typeName = ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) ? `${resolved}Key` : resolved
|
|
36
|
+
|
|
37
|
+
return { enumName, typeName, refName: resolved }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Renders the enum declaration(s) for a single named `EnumSchemaNode`.
|
|
42
|
+
*
|
|
43
|
+
* Depending on `enumType` this may emit:
|
|
44
|
+
* - A runtime object (`asConst` / `asPascalConst`) plus a `typeof` type alias
|
|
45
|
+
* - A `const enum` or plain `enum` declaration (`constEnum` / `enum`)
|
|
46
|
+
* - A union literal type alias (`literal`)
|
|
47
|
+
*
|
|
48
|
+
* The emitted `File.Source` nodes carry the resolved names so that the barrel
|
|
49
|
+
* index picks up the correct export identifiers.
|
|
50
|
+
*/
|
|
51
|
+
export function Enum({ node, enumType, enumKeyCasing, resolver }: Props): FabricReactNode {
|
|
52
|
+
const { enumName, typeName, refName } = getEnumNames({ node, enumType, resolver })
|
|
53
|
+
|
|
54
|
+
const [nameNode, typeNode] = factory.createEnumDeclaration({
|
|
55
|
+
name: enumName,
|
|
56
|
+
typeName,
|
|
57
|
+
enums: (node.namedEnumValues?.map((v) => [trimQuotes(v.name.toString()), v.value]) ??
|
|
58
|
+
node.enumValues?.filter((v): v is NonNullable<typeof v> => v !== null && v !== undefined).map((v) => [trimQuotes(v.toString()), v]) ??
|
|
59
|
+
[]) as unknown as Array<[string, string]>,
|
|
60
|
+
type: enumType,
|
|
61
|
+
enumKeyCasing,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const needsRefAlias = ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && refName !== typeName
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<>
|
|
68
|
+
{nameNode && (
|
|
69
|
+
<File.Source name={enumName} isExportable isIndexable isTypeOnly={false}>
|
|
70
|
+
{safePrint(nameNode)}
|
|
71
|
+
</File.Source>
|
|
72
|
+
)}
|
|
73
|
+
<File.Source name={typeName} isIndexable isExportable={ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumType)} isTypeOnly={ENUM_TYPES_WITH_TYPE_ONLY.has(enumType)}>
|
|
74
|
+
{safePrint(typeNode)}
|
|
75
|
+
</File.Source>
|
|
76
|
+
{needsRefAlias && (
|
|
77
|
+
<File.Source name={refName} isExportable isIndexable isTypeOnly>
|
|
78
|
+
{`export type ${refName} = ${typeName}`}
|
|
79
|
+
</File.Source>
|
|
80
|
+
)}
|
|
81
|
+
</>
|
|
82
|
+
)
|
|
83
|
+
}
|