@kubb/plugin-ts 5.0.8 → 5.0.10

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.
@@ -0,0 +1,69 @@
1
+ # Customize generated types
2
+
3
+ Prefix TypeScript names, change schema type output, and remove descriptions.
4
+
5
+ Start with the [TypeScript plugin configuration](https://kubb.dev/extensions/kubb-labs/plugin-ts#example). Apply these changes to `pluginTs()` in that configuration.
6
+
7
+ ## Prefix type names
8
+
9
+ Use `resolverTs.name` to preserve the plugin's PascalCase rule before adding a prefix.
10
+
11
+ ```typescript [kubb.config.ts]
12
+ import { pluginTs, resolverTs } from '@kubb/plugin-ts'
13
+
14
+ pluginTs({
15
+ resolver: {
16
+ name(name) {
17
+ return 'Api' + resolverTs.name(name)
18
+ },
19
+ },
20
+ })
21
+ ```
22
+
23
+ A generated `Pet` becomes `ApiPet`. File names stay unchanged unless you also customize the file resolver.
24
+
25
+ ## Map schema types
26
+
27
+ Replace [printer handlers](https://kubb.dev/extensions/kubb-labs/plugin-ts/options#printer) with TypeScript AST nodes.
28
+
29
+ ```typescript [kubb.config.ts]
30
+ import ts from 'typescript'
31
+ import { pluginTs } from '@kubb/plugin-ts'
32
+
33
+ pluginTs({
34
+ printer: {
35
+ nodes: {
36
+ date() {
37
+ return ts.factory.createTypeReferenceNode('Date', [])
38
+ },
39
+ integer() {
40
+ return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword)
41
+ },
42
+ },
43
+ },
44
+ })
45
+ ```
46
+
47
+ This maps `date` fields to `Date` and `int32` integers to `bigint`. The separate `bigint` handler controls `int64` fields, which already use `bigint` by default. A `date-time` field uses the `datetime` handler.
48
+
49
+ ## Remove descriptions
50
+
51
+ Clear schema descriptions with a [macro](https://kubb.dev/docs/5.x/reference/plugin-options#macros).
52
+
53
+ ```typescript [kubb.config.ts]
54
+ import { pluginTs } from '@kubb/plugin-ts'
55
+ import { ast } from 'kubb/kit'
56
+
57
+ const dropDescriptions = ast.defineMacro({
58
+ name: 'drop-descriptions',
59
+ schema(node) {
60
+ return 'description' in node && node.description
61
+ ? { ...node, description: undefined }
62
+ : undefined
63
+ },
64
+ })
65
+
66
+ pluginTs({ macros: [dropDescriptions] })
67
+ ```
68
+
69
+ The generated types omit description JSDoc. The OpenAPI document stays unchanged. The same macro works with `pluginZod`.
@@ -0,0 +1,280 @@
1
+ # Options
2
+
3
+ Pass these options to `pluginTs()`. Shared options link to [Shared plugin options](https://kubb.dev/docs/5.x/reference/plugin-options) for details.
4
+
5
+ ## Options overview
6
+
7
+ | Option | Purpose | Default |
8
+ | ------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
9
+ | [`output`](https://kubb.dev/docs/5.x/reference/plugin-options#output) | Where files are written: `path`, `mode`, `barrel`, `banner`, `footer`. | `{ path: 'types', barrel: { type: 'named' } }` |
10
+ | [`group`](https://kubb.dev/docs/5.x/reference/plugin-options#group) | Split output by tag or path: `type`, `name`. | None |
11
+ | [`enum`](#enum) | How enums are generated and cased. | `{ type: 'asConst', constCasing: 'camelCase', typeSuffix: 'Key', keyCasing: 'none' }` |
12
+ | [`syntaxType`](#syntaxtype) | Emit object schemas as type aliases or interfaces. | `'type'` |
13
+ | [`optionalType`](#optionaltype) | How optional properties are written. | `'questionToken'` |
14
+ | [`arrayType`](#arraytype) | `Type[]` or `Array<Type>`. | `'array'` |
15
+ | [`include`](https://kubb.dev/docs/5.x/reference/plugin-options#include) | Keep only operations and schemas that match. | None |
16
+ | [`exclude`](https://kubb.dev/docs/5.x/reference/plugin-options#exclude) | Skip operations and schemas that match. | `[]` |
17
+ | [`override`](https://kubb.dev/docs/5.x/reference/plugin-options#override) | Apply different options per pattern. | `[]` |
18
+ | [`resolver`](#resolver) | Customize generated names and file paths. | `resolverTs` |
19
+ | [`macros`](https://kubb.dev/docs/5.x/reference/plugin-options#macros) | Rewrite AST nodes before printing. | `[]` |
20
+ | [`printer`](#printer) | Replace the handler for a schema type. | None |
21
+
22
+ ## Option details
23
+
24
+ ### enum
25
+
26
+ How OpenAPI enums are represented in the generated TypeScript, and how their names are cased.
27
+
28
+ | | |
29
+ | -------- | ------------------------------------------------------------------------------------- |
30
+ | Type | `{ type?, constCasing?, typeSuffix?, keyCasing? }` |
31
+ | Required | `false` |
32
+ | Default | `{ type: 'asConst', constCasing: 'camelCase', typeSuffix: 'Key', keyCasing: 'none' }` |
33
+
34
+ `constCasing` and `typeSuffix` apply only to `type: 'asConst'`. `keyCasing` applies to `'asConst'`, `'enum'` and `'constEnum'`, since the literal modes emit values only.
35
+
36
+ #### enum.type
37
+
38
+ Representation of each enum.
39
+
40
+ | | |
41
+ | -------- | -------------------------------------------------------------------- |
42
+ | Type | `'asConst' \| 'enum' \| 'constEnum' \| 'literal' \| 'inlineLiteral'` |
43
+ | Required | `false` |
44
+ | Default | `'asConst'` |
45
+
46
+ ::field-group
47
+
48
+ :::field{name="'asConst'"}
49
+ Emits an `as const` object and a union type of its values. Bundlers drop the object when nothing uses it.
50
+ :::
51
+
52
+ :::field{name="'enum'"}
53
+ Emits a TypeScript `enum` with JavaScript runtime code.
54
+ :::
55
+
56
+ :::field{name="'constEnum'"}
57
+ Emits a `const enum`, inlined at compile time and incompatible with `--isolatedModules`.
58
+ :::
59
+
60
+ :::field{name="'literal'"}
61
+ Emits a named union type with no runtime value.
62
+ :::
63
+
64
+ :::field{name="'inlineLiteral'"}
65
+ Emits the same union as `'literal'` but inlines it at each usage site instead of naming it.
66
+ :::
67
+
68
+ ::
69
+
70
+ ::code-group
71
+
72
+ ```typescript ['asConst' (default)]
73
+ export const petStatus = {
74
+ available: 'available',
75
+ pending: 'pending',
76
+ sold: 'sold',
77
+ } as const
78
+
79
+ export type PetStatusKey = (typeof petStatus)[keyof typeof petStatus]
80
+ ```
81
+
82
+ ```typescript ['enum']
83
+ export enum PetStatus {
84
+ available = 'available',
85
+ pending = 'pending',
86
+ sold = 'sold',
87
+ }
88
+ ```
89
+
90
+ ```typescript ['literal']
91
+ export type PetStatus = 'available' | 'pending' | 'sold'
92
+ ```
93
+
94
+ ::
95
+
96
+ #### enum.constCasing
97
+
98
+ Casing of the generated const object when `type` is `'asConst'`.
99
+
100
+ | | |
101
+ | -------- | ----------------------------- |
102
+ | Type | `'camelCase' \| 'pascalCase'` |
103
+ | Required | `false` |
104
+ | Default | `'camelCase'` |
105
+
106
+ ::field-group
107
+
108
+ :::field{name="'camelCase'"}
109
+ Names the const `petStatus`.
110
+ :::
111
+
112
+ :::field{name="'pascalCase'"}
113
+ Names the const `PetStatus`, matching the schema name.
114
+ :::
115
+
116
+ ::
117
+
118
+ #### enum.typeSuffix
119
+
120
+ Suffix on the companion type alias when `type` is `'asConst'`. It never changes the const object name. Set it to `''` to drop the suffix, which with `constCasing: 'pascalCase'` merges the const and the type under one name, `PetStatus`.
121
+
122
+ | | |
123
+ | -------- | -------- |
124
+ | Type | `string` |
125
+ | Required | `false` |
126
+ | Default | `'Key'` |
127
+
128
+ ```typescript [typeSuffix: 'Value']
129
+ export type PetStatusValue = (typeof petStatus)[keyof typeof petStatus]
130
+ ```
131
+
132
+ #### enum.keyCasing
133
+
134
+ Casing applied to enum key names.
135
+
136
+ | | |
137
+ | -------- | ------------------------------------------------------------------------------ |
138
+ | Type | `'none' \| 'camelCase' \| 'pascalCase' \| 'snakeCase' \| 'screamingSnakeCase'` |
139
+ | Required | `false` |
140
+ | Default | `'none'` |
141
+
142
+ ::field-group
143
+
144
+ :::field{name="'none'"}
145
+ Keeps the raw enum value from the spec as the key.
146
+ :::
147
+
148
+ :::field{name="'camelCase'"}
149
+ Formats keys as `enumValue`.
150
+ :::
151
+
152
+ :::field{name="'pascalCase'"}
153
+ Formats keys as `EnumValue`.
154
+ :::
155
+
156
+ :::field{name="'snakeCase'"}
157
+ Formats keys as `enum_value`.
158
+ :::
159
+
160
+ :::field{name="'screamingSnakeCase'"}
161
+ Formats keys as `ENUM_VALUE`.
162
+ :::
163
+
164
+ ::
165
+
166
+ ### syntaxType
167
+
168
+ How object schemas are declared.
169
+
170
+ | | |
171
+ | -------- | ----------------------- |
172
+ | Type | `'type' \| 'interface'` |
173
+ | Required | `false` |
174
+ | Default | `'type'` |
175
+
176
+ ::field-group
177
+
178
+ :::field{name="'type'"}
179
+ Generates type aliases, `export type Pet = { name: string }`.
180
+ :::
181
+
182
+ :::field{name="'interface'"}
183
+ Generates interface declarations, `export interface Pet { name: string }`. Use it when consumers need declaration merging. See [Type vs Interface](https://www.totaltypescript.com/type-vs-interface-which-should-you-use).
184
+ :::
185
+
186
+ ::
187
+
188
+ ### optionalType
189
+
190
+ How optional properties are written.
191
+
192
+ | | |
193
+ | -------- | --------------------------------------------------------------- |
194
+ | Type | `'questionToken' \| 'undefined' \| 'questionTokenAndUndefined'` |
195
+ | Required | `false` |
196
+ | Default | `'questionToken'` |
197
+
198
+ ::field-group
199
+
200
+ :::field{name="'questionToken'"}
201
+ Writes `type?: string`, so the property may be missing.
202
+ :::
203
+
204
+ :::field{name="'undefined'"}
205
+ Writes `type: string | undefined`, so it must exist but may be `undefined`.
206
+ :::
207
+
208
+ :::field{name="'questionTokenAndUndefined'"}
209
+ Writes `type?: string | undefined`, so the property may be missing or `undefined`. Use it with `"exactOptionalPropertyTypes": true`.
210
+ :::
211
+
212
+ ::
213
+
214
+ ### arrayType
215
+
216
+ Syntax for array types.
217
+
218
+ | | |
219
+ | -------- | ---------------------- |
220
+ | Type | `'array' \| 'generic'` |
221
+ | Required | `false` |
222
+ | Default | `'array'` |
223
+
224
+ ::field-group
225
+
226
+ :::field{name="'array'"}
227
+ Uses the postfix `tags: string[]`.
228
+ :::
229
+
230
+ :::field{name="'generic'"}
231
+ Uses `tags: Array<string>`, which reads better for complex elements like `Array<{ id: number }>`.
232
+ :::
233
+
234
+ ::
235
+
236
+ ### resolver
237
+
238
+ Overrides generated file and symbol names. Omitted members keep `resolverTs`. The shared members (`name`, `file`, `imports`) and the `this` context are described under [`resolver`](https://kubb.dev/docs/5.x/reference/plugin-options#resolver).
239
+
240
+ | | |
241
+ | -------- | --------------------------- |
242
+ | Type | `ResolverPatch<ResolverTs>` |
243
+ | Required | `false` |
244
+
245
+ ::code-collapse{name="ResolverTs patch members"}
246
+
247
+ ```typescript [Partial override]
248
+ type ResolverTsPatch = {
249
+ // plus the shared name, file, and imports members
250
+ param?: {
251
+ name?(node: OperationNode, param: ParameterNode): string // → 'DeletePetPathPetId'
252
+ path?(node: OperationNode, param: ParameterNode): string // → 'GetPetByIdPath'
253
+ query?(node: OperationNode, param: ParameterNode): string // → 'FindPetsByStatusQuery'
254
+ headers?(node: OperationNode, param: ParameterNode): string // → 'DeletePetHeaders'
255
+ }
256
+ response?: {
257
+ status?(node: OperationNode, statusCode: StatusCode): string // → 'ListPetsStatus200'
258
+ options?(node: OperationNode): string // → 'ListPetsOptions'
259
+ responses?(node: OperationNode): string // → 'ListPetsResponses'
260
+ response?(node: OperationNode): string // → 'ListPetsResponse'
261
+ body?(node: OperationNode): string // → 'CreatePetBody'
262
+ }
263
+ enum?: {
264
+ keyName?(node: { name?: string | null }, enumTypeSuffix?: string): string // → 'PetStatusKey'
265
+ }
266
+ }
267
+ ```
268
+
269
+ ::
270
+
271
+ ### printer
272
+
273
+ Replaces the node handler for a schema type such as `'integer'` or `'date'` with one that builds its TypeScript AST node. Use `this.transform` to recurse into nested nodes and `this.options` to read printer options. The [printer guide](https://kubb.dev/docs/5.x/how-to/printers) covers the handler context and how overrides compose with macros.
274
+
275
+ | | |
276
+ | -------- | ---------------------------- |
277
+ | Type | `{ nodes?: PrinterTsNodes }` |
278
+ | Required | `false` |
279
+
280
+ See [Map schema types](https://kubb.dev/extensions/kubb-labs/plugin-ts/guides/customization#map-schema-types) for an example.
@@ -0,0 +1,38 @@
1
+ # TypeScript
2
+
3
+ The TypeScript plugin generates a type or interface for every schema in your OpenAPI spec, plus the request, response, and parameter types of each operation.
4
+
5
+ Kubb runs this plugin before the plugins that import its types, whatever the order of the `plugins` array.
6
+
7
+ ## Setup
8
+
9
+ No other plugins or packages are needed.
10
+
11
+ ## Options worth knowing
12
+
13
+ - `enum.type` sets how enums are written: `asConst` (default), `enum`, `constEnum`, `literal`, or `inlineLiteral`. `enum.constCasing`, `enum.typeSuffix`, and `enum.keyCasing` tune the names.
14
+ - `syntaxType` writes object schemas as `type` aliases (default) or `interface` declarations.
15
+ - `optionalType` sets how optional properties are written: `questionToken` (default), `undefined`, or `questionTokenAndUndefined`.
16
+ - `arrayType` writes arrays as `T[]` (default) or `Array<T>`.
17
+ - `group` splits the output into folders by tag or path. It does not work with `output.mode: 'file'`.
18
+
19
+ With the default OpenAPI adapter, `int64` fields are typed as `bigint`, while JSON responses hold numbers. Set `integerType: 'number'` on `adapterOas` for most JSON APIs.
20
+
21
+ See [Options](https://kubb.dev/extensions/kubb-labs/plugin-ts/options) for every option and its default.
22
+
23
+ ## Example
24
+
25
+ The config below generates the types into `src/gen/types`.
26
+
27
+ ```typescript [kubb.config.ts]
28
+ import { defineConfig } from 'kubb/config'
29
+ import { pluginTs } from '@kubb/plugin-ts'
30
+
31
+ export default defineConfig({
32
+ input: './petStore.yaml',
33
+ output: { path: './src/gen' },
34
+ plugins: [
35
+ pluginTs(),
36
+ ],
37
+ })
38
+ ```
@@ -10,9 +10,15 @@
10
10
  "light": "https://kubb.dev/feature/typescript.svg"
11
11
  },
12
12
  "tags": ["typescript", "types", "interfaces", "codegen", "openapi"],
13
- "docs": "https://kubb.dev/plugins/plugin-ts",
13
+ "docs": "https://kubb.dev/extensions/kubb-labs/plugin-ts",
14
+ "maintainers": [
15
+ {
16
+ "name": "Stijn Van Hulle",
17
+ "github": "stijnvanhulle"
18
+ }
19
+ ],
14
20
  "compatibility": {
15
- "kubb": ">=5.8.0",
21
+ "kubb": ">=5.8.3",
16
22
  "node": ">=22"
17
23
  },
18
24
  "options": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/plugin-ts",
3
- "version": "5.0.8",
3
+ "version": "5.0.10",
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",
@@ -13,7 +13,7 @@
13
13
  "types",
14
14
  "typescript"
15
15
  ],
16
- "homepage": "https://kubb.dev/plugins/plugin-ts",
16
+ "homepage": "https://kubb.dev/extensions/kubb-labs/plugin-ts",
17
17
  "license": "MIT",
18
18
  "author": "stijnvanhulle",
19
19
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "files": [
39
39
  "dist",
40
40
  "kubb-extension.json",
41
- "OVERVIEW.md",
41
+ "docs",
42
42
  "!/**/**.test.**",
43
43
  "!/**/__tests__/**",
44
44
  "!/**/__snapshots__/**"
@@ -62,16 +62,16 @@
62
62
  "registry": "https://registry.npmjs.org/"
63
63
  },
64
64
  "dependencies": {
65
- "@kubb/parser-ts": "^5.8.1",
65
+ "@kubb/parser-ts": "^5.8.3",
66
66
  "typescript": "^6.0.3"
67
67
  },
68
68
  "devDependencies": {
69
69
  "@internals/shared": "0.0.0",
70
70
  "@internals/utils": "0.0.0",
71
- "kubb": "^5.8.1"
71
+ "kubb": "^5.8.3"
72
72
  },
73
73
  "peerDependencies": {
74
- "kubb": "^5.8.1"
74
+ "kubb": "^5.8.3"
75
75
  },
76
76
  "engines": {
77
77
  "node": ">=22"
package/OVERVIEW.md DELETED
@@ -1,17 +0,0 @@
1
- # TypeScript
2
-
3
- Generates TypeScript types and interfaces from your OpenAPI spec. You get a type for every schema, plus the request, response, and parameter types of each operation.
4
-
5
- Most other Kubb plugins build on these types. The client, query, mock, and test plugins import them, so add this plugin first.
6
-
7
- ## Setup
8
-
9
- No other plugins or packages are needed.
10
-
11
- ## Options worth knowing
12
-
13
- - `enum.type` sets how enums are written: `asConst` (default), `enum`, `constEnum`, `literal`, or `inlineLiteral`. `enum.constCasing`, `enum.typeSuffix`, and `enum.keyCasing` tune the names.
14
- - `syntaxType` writes object schemas as `type` aliases (default) or `interface` declarations.
15
- - `optionalType` sets how optional properties are written: `questionToken` (default), `undefined`, or `questionTokenAndUndefined`.
16
- - `arrayType` writes arrays as `T[]` (default) or `Array<T>`.
17
- - `group` splits the output into folders by tag or path. It does not work with `output.mode: 'file'`.