@kubb/plugin-ts 5.5.2 → 5.5.4
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 +2 -2
- package/dist/index.js +2 -2
- package/docs/guides/customization.md +66 -8
- package/docs/options.md +61 -60
- package/docs/overview.md +38 -14
- package/kubb-extension.json +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -1952,11 +1952,11 @@ const typeGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
1952
1952
|
});
|
|
1953
1953
|
// kubb-extension.json
|
|
1954
1954
|
var compatibility = {
|
|
1955
|
-
"kubb": ">=5.
|
|
1955
|
+
"kubb": ">=5.19.0",
|
|
1956
1956
|
"node": ">=22.22"
|
|
1957
1957
|
};
|
|
1958
1958
|
// package.json
|
|
1959
|
-
var version = "5.5.
|
|
1959
|
+
var version = "5.5.4";
|
|
1960
1960
|
// src/resolvers/resolverTs.ts
|
|
1961
1961
|
/**
|
|
1962
1962
|
* Default resolver used by `@kubb/plugin-ts`. Decides the names and file paths
|
package/dist/index.js
CHANGED
|
@@ -1948,11 +1948,11 @@ const typeGenerator = defineGenerator({
|
|
|
1948
1948
|
});
|
|
1949
1949
|
// kubb-extension.json
|
|
1950
1950
|
var compatibility = {
|
|
1951
|
-
"kubb": ">=5.
|
|
1951
|
+
"kubb": ">=5.19.0",
|
|
1952
1952
|
"node": ">=22.22"
|
|
1953
1953
|
};
|
|
1954
1954
|
// package.json
|
|
1955
|
-
var version = "5.5.
|
|
1955
|
+
var version = "5.5.4";
|
|
1956
1956
|
// src/resolvers/resolverTs.ts
|
|
1957
1957
|
/**
|
|
1958
1958
|
* Default resolver used by `@kubb/plugin-ts`. Decides the names and file paths
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# Customize generated types
|
|
2
2
|
|
|
3
|
-
Prefix TypeScript names, change schema type
|
|
3
|
+
Prefix TypeScript names, change how a schema type prints, and remove descriptions.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Add each change to `pluginTs()` in the [TypeScript plugin configuration](https://kubb.dev/extensions/kubb-labs/plugin-ts#example).
|
|
6
6
|
|
|
7
7
|
## Prefix type names
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Override `resolver.name` and call `resolverTs.name` first, so the plugin's PascalCase rule still applies.
|
|
10
|
+
|
|
11
|
+
::code-group
|
|
10
12
|
|
|
11
13
|
```typescript [kubb.config.ts]
|
|
12
14
|
import { pluginTs, resolverTs } from '@kubb/plugin-ts'
|
|
@@ -20,11 +22,23 @@ pluginTs({
|
|
|
20
22
|
})
|
|
21
23
|
```
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
```typescript [Before]
|
|
26
|
+
export type Pet = { name: string }
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```typescript [After]
|
|
30
|
+
export type ApiPet = { name: string }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
::
|
|
34
|
+
|
|
35
|
+
File names stay the same unless you also override `resolver.file`. See every [resolver member](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#resolver-members).
|
|
24
36
|
|
|
25
37
|
## Map schema types
|
|
26
38
|
|
|
27
|
-
Replace [printer
|
|
39
|
+
Replace a [printer handler](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#printer). Each handler returns the TypeScript type as a string.
|
|
40
|
+
|
|
41
|
+
::code-group
|
|
28
42
|
|
|
29
43
|
```typescript [kubb.config.ts]
|
|
30
44
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
@@ -43,14 +57,40 @@ pluginTs({
|
|
|
43
57
|
})
|
|
44
58
|
```
|
|
45
59
|
|
|
46
|
-
|
|
60
|
+
```typescript [Before]
|
|
61
|
+
export type Pet = {
|
|
62
|
+
birthday?: string
|
|
63
|
+
age?: number
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```typescript [After]
|
|
68
|
+
export type Pet = {
|
|
69
|
+
birthday?: Date
|
|
70
|
+
age?: bigint
|
|
71
|
+
}
|
|
72
|
+
```
|
|
47
73
|
|
|
48
|
-
|
|
74
|
+
::
|
|
75
|
+
|
|
76
|
+
Each schema has its own handler:
|
|
77
|
+
|
|
78
|
+
| Schema | Handler |
|
|
79
|
+
| --------------------------------------- | ------------------------------------- |
|
|
80
|
+
| `format: date` | `date` |
|
|
81
|
+
| `format: date-time` | `datetime` |
|
|
82
|
+
| `integer`, `format: int32` or no format | `integer` |
|
|
83
|
+
| `integer`, `format: int64` | `bigint`, already `bigint` by default |
|
|
84
|
+
|
|
85
|
+
> [!WARNING]
|
|
86
|
+
> A handler that returns a TypeScript compiler node is deprecated and is removed in the next major. Until then, it needs `@kubb/parser-ts`, plus `typescript` 5 or 6, or `@typescript/typescript6` next to TypeScript 7. Without `@kubb/parser-ts`, the build stops with an error that tells you to install it.
|
|
49
87
|
|
|
50
88
|
## Remove descriptions
|
|
51
89
|
|
|
52
90
|
Clear schema descriptions with a [macro](https://kubb.dev/docs/5.x/reference/plugin-options#macros).
|
|
53
91
|
|
|
92
|
+
::code-group
|
|
93
|
+
|
|
54
94
|
```typescript [kubb.config.ts]
|
|
55
95
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
56
96
|
import { defineMacro } from 'kubb/kit'
|
|
@@ -67,4 +107,22 @@ const dropDescriptions = defineMacro({
|
|
|
67
107
|
pluginTs({ macros: [dropDescriptions] })
|
|
68
108
|
```
|
|
69
109
|
|
|
70
|
-
|
|
110
|
+
```typescript [Before]
|
|
111
|
+
export type Pet = {
|
|
112
|
+
/**
|
|
113
|
+
* @description The name of the pet
|
|
114
|
+
* @type string
|
|
115
|
+
*/
|
|
116
|
+
name: string
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```typescript [After]
|
|
121
|
+
export type Pet = {
|
|
122
|
+
name: string
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
::
|
|
127
|
+
|
|
128
|
+
The OpenAPI document stays unchanged. The same macro works with `pluginZod`, with [one exception](https://kubb.dev/extensions/kubb-labs/plugin-zod/guides/customization#remove-descriptions).
|
package/docs/options.md
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
# Options
|
|
2
2
|
|
|
3
|
-
Pass these options to `pluginTs()`. Shared options link to [Shared plugin options](https://kubb.dev/docs/5.x/reference/plugin-options)
|
|
3
|
+
Pass these options to `pluginTs()`. Shared options link to [Shared plugin options](https://kubb.dev/docs/5.x/reference/plugin-options), and this page gives only their default here.
|
|
4
4
|
|
|
5
5
|
## Options overview
|
|
6
6
|
|
|
7
|
-
| Option | Purpose
|
|
8
|
-
| ------------------------------------------------------------------------- |
|
|
9
|
-
| [`output`](https://kubb.dev/docs/5.x/reference/plugin-options#output) | Where files are written
|
|
10
|
-
| [`group`](https://kubb.dev/docs/5.x/reference/plugin-options#group) | Split output by tag or path
|
|
11
|
-
| [`enum`](#enum) | How enums are generated and
|
|
12
|
-
| [`
|
|
13
|
-
| [`
|
|
14
|
-
| [`
|
|
15
|
-
| [`
|
|
16
|
-
| [`
|
|
17
|
-
| [`
|
|
18
|
-
| [`
|
|
19
|
-
| [`
|
|
20
|
-
| [`
|
|
21
|
-
| [`
|
|
7
|
+
| Option | Purpose | Default |
|
|
8
|
+
| ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------- |
|
|
9
|
+
| [`output`](https://kubb.dev/docs/5.x/reference/plugin-options#output) | Where files are written. | `{ path: 'types', barrel: { type: 'named' } }` |
|
|
10
|
+
| [`group`](https://kubb.dev/docs/5.x/reference/plugin-options#group) | Split output by tag or path. | None |
|
|
11
|
+
| [`enum`](#enum) | How enums are generated and named. | `{ type: 'asConst', constCasing: 'camelCase', typeSuffix: 'Key', keyCasing: 'none' }` |
|
|
12
|
+
| ↳ [`enum.type`](#enumtype) | Representation of each enum. | `'asConst'` |
|
|
13
|
+
| ↳ [`enum.constCasing`](#enumconstcasing) | Casing of the `as const` object. | `'camelCase'` |
|
|
14
|
+
| ↳ [`enum.typeSuffix`](#enumtypesuffix) | Suffix of the companion type. | `'Key'` |
|
|
15
|
+
| ↳ [`enum.keyCasing`](#enumkeycasing) | Casing of enum keys. | `'none'` |
|
|
16
|
+
| [`syntaxType`](#syntaxtype) | Type aliases or interfaces for object schemas. | `'type'` |
|
|
17
|
+
| [`optionalType`](#optionaltype) | How optional properties are written. | `'questionToken'` |
|
|
18
|
+
| [`arrayType`](#arraytype) | `Type[]` or `Array<Type>`. | `'array'` |
|
|
19
|
+
| [`operations`](#operations) | Per-operation type files, or inline types in consumers. | `'export'` |
|
|
20
|
+
| [`printer`](#printer) | Replace the handler for a schema type. | None |
|
|
21
|
+
| [`include`](https://kubb.dev/docs/5.x/reference/plugin-options#include) | Keep only operations and schemas that match. | None |
|
|
22
|
+
| [`exclude`](https://kubb.dev/docs/5.x/reference/plugin-options#exclude) | Skip operations and schemas that match. | `[]` |
|
|
23
|
+
| [`override`](https://kubb.dev/docs/5.x/reference/plugin-options#override) | Apply different options per pattern. | `[]` |
|
|
24
|
+
| [`resolver`](https://kubb.dev/docs/5.x/reference/plugin-options#resolver) | Customize generated names and file paths. See [resolver members](#resolver-members). | `resolverTs` |
|
|
25
|
+
| [`macros`](https://kubb.dev/docs/5.x/reference/plugin-options#macros) | Rewrite AST nodes before printing. | `[]` |
|
|
22
26
|
|
|
23
27
|
## Option details
|
|
24
28
|
|
|
25
29
|
### enum
|
|
26
30
|
|
|
27
|
-
How
|
|
31
|
+
How enums are generated and how their names are cased.
|
|
28
32
|
|
|
29
33
|
| | |
|
|
30
34
|
| -------- | ------------------------------------------------------------------------------------- |
|
|
@@ -32,7 +36,7 @@ How OpenAPI enums are represented in the generated TypeScript, and how their nam
|
|
|
32
36
|
| Required | `false` |
|
|
33
37
|
| Default | `{ type: 'asConst', constCasing: 'camelCase', typeSuffix: 'Key', keyCasing: 'none' }` |
|
|
34
38
|
|
|
35
|
-
`constCasing` and `typeSuffix` apply only to `
|
|
39
|
+
`constCasing` and `typeSuffix` apply only to `'asConst'`. `keyCasing` also applies to `'enum'` and `'constEnum'`.
|
|
36
40
|
|
|
37
41
|
#### enum.type
|
|
38
42
|
|
|
@@ -47,23 +51,23 @@ Representation of each enum.
|
|
|
47
51
|
::field-group
|
|
48
52
|
|
|
49
53
|
:::field{name="'asConst'"}
|
|
50
|
-
|
|
54
|
+
Writes an `as const` object and a union type of its values. Bundlers drop the object when nothing uses it.
|
|
51
55
|
:::
|
|
52
56
|
|
|
53
57
|
:::field{name="'enum'"}
|
|
54
|
-
|
|
58
|
+
Writes a TypeScript `enum`, which emits JavaScript runtime code.
|
|
55
59
|
:::
|
|
56
60
|
|
|
57
61
|
:::field{name="'constEnum'"}
|
|
58
|
-
|
|
62
|
+
Writes a `const enum`, inlined at compile time. With `--isolatedModules`, code cannot use it through a `.d.ts` file, such as the types of a published package.
|
|
59
63
|
:::
|
|
60
64
|
|
|
61
65
|
:::field{name="'literal'"}
|
|
62
|
-
|
|
66
|
+
Writes a named union type with no runtime value.
|
|
63
67
|
:::
|
|
64
68
|
|
|
65
69
|
:::field{name="'inlineLiteral'"}
|
|
66
|
-
|
|
70
|
+
Inlines the `'literal'` union of an enum declared on a property. A component enum reached through a `$ref` keeps its named type.
|
|
67
71
|
:::
|
|
68
72
|
|
|
69
73
|
::
|
|
@@ -96,7 +100,7 @@ export type PetStatus = 'available' | 'pending' | 'sold'
|
|
|
96
100
|
|
|
97
101
|
#### enum.constCasing
|
|
98
102
|
|
|
99
|
-
Casing of the
|
|
103
|
+
Casing of the const object when `type` is `'asConst'`.
|
|
100
104
|
|
|
101
105
|
| | |
|
|
102
106
|
| -------- | ----------------------------- |
|
|
@@ -111,14 +115,14 @@ Names the const `petStatus`.
|
|
|
111
115
|
:::
|
|
112
116
|
|
|
113
117
|
:::field{name="'pascalCase'"}
|
|
114
|
-
Names the const `PetStatus`,
|
|
118
|
+
Names the const `PetStatus`, the same as the schema.
|
|
115
119
|
:::
|
|
116
120
|
|
|
117
121
|
::
|
|
118
122
|
|
|
119
123
|
#### enum.typeSuffix
|
|
120
124
|
|
|
121
|
-
Suffix
|
|
125
|
+
Suffix of the companion type when `type` is `'asConst'`. The const name never changes. Set `''` with `constCasing: 'pascalCase'` to give the const and the type one name, `PetStatus`.
|
|
122
126
|
|
|
123
127
|
| | |
|
|
124
128
|
| -------- | -------- |
|
|
@@ -132,7 +136,7 @@ export type PetStatusValue = (typeof petStatus)[keyof typeof petStatus]
|
|
|
132
136
|
|
|
133
137
|
#### enum.keyCasing
|
|
134
138
|
|
|
135
|
-
Casing
|
|
139
|
+
Casing of enum key names.
|
|
136
140
|
|
|
137
141
|
| | |
|
|
138
142
|
| -------- | ------------------------------------------------------------------------------ |
|
|
@@ -140,26 +144,28 @@ Casing applied to enum key names.
|
|
|
140
144
|
| Required | `false` |
|
|
141
145
|
| Default | `'none'` |
|
|
142
146
|
|
|
147
|
+
For the enum value `enum value`, each casing writes this key:
|
|
148
|
+
|
|
143
149
|
::field-group
|
|
144
150
|
|
|
145
151
|
:::field{name="'none'"}
|
|
146
|
-
Keeps the raw
|
|
152
|
+
Keeps the raw value from the spec.
|
|
147
153
|
:::
|
|
148
154
|
|
|
149
155
|
:::field{name="'camelCase'"}
|
|
150
|
-
|
|
156
|
+
Writes `enumValue`.
|
|
151
157
|
:::
|
|
152
158
|
|
|
153
159
|
:::field{name="'pascalCase'"}
|
|
154
|
-
|
|
160
|
+
Writes `EnumValue`.
|
|
155
161
|
:::
|
|
156
162
|
|
|
157
163
|
:::field{name="'snakeCase'"}
|
|
158
|
-
|
|
164
|
+
Writes `enum_value`.
|
|
159
165
|
:::
|
|
160
166
|
|
|
161
167
|
:::field{name="'screamingSnakeCase'"}
|
|
162
|
-
|
|
168
|
+
Writes `ENUM_VALUE`.
|
|
163
169
|
:::
|
|
164
170
|
|
|
165
171
|
::
|
|
@@ -177,11 +183,11 @@ How object schemas are declared.
|
|
|
177
183
|
::field-group
|
|
178
184
|
|
|
179
185
|
:::field{name="'type'"}
|
|
180
|
-
|
|
186
|
+
Writes `export type Pet = { name: string }`.
|
|
181
187
|
:::
|
|
182
188
|
|
|
183
189
|
:::field{name="'interface'"}
|
|
184
|
-
|
|
190
|
+
Writes `export interface Pet { name: string }`. Use it when consumers need [declaration merging](https://www.totaltypescript.com/type-vs-interface-which-should-you-use).
|
|
185
191
|
:::
|
|
186
192
|
|
|
187
193
|
::
|
|
@@ -199,15 +205,15 @@ How optional properties are written.
|
|
|
199
205
|
::field-group
|
|
200
206
|
|
|
201
207
|
:::field{name="'questionToken'"}
|
|
202
|
-
Writes `type?: string
|
|
208
|
+
Writes `type?: string`. The property may be missing.
|
|
203
209
|
:::
|
|
204
210
|
|
|
205
211
|
:::field{name="'undefined'"}
|
|
206
|
-
Writes `type: string | undefined
|
|
212
|
+
Writes `type: string | undefined`. The property must exist but may be `undefined`.
|
|
207
213
|
:::
|
|
208
214
|
|
|
209
215
|
:::field{name="'questionTokenAndUndefined'"}
|
|
210
|
-
Writes `type?: string | undefined
|
|
216
|
+
Writes `type?: string | undefined`. The property may be missing or `undefined`. Use it with `"exactOptionalPropertyTypes": true`.
|
|
211
217
|
:::
|
|
212
218
|
|
|
213
219
|
::
|
|
@@ -225,18 +231,18 @@ Syntax for array types.
|
|
|
225
231
|
::field-group
|
|
226
232
|
|
|
227
233
|
:::field{name="'array'"}
|
|
228
|
-
|
|
234
|
+
Writes `tags: string[]`.
|
|
229
235
|
:::
|
|
230
236
|
|
|
231
237
|
:::field{name="'generic'"}
|
|
232
|
-
|
|
238
|
+
Writes `tags: Array<string>`, which reads better for complex elements.
|
|
233
239
|
:::
|
|
234
240
|
|
|
235
241
|
::
|
|
236
242
|
|
|
237
243
|
### operations
|
|
238
244
|
|
|
239
|
-
Whether each operation gets its own types file
|
|
245
|
+
Whether each operation gets its own types file, with types such as `AddPetOptions`, `AddPetResponses`, and `AddPetStatus200`.
|
|
240
246
|
|
|
241
247
|
| | |
|
|
242
248
|
| -------- | ---------------------- |
|
|
@@ -247,29 +253,35 @@ Whether each operation gets its own types file (`AddPetOptions`, `AddPetResponse
|
|
|
247
253
|
::field-group
|
|
248
254
|
|
|
249
255
|
:::field{name="'export'"}
|
|
250
|
-
|
|
256
|
+
Writes one file per operation with its aliases and aggregates, such as `export type AddPetStatus200 = Pet`.
|
|
251
257
|
:::
|
|
252
258
|
|
|
253
259
|
:::field{name="'inline'"}
|
|
254
|
-
|
|
260
|
+
Writes only the schema types. Plugins that use them, such as clients and MSW handlers, print the operation types inline, such as `Options<{ body: Order; path?: never; query?: never; headers?: never }>`. An enum declared inside an operation becomes a literal union. Needs a Kubb version with inline sources.
|
|
255
261
|
:::
|
|
256
262
|
|
|
257
263
|
::
|
|
258
264
|
|
|
259
|
-
###
|
|
265
|
+
### printer
|
|
266
|
+
|
|
267
|
+
Replaces the handler for a schema type such as `'integer'` or `'date'`. Each handler returns the TypeScript type as a string. Inside a handler, `this.transform(node)` prints a nested node, and `this.options` holds the printer options. See the [printer guide](https://kubb.dev/docs/5.x/how-to/customize#printers).
|
|
268
|
+
|
|
269
|
+
| | |
|
|
270
|
+
| -------- | ---------------------------- |
|
|
271
|
+
| Type | `{ nodes?: PrinterTsNodes }` |
|
|
272
|
+
| Required | `false` |
|
|
273
|
+
|
|
274
|
+
> [!WARNING]
|
|
275
|
+
> Returning a TypeScript compiler node is deprecated. See [Map schema types](https://kubb.dev/extensions/kubb-labs/plugin-ts/guides/customization#map-schema-types) for an example of a string handler.
|
|
260
276
|
|
|
261
|
-
|
|
277
|
+
## Resolver members
|
|
262
278
|
|
|
263
|
-
|
|
264
|
-
| -------- | --------------------------- |
|
|
265
|
-
| Type | `ResolverPatch<ResolverTs>` |
|
|
266
|
-
| Required | `false` |
|
|
279
|
+
Members you leave out of a `resolver` patch keep `resolverTs`. Besides the shared `name`, `file`, and `imports` members of [`resolver`](https://kubb.dev/docs/5.x/reference/plugin-options#resolver), `resolverTs` has these:
|
|
267
280
|
|
|
268
281
|
::code-collapse{name="ResolverTs patch members"}
|
|
269
282
|
|
|
270
283
|
```typescript [Partial override]
|
|
271
284
|
type ResolverTsPatch = {
|
|
272
|
-
// plus the shared name, file, and imports members
|
|
273
285
|
param?: {
|
|
274
286
|
name?(node: OperationNode, param: ParameterNode): string // → 'DeletePetPathPetId'
|
|
275
287
|
path?(node: OperationNode, param: ParameterNode): string // → 'GetPetByIdPath'
|
|
@@ -292,14 +304,3 @@ type ResolverTsPatch = {
|
|
|
292
304
|
```
|
|
293
305
|
|
|
294
306
|
::
|
|
295
|
-
|
|
296
|
-
### printer
|
|
297
|
-
|
|
298
|
-
Replaces the handler for a schema type such as `'integer'` or `'date'`. Each handler returns the TypeScript type as a string. Inside a handler, `this.transform(node)` returns the printed type of a nested node and `this.options` holds the printer options. A handler that returns a TypeScript compiler node is deprecated. The [printer guide](https://kubb.dev/docs/5.x/how-to/printers) covers the handler context and how overrides compose with macros.
|
|
299
|
-
|
|
300
|
-
| | |
|
|
301
|
-
| -------- | ---------------------------- |
|
|
302
|
-
| Type | `{ nodes?: PrinterTsNodes }` |
|
|
303
|
-
| Required | `false` |
|
|
304
|
-
|
|
305
|
-
See [Map schema types](https://kubb.dev/extensions/kubb-labs/plugin-ts/guides/customization#map-schema-types) for an example.
|
package/docs/overview.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# TypeScript
|
|
2
2
|
|
|
3
|
-
The TypeScript plugin generates a type
|
|
3
|
+
The TypeScript plugin generates a type for every schema in your OpenAPI spec, plus the request, response, and parameter types of each operation.
|
|
4
4
|
|
|
5
|
-
Kubb
|
|
5
|
+
The other Kubb plugins import these types, and Kubb runs this plugin before them, whatever the order of `plugins`.
|
|
6
6
|
|
|
7
7
|
## Setup
|
|
8
8
|
|
|
@@ -10,19 +10,23 @@ No other plugins or packages are needed.
|
|
|
10
10
|
|
|
11
11
|
## Options worth knowing
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
- `
|
|
16
|
-
- `
|
|
17
|
-
|
|
13
|
+
| Option | Default | What it does |
|
|
14
|
+
| -------------------------------------------------------------------------------------- | ----------------- | ---------------------------------------------------------------------------------------- |
|
|
15
|
+
| [`enum.type`](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#enumtype) | `'asConst'` | Writes enums as an `as const` object, an `enum`, or a literal union. |
|
|
16
|
+
| [`syntaxType`](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#syntaxtype) | `'type'` | Writes object schemas as `type` or `interface`. |
|
|
17
|
+
| [`optionalType`](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#optionaltype) | `'questionToken'` | Writes `a?: T`, `a: T \| undefined`, or both. |
|
|
18
|
+
| [`arrayType`](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#arraytype) | `'array'` | Writes arrays as `T[]` or `Array<T>`. |
|
|
19
|
+
| [`operations`](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#operations) | `'export'` | `'inline'` skips the per-operation type files. |
|
|
20
|
+
| [`group`](https://kubb.dev/docs/5.x/reference/plugin-options#group) | None | Splits the output into folders by tag or path. Does not work with `output.mode: 'file'`. |
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
See [Options](https://kubb.dev/extensions/kubb-labs/plugin-ts/options) for every option and its default.
|
|
22
|
+
> [!TIP]
|
|
23
|
+
> The OpenAPI adapter types `int64` fields as `bigint`, but JSON responses hold numbers. For most JSON APIs, set [`integerType: 'number'`](https://kubb.dev/extensions/kubb-labs/adapter-oas/options#integertype) on `adapterOas`.
|
|
22
24
|
|
|
23
25
|
## Example
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
This config writes the types to `src/gen/types`. For a `Pet` schema with a `status` enum, the plugin writes `Pet.ts`:
|
|
28
|
+
|
|
29
|
+
::code-group
|
|
26
30
|
|
|
27
31
|
```typescript [kubb.config.ts]
|
|
28
32
|
import { defineConfig } from 'kubb/config'
|
|
@@ -31,8 +35,28 @@ import { pluginTs } from '@kubb/plugin-ts'
|
|
|
31
35
|
export default defineConfig({
|
|
32
36
|
input: './petStore.yaml',
|
|
33
37
|
output: { path: './src/gen' },
|
|
34
|
-
plugins: [
|
|
35
|
-
pluginTs(),
|
|
36
|
-
],
|
|
38
|
+
plugins: [pluginTs()],
|
|
37
39
|
})
|
|
38
40
|
```
|
|
41
|
+
|
|
42
|
+
```typescript [src/gen/types/Pet.ts]
|
|
43
|
+
// excerpt, without JSDoc
|
|
44
|
+
export const petStatusEnum = {
|
|
45
|
+
available: 'available',
|
|
46
|
+
pending: 'pending',
|
|
47
|
+
sold: 'sold',
|
|
48
|
+
} as const
|
|
49
|
+
|
|
50
|
+
export type PetStatusEnumKey = (typeof petStatusEnum)[keyof typeof petStatusEnum]
|
|
51
|
+
|
|
52
|
+
export type Pet = {
|
|
53
|
+
id?: bigint
|
|
54
|
+
name: string
|
|
55
|
+
photoUrls: string[]
|
|
56
|
+
status?: PetStatusEnumKey
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
::
|
|
61
|
+
|
|
62
|
+
Next, [customize the generated types](https://kubb.dev/extensions/kubb-labs/plugin-ts/guides/customization).
|
package/kubb-extension.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-ts",
|
|
3
|
-
"version": "5.5.
|
|
3
|
+
"version": "5.5.4",
|
|
4
4
|
"description": "Generate TypeScript types, interfaces, and enums with Kubb. The base plugin other Kubb plugins build on for type safety.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-generation",
|
|
@@ -67,18 +67,18 @@
|
|
|
67
67
|
"registry": "https://registry.npmjs.org/"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@kubb/kit": "^5.
|
|
71
|
-
"@kubb/renderer-jsx": "^5.
|
|
70
|
+
"@kubb/kit": "^5.20.1",
|
|
71
|
+
"@kubb/renderer-jsx": "^5.20.1",
|
|
72
72
|
"yuku-codegen": "^0.19.0"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
75
|
"@internals/shared": "0.0.0",
|
|
76
76
|
"@internals/utils": "0.0.0",
|
|
77
|
-
"@kubb/parser-ts": "^5.
|
|
77
|
+
"@kubb/parser-ts": "^5.20.1",
|
|
78
78
|
"typescript": "^7.0.2"
|
|
79
79
|
},
|
|
80
80
|
"peerDependencies": {
|
|
81
|
-
"@kubb/parser-ts": "^5.
|
|
81
|
+
"@kubb/parser-ts": "^5.20.1",
|
|
82
82
|
"@typescript/typescript6": "^6.0.2",
|
|
83
83
|
"typescript": ">=5.0.0"
|
|
84
84
|
},
|