@kubb/plugin-ts 5.0.0-beta.4 → 5.0.0-beta.42
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/README.md +39 -22
- package/dist/index.cjs +432 -126
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +84 -62
- package/dist/index.js +427 -125
- package/dist/index.js.map +1 -1
- package/extension.yaml +713 -265
- package/package.json +10 -12
- package/src/components/Enum.tsx +3 -3
- package/src/factory.ts +54 -25
- package/src/generators/typeGenerator.tsx +100 -39
- package/src/plugin.ts +20 -21
- package/src/printers/functionPrinter.ts +2 -2
- package/src/printers/printerTs.ts +31 -30
- package/src/resolvers/resolverTs.ts +28 -25
- package/src/types.ts +44 -37
- package/src/utils.ts +23 -13
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/extension.yaml
CHANGED
|
@@ -2,18 +2,18 @@ $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
|
-
npmPackage:
|
|
8
|
+
npmPackage: '@kubb/plugin-ts'
|
|
9
9
|
docsPath: /plugins/plugin-ts
|
|
10
10
|
repo: https://github.com/kubb-labs/plugins
|
|
11
11
|
maintainers:
|
|
12
12
|
- name: Stijn Van Hulle
|
|
13
13
|
github: stijnvanhulle
|
|
14
14
|
compatibility:
|
|
15
|
-
kubb:
|
|
16
|
-
node:
|
|
15
|
+
kubb: '>=5.0.0'
|
|
16
|
+
node: '>=22'
|
|
17
17
|
tags:
|
|
18
18
|
- typescript
|
|
19
19
|
- types
|
|
@@ -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
|
|
|
@@ -43,582 +45,1029 @@ options:
|
|
|
43
45
|
- name: output
|
|
44
46
|
type: Output
|
|
45
47
|
required: false
|
|
46
|
-
default: "{ path: 'types',
|
|
47
|
-
description:
|
|
48
|
+
default: "{ path: 'types', barrel: { type: 'named' } }"
|
|
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
|
-
- name:
|
|
57
|
-
type: "
|
|
89
|
+
- name: barrel
|
|
90
|
+
type: "{ type: 'named' | 'all', nested?: boolean } | false"
|
|
58
91
|
required: false
|
|
59
|
-
default: "'named'"
|
|
60
|
-
description:
|
|
92
|
+
default: "{ type: 'named' }"
|
|
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`.
|
|
61
100
|
tip: |
|
|
62
|
-
|
|
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.
|
|
63
102
|
examples:
|
|
64
|
-
- name:
|
|
103
|
+
- name: "'named' (default)"
|
|
65
104
|
files:
|
|
66
|
-
-
|
|
105
|
+
- name: kubb.config.ts
|
|
106
|
+
lang: typescript
|
|
107
|
+
twoslash: false
|
|
67
108
|
code: |
|
|
68
|
-
|
|
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
|
|
69
123
|
twoslash: false
|
|
70
|
-
|
|
124
|
+
code: |
|
|
125
|
+
export { Pet, PetStatus } from './Pet'
|
|
126
|
+
export { Store } from './Store'
|
|
127
|
+
- name: "'all'"
|
|
71
128
|
files:
|
|
72
|
-
-
|
|
129
|
+
- name: kubb.config.ts
|
|
130
|
+
lang: typescript
|
|
131
|
+
twoslash: false
|
|
73
132
|
code: |
|
|
74
|
-
|
|
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
|
|
75
147
|
twoslash: false
|
|
76
|
-
|
|
148
|
+
code: |
|
|
149
|
+
export * from './Pet'
|
|
150
|
+
export * from './Store'
|
|
151
|
+
- name: nested
|
|
77
152
|
files:
|
|
78
|
-
-
|
|
79
|
-
|
|
153
|
+
- name: kubb.config.ts
|
|
154
|
+
lang: typescript
|
|
155
|
+
twoslash: false
|
|
156
|
+
code: |
|
|
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
|
|
80
171
|
twoslash: false
|
|
81
|
-
|
|
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
|
|
181
|
+
- name: 'false'
|
|
82
182
|
files:
|
|
83
|
-
-
|
|
84
|
-
|
|
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
|
|
85
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.
|
|
86
205
|
- name: banner
|
|
87
|
-
type:
|
|
206
|
+
type: 'string | ((node: RootNode) => string)'
|
|
88
207
|
required: false
|
|
89
|
-
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
|
+
})
|
|
90
263
|
- name: footer
|
|
91
|
-
type:
|
|
264
|
+
type: 'string | ((node: RootNode) => string)'
|
|
92
265
|
required: false
|
|
93
|
-
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
|
+
})
|
|
94
292
|
- name: override
|
|
95
293
|
type: boolean
|
|
96
294
|
required: false
|
|
97
|
-
default:
|
|
98
|
-
description:
|
|
295
|
+
default: 'false'
|
|
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
|
+
})
|
|
99
321
|
- name: contentType
|
|
100
322
|
type: "'application/json' | (string & {})"
|
|
101
323
|
required: false
|
|
102
324
|
description: |
|
|
103
|
-
|
|
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'
|
|
104
336
|
|
|
105
|
-
|
|
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
|
+
})
|
|
106
346
|
- name: group
|
|
107
347
|
type: Group
|
|
108
348
|
required: false
|
|
109
349
|
description: |
|
|
110
|
-
|
|
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.
|
|
111
355
|
examples:
|
|
112
356
|
- name: kubb.config.ts
|
|
113
357
|
files:
|
|
114
358
|
- lang: typescript
|
|
115
|
-
code: |
|
|
116
|
-
group: {
|
|
117
|
-
type: 'tag',
|
|
118
|
-
name({ group }) {
|
|
119
|
-
return `${group}Controller`
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
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
|
+
})
|
|
123
376
|
body: |
|
|
124
377
|
With the configuration above, the generator emits:
|
|
125
378
|
|
|
126
379
|
```text
|
|
127
|
-
|
|
128
|
-
├──
|
|
129
|
-
│
|
|
130
|
-
│
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
│ └── getStoreById.ts
|
|
135
|
-
├── petStore.yaml
|
|
136
|
-
├── kubb.config.ts
|
|
137
|
-
└── package.json
|
|
380
|
+
src/gen/
|
|
381
|
+
├── petController/
|
|
382
|
+
│ ├── AddPet.ts
|
|
383
|
+
│ └── GetPet.ts
|
|
384
|
+
└── storeController/
|
|
385
|
+
├── CreateStore.ts
|
|
386
|
+
└── GetStoreById.ts
|
|
138
387
|
```
|
|
139
388
|
properties:
|
|
140
389
|
- name: type
|
|
141
390
|
type: "'tag'"
|
|
142
391
|
required: true
|
|
143
|
-
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.
|
|
144
396
|
note: |
|
|
145
|
-
`Required: true*`
|
|
146
|
-
body: |
|
|
147
|
-
- `'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.
|
|
148
398
|
- name: name
|
|
149
|
-
type:
|
|
399
|
+
type: '(context: GroupContext) => string'
|
|
150
400
|
required: false
|
|
151
401
|
default: (ctx) => `${ctx.group}Controller`
|
|
152
|
-
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.
|
|
153
406
|
- name: enumType
|
|
154
407
|
type: "'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral'"
|
|
155
408
|
required: false
|
|
156
409
|
default: "'asConst'"
|
|
157
|
-
description:
|
|
158
|
-
|
|
159
|
-
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.
|
|
160
412
|
|
|
161
|
-
- `asConst
|
|
162
|
-
- `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.
|
|
163
421
|
examples:
|
|
164
422
|
- name: "'enum'"
|
|
165
423
|
files:
|
|
166
424
|
- lang: typescript
|
|
425
|
+
twoslash: false
|
|
167
426
|
code: |
|
|
168
427
|
enum PetType {
|
|
169
428
|
Dog = 'dog',
|
|
170
429
|
Cat = 'cat',
|
|
171
430
|
}
|
|
172
|
-
|
|
173
|
-
- name: "'asConst'"
|
|
431
|
+
- name: "'asConst' (default)"
|
|
174
432
|
files:
|
|
175
433
|
- lang: typescript
|
|
434
|
+
twoslash: false
|
|
176
435
|
code: |
|
|
177
|
-
const petType = {
|
|
436
|
+
export const petType = {
|
|
178
437
|
Dog: 'dog',
|
|
179
438
|
Cat: 'cat',
|
|
180
|
-
} as const
|
|
181
|
-
|
|
439
|
+
} as const
|
|
440
|
+
|
|
441
|
+
export type PetTypeKey = (typeof petType)[keyof typeof petType]
|
|
182
442
|
- name: "'asPascalConst'"
|
|
183
443
|
files:
|
|
184
444
|
- lang: typescript
|
|
445
|
+
twoslash: false
|
|
185
446
|
code: |
|
|
186
|
-
const PetType = {
|
|
447
|
+
export const PetType = {
|
|
187
448
|
Dog: 'dog',
|
|
188
449
|
Cat: 'cat',
|
|
189
|
-
} as const
|
|
190
|
-
|
|
450
|
+
} as const
|
|
451
|
+
|
|
452
|
+
export type PetTypeKey = (typeof PetType)[keyof typeof PetType]
|
|
191
453
|
- name: "'constEnum'"
|
|
192
454
|
files:
|
|
193
455
|
- lang: typescript
|
|
456
|
+
twoslash: false
|
|
194
457
|
code: |
|
|
195
458
|
const enum PetType {
|
|
196
459
|
Dog = 'dog',
|
|
197
460
|
Cat = 'cat',
|
|
198
461
|
}
|
|
199
|
-
twoslash: false
|
|
200
462
|
- name: "'literal'"
|
|
201
463
|
files:
|
|
202
464
|
- lang: typescript
|
|
203
|
-
code: |
|
|
204
|
-
type PetType = 'dog' | 'cat';
|
|
205
465
|
twoslash: false
|
|
466
|
+
code: |
|
|
467
|
+
export type PetType = 'dog' | 'cat'
|
|
206
468
|
- name: "'inlineLiteral'"
|
|
207
469
|
files:
|
|
208
470
|
- lang: typescript
|
|
471
|
+
twoslash: false
|
|
209
472
|
code: |
|
|
210
|
-
//
|
|
473
|
+
// No separate enum type — values are inlined wherever PetType would have been used
|
|
211
474
|
export interface Pet {
|
|
212
|
-
status?: 'available' | 'pending' | 'sold'
|
|
475
|
+
status?: 'available' | 'pending' | 'sold'
|
|
213
476
|
}
|
|
214
|
-
twoslash: false
|
|
215
477
|
body: |
|
|
216
478
|
> [!TIP]
|
|
217
|
-
>
|
|
479
|
+
> `'inlineLiteral'` produces the cleanest output for small enums — the values appear directly where they are used instead of via a named alias.
|
|
218
480
|
- name: enumSuffix
|
|
219
481
|
required: false
|
|
220
|
-
description:
|
|
482
|
+
description: ''
|
|
221
483
|
warning: |
|
|
222
|
-
|
|
484
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#enumSuffix). Use `adapterOas({ enumSuffix })` instead.
|
|
223
485
|
- name: enumTypeSuffix
|
|
224
486
|
type: string
|
|
225
487
|
required: false
|
|
226
488
|
default: "'Key'"
|
|
227
489
|
description: |
|
|
228
|
-
Suffix appended to the
|
|
490
|
+
Suffix appended to the type alias generated for enums when `enumType` is `'asConst'` or `'asPascalConst'`.
|
|
229
491
|
|
|
230
|
-
|
|
492
|
+
The const object name (e.g. `petType`) is unaffected — only the companion type alias is renamed.
|
|
231
493
|
examples:
|
|
232
494
|
- name: "'Key' (default)"
|
|
233
495
|
files:
|
|
234
496
|
- lang: typescript
|
|
497
|
+
twoslash: false
|
|
235
498
|
code: |
|
|
236
|
-
const petType = {
|
|
499
|
+
export const petType = {
|
|
237
500
|
Dog: 'dog',
|
|
238
501
|
Cat: 'cat',
|
|
239
|
-
} as const
|
|
502
|
+
} as const
|
|
240
503
|
|
|
241
|
-
export type PetTypeKey = (typeof petType)[keyof typeof petType]
|
|
242
|
-
twoslash: false
|
|
504
|
+
export type PetTypeKey = (typeof petType)[keyof typeof petType]
|
|
243
505
|
- name: "'Value'"
|
|
244
506
|
files:
|
|
245
507
|
- lang: typescript
|
|
508
|
+
twoslash: false
|
|
246
509
|
code: |
|
|
247
|
-
|
|
248
|
-
const petType = {
|
|
510
|
+
export const petType = {
|
|
249
511
|
Dog: 'dog',
|
|
250
512
|
Cat: 'cat',
|
|
251
|
-
} as const
|
|
513
|
+
} as const
|
|
252
514
|
|
|
253
|
-
export type PetTypeValue = (typeof petType)[keyof typeof petType]
|
|
254
|
-
twoslash: false
|
|
515
|
+
export type PetTypeValue = (typeof petType)[keyof typeof petType]
|
|
255
516
|
- name: "'' (no suffix)"
|
|
256
517
|
files:
|
|
257
518
|
- lang: typescript
|
|
519
|
+
twoslash: false
|
|
258
520
|
code: |
|
|
259
|
-
|
|
260
|
-
const petType = {
|
|
521
|
+
export const petType = {
|
|
261
522
|
Dog: 'dog',
|
|
262
523
|
Cat: 'cat',
|
|
263
|
-
} as const
|
|
524
|
+
} as const
|
|
264
525
|
|
|
265
|
-
export type PetType = (typeof petType)[keyof typeof petType]
|
|
266
|
-
twoslash: false
|
|
526
|
+
export type PetType = (typeof petType)[keyof typeof petType]
|
|
267
527
|
- name: enumKeyCasing
|
|
268
528
|
type: "'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none'"
|
|
269
529
|
required: false
|
|
270
530
|
default: "'none'"
|
|
271
|
-
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.
|
|
272
533
|
body: |
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
534
|
+
| Value | Example key |
|
|
535
|
+
| --- | --- |
|
|
536
|
+
| `'screamingSnakeCase'` | `ENUM_VALUE` |
|
|
537
|
+
| `'snakeCase'` | `enum_value` |
|
|
538
|
+
| `'pascalCase'` | `EnumValue` |
|
|
539
|
+
| `'camelCase'` | `enumValue` |
|
|
540
|
+
| `'none'` (default) | as-is |
|
|
278
541
|
- name: dateType
|
|
279
542
|
required: false
|
|
280
|
-
description:
|
|
543
|
+
description: ''
|
|
281
544
|
warning: |
|
|
282
|
-
|
|
545
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#dateType). Use `adapterOas({ dateType })` instead.
|
|
283
546
|
- name: integerType
|
|
284
547
|
required: false
|
|
285
|
-
description:
|
|
548
|
+
description: ''
|
|
286
549
|
warning: |
|
|
287
|
-
|
|
550
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#integerType). Use `adapterOas({ integerType })` instead.
|
|
288
551
|
- name: syntaxType
|
|
289
552
|
type: "'type' | 'interface'"
|
|
290
553
|
required: false
|
|
291
554
|
default: "'type'"
|
|
292
555
|
description: |
|
|
293
|
-
|
|
294
|
-
|
|
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).
|
|
295
561
|
examples:
|
|
296
|
-
- name: "'type'"
|
|
562
|
+
- name: "'type' (default)"
|
|
297
563
|
files:
|
|
298
564
|
- lang: typescript
|
|
299
|
-
code: |
|
|
300
|
-
type Pet = {
|
|
301
|
-
name: string;
|
|
302
|
-
};
|
|
303
565
|
twoslash: false
|
|
566
|
+
code: |
|
|
567
|
+
export type Pet = {
|
|
568
|
+
name: string
|
|
569
|
+
}
|
|
304
570
|
- name: "'interface'"
|
|
305
571
|
files:
|
|
306
572
|
- lang: typescript
|
|
573
|
+
twoslash: false
|
|
307
574
|
code: |
|
|
308
|
-
interface Pet {
|
|
309
|
-
name: string
|
|
575
|
+
export interface Pet {
|
|
576
|
+
name: string
|
|
310
577
|
}
|
|
311
|
-
twoslash: false
|
|
312
578
|
- name: unknownType
|
|
313
579
|
required: false
|
|
314
|
-
description:
|
|
580
|
+
description: ''
|
|
315
581
|
warning: |
|
|
316
|
-
|
|
582
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#unknownType). Use `adapterOas({ unknownType })` instead.
|
|
317
583
|
- name: emptySchemaType
|
|
318
584
|
required: false
|
|
319
|
-
description:
|
|
585
|
+
description: ''
|
|
320
586
|
warning: |
|
|
321
|
-
|
|
587
|
+
Moved to [`adapterOas`](/adapters/adapter-oas#emptySchemaType). Use `adapterOas({ emptySchemaType })` instead.
|
|
322
588
|
- name: optionalType
|
|
323
589
|
type: "'questionToken' | 'undefined' | 'questionTokenAndUndefined'"
|
|
324
590
|
required: false
|
|
325
591
|
default: "'questionToken'"
|
|
326
|
-
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.
|
|
327
600
|
examples:
|
|
328
|
-
- name: "'questionToken'"
|
|
601
|
+
- name: "'questionToken' (default)"
|
|
329
602
|
files:
|
|
330
603
|
- lang: typescript
|
|
331
|
-
code: |
|
|
332
|
-
type Pet = {
|
|
333
|
-
type?: string;
|
|
334
|
-
};
|
|
335
604
|
twoslash: false
|
|
605
|
+
code: |
|
|
606
|
+
export type Pet = {
|
|
607
|
+
type?: string
|
|
608
|
+
}
|
|
336
609
|
- name: "'undefined'"
|
|
337
610
|
files:
|
|
338
611
|
- lang: typescript
|
|
339
|
-
code: |
|
|
340
|
-
type Pet = {
|
|
341
|
-
type: string | undefined;
|
|
342
|
-
};
|
|
343
612
|
twoslash: false
|
|
613
|
+
code: |
|
|
614
|
+
export type Pet = {
|
|
615
|
+
type: string | undefined
|
|
616
|
+
}
|
|
344
617
|
- name: "'questionTokenAndUndefined'"
|
|
345
618
|
files:
|
|
346
619
|
- lang: typescript
|
|
347
|
-
code: |
|
|
348
|
-
type Pet = {
|
|
349
|
-
type?: string | undefined;
|
|
350
|
-
};
|
|
351
620
|
twoslash: false
|
|
621
|
+
code: |
|
|
622
|
+
export type Pet = {
|
|
623
|
+
type?: string | undefined
|
|
624
|
+
}
|
|
352
625
|
- name: arrayType
|
|
353
626
|
type: "'array' | 'generic'"
|
|
354
627
|
required: false
|
|
355
628
|
default: "'array'"
|
|
356
|
-
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 }>`).
|
|
357
634
|
examples:
|
|
358
|
-
- name: "'array'"
|
|
635
|
+
- name: "'array' (default)"
|
|
359
636
|
files:
|
|
360
637
|
- lang: typescript
|
|
361
|
-
code: |
|
|
362
|
-
type Pet = {
|
|
363
|
-
tags: string[];
|
|
364
|
-
};
|
|
365
638
|
twoslash: false
|
|
639
|
+
code: |
|
|
640
|
+
export type Pet = {
|
|
641
|
+
tags: string[]
|
|
642
|
+
}
|
|
366
643
|
- name: "'generic'"
|
|
367
644
|
files:
|
|
368
645
|
- lang: typescript
|
|
369
|
-
code: |
|
|
370
|
-
type Pet = {
|
|
371
|
-
tags: Array<string>;
|
|
372
|
-
};
|
|
373
646
|
twoslash: false
|
|
647
|
+
code: |
|
|
648
|
+
export type Pet = {
|
|
649
|
+
tags: Array<string>
|
|
650
|
+
}
|
|
374
651
|
- name: paramsCasing
|
|
375
652
|
type: "'camelcase'"
|
|
376
653
|
required: false
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
important: |
|
|
380
|
-
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.
|
|
381
656
|
|
|
382
|
-
|
|
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.
|
|
383
660
|
examples:
|
|
384
|
-
- name:
|
|
661
|
+
- name: Without `paramsCasing`
|
|
385
662
|
files:
|
|
386
663
|
- lang: typescript
|
|
664
|
+
twoslash: false
|
|
387
665
|
code: |
|
|
388
|
-
// OpenAPI spec
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
step_id: string;
|
|
393
|
-
};
|
|
666
|
+
// OpenAPI spec: step_id, bool_param, X-Custom-Header
|
|
667
|
+
export type FindPetsByStatusPathParams = {
|
|
668
|
+
step_id: string
|
|
669
|
+
}
|
|
394
670
|
|
|
395
|
-
type FindPetsByStatusQueryParams = {
|
|
396
|
-
bool_param?: boolean
|
|
397
|
-
}
|
|
671
|
+
export type FindPetsByStatusQueryParams = {
|
|
672
|
+
bool_param?: boolean
|
|
673
|
+
}
|
|
398
674
|
|
|
399
|
-
type FindPetsByStatusHeaderParams = {
|
|
400
|
-
'X-Custom-Header'?: string
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
- name: "With paramsCasing: 'camelcase'"
|
|
675
|
+
export type FindPetsByStatusHeaderParams = {
|
|
676
|
+
'X-Custom-Header'?: string
|
|
677
|
+
}
|
|
678
|
+
- name: "With `paramsCasing: 'camelcase'`"
|
|
404
679
|
files:
|
|
405
680
|
- lang: typescript
|
|
681
|
+
twoslash: false
|
|
406
682
|
code: |
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
stepId: string; // ✓ camelCase
|
|
411
|
-
};
|
|
683
|
+
export type FindPetsByStatusPathParams = {
|
|
684
|
+
stepId: string
|
|
685
|
+
}
|
|
412
686
|
|
|
413
|
-
type FindPetsByStatusQueryParams = {
|
|
414
|
-
boolParam?: boolean
|
|
415
|
-
}
|
|
687
|
+
export type FindPetsByStatusQueryParams = {
|
|
688
|
+
boolParam?: boolean
|
|
689
|
+
}
|
|
416
690
|
|
|
417
|
-
type FindPetsByStatusHeaderParams = {
|
|
418
|
-
xCustomHeader?: string
|
|
419
|
-
}
|
|
420
|
-
twoslash: false
|
|
691
|
+
export type FindPetsByStatusHeaderParams = {
|
|
692
|
+
xCustomHeader?: string
|
|
693
|
+
}
|
|
421
694
|
- name: resolver
|
|
422
695
|
type: Partial<ResolverTs> & ThisType<ResolverTs>
|
|
423
696
|
required: false
|
|
424
697
|
description: |
|
|
425
|
-
|
|
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.
|
|
699
|
+
|
|
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.
|
|
426
701
|
|
|
427
|
-
|
|
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.
|
|
428
703
|
body: |
|
|
429
|
-
Each plugin ships a
|
|
704
|
+
Each plugin ships with a default resolver:
|
|
430
705
|
|
|
431
706
|
| Plugin | Default resolver |
|
|
432
707
|
| --- | --- |
|
|
433
708
|
| `@kubb/plugin-ts` | `resolverTs` |
|
|
434
709
|
| `@kubb/plugin-zod` | `resolverZod` |
|
|
710
|
+
| `@kubb/plugin-faker` | `resolverFaker` |
|
|
435
711
|
| `@kubb/plugin-cypress` | `resolverCypress` |
|
|
712
|
+
| `@kubb/plugin-msw` | `resolverMsw` |
|
|
436
713
|
| `@kubb/plugin-mcp` | `resolverMcp` |
|
|
714
|
+
| `@kubb/plugin-client` | `resolverClient` |
|
|
437
715
|
codeBlock:
|
|
438
716
|
lang: typescript
|
|
439
|
-
title:
|
|
717
|
+
title: Add an Api prefix to every name
|
|
440
718
|
code: |
|
|
719
|
+
import { defineConfig } from 'kubb'
|
|
441
720
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
442
721
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
722
|
+
export default defineConfig({
|
|
723
|
+
input: { path: './petStore.yaml' },
|
|
724
|
+
output: { path: './src/gen' },
|
|
725
|
+
plugins: [
|
|
726
|
+
pluginTs({
|
|
727
|
+
resolver: {
|
|
728
|
+
resolveName(name) {
|
|
729
|
+
return `Api${this.default(name, 'function')}`
|
|
730
|
+
},
|
|
731
|
+
},
|
|
732
|
+
}),
|
|
733
|
+
],
|
|
451
734
|
})
|
|
452
735
|
tip: |
|
|
453
|
-
Use `resolver` for
|
|
736
|
+
Use `resolver` for naming and file-location tweaks. For changing the AST nodes themselves (e.g. stripping descriptions), use `transformer` instead.
|
|
454
737
|
examples:
|
|
455
|
-
- name: Custom prefix
|
|
738
|
+
- name: Add a Custom prefix to every name
|
|
456
739
|
files:
|
|
457
740
|
- lang: typescript
|
|
741
|
+
twoslash: false
|
|
458
742
|
code: |
|
|
743
|
+
import { defineConfig } from 'kubb'
|
|
459
744
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
460
745
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
746
|
+
export default defineConfig({
|
|
747
|
+
input: { path: './petStore.yaml' },
|
|
748
|
+
output: { path: './src/gen' },
|
|
749
|
+
plugins: [
|
|
750
|
+
pluginTs({
|
|
751
|
+
resolver: {
|
|
752
|
+
resolveName(name) {
|
|
753
|
+
return `Custom${this.default(name, 'function')}`
|
|
754
|
+
},
|
|
755
|
+
},
|
|
756
|
+
}),
|
|
757
|
+
],
|
|
467
758
|
})
|
|
468
|
-
twoslash: false
|
|
469
759
|
- name: include
|
|
470
760
|
type: Array<Include>
|
|
471
761
|
required: false
|
|
472
|
-
description:
|
|
762
|
+
description: |
|
|
763
|
+
Restricts generation to operations that match at least one entry in the list. Anything not matched is skipped.
|
|
764
|
+
|
|
765
|
+
Each entry filters by one of:
|
|
766
|
+
|
|
767
|
+
- `tag` — the operation's first tag in the OpenAPI spec.
|
|
768
|
+
- `operationId` — the operation's `operationId`.
|
|
769
|
+
- `path` — the URL pattern (`'/pet/{petId}'`).
|
|
770
|
+
- `method` — HTTP method (`'get'`, `'post'`, ...).
|
|
771
|
+
- `contentType` — the media type of the request body.
|
|
772
|
+
|
|
773
|
+
`pattern` accepts either a string (exact match) or a `RegExp` for fuzzy matches.
|
|
473
774
|
codeBlock:
|
|
474
775
|
lang: typescript
|
|
475
|
-
title:
|
|
776
|
+
title: Type definition
|
|
476
777
|
code: |
|
|
477
778
|
export type Include = {
|
|
478
779
|
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
479
780
|
pattern: string | RegExp
|
|
480
781
|
}
|
|
782
|
+
examples:
|
|
783
|
+
- name: Only the pet tag
|
|
784
|
+
files:
|
|
785
|
+
- name: kubb.config.ts
|
|
786
|
+
lang: typescript
|
|
787
|
+
twoslash: false
|
|
788
|
+
code: |
|
|
789
|
+
import { defineConfig } from 'kubb'
|
|
790
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
791
|
+
|
|
792
|
+
export default defineConfig({
|
|
793
|
+
input: { path: './petStore.yaml' },
|
|
794
|
+
output: { path: './src/gen' },
|
|
795
|
+
plugins: [
|
|
796
|
+
pluginTs({
|
|
797
|
+
include: [
|
|
798
|
+
{ type: 'tag', pattern: 'pet' },
|
|
799
|
+
],
|
|
800
|
+
}),
|
|
801
|
+
],
|
|
802
|
+
})
|
|
803
|
+
- name: Only GET operations under /pet
|
|
804
|
+
files:
|
|
805
|
+
- name: kubb.config.ts
|
|
806
|
+
lang: typescript
|
|
807
|
+
twoslash: false
|
|
808
|
+
code: |
|
|
809
|
+
import { defineConfig } from 'kubb'
|
|
810
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
811
|
+
|
|
812
|
+
export default defineConfig({
|
|
813
|
+
input: { path: './petStore.yaml' },
|
|
814
|
+
output: { path: './src/gen' },
|
|
815
|
+
plugins: [
|
|
816
|
+
pluginTs({
|
|
817
|
+
include: [
|
|
818
|
+
{ type: 'method', pattern: 'get' },
|
|
819
|
+
{ type: 'path', pattern: /^\/pet/ },
|
|
820
|
+
],
|
|
821
|
+
}),
|
|
822
|
+
],
|
|
823
|
+
})
|
|
481
824
|
- name: exclude
|
|
482
825
|
type: Array<Exclude>
|
|
483
826
|
required: false
|
|
484
|
-
description:
|
|
827
|
+
description: |
|
|
828
|
+
Skips any operation that matches at least one entry in the list. The opposite of `include`.
|
|
829
|
+
|
|
830
|
+
Each entry filters by one of:
|
|
831
|
+
|
|
832
|
+
- `tag` — the operation's first tag.
|
|
833
|
+
- `operationId` — the operation's `operationId`.
|
|
834
|
+
- `path` — the URL pattern (`'/pet/{petId}'`).
|
|
835
|
+
- `method` — HTTP method (`'get'`, `'post'`, ...).
|
|
836
|
+
- `contentType` — the media type of the request body.
|
|
837
|
+
|
|
838
|
+
`pattern` accepts a plain string or a `RegExp`. When both `include` and `exclude` are set, `exclude` wins.
|
|
485
839
|
codeBlock:
|
|
486
840
|
lang: typescript
|
|
487
|
-
title:
|
|
841
|
+
title: Type definition
|
|
488
842
|
code: |
|
|
489
843
|
export type Exclude = {
|
|
490
844
|
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
491
845
|
pattern: string | RegExp
|
|
492
846
|
}
|
|
847
|
+
examples:
|
|
848
|
+
- name: Skip everything under the store tag
|
|
849
|
+
files:
|
|
850
|
+
- name: kubb.config.ts
|
|
851
|
+
lang: typescript
|
|
852
|
+
twoslash: false
|
|
853
|
+
code: |
|
|
854
|
+
import { defineConfig } from 'kubb'
|
|
855
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
856
|
+
|
|
857
|
+
export default defineConfig({
|
|
858
|
+
input: { path: './petStore.yaml' },
|
|
859
|
+
output: { path: './src/gen' },
|
|
860
|
+
plugins: [
|
|
861
|
+
pluginTs({
|
|
862
|
+
exclude: [
|
|
863
|
+
{ type: 'tag', pattern: 'store' },
|
|
864
|
+
],
|
|
865
|
+
}),
|
|
866
|
+
],
|
|
867
|
+
})
|
|
868
|
+
- name: Skip a specific operation and all delete methods
|
|
869
|
+
files:
|
|
870
|
+
- name: kubb.config.ts
|
|
871
|
+
lang: typescript
|
|
872
|
+
twoslash: false
|
|
873
|
+
code: |
|
|
874
|
+
import { defineConfig } from 'kubb'
|
|
875
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
876
|
+
|
|
877
|
+
export default defineConfig({
|
|
878
|
+
input: { path: './petStore.yaml' },
|
|
879
|
+
output: { path: './src/gen' },
|
|
880
|
+
plugins: [
|
|
881
|
+
pluginTs({
|
|
882
|
+
exclude: [
|
|
883
|
+
{ type: 'operationId', pattern: 'deletePet' },
|
|
884
|
+
{ type: 'method', pattern: 'delete' },
|
|
885
|
+
],
|
|
886
|
+
}),
|
|
887
|
+
],
|
|
888
|
+
})
|
|
493
889
|
- name: override
|
|
494
890
|
type: Array<Override>
|
|
495
891
|
required: false
|
|
496
|
-
description:
|
|
892
|
+
description: |
|
|
893
|
+
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.
|
|
894
|
+
|
|
895
|
+
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.
|
|
896
|
+
|
|
897
|
+
Entries are evaluated top to bottom. The first matching entry's `options` is merged onto the plugin defaults; later entries do not stack.
|
|
497
898
|
codeBlock:
|
|
498
899
|
lang: typescript
|
|
499
|
-
title:
|
|
900
|
+
title: Type definition
|
|
500
901
|
code: |
|
|
501
902
|
export type Override = {
|
|
502
903
|
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
503
904
|
pattern: string | RegExp
|
|
504
905
|
options: PluginOptions
|
|
505
906
|
}
|
|
907
|
+
examples:
|
|
908
|
+
- name: Use a different enum style for the user tag
|
|
909
|
+
files:
|
|
910
|
+
- name: kubb.config.ts
|
|
911
|
+
lang: typescript
|
|
912
|
+
twoslash: false
|
|
913
|
+
code: |
|
|
914
|
+
import { defineConfig } from 'kubb'
|
|
915
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
916
|
+
|
|
917
|
+
export default defineConfig({
|
|
918
|
+
input: { path: './petStore.yaml' },
|
|
919
|
+
output: { path: './src/gen' },
|
|
920
|
+
plugins: [
|
|
921
|
+
pluginTs({
|
|
922
|
+
enumType: 'asConst',
|
|
923
|
+
override: [
|
|
924
|
+
{
|
|
925
|
+
type: 'tag',
|
|
926
|
+
pattern: 'user',
|
|
927
|
+
options: { enumType: 'literal' },
|
|
928
|
+
},
|
|
929
|
+
],
|
|
930
|
+
}),
|
|
931
|
+
],
|
|
932
|
+
})
|
|
506
933
|
- name: generators
|
|
507
934
|
type: Array<Generator<PluginTs>>
|
|
508
935
|
required: false
|
|
509
936
|
experimental: true
|
|
510
937
|
description: |
|
|
511
|
-
|
|
938
|
+
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.
|
|
512
939
|
|
|
513
|
-
|
|
940
|
+
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).
|
|
941
|
+
warning: |
|
|
942
|
+
Generators are an experimental, low-level API. The signature may change between minor releases.
|
|
514
943
|
- name: transformer
|
|
515
944
|
type: Visitor
|
|
516
945
|
required: false
|
|
517
946
|
description: |
|
|
518
|
-
|
|
947
|
+
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.
|
|
519
948
|
|
|
520
|
-
|
|
949
|
+
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.
|
|
521
950
|
examples:
|
|
522
951
|
- name: Strip descriptions before printing
|
|
523
952
|
files:
|
|
524
|
-
-
|
|
953
|
+
- name: kubb.config.ts
|
|
954
|
+
lang: typescript
|
|
955
|
+
twoslash: false
|
|
525
956
|
code: |
|
|
957
|
+
import { defineConfig } from 'kubb'
|
|
526
958
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
527
959
|
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
960
|
+
export default defineConfig({
|
|
961
|
+
input: { path: './petStore.yaml' },
|
|
962
|
+
output: { path: './src/gen' },
|
|
963
|
+
plugins: [
|
|
964
|
+
pluginTs({
|
|
965
|
+
transformer: {
|
|
966
|
+
schema(node) {
|
|
967
|
+
return { ...node, description: undefined }
|
|
968
|
+
},
|
|
969
|
+
},
|
|
970
|
+
}),
|
|
971
|
+
],
|
|
534
972
|
})
|
|
535
|
-
twoslash: false
|
|
536
973
|
- name: Prefix every operationId
|
|
537
974
|
files:
|
|
538
|
-
-
|
|
975
|
+
- name: kubb.config.ts
|
|
976
|
+
lang: typescript
|
|
977
|
+
twoslash: false
|
|
539
978
|
code: |
|
|
979
|
+
import { defineConfig } from 'kubb'
|
|
540
980
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
541
981
|
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
982
|
+
export default defineConfig({
|
|
983
|
+
input: { path: './petStore.yaml' },
|
|
984
|
+
output: { path: './src/gen' },
|
|
985
|
+
plugins: [
|
|
986
|
+
pluginTs({
|
|
987
|
+
transformer: {
|
|
988
|
+
operation(node) {
|
|
989
|
+
return { ...node, operationId: `api_${node.operationId}` }
|
|
990
|
+
},
|
|
991
|
+
},
|
|
992
|
+
}),
|
|
993
|
+
],
|
|
548
994
|
})
|
|
549
|
-
twoslash: false
|
|
550
995
|
tip: |
|
|
551
|
-
Use `transformer` to rewrite node properties before printing. For
|
|
996
|
+
Use `transformer` to rewrite node properties before printing. For changing the names of generated symbols and files, use `resolver` instead.
|
|
552
997
|
note: |
|
|
553
|
-
`@kubb/plugin-ts` uses AST
|
|
998
|
+
`@kubb/plugin-ts` uses AST visitors for schema/operation node transforms. For renaming generated symbols, use `resolver` instead.
|
|
554
999
|
- name: printer
|
|
555
|
-
type:
|
|
1000
|
+
type: '{ nodes?: PrinterTsNodes }'
|
|
556
1001
|
required: false
|
|
557
1002
|
description: |
|
|
558
|
-
|
|
1003
|
+
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.
|
|
559
1004
|
|
|
560
|
-
|
|
1005
|
+
Use `this.transform` to recurse into nested schema nodes and `this.options` to read printer options.
|
|
561
1006
|
examples:
|
|
562
|
-
- name:
|
|
1007
|
+
- name: Use the JavaScript Date object for date schemas
|
|
563
1008
|
files:
|
|
564
1009
|
- lang: typescript
|
|
1010
|
+
twoslash: false
|
|
565
1011
|
code: |
|
|
566
1012
|
import ts from 'typescript'
|
|
1013
|
+
import { defineConfig } from 'kubb'
|
|
567
1014
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
568
1015
|
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
1016
|
+
export default defineConfig({
|
|
1017
|
+
input: { path: './petStore.yaml' },
|
|
1018
|
+
output: { path: './src/gen' },
|
|
1019
|
+
plugins: [
|
|
1020
|
+
pluginTs({
|
|
1021
|
+
printer: {
|
|
1022
|
+
nodes: {
|
|
1023
|
+
date() {
|
|
1024
|
+
return ts.factory.createTypeReferenceNode('Date', [])
|
|
1025
|
+
},
|
|
1026
|
+
},
|
|
574
1027
|
},
|
|
575
|
-
},
|
|
576
|
-
|
|
1028
|
+
}),
|
|
1029
|
+
],
|
|
577
1030
|
})
|
|
578
|
-
|
|
579
|
-
- name: Override integer to bigint
|
|
1031
|
+
- name: Use bigint for integers
|
|
580
1032
|
files:
|
|
581
1033
|
- lang: typescript
|
|
1034
|
+
twoslash: false
|
|
582
1035
|
code: |
|
|
583
1036
|
import ts from 'typescript'
|
|
1037
|
+
import { defineConfig } from 'kubb'
|
|
584
1038
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
585
1039
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
1040
|
+
export default defineConfig({
|
|
1041
|
+
input: { path: './petStore.yaml' },
|
|
1042
|
+
output: { path: './src/gen' },
|
|
1043
|
+
plugins: [
|
|
1044
|
+
pluginTs({
|
|
1045
|
+
printer: {
|
|
1046
|
+
nodes: {
|
|
1047
|
+
integer() {
|
|
1048
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword)
|
|
1049
|
+
},
|
|
1050
|
+
},
|
|
591
1051
|
},
|
|
592
|
-
},
|
|
593
|
-
|
|
1052
|
+
}),
|
|
1053
|
+
],
|
|
594
1054
|
})
|
|
595
|
-
twoslash: false
|
|
596
1055
|
examples:
|
|
597
1056
|
- name: kubb.config.ts
|
|
598
1057
|
files:
|
|
599
1058
|
- lang: typescript
|
|
1059
|
+
twoslash: false
|
|
600
1060
|
code: |
|
|
601
|
-
import { defineConfig } from 'kubb'
|
|
602
|
-
import { pluginTs } from '@kubb/plugin-ts'
|
|
1061
|
+
import { defineConfig } from 'kubb'
|
|
1062
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
603
1063
|
|
|
604
1064
|
export default defineConfig({
|
|
605
|
-
input: {
|
|
606
|
-
|
|
607
|
-
},
|
|
608
|
-
output: {
|
|
609
|
-
path: './src/gen',
|
|
610
|
-
},
|
|
1065
|
+
input: { path: './petStore.yaml' },
|
|
1066
|
+
output: { path: './src/gen' },
|
|
611
1067
|
plugins: [
|
|
612
1068
|
pluginTs({
|
|
613
|
-
output: {
|
|
614
|
-
|
|
615
|
-
},
|
|
616
|
-
exclude: [
|
|
617
|
-
{
|
|
618
|
-
type: 'tag',
|
|
619
|
-
pattern: 'store',
|
|
620
|
-
},
|
|
621
|
-
],
|
|
1069
|
+
output: { path: './types' },
|
|
1070
|
+
exclude: [{ type: 'tag', pattern: 'store' }],
|
|
622
1071
|
group: {
|
|
623
1072
|
type: 'tag',
|
|
624
1073
|
name: ({ group }) => `${group}Controller`,
|
|
@@ -629,4 +1078,3 @@ examples:
|
|
|
629
1078
|
}),
|
|
630
1079
|
],
|
|
631
1080
|
})
|
|
632
|
-
twoslash: false
|