@kubb/plugin-ts 5.0.0-beta.22 → 5.0.0-beta.27
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 +183 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +78 -55
- package/dist/index.js +183 -40
- package/dist/index.js.map +1 -1
- package/extension.yaml +690 -247
- package/package.json +5 -5
- package/src/components/Enum.tsx +3 -3
- package/src/factory.ts +6 -6
- package/src/generators/typeGenerator.tsx +13 -4
- package/src/plugin.ts +18 -9
- package/src/printers/printerTs.ts +3 -3
- package/src/resolvers/resolverTs.ts +17 -14
- package/src/types.ts +44 -37
- package/src/utils.ts +9 -9
package/extension.yaml
CHANGED
|
@@ -2,7 +2,7 @@ $schema: https://kubb.dev/schemas/extension.json
|
|
|
2
2
|
kind: plugin
|
|
3
3
|
id: plugin-ts
|
|
4
4
|
name: TypeScript
|
|
5
|
-
description: Generate TypeScript types and interfaces from OpenAPI schemas with type
|
|
5
|
+
description: Generate TypeScript types and interfaces from OpenAPI schemas with full type safety end to end.
|
|
6
6
|
category: types
|
|
7
7
|
type: official
|
|
8
8
|
npmPackage: '@kubb/plugin-ts'
|
|
@@ -33,7 +33,9 @@ icon:
|
|
|
33
33
|
intro: |
|
|
34
34
|
# @kubb/plugin-ts
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
`@kubb/plugin-ts` turns your OpenAPI schema into TypeScript `type` aliases and `interface` declarations. It is the foundation that every other Kubb plugin builds on — clients, query hooks, mocks, and validators all reference the names this plugin produces.
|
|
37
|
+
|
|
38
|
+
Add it once and every request payload, response, path parameter, and enum becomes a compile-time check.
|
|
37
39
|
|
|
38
40
|
**See also**
|
|
39
41
|
|
|
@@ -44,392 +46,662 @@ options:
|
|
|
44
46
|
type: Output
|
|
45
47
|
required: false
|
|
46
48
|
default: "{ path: 'types', barrel: { type: 'named' } }"
|
|
47
|
-
description:
|
|
49
|
+
description: Where the generated `.ts` files are written and how they are exported.
|
|
48
50
|
properties:
|
|
49
51
|
- name: path
|
|
50
52
|
type: string
|
|
51
53
|
required: true
|
|
52
|
-
description:
|
|
54
|
+
description: |
|
|
55
|
+
Folder (or single file) where the plugin writes its generated code. The path is resolved against the global `output.path` set on `defineConfig`.
|
|
56
|
+
|
|
57
|
+
Use a folder to keep each generator's output isolated (`'types'`, `'clients'`, `'hooks'`). Use a single file when you want everything in one place, for example `'api.ts'`.
|
|
53
58
|
tip: |
|
|
54
|
-
|
|
59
|
+
When `output.path` points to a single file, the `group` option cannot be used because every operation ends up in the same file.
|
|
60
|
+
examples:
|
|
61
|
+
- name: kubb.config.ts
|
|
62
|
+
files:
|
|
63
|
+
- lang: typescript
|
|
64
|
+
twoslash: false
|
|
65
|
+
code: |
|
|
66
|
+
import { defineConfig } from 'kubb'
|
|
67
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
68
|
+
|
|
69
|
+
export default defineConfig({
|
|
70
|
+
input: { path: './petStore.yaml' },
|
|
71
|
+
output: { path: './src/gen' },
|
|
72
|
+
plugins: [
|
|
73
|
+
pluginTs({
|
|
74
|
+
output: { path: './types' },
|
|
75
|
+
}),
|
|
76
|
+
],
|
|
77
|
+
})
|
|
78
|
+
- name: Resulting tree
|
|
79
|
+
files:
|
|
80
|
+
- lang: text
|
|
81
|
+
twoslash: false
|
|
82
|
+
code: |
|
|
83
|
+
src/
|
|
84
|
+
└── gen/
|
|
85
|
+
└── types/
|
|
86
|
+
├── Pet.ts
|
|
87
|
+
└── Store.ts
|
|
55
88
|
default: "'types'"
|
|
56
89
|
- name: barrel
|
|
57
90
|
type: "{ type: 'named' | 'all', nested?: boolean } | false"
|
|
58
91
|
required: false
|
|
59
92
|
default: "{ type: 'named' }"
|
|
60
|
-
description:
|
|
93
|
+
description: |
|
|
94
|
+
Controls how the generated `index.ts` (barrel) file re-exports the plugin's output.
|
|
95
|
+
|
|
96
|
+
- `{ type: 'named' }` re-exports each symbol by name. Best for tree-shaking and explicit imports.
|
|
97
|
+
- `{ type: 'all' }` uses `export *`. Smaller barrel file, but exports everything.
|
|
98
|
+
- `{ nested: true }` creates a barrel in every subdirectory, so callers can import from any depth.
|
|
99
|
+
- `false` skips the barrel entirely. The plugin's files are also excluded from the root `index.ts`.
|
|
100
|
+
tip: |
|
|
101
|
+
Pick `'named'` when consumers care about which symbols they import (better tree-shaking, friendlier auto-import). Pick `'all'` when the file count is small and you want a one-line barrel.
|
|
61
102
|
examples:
|
|
62
|
-
- name:
|
|
103
|
+
- name: "'named' (default)"
|
|
63
104
|
files:
|
|
64
|
-
-
|
|
105
|
+
- name: kubb.config.ts
|
|
106
|
+
lang: typescript
|
|
107
|
+
twoslash: false
|
|
65
108
|
code: |
|
|
66
|
-
|
|
109
|
+
import { defineConfig } from 'kubb'
|
|
110
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
111
|
+
|
|
112
|
+
export default defineConfig({
|
|
113
|
+
input: { path: './petStore.yaml' },
|
|
114
|
+
output: { path: './src/gen' },
|
|
115
|
+
plugins: [
|
|
116
|
+
pluginTs({
|
|
117
|
+
output: { barrel: { type: 'named' } },
|
|
118
|
+
}),
|
|
119
|
+
],
|
|
120
|
+
})
|
|
121
|
+
- name: src/gen/types/index.ts
|
|
122
|
+
lang: typescript
|
|
67
123
|
twoslash: false
|
|
68
|
-
|
|
124
|
+
code: |
|
|
125
|
+
export { Pet, PetStatus } from './Pet'
|
|
126
|
+
export { Store } from './Store'
|
|
127
|
+
- name: "'all'"
|
|
69
128
|
files:
|
|
70
|
-
-
|
|
129
|
+
- name: kubb.config.ts
|
|
130
|
+
lang: typescript
|
|
131
|
+
twoslash: false
|
|
71
132
|
code: |
|
|
72
|
-
|
|
133
|
+
import { defineConfig } from 'kubb'
|
|
134
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
135
|
+
|
|
136
|
+
export default defineConfig({
|
|
137
|
+
input: { path: './petStore.yaml' },
|
|
138
|
+
output: { path: './src/gen' },
|
|
139
|
+
plugins: [
|
|
140
|
+
pluginTs({
|
|
141
|
+
output: { barrel: { type: 'all' } },
|
|
142
|
+
}),
|
|
143
|
+
],
|
|
144
|
+
})
|
|
145
|
+
- name: src/gen/types/index.ts
|
|
146
|
+
lang: typescript
|
|
73
147
|
twoslash: false
|
|
148
|
+
code: |
|
|
149
|
+
export * from './Pet'
|
|
150
|
+
export * from './Store'
|
|
74
151
|
- name: nested
|
|
75
152
|
files:
|
|
76
153
|
- name: kubb.config.ts
|
|
77
154
|
lang: typescript
|
|
155
|
+
twoslash: false
|
|
78
156
|
code: |
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
157
|
+
import { defineConfig } from 'kubb'
|
|
158
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
159
|
+
|
|
160
|
+
export default defineConfig({
|
|
161
|
+
input: { path: './petStore.yaml' },
|
|
162
|
+
output: { path: './src/gen' },
|
|
163
|
+
plugins: [
|
|
164
|
+
pluginTs({
|
|
165
|
+
output: { barrel: { type: 'named', nested: true } },
|
|
166
|
+
}),
|
|
167
|
+
],
|
|
168
|
+
})
|
|
169
|
+
- name: Generated tree
|
|
170
|
+
lang: text
|
|
83
171
|
twoslash: false
|
|
172
|
+
code: |
|
|
173
|
+
src/gen/types/
|
|
174
|
+
├── index.ts # re-exports ./petController and ./storeController
|
|
175
|
+
├── petController/
|
|
176
|
+
│ ├── index.ts # re-exports Pet, Store, ...
|
|
177
|
+
│ └── Pet.ts
|
|
178
|
+
└── storeController/
|
|
179
|
+
├── index.ts
|
|
180
|
+
└── Store.ts
|
|
84
181
|
- name: 'false'
|
|
85
182
|
files:
|
|
86
|
-
-
|
|
87
|
-
|
|
183
|
+
- name: kubb.config.ts
|
|
184
|
+
lang: typescript
|
|
185
|
+
twoslash: false
|
|
186
|
+
code: |
|
|
187
|
+
import { defineConfig } from 'kubb'
|
|
188
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
189
|
+
|
|
190
|
+
export default defineConfig({
|
|
191
|
+
input: { path: './petStore.yaml' },
|
|
192
|
+
output: { path: './src/gen' },
|
|
193
|
+
plugins: [
|
|
194
|
+
pluginTs({
|
|
195
|
+
output: { barrel: false },
|
|
196
|
+
}),
|
|
197
|
+
],
|
|
198
|
+
})
|
|
199
|
+
- name: Result
|
|
200
|
+
lang: text
|
|
88
201
|
twoslash: false
|
|
202
|
+
code: |
|
|
203
|
+
# No index.ts is generated for this plugin.
|
|
204
|
+
# Its files are also excluded from the root index.ts.
|
|
89
205
|
- name: banner
|
|
90
206
|
type: 'string | ((node: RootNode) => string)'
|
|
91
207
|
required: false
|
|
92
|
-
description:
|
|
208
|
+
description: |
|
|
209
|
+
Text prepended to every generated file. Useful for license headers, lint disables, or `@ts-nocheck` directives.
|
|
210
|
+
|
|
211
|
+
Pass a string for a static banner. Pass a function to compute the banner from each file's `RootNode` (the AST root containing path, schema, and operation context).
|
|
212
|
+
examples:
|
|
213
|
+
- name: Static banner
|
|
214
|
+
files:
|
|
215
|
+
- name: kubb.config.ts
|
|
216
|
+
lang: typescript
|
|
217
|
+
twoslash: false
|
|
218
|
+
code: |
|
|
219
|
+
import { defineConfig } from 'kubb'
|
|
220
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
221
|
+
|
|
222
|
+
export default defineConfig({
|
|
223
|
+
input: { path: './petStore.yaml' },
|
|
224
|
+
output: { path: './src/gen' },
|
|
225
|
+
plugins: [
|
|
226
|
+
pluginTs({
|
|
227
|
+
output: {
|
|
228
|
+
banner: '/* eslint-disable */\n// @ts-nocheck',
|
|
229
|
+
},
|
|
230
|
+
}),
|
|
231
|
+
],
|
|
232
|
+
})
|
|
233
|
+
- name: Generated file
|
|
234
|
+
lang: typescript
|
|
235
|
+
twoslash: false
|
|
236
|
+
code: |
|
|
237
|
+
/* eslint-disable */
|
|
238
|
+
// @ts-nocheck
|
|
239
|
+
export type Pet = {
|
|
240
|
+
id: number
|
|
241
|
+
name: string
|
|
242
|
+
}
|
|
243
|
+
- name: Dynamic banner
|
|
244
|
+
files:
|
|
245
|
+
- name: kubb.config.ts
|
|
246
|
+
lang: typescript
|
|
247
|
+
twoslash: false
|
|
248
|
+
code: |
|
|
249
|
+
import { defineConfig } from 'kubb'
|
|
250
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
251
|
+
|
|
252
|
+
export default defineConfig({
|
|
253
|
+
input: { path: './petStore.yaml' },
|
|
254
|
+
output: { path: './src/gen' },
|
|
255
|
+
plugins: [
|
|
256
|
+
pluginTs({
|
|
257
|
+
output: {
|
|
258
|
+
banner: (node) => `// Source: ${node.path}\n// Generated at ${new Date().toISOString()}`,
|
|
259
|
+
},
|
|
260
|
+
}),
|
|
261
|
+
],
|
|
262
|
+
})
|
|
93
263
|
- name: footer
|
|
94
264
|
type: 'string | ((node: RootNode) => string)'
|
|
95
265
|
required: false
|
|
96
|
-
description:
|
|
266
|
+
description: |
|
|
267
|
+
Text appended at the end of every generated file. The mirror of `banner` — use it for closing comments, re-enabling lint rules, or marker lines.
|
|
268
|
+
|
|
269
|
+
Pass a string for a static footer, or a function that receives the file's `RootNode` and returns the footer text.
|
|
270
|
+
examples:
|
|
271
|
+
- name: Re-enable lint after a banner disable
|
|
272
|
+
files:
|
|
273
|
+
- name: kubb.config.ts
|
|
274
|
+
lang: typescript
|
|
275
|
+
twoslash: false
|
|
276
|
+
code: |
|
|
277
|
+
import { defineConfig } from 'kubb'
|
|
278
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
279
|
+
|
|
280
|
+
export default defineConfig({
|
|
281
|
+
input: { path: './petStore.yaml' },
|
|
282
|
+
output: { path: './src/gen' },
|
|
283
|
+
plugins: [
|
|
284
|
+
pluginTs({
|
|
285
|
+
output: {
|
|
286
|
+
banner: '/* eslint-disable */',
|
|
287
|
+
footer: '/* eslint-enable */',
|
|
288
|
+
},
|
|
289
|
+
}),
|
|
290
|
+
],
|
|
291
|
+
})
|
|
97
292
|
- name: override
|
|
98
293
|
type: boolean
|
|
99
294
|
required: false
|
|
100
295
|
default: 'false'
|
|
101
|
-
description:
|
|
296
|
+
description: |
|
|
297
|
+
Allows the plugin to overwrite hand-written files that share a name with a generated file.
|
|
298
|
+
|
|
299
|
+
- `false` (default): Kubb skips a file if it already exists and is not marked as generated. This protects manual edits.
|
|
300
|
+
- `true`: Kubb overwrites any file at the target path, including hand-written ones.
|
|
301
|
+
warning: |
|
|
302
|
+
Enable this only when you are sure the target folder contains nothing you need to keep. Local edits are lost on the next generation.
|
|
303
|
+
examples:
|
|
304
|
+
- name: kubb.config.ts
|
|
305
|
+
files:
|
|
306
|
+
- lang: typescript
|
|
307
|
+
twoslash: false
|
|
308
|
+
code: |
|
|
309
|
+
import { defineConfig } from 'kubb'
|
|
310
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
311
|
+
|
|
312
|
+
export default defineConfig({
|
|
313
|
+
input: { path: './petStore.yaml' },
|
|
314
|
+
output: { path: './src/gen' },
|
|
315
|
+
plugins: [
|
|
316
|
+
pluginTs({
|
|
317
|
+
output: { override: true },
|
|
318
|
+
}),
|
|
319
|
+
],
|
|
320
|
+
})
|
|
102
321
|
- name: contentType
|
|
103
322
|
type: "'application/json' | (string & {})"
|
|
104
323
|
required: false
|
|
105
324
|
description: |
|
|
106
|
-
|
|
325
|
+
Selects which request/response media type the generator reads from the OpenAPI spec.
|
|
326
|
+
|
|
327
|
+
When omitted, Kubb picks the first JSON-compatible media type it finds (`application/json`, `application/vnd.api+json`, anything ending in `+json`). Set this when your spec defines multiple media types for the same operation and you want a non-default one.
|
|
328
|
+
examples:
|
|
329
|
+
- name: JSON API media type
|
|
330
|
+
files:
|
|
331
|
+
- lang: typescript
|
|
332
|
+
twoslash: false
|
|
333
|
+
code: |
|
|
334
|
+
import { defineConfig } from 'kubb'
|
|
335
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
107
336
|
|
|
108
|
-
|
|
337
|
+
export default defineConfig({
|
|
338
|
+
input: { path: './petStore.yaml' },
|
|
339
|
+
output: { path: './src/gen' },
|
|
340
|
+
plugins: [
|
|
341
|
+
pluginTs({
|
|
342
|
+
contentType: 'application/vnd.api+json',
|
|
343
|
+
}),
|
|
344
|
+
],
|
|
345
|
+
})
|
|
109
346
|
- name: group
|
|
110
347
|
type: Group
|
|
111
348
|
required: false
|
|
112
349
|
description: |
|
|
113
|
-
|
|
350
|
+
Splits generated files into subfolders based on the operation's tag, so each tag in your OpenAPI spec gets its own directory.
|
|
351
|
+
|
|
352
|
+
Without `group`, every file lands in the plugin's `output.path` folder. With `group`, files are bucketed under `{output.path}/{groupName}/`, where `groupName` is derived from the operation's first tag.
|
|
353
|
+
tip: |
|
|
354
|
+
Use `group` to mirror your API's domain structure (pet, store, user) in the generated code. Combine it with `output.barrel: { type: 'named', nested: true }` to get per-tag barrel files.
|
|
114
355
|
examples:
|
|
115
356
|
- name: kubb.config.ts
|
|
116
357
|
files:
|
|
117
358
|
- lang: typescript
|
|
118
|
-
code: |
|
|
119
|
-
group: {
|
|
120
|
-
type: 'tag',
|
|
121
|
-
name({ group }) {
|
|
122
|
-
return `${group}Controller`
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
359
|
twoslash: false
|
|
360
|
+
code: |
|
|
361
|
+
import { defineConfig } from 'kubb'
|
|
362
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
363
|
+
|
|
364
|
+
export default defineConfig({
|
|
365
|
+
input: { path: './petStore.yaml' },
|
|
366
|
+
output: { path: './src/gen' },
|
|
367
|
+
plugins: [
|
|
368
|
+
pluginTs({
|
|
369
|
+
group: {
|
|
370
|
+
type: 'tag',
|
|
371
|
+
name: ({ group }) => `${group}Controller`,
|
|
372
|
+
},
|
|
373
|
+
}),
|
|
374
|
+
],
|
|
375
|
+
})
|
|
126
376
|
body: |
|
|
127
377
|
With the configuration above, the generator emits:
|
|
128
378
|
|
|
129
379
|
```text
|
|
130
|
-
|
|
131
|
-
├──
|
|
132
|
-
│
|
|
133
|
-
│
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
│ └── getStoreById.ts
|
|
138
|
-
├── petStore.yaml
|
|
139
|
-
├── kubb.config.ts
|
|
140
|
-
└── package.json
|
|
380
|
+
src/gen/
|
|
381
|
+
├── petController/
|
|
382
|
+
│ ├── AddPet.ts
|
|
383
|
+
│ └── GetPet.ts
|
|
384
|
+
└── storeController/
|
|
385
|
+
├── CreateStore.ts
|
|
386
|
+
└── GetStoreById.ts
|
|
141
387
|
```
|
|
142
388
|
properties:
|
|
143
389
|
- name: type
|
|
144
390
|
type: "'tag'"
|
|
145
391
|
required: true
|
|
146
|
-
description:
|
|
392
|
+
description: |
|
|
393
|
+
Property used to assign each operation to a group. Required whenever `group` is set.
|
|
394
|
+
|
|
395
|
+
Today only `'tag'` is supported: Kubb reads the first tag on the operation (`operation.getTags().at(0)?.name`) and uses it as the group key. Operations without a tag are placed in a default group.
|
|
147
396
|
note: |
|
|
148
|
-
`Required: true*`
|
|
149
|
-
body: |
|
|
150
|
-
- `'tag'`: Uses the first tag from `operation.getTags().at(0)?.name`
|
|
397
|
+
`Required: true*` is conditional — only required when the parent `group` option is used. `group` itself stays optional.
|
|
151
398
|
- name: name
|
|
152
399
|
type: '(context: GroupContext) => string'
|
|
153
400
|
required: false
|
|
154
401
|
default: (ctx) => `${ctx.group}Controller`
|
|
155
|
-
description:
|
|
402
|
+
description: |
|
|
403
|
+
Function that turns a group key (the operation's first tag) into a folder/identifier name.
|
|
404
|
+
|
|
405
|
+
The result is used as both the subdirectory name under `output.path` and as a suffix when naming aggregate files.
|
|
156
406
|
- name: enumType
|
|
157
407
|
type: "'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral'"
|
|
158
408
|
required: false
|
|
159
409
|
default: "'asConst'"
|
|
160
|
-
description:
|
|
161
|
-
|
|
162
|
-
The difference between `asConst` and `asPascalConst` is the casing of the constant variable name:
|
|
410
|
+
description: |
|
|
411
|
+
How OpenAPI enums are represented in the generated TypeScript.
|
|
163
412
|
|
|
164
|
-
- `asConst
|
|
165
|
-
- `asPascalConst
|
|
413
|
+
- `'asConst'` (default) — `as const` object plus a key/value type. Tree-shakeable, no enum runtime.
|
|
414
|
+
- `'asPascalConst'` — same as `asConst`, but the const is named in PascalCase.
|
|
415
|
+
- `'enum'` — a regular TypeScript `enum`. Produces JavaScript runtime code.
|
|
416
|
+
- `'constEnum'` — a `const enum`. Inlines at compile time; not compatible with `--isolatedModules`.
|
|
417
|
+
- `'literal'` — a plain union type (`'dog' | 'cat'`). No runtime value.
|
|
418
|
+
- `'inlineLiteral'` — the union is inlined at every usage site instead of giving it a name.
|
|
419
|
+
tip: |
|
|
420
|
+
`'asConst'` vs `'asPascalConst'` differs only in the casing of the const variable: `petType` vs `PetType`. The type alias is always PascalCase.
|
|
166
421
|
examples:
|
|
167
422
|
- name: "'enum'"
|
|
168
423
|
files:
|
|
169
424
|
- lang: typescript
|
|
425
|
+
twoslash: false
|
|
170
426
|
code: |
|
|
171
427
|
enum PetType {
|
|
172
428
|
Dog = 'dog',
|
|
173
429
|
Cat = 'cat',
|
|
174
430
|
}
|
|
175
|
-
|
|
176
|
-
- name: "'asConst'"
|
|
431
|
+
- name: "'asConst' (default)"
|
|
177
432
|
files:
|
|
178
433
|
- lang: typescript
|
|
434
|
+
twoslash: false
|
|
179
435
|
code: |
|
|
180
|
-
const petType = {
|
|
436
|
+
export const petType = {
|
|
181
437
|
Dog: 'dog',
|
|
182
438
|
Cat: 'cat',
|
|
183
|
-
} as const
|
|
184
|
-
|
|
439
|
+
} as const
|
|
440
|
+
|
|
441
|
+
export type PetTypeKey = (typeof petType)[keyof typeof petType]
|
|
185
442
|
- name: "'asPascalConst'"
|
|
186
443
|
files:
|
|
187
444
|
- lang: typescript
|
|
445
|
+
twoslash: false
|
|
188
446
|
code: |
|
|
189
|
-
const PetType = {
|
|
447
|
+
export const PetType = {
|
|
190
448
|
Dog: 'dog',
|
|
191
449
|
Cat: 'cat',
|
|
192
|
-
} as const
|
|
193
|
-
|
|
450
|
+
} as const
|
|
451
|
+
|
|
452
|
+
export type PetTypeKey = (typeof PetType)[keyof typeof PetType]
|
|
194
453
|
- name: "'constEnum'"
|
|
195
454
|
files:
|
|
196
455
|
- lang: typescript
|
|
456
|
+
twoslash: false
|
|
197
457
|
code: |
|
|
198
458
|
const enum PetType {
|
|
199
459
|
Dog = 'dog',
|
|
200
460
|
Cat = 'cat',
|
|
201
461
|
}
|
|
202
|
-
twoslash: false
|
|
203
462
|
- name: "'literal'"
|
|
204
463
|
files:
|
|
205
464
|
- lang: typescript
|
|
206
|
-
code: |
|
|
207
|
-
type PetType = 'dog' | 'cat';
|
|
208
465
|
twoslash: false
|
|
466
|
+
code: |
|
|
467
|
+
export type PetType = 'dog' | 'cat'
|
|
209
468
|
- name: "'inlineLiteral'"
|
|
210
469
|
files:
|
|
211
470
|
- lang: typescript
|
|
471
|
+
twoslash: false
|
|
212
472
|
code: |
|
|
213
|
-
//
|
|
473
|
+
// No separate enum type — values are inlined wherever PetType would have been used
|
|
214
474
|
export interface Pet {
|
|
215
|
-
status?: 'available' | 'pending' | 'sold'
|
|
475
|
+
status?: 'available' | 'pending' | 'sold'
|
|
216
476
|
}
|
|
217
|
-
twoslash: false
|
|
218
477
|
body: |
|
|
219
478
|
> [!TIP]
|
|
220
|
-
>
|
|
479
|
+
> `'inlineLiteral'` produces the cleanest output for small enums — the values appear directly where they are used instead of via a named alias.
|
|
221
480
|
- name: enumSuffix
|
|
222
481
|
required: false
|
|
223
482
|
description: ''
|
|
224
483
|
warning: |
|
|
225
|
-
|
|
484
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#enumSuffix). Use `adapterOas({ enumSuffix })` instead.
|
|
226
485
|
- name: enumTypeSuffix
|
|
227
486
|
type: string
|
|
228
487
|
required: false
|
|
229
488
|
default: "'Key'"
|
|
230
489
|
description: |
|
|
231
|
-
Suffix appended to the
|
|
490
|
+
Suffix appended to the type alias generated for enums when `enumType` is `'asConst'` or `'asPascalConst'`.
|
|
232
491
|
|
|
233
|
-
|
|
492
|
+
The const object name (e.g. `petType`) is unaffected — only the companion type alias is renamed.
|
|
234
493
|
examples:
|
|
235
494
|
- name: "'Key' (default)"
|
|
236
495
|
files:
|
|
237
496
|
- lang: typescript
|
|
497
|
+
twoslash: false
|
|
238
498
|
code: |
|
|
239
|
-
const petType = {
|
|
499
|
+
export const petType = {
|
|
240
500
|
Dog: 'dog',
|
|
241
501
|
Cat: 'cat',
|
|
242
|
-
} as const
|
|
502
|
+
} as const
|
|
243
503
|
|
|
244
|
-
export type PetTypeKey = (typeof petType)[keyof typeof petType]
|
|
245
|
-
twoslash: false
|
|
504
|
+
export type PetTypeKey = (typeof petType)[keyof typeof petType]
|
|
246
505
|
- name: "'Value'"
|
|
247
506
|
files:
|
|
248
507
|
- lang: typescript
|
|
508
|
+
twoslash: false
|
|
249
509
|
code: |
|
|
250
|
-
|
|
251
|
-
const petType = {
|
|
510
|
+
export const petType = {
|
|
252
511
|
Dog: 'dog',
|
|
253
512
|
Cat: 'cat',
|
|
254
|
-
} as const
|
|
513
|
+
} as const
|
|
255
514
|
|
|
256
|
-
export type PetTypeValue = (typeof petType)[keyof typeof petType]
|
|
257
|
-
twoslash: false
|
|
515
|
+
export type PetTypeValue = (typeof petType)[keyof typeof petType]
|
|
258
516
|
- name: "'' (no suffix)"
|
|
259
517
|
files:
|
|
260
518
|
- lang: typescript
|
|
519
|
+
twoslash: false
|
|
261
520
|
code: |
|
|
262
|
-
|
|
263
|
-
const petType = {
|
|
521
|
+
export const petType = {
|
|
264
522
|
Dog: 'dog',
|
|
265
523
|
Cat: 'cat',
|
|
266
|
-
} as const
|
|
524
|
+
} as const
|
|
267
525
|
|
|
268
|
-
export type PetType = (typeof petType)[keyof typeof petType]
|
|
269
|
-
twoslash: false
|
|
526
|
+
export type PetType = (typeof petType)[keyof typeof petType]
|
|
270
527
|
- name: enumKeyCasing
|
|
271
528
|
type: "'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none'"
|
|
272
529
|
required: false
|
|
273
530
|
default: "'none'"
|
|
274
|
-
description:
|
|
531
|
+
description: |
|
|
532
|
+
Casing applied to enum key names. By default the key is the raw value from the spec; switch to a project convention when needed.
|
|
275
533
|
body: |
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
534
|
+
| Value | Example key |
|
|
535
|
+
| --- | --- |
|
|
536
|
+
| `'screamingSnakeCase'` | `ENUM_VALUE` |
|
|
537
|
+
| `'snakeCase'` | `enum_value` |
|
|
538
|
+
| `'pascalCase'` | `EnumValue` |
|
|
539
|
+
| `'camelCase'` | `enumValue` |
|
|
540
|
+
| `'none'` (default) | as-is |
|
|
281
541
|
- name: dateType
|
|
282
542
|
required: false
|
|
283
543
|
description: ''
|
|
284
544
|
warning: |
|
|
285
|
-
|
|
545
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#dateType). Use `adapterOas({ dateType })` instead.
|
|
286
546
|
- name: integerType
|
|
287
547
|
required: false
|
|
288
548
|
description: ''
|
|
289
549
|
warning: |
|
|
290
|
-
|
|
550
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#integerType). Use `adapterOas({ integerType })` instead.
|
|
291
551
|
- name: syntaxType
|
|
292
552
|
type: "'type' | 'interface'"
|
|
293
553
|
required: false
|
|
294
554
|
default: "'type'"
|
|
295
555
|
description: |
|
|
296
|
-
|
|
297
|
-
|
|
556
|
+
Whether object schemas are emitted as `type` aliases or `interface` declarations.
|
|
557
|
+
|
|
558
|
+
`type` is the safer default for generated code: declarations are closed, intersections work cleanly, and unions are fine. Pick `interface` when consumers need to use declaration merging (rare for generated code).
|
|
559
|
+
|
|
560
|
+
For more background, see [Type vs Interface](https://www.totaltypescript.com/type-vs-interface-which-should-you-use).
|
|
298
561
|
examples:
|
|
299
|
-
- name: "'type'"
|
|
562
|
+
- name: "'type' (default)"
|
|
300
563
|
files:
|
|
301
564
|
- lang: typescript
|
|
302
|
-
code: |
|
|
303
|
-
type Pet = {
|
|
304
|
-
name: string;
|
|
305
|
-
};
|
|
306
565
|
twoslash: false
|
|
566
|
+
code: |
|
|
567
|
+
export type Pet = {
|
|
568
|
+
name: string
|
|
569
|
+
}
|
|
307
570
|
- name: "'interface'"
|
|
308
571
|
files:
|
|
309
572
|
- lang: typescript
|
|
573
|
+
twoslash: false
|
|
310
574
|
code: |
|
|
311
|
-
interface Pet {
|
|
312
|
-
name: string
|
|
575
|
+
export interface Pet {
|
|
576
|
+
name: string
|
|
313
577
|
}
|
|
314
|
-
twoslash: false
|
|
315
578
|
- name: unknownType
|
|
316
579
|
required: false
|
|
317
580
|
description: ''
|
|
318
581
|
warning: |
|
|
319
|
-
|
|
582
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#unknownType). Use `adapterOas({ unknownType })` instead.
|
|
320
583
|
- name: emptySchemaType
|
|
321
584
|
required: false
|
|
322
585
|
description: ''
|
|
323
586
|
warning: |
|
|
324
|
-
|
|
587
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#emptySchemaType). Use `adapterOas({ emptySchemaType })` instead.
|
|
325
588
|
- name: optionalType
|
|
326
589
|
type: "'questionToken' | 'undefined' | 'questionTokenAndUndefined'"
|
|
327
590
|
required: false
|
|
328
591
|
default: "'questionToken'"
|
|
329
|
-
description:
|
|
592
|
+
description: |
|
|
593
|
+
How optional properties are written in generated types.
|
|
594
|
+
|
|
595
|
+
- `'questionToken'` (default) — `type?: string`. The property may be missing.
|
|
596
|
+
- `'undefined'` — `type: string | undefined`. The property is required to exist but may be `undefined`.
|
|
597
|
+
- `'questionTokenAndUndefined'` — `type?: string | undefined`. Strictest — the property may be missing or explicitly set to `undefined`.
|
|
598
|
+
tip: |
|
|
599
|
+
Choose `'questionTokenAndUndefined'` when your project enables `"exactOptionalPropertyTypes": true` in `tsconfig.json` — it keeps generated types compatible with that setting.
|
|
330
600
|
examples:
|
|
331
|
-
- name: "'questionToken'"
|
|
601
|
+
- name: "'questionToken' (default)"
|
|
332
602
|
files:
|
|
333
603
|
- lang: typescript
|
|
334
|
-
code: |
|
|
335
|
-
type Pet = {
|
|
336
|
-
type?: string;
|
|
337
|
-
};
|
|
338
604
|
twoslash: false
|
|
605
|
+
code: |
|
|
606
|
+
export type Pet = {
|
|
607
|
+
type?: string
|
|
608
|
+
}
|
|
339
609
|
- name: "'undefined'"
|
|
340
610
|
files:
|
|
341
611
|
- lang: typescript
|
|
342
|
-
code: |
|
|
343
|
-
type Pet = {
|
|
344
|
-
type: string | undefined;
|
|
345
|
-
};
|
|
346
612
|
twoslash: false
|
|
613
|
+
code: |
|
|
614
|
+
export type Pet = {
|
|
615
|
+
type: string | undefined
|
|
616
|
+
}
|
|
347
617
|
- name: "'questionTokenAndUndefined'"
|
|
348
618
|
files:
|
|
349
619
|
- lang: typescript
|
|
350
|
-
code: |
|
|
351
|
-
type Pet = {
|
|
352
|
-
type?: string | undefined;
|
|
353
|
-
};
|
|
354
620
|
twoslash: false
|
|
621
|
+
code: |
|
|
622
|
+
export type Pet = {
|
|
623
|
+
type?: string | undefined
|
|
624
|
+
}
|
|
355
625
|
- name: arrayType
|
|
356
626
|
type: "'array' | 'generic'"
|
|
357
627
|
required: false
|
|
358
628
|
default: "'array'"
|
|
359
|
-
description:
|
|
629
|
+
description: |
|
|
630
|
+
Syntax used for array types in generated code.
|
|
631
|
+
|
|
632
|
+
- `'array'` (default) — postfix `Type[]`. Slightly shorter.
|
|
633
|
+
- `'generic'` — `Array<Type>`. More readable for complex element types (`Array<{ id: number }>`).
|
|
360
634
|
examples:
|
|
361
|
-
- name: "'array'"
|
|
635
|
+
- name: "'array' (default)"
|
|
362
636
|
files:
|
|
363
637
|
- lang: typescript
|
|
364
|
-
code: |
|
|
365
|
-
type Pet = {
|
|
366
|
-
tags: string[];
|
|
367
|
-
};
|
|
368
638
|
twoslash: false
|
|
639
|
+
code: |
|
|
640
|
+
export type Pet = {
|
|
641
|
+
tags: string[]
|
|
642
|
+
}
|
|
369
643
|
- name: "'generic'"
|
|
370
644
|
files:
|
|
371
645
|
- lang: typescript
|
|
372
|
-
code: |
|
|
373
|
-
type Pet = {
|
|
374
|
-
tags: Array<string>;
|
|
375
|
-
};
|
|
376
646
|
twoslash: false
|
|
647
|
+
code: |
|
|
648
|
+
export type Pet = {
|
|
649
|
+
tags: Array<string>
|
|
650
|
+
}
|
|
377
651
|
- name: paramsCasing
|
|
378
652
|
type: "'camelcase'"
|
|
379
653
|
required: false
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
important: |
|
|
383
|
-
When enabled, this option transforms property names in `PathParams`, `QueryParams`, and `HeaderParams` types to the specified casing. Response and request body types are **not** affected.
|
|
654
|
+
description: |
|
|
655
|
+
Renames properties inside `PathParams`, `QueryParams`, and `HeaderParams` types. Response and request body types are not touched.
|
|
384
656
|
|
|
385
|
-
|
|
657
|
+
Use this when your OpenAPI parameters use snake_case or kebab-case but you want camelCase in TypeScript.
|
|
658
|
+
important: |
|
|
659
|
+
Every plugin that references parameters must use the same `paramsCasing` value — `@kubb/plugin-client`, `@kubb/plugin-react-query`, `@kubb/plugin-vue-query`, `@kubb/plugin-faker`, `@kubb/plugin-mcp`. Mismatched casing breaks the generated type chain.
|
|
386
660
|
examples:
|
|
387
|
-
- name:
|
|
661
|
+
- name: Without `paramsCasing`
|
|
388
662
|
files:
|
|
389
663
|
- lang: typescript
|
|
664
|
+
twoslash: false
|
|
390
665
|
code: |
|
|
391
|
-
// OpenAPI spec
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
step_id: string;
|
|
396
|
-
};
|
|
666
|
+
// OpenAPI spec: step_id, bool_param, X-Custom-Header
|
|
667
|
+
export type FindPetsByStatusPathParams = {
|
|
668
|
+
step_id: string
|
|
669
|
+
}
|
|
397
670
|
|
|
398
|
-
type FindPetsByStatusQueryParams = {
|
|
399
|
-
bool_param?: boolean
|
|
400
|
-
}
|
|
671
|
+
export type FindPetsByStatusQueryParams = {
|
|
672
|
+
bool_param?: boolean
|
|
673
|
+
}
|
|
401
674
|
|
|
402
|
-
type FindPetsByStatusHeaderParams = {
|
|
403
|
-
'X-Custom-Header'?: string
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
- name: "With paramsCasing: 'camelcase'"
|
|
675
|
+
export type FindPetsByStatusHeaderParams = {
|
|
676
|
+
'X-Custom-Header'?: string
|
|
677
|
+
}
|
|
678
|
+
- name: "With `paramsCasing: 'camelcase'`"
|
|
407
679
|
files:
|
|
408
680
|
- lang: typescript
|
|
681
|
+
twoslash: false
|
|
409
682
|
code: |
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
stepId: string; // ✓ camelCase
|
|
414
|
-
};
|
|
683
|
+
export type FindPetsByStatusPathParams = {
|
|
684
|
+
stepId: string
|
|
685
|
+
}
|
|
415
686
|
|
|
416
|
-
type FindPetsByStatusQueryParams = {
|
|
417
|
-
boolParam?: boolean
|
|
418
|
-
}
|
|
687
|
+
export type FindPetsByStatusQueryParams = {
|
|
688
|
+
boolParam?: boolean
|
|
689
|
+
}
|
|
419
690
|
|
|
420
|
-
type FindPetsByStatusHeaderParams = {
|
|
421
|
-
xCustomHeader?: string
|
|
422
|
-
}
|
|
423
|
-
twoslash: false
|
|
691
|
+
export type FindPetsByStatusHeaderParams = {
|
|
692
|
+
xCustomHeader?: string
|
|
693
|
+
}
|
|
424
694
|
- name: resolver
|
|
425
695
|
type: Partial<ResolverTs> & ThisType<ResolverTs>
|
|
426
696
|
required: false
|
|
427
697
|
description: |
|
|
428
|
-
|
|
698
|
+
Overrides how the plugin builds names and paths for generated files and symbols. Use this to add prefixes, suffixes, or to swap the casing strategy without forking the plugin.
|
|
429
699
|
|
|
430
|
-
|
|
700
|
+
Only override the methods you want to change. Anything you omit falls back to the plugin's default resolver. A method that returns `null` or `undefined` also falls back.
|
|
701
|
+
|
|
702
|
+
Inside each method, `this` is bound to the full resolver, so you can call `this.default(name, 'function')` to delegate to the built-in implementation.
|
|
431
703
|
body: |
|
|
432
|
-
Each plugin ships a
|
|
704
|
+
Each plugin ships with a default resolver:
|
|
433
705
|
|
|
434
706
|
| Plugin | Default resolver |
|
|
435
707
|
| --- | --- |
|
|
@@ -437,191 +709,363 @@ options:
|
|
|
437
709
|
| `@kubb/plugin-zod` | `resolverZod` |
|
|
438
710
|
| `@kubb/plugin-cypress` | `resolverCypress` |
|
|
439
711
|
| `@kubb/plugin-mcp` | `resolverMcp` |
|
|
712
|
+
| `@kubb/plugin-client` | `resolverClient` |
|
|
440
713
|
codeBlock:
|
|
441
714
|
lang: typescript
|
|
442
|
-
title:
|
|
715
|
+
title: Add an Api prefix to every name
|
|
443
716
|
code: |
|
|
717
|
+
import { defineConfig } from 'kubb'
|
|
444
718
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
445
719
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
720
|
+
export default defineConfig({
|
|
721
|
+
input: { path: './petStore.yaml' },
|
|
722
|
+
output: { path: './src/gen' },
|
|
723
|
+
plugins: [
|
|
724
|
+
pluginTs({
|
|
725
|
+
resolver: {
|
|
726
|
+
resolveName(name) {
|
|
727
|
+
return `Api${this.default(name, 'function')}`
|
|
728
|
+
},
|
|
729
|
+
},
|
|
730
|
+
}),
|
|
731
|
+
],
|
|
454
732
|
})
|
|
455
733
|
tip: |
|
|
456
|
-
Use `resolver` for
|
|
734
|
+
Use `resolver` for naming and file-location tweaks. For changing the AST nodes themselves (e.g. stripping descriptions), use `transformer` instead.
|
|
457
735
|
examples:
|
|
458
|
-
- name: Custom prefix
|
|
736
|
+
- name: Add a Custom prefix to every name
|
|
459
737
|
files:
|
|
460
738
|
- lang: typescript
|
|
739
|
+
twoslash: false
|
|
461
740
|
code: |
|
|
741
|
+
import { defineConfig } from 'kubb'
|
|
462
742
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
463
743
|
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
744
|
+
export default defineConfig({
|
|
745
|
+
input: { path: './petStore.yaml' },
|
|
746
|
+
output: { path: './src/gen' },
|
|
747
|
+
plugins: [
|
|
748
|
+
pluginTs({
|
|
749
|
+
resolver: {
|
|
750
|
+
resolveName(name) {
|
|
751
|
+
return `Custom${this.default(name, 'function')}`
|
|
752
|
+
},
|
|
753
|
+
},
|
|
754
|
+
}),
|
|
755
|
+
],
|
|
470
756
|
})
|
|
471
|
-
twoslash: false
|
|
472
757
|
- name: include
|
|
473
758
|
type: Array<Include>
|
|
474
759
|
required: false
|
|
475
|
-
description:
|
|
760
|
+
description: |
|
|
761
|
+
Restricts generation to operations that match at least one entry in the list. Anything not matched is skipped.
|
|
762
|
+
|
|
763
|
+
Each entry filters by one of:
|
|
764
|
+
|
|
765
|
+
- `tag` — the operation's first tag in the OpenAPI spec.
|
|
766
|
+
- `operationId` — the operation's `operationId`.
|
|
767
|
+
- `path` — the URL pattern (`'/pet/{petId}'`).
|
|
768
|
+
- `method` — HTTP method (`'get'`, `'post'`, ...).
|
|
769
|
+
- `contentType` — the media type of the request body.
|
|
770
|
+
|
|
771
|
+
`pattern` accepts either a string (exact match) or a `RegExp` for fuzzy matches.
|
|
476
772
|
codeBlock:
|
|
477
773
|
lang: typescript
|
|
478
|
-
title:
|
|
774
|
+
title: Type definition
|
|
479
775
|
code: |
|
|
480
776
|
export type Include = {
|
|
481
777
|
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
482
778
|
pattern: string | RegExp
|
|
483
779
|
}
|
|
780
|
+
examples:
|
|
781
|
+
- name: Only the pet tag
|
|
782
|
+
files:
|
|
783
|
+
- name: kubb.config.ts
|
|
784
|
+
lang: typescript
|
|
785
|
+
twoslash: false
|
|
786
|
+
code: |
|
|
787
|
+
import { defineConfig } from 'kubb'
|
|
788
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
789
|
+
|
|
790
|
+
export default defineConfig({
|
|
791
|
+
input: { path: './petStore.yaml' },
|
|
792
|
+
output: { path: './src/gen' },
|
|
793
|
+
plugins: [
|
|
794
|
+
pluginTs({
|
|
795
|
+
include: [
|
|
796
|
+
{ type: 'tag', pattern: 'pet' },
|
|
797
|
+
],
|
|
798
|
+
}),
|
|
799
|
+
],
|
|
800
|
+
})
|
|
801
|
+
- name: Only GET operations under /pet
|
|
802
|
+
files:
|
|
803
|
+
- name: kubb.config.ts
|
|
804
|
+
lang: typescript
|
|
805
|
+
twoslash: false
|
|
806
|
+
code: |
|
|
807
|
+
import { defineConfig } from 'kubb'
|
|
808
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
809
|
+
|
|
810
|
+
export default defineConfig({
|
|
811
|
+
input: { path: './petStore.yaml' },
|
|
812
|
+
output: { path: './src/gen' },
|
|
813
|
+
plugins: [
|
|
814
|
+
pluginTs({
|
|
815
|
+
include: [
|
|
816
|
+
{ type: 'method', pattern: 'get' },
|
|
817
|
+
{ type: 'path', pattern: /^\/pet/ },
|
|
818
|
+
],
|
|
819
|
+
}),
|
|
820
|
+
],
|
|
821
|
+
})
|
|
484
822
|
- name: exclude
|
|
485
823
|
type: Array<Exclude>
|
|
486
824
|
required: false
|
|
487
|
-
description:
|
|
825
|
+
description: |
|
|
826
|
+
Skips any operation that matches at least one entry in the list. The opposite of `include`.
|
|
827
|
+
|
|
828
|
+
Each entry filters by one of:
|
|
829
|
+
|
|
830
|
+
- `tag` — the operation's first tag.
|
|
831
|
+
- `operationId` — the operation's `operationId`.
|
|
832
|
+
- `path` — the URL pattern (`'/pet/{petId}'`).
|
|
833
|
+
- `method` — HTTP method (`'get'`, `'post'`, ...).
|
|
834
|
+
- `contentType` — the media type of the request body.
|
|
835
|
+
|
|
836
|
+
`pattern` accepts a plain string or a `RegExp`. When both `include` and `exclude` are set, `exclude` wins.
|
|
488
837
|
codeBlock:
|
|
489
838
|
lang: typescript
|
|
490
|
-
title:
|
|
839
|
+
title: Type definition
|
|
491
840
|
code: |
|
|
492
841
|
export type Exclude = {
|
|
493
842
|
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
494
843
|
pattern: string | RegExp
|
|
495
844
|
}
|
|
845
|
+
examples:
|
|
846
|
+
- name: Skip everything under the store tag
|
|
847
|
+
files:
|
|
848
|
+
- name: kubb.config.ts
|
|
849
|
+
lang: typescript
|
|
850
|
+
twoslash: false
|
|
851
|
+
code: |
|
|
852
|
+
import { defineConfig } from 'kubb'
|
|
853
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
854
|
+
|
|
855
|
+
export default defineConfig({
|
|
856
|
+
input: { path: './petStore.yaml' },
|
|
857
|
+
output: { path: './src/gen' },
|
|
858
|
+
plugins: [
|
|
859
|
+
pluginTs({
|
|
860
|
+
exclude: [
|
|
861
|
+
{ type: 'tag', pattern: 'store' },
|
|
862
|
+
],
|
|
863
|
+
}),
|
|
864
|
+
],
|
|
865
|
+
})
|
|
866
|
+
- name: Skip a specific operation and all delete methods
|
|
867
|
+
files:
|
|
868
|
+
- name: kubb.config.ts
|
|
869
|
+
lang: typescript
|
|
870
|
+
twoslash: false
|
|
871
|
+
code: |
|
|
872
|
+
import { defineConfig } from 'kubb'
|
|
873
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
874
|
+
|
|
875
|
+
export default defineConfig({
|
|
876
|
+
input: { path: './petStore.yaml' },
|
|
877
|
+
output: { path: './src/gen' },
|
|
878
|
+
plugins: [
|
|
879
|
+
pluginTs({
|
|
880
|
+
exclude: [
|
|
881
|
+
{ type: 'operationId', pattern: 'deletePet' },
|
|
882
|
+
{ type: 'method', pattern: 'delete' },
|
|
883
|
+
],
|
|
884
|
+
}),
|
|
885
|
+
],
|
|
886
|
+
})
|
|
496
887
|
- name: override
|
|
497
888
|
type: Array<Override>
|
|
498
889
|
required: false
|
|
499
|
-
description:
|
|
890
|
+
description: |
|
|
891
|
+
Applies a different set of plugin options to operations that match a pattern. Use this when most of your API should follow the global config, but a handful of endpoints need different treatment.
|
|
892
|
+
|
|
893
|
+
Each entry has the same `type` and `pattern` shape as `include`/`exclude`, plus an `options` object that overrides the plugin's options for matched operations.
|
|
894
|
+
|
|
895
|
+
Entries are evaluated top to bottom. The first matching entry's `options` is merged onto the plugin defaults; later entries do not stack.
|
|
500
896
|
codeBlock:
|
|
501
897
|
lang: typescript
|
|
502
|
-
title:
|
|
898
|
+
title: Type definition
|
|
503
899
|
code: |
|
|
504
900
|
export type Override = {
|
|
505
901
|
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
506
902
|
pattern: string | RegExp
|
|
507
903
|
options: PluginOptions
|
|
508
904
|
}
|
|
905
|
+
examples:
|
|
906
|
+
- name: Use a different enum style for the user tag
|
|
907
|
+
files:
|
|
908
|
+
- name: kubb.config.ts
|
|
909
|
+
lang: typescript
|
|
910
|
+
twoslash: false
|
|
911
|
+
code: |
|
|
912
|
+
import { defineConfig } from 'kubb'
|
|
913
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
914
|
+
|
|
915
|
+
export default defineConfig({
|
|
916
|
+
input: { path: './petStore.yaml' },
|
|
917
|
+
output: { path: './src/gen' },
|
|
918
|
+
plugins: [
|
|
919
|
+
pluginTs({
|
|
920
|
+
enumType: 'asConst',
|
|
921
|
+
override: [
|
|
922
|
+
{
|
|
923
|
+
type: 'tag',
|
|
924
|
+
pattern: 'user',
|
|
925
|
+
options: { enumType: 'literal' },
|
|
926
|
+
},
|
|
927
|
+
],
|
|
928
|
+
}),
|
|
929
|
+
],
|
|
930
|
+
})
|
|
509
931
|
- name: generators
|
|
510
932
|
type: Array<Generator<PluginTs>>
|
|
511
933
|
required: false
|
|
512
934
|
experimental: true
|
|
513
935
|
description: |
|
|
514
|
-
|
|
936
|
+
Adds custom generators that run alongside the plugin's built-in generators. Each generator can emit additional files or post-process existing ones using the plugin's AST and options.
|
|
515
937
|
|
|
516
|
-
|
|
938
|
+
Use this when you need output the plugin does not produce out of the box (a custom client wrapper, an extra index, a metadata file). For end-to-end guidance, see [Creating plugins](https://kubb.dev/docs/5.x/guides/creating-plugins).
|
|
939
|
+
warning: |
|
|
940
|
+
Generators are an experimental, low-level API. The signature may change between minor releases.
|
|
517
941
|
- name: transformer
|
|
518
942
|
type: Visitor
|
|
519
943
|
required: false
|
|
520
944
|
description: |
|
|
521
|
-
|
|
945
|
+
Modifies AST nodes before they are printed to source code. Use this when you need to rewrite operation IDs, drop descriptions, or change schema metadata without forking the generator.
|
|
522
946
|
|
|
523
|
-
|
|
947
|
+
Each visitor method (e.g. `schema`, `operation`) receives the node and a context object. Return a new node to replace it, or return `undefined` to leave it untouched. Methods you omit keep the plugin's default behavior.
|
|
524
948
|
examples:
|
|
525
949
|
- name: Strip descriptions before printing
|
|
526
950
|
files:
|
|
527
|
-
-
|
|
951
|
+
- name: kubb.config.ts
|
|
952
|
+
lang: typescript
|
|
953
|
+
twoslash: false
|
|
528
954
|
code: |
|
|
955
|
+
import { defineConfig } from 'kubb'
|
|
529
956
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
530
957
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
958
|
+
export default defineConfig({
|
|
959
|
+
input: { path: './petStore.yaml' },
|
|
960
|
+
output: { path: './src/gen' },
|
|
961
|
+
plugins: [
|
|
962
|
+
pluginTs({
|
|
963
|
+
transformer: {
|
|
964
|
+
schema(node) {
|
|
965
|
+
return { ...node, description: undefined }
|
|
966
|
+
},
|
|
967
|
+
},
|
|
968
|
+
}),
|
|
969
|
+
],
|
|
537
970
|
})
|
|
538
|
-
twoslash: false
|
|
539
971
|
- name: Prefix every operationId
|
|
540
972
|
files:
|
|
541
|
-
-
|
|
973
|
+
- name: kubb.config.ts
|
|
974
|
+
lang: typescript
|
|
975
|
+
twoslash: false
|
|
542
976
|
code: |
|
|
977
|
+
import { defineConfig } from 'kubb'
|
|
543
978
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
544
979
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
980
|
+
export default defineConfig({
|
|
981
|
+
input: { path: './petStore.yaml' },
|
|
982
|
+
output: { path: './src/gen' },
|
|
983
|
+
plugins: [
|
|
984
|
+
pluginTs({
|
|
985
|
+
transformer: {
|
|
986
|
+
operation(node) {
|
|
987
|
+
return { ...node, operationId: `api_${node.operationId}` }
|
|
988
|
+
},
|
|
989
|
+
},
|
|
990
|
+
}),
|
|
991
|
+
],
|
|
551
992
|
})
|
|
552
|
-
twoslash: false
|
|
553
993
|
tip: |
|
|
554
|
-
Use `transformer` to rewrite node properties before printing. For
|
|
994
|
+
Use `transformer` to rewrite node properties before printing. For changing the names of generated symbols and files, use `resolver` instead.
|
|
555
995
|
note: |
|
|
556
|
-
`@kubb/plugin-ts` uses AST
|
|
996
|
+
`@kubb/plugin-ts` uses AST visitors for schema/operation node transforms. For renaming generated symbols, use `resolver` instead.
|
|
557
997
|
- name: printer
|
|
558
998
|
type: '{ nodes?: PrinterTsNodes }'
|
|
559
999
|
required: false
|
|
560
1000
|
description: |
|
|
561
|
-
|
|
1001
|
+
Replaces the TypeScript node handler for a specific schema type (e.g. `'integer'`, `'date'`, `'string'`). Each handler is a function that builds a TypeScript AST node for that schema type.
|
|
562
1002
|
|
|
563
|
-
|
|
1003
|
+
Use `this.transform` to recurse into nested schema nodes and `this.options` to read printer options.
|
|
564
1004
|
examples:
|
|
565
|
-
- name:
|
|
1005
|
+
- name: Use the JavaScript Date object for date schemas
|
|
566
1006
|
files:
|
|
567
1007
|
- lang: typescript
|
|
1008
|
+
twoslash: false
|
|
568
1009
|
code: |
|
|
569
1010
|
import ts from 'typescript'
|
|
1011
|
+
import { defineConfig } from 'kubb'
|
|
570
1012
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
571
1013
|
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
1014
|
+
export default defineConfig({
|
|
1015
|
+
input: { path: './petStore.yaml' },
|
|
1016
|
+
output: { path: './src/gen' },
|
|
1017
|
+
plugins: [
|
|
1018
|
+
pluginTs({
|
|
1019
|
+
printer: {
|
|
1020
|
+
nodes: {
|
|
1021
|
+
date() {
|
|
1022
|
+
return ts.factory.createTypeReferenceNode('Date', [])
|
|
1023
|
+
},
|
|
1024
|
+
},
|
|
577
1025
|
},
|
|
578
|
-
},
|
|
579
|
-
|
|
1026
|
+
}),
|
|
1027
|
+
],
|
|
580
1028
|
})
|
|
581
|
-
|
|
582
|
-
- name: Override integer to bigint
|
|
1029
|
+
- name: Use bigint for integers
|
|
583
1030
|
files:
|
|
584
1031
|
- lang: typescript
|
|
1032
|
+
twoslash: false
|
|
585
1033
|
code: |
|
|
586
1034
|
import ts from 'typescript'
|
|
1035
|
+
import { defineConfig } from 'kubb'
|
|
587
1036
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
588
1037
|
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
1038
|
+
export default defineConfig({
|
|
1039
|
+
input: { path: './petStore.yaml' },
|
|
1040
|
+
output: { path: './src/gen' },
|
|
1041
|
+
plugins: [
|
|
1042
|
+
pluginTs({
|
|
1043
|
+
printer: {
|
|
1044
|
+
nodes: {
|
|
1045
|
+
integer() {
|
|
1046
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword)
|
|
1047
|
+
},
|
|
1048
|
+
},
|
|
594
1049
|
},
|
|
595
|
-
},
|
|
596
|
-
|
|
1050
|
+
}),
|
|
1051
|
+
],
|
|
597
1052
|
})
|
|
598
|
-
twoslash: false
|
|
599
1053
|
examples:
|
|
600
1054
|
- name: kubb.config.ts
|
|
601
1055
|
files:
|
|
602
1056
|
- lang: typescript
|
|
1057
|
+
twoslash: false
|
|
603
1058
|
code: |
|
|
604
|
-
import { defineConfig } from 'kubb'
|
|
605
|
-
import { pluginTs } from '@kubb/plugin-ts'
|
|
1059
|
+
import { defineConfig } from 'kubb'
|
|
1060
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
606
1061
|
|
|
607
1062
|
export default defineConfig({
|
|
608
|
-
input: {
|
|
609
|
-
|
|
610
|
-
},
|
|
611
|
-
output: {
|
|
612
|
-
path: './src/gen',
|
|
613
|
-
},
|
|
1063
|
+
input: { path: './petStore.yaml' },
|
|
1064
|
+
output: { path: './src/gen' },
|
|
614
1065
|
plugins: [
|
|
615
1066
|
pluginTs({
|
|
616
|
-
output: {
|
|
617
|
-
|
|
618
|
-
},
|
|
619
|
-
exclude: [
|
|
620
|
-
{
|
|
621
|
-
type: 'tag',
|
|
622
|
-
pattern: 'store',
|
|
623
|
-
},
|
|
624
|
-
],
|
|
1067
|
+
output: { path: './types' },
|
|
1068
|
+
exclude: [{ type: 'tag', pattern: 'store' }],
|
|
625
1069
|
group: {
|
|
626
1070
|
type: 'tag',
|
|
627
1071
|
name: ({ group }) => `${group}Controller`,
|
|
@@ -632,4 +1076,3 @@ examples:
|
|
|
632
1076
|
}),
|
|
633
1077
|
],
|
|
634
1078
|
})
|
|
635
|
-
twoslash: false
|