@kubb/plugin-ts 5.0.0-alpha.9 → 5.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +17 -10
- package/README.md +1 -3
- package/dist/index.cjs +1452 -5
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +558 -27
- package/dist/index.js +1418 -2
- package/dist/index.js.map +1 -0
- package/extension.yaml +632 -0
- package/package.json +44 -64
- package/src/components/{v2/Enum.tsx → Enum.tsx} +33 -17
- package/src/components/Type.tsx +31 -161
- package/src/constants.ts +10 -0
- package/src/factory.ts +283 -35
- package/src/generators/typeGenerator.tsx +189 -424
- package/src/index.ts +9 -3
- package/src/plugin.ts +66 -205
- package/src/printers/functionPrinter.ts +197 -0
- package/src/printers/printerTs.ts +325 -0
- package/src/resolvers/resolverTs.ts +66 -0
- package/src/types.ts +233 -221
- package/src/utils.ts +130 -0
- package/dist/components-CRu8IKY3.js +0 -729
- package/dist/components-CRu8IKY3.js.map +0 -1
- package/dist/components-DeNDKlzf.cjs +0 -982
- package/dist/components-DeNDKlzf.cjs.map +0 -1
- package/dist/components.cjs +0 -3
- package/dist/components.d.ts +0 -36
- package/dist/components.js +0 -2
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -509
- package/dist/generators.js +0 -2
- package/dist/plugin-BZkBwnEA.js +0 -1269
- package/dist/plugin-BZkBwnEA.js.map +0 -1
- package/dist/plugin-Bunz1oGa.cjs +0 -1322
- package/dist/plugin-Bunz1oGa.cjs.map +0 -1
- package/dist/types-mSXmB8WU.d.ts +0 -298
- package/src/components/index.ts +0 -1
- package/src/components/v2/Type.tsx +0 -59
- package/src/generators/index.ts +0 -2
- package/src/generators/v2/typeGenerator.tsx +0 -167
- package/src/generators/v2/utils.ts +0 -140
- package/src/parser.ts +0 -389
- package/src/printer.ts +0 -368
- package/src/resolverTs.ts +0 -77
package/extension.yaml
ADDED
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
$schema: https://kubb.dev/schemas/extension.json
|
|
2
|
+
kind: plugin
|
|
3
|
+
id: plugin-ts
|
|
4
|
+
name: TypeScript
|
|
5
|
+
description: Generate TypeScript types and interfaces from OpenAPI schemas with type-safe representations.
|
|
6
|
+
category: types
|
|
7
|
+
type: official
|
|
8
|
+
npmPackage: "@kubb/plugin-ts"
|
|
9
|
+
docsPath: /plugins/plugin-ts
|
|
10
|
+
repo: https://github.com/kubb-labs/plugins
|
|
11
|
+
maintainers:
|
|
12
|
+
- name: Stijn Van Hulle
|
|
13
|
+
github: stijnvanhulle
|
|
14
|
+
compatibility:
|
|
15
|
+
kubb: ">=5.0.0"
|
|
16
|
+
node: ">=22"
|
|
17
|
+
tags:
|
|
18
|
+
- typescript
|
|
19
|
+
- types
|
|
20
|
+
- interfaces
|
|
21
|
+
- codegen
|
|
22
|
+
- openapi
|
|
23
|
+
dependencies: []
|
|
24
|
+
resources:
|
|
25
|
+
documentation: https://kubb.dev/plugins/plugin-ts
|
|
26
|
+
repository: https://github.com/kubb-labs/plugins
|
|
27
|
+
issues: https://github.com/kubb-labs/plugins/issues
|
|
28
|
+
changelog: https://github.com/kubb-labs/plugins/blob/main/packages/plugin-ts/CHANGELOG.md
|
|
29
|
+
codesandbox: https://codesandbox.io/p/github/kubb-labs/plugins/main/examples/typescript
|
|
30
|
+
featured: true
|
|
31
|
+
icon:
|
|
32
|
+
light: https://kubb.dev/feature/typescript.svg
|
|
33
|
+
intro: |
|
|
34
|
+
# @kubb/plugin-ts
|
|
35
|
+
|
|
36
|
+
Generate TypeScript types from your OpenAPI schema. Use this plugin to produce type-safe representations of your API's request and response shapes, giving your TypeScript project compile-time guarantees over every API interaction.
|
|
37
|
+
|
|
38
|
+
**See also**
|
|
39
|
+
|
|
40
|
+
- [TypeScript](https://www.typescriptlang.org/)
|
|
41
|
+
- [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API)
|
|
42
|
+
options:
|
|
43
|
+
- name: output
|
|
44
|
+
type: Output
|
|
45
|
+
required: false
|
|
46
|
+
default: "{ path: 'types', barrelType: 'named' }"
|
|
47
|
+
description: Specify the export location for the files and define the behavior of the output.
|
|
48
|
+
properties:
|
|
49
|
+
- name: path
|
|
50
|
+
type: string
|
|
51
|
+
required: true
|
|
52
|
+
description: Output directory or file for the generated code, relative to the global `output.path`.
|
|
53
|
+
tip: |
|
|
54
|
+
if `output.path` is a file, `group` cannot be used.
|
|
55
|
+
default: "'types'"
|
|
56
|
+
- name: barrelType
|
|
57
|
+
type: "'all' | 'named' | 'propagate' | false"
|
|
58
|
+
required: false
|
|
59
|
+
default: "'named'"
|
|
60
|
+
description: Specify what to export and optionally disable barrel-file generation.
|
|
61
|
+
tip: |
|
|
62
|
+
Using `propagate` will prevent a plugin from creating a barrel file, but it will still propagate, allowing [`output.barrelType`](https://kubb.dev/docs/5.x/configuration#output-barreltype) to export the specific function or type.
|
|
63
|
+
examples:
|
|
64
|
+
- name: all
|
|
65
|
+
files:
|
|
66
|
+
- lang: typescript
|
|
67
|
+
code: |
|
|
68
|
+
export * from './gen/petService.ts'
|
|
69
|
+
twoslash: false
|
|
70
|
+
- name: named
|
|
71
|
+
files:
|
|
72
|
+
- lang: typescript
|
|
73
|
+
code: |
|
|
74
|
+
export { PetService } from './gen/petService.ts'
|
|
75
|
+
twoslash: false
|
|
76
|
+
- name: propagate
|
|
77
|
+
files:
|
|
78
|
+
- lang: typescript
|
|
79
|
+
code: ""
|
|
80
|
+
twoslash: false
|
|
81
|
+
- name: "false"
|
|
82
|
+
files:
|
|
83
|
+
- lang: typescript
|
|
84
|
+
code: ""
|
|
85
|
+
twoslash: false
|
|
86
|
+
- name: banner
|
|
87
|
+
type: "string | ((node: RootNode) => string)"
|
|
88
|
+
required: false
|
|
89
|
+
description: Add a banner comment at the top of every generated file. Accepts a static string or a function that receives the `RootNode` and returns a string.
|
|
90
|
+
- name: footer
|
|
91
|
+
type: "string | ((node: RootNode) => string)"
|
|
92
|
+
required: false
|
|
93
|
+
description: Add a footer comment at the end of every generated file. Accepts a static string or a function that receives the `RootNode` and returns a string.
|
|
94
|
+
- name: override
|
|
95
|
+
type: boolean
|
|
96
|
+
required: false
|
|
97
|
+
default: "false"
|
|
98
|
+
description: Whether Kubb overrides existing external files that can be generated if they already exist.
|
|
99
|
+
- name: contentType
|
|
100
|
+
type: "'application/json' | (string & {})"
|
|
101
|
+
required: false
|
|
102
|
+
description: |
|
|
103
|
+
Define which content type to use.
|
|
104
|
+
|
|
105
|
+
By default, Kubb uses the first JSON-valid media type.
|
|
106
|
+
- name: group
|
|
107
|
+
type: Group
|
|
108
|
+
required: false
|
|
109
|
+
description: |
|
|
110
|
+
Grouping combines files in a folder based on a specific `type`.
|
|
111
|
+
examples:
|
|
112
|
+
- name: kubb.config.ts
|
|
113
|
+
files:
|
|
114
|
+
- lang: typescript
|
|
115
|
+
code: |
|
|
116
|
+
group: {
|
|
117
|
+
type: 'tag',
|
|
118
|
+
name({ group }) {
|
|
119
|
+
return `${group}Controller`
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
twoslash: false
|
|
123
|
+
body: |
|
|
124
|
+
With the configuration above, the generator emits:
|
|
125
|
+
|
|
126
|
+
```text
|
|
127
|
+
.
|
|
128
|
+
├── src/
|
|
129
|
+
│ └── petController/
|
|
130
|
+
│ │ ├── addPet.ts
|
|
131
|
+
│ │ └── getPet.ts
|
|
132
|
+
│ └── storeController/
|
|
133
|
+
│ ├── createStore.ts
|
|
134
|
+
│ └── getStoreById.ts
|
|
135
|
+
├── petStore.yaml
|
|
136
|
+
├── kubb.config.ts
|
|
137
|
+
└── package.json
|
|
138
|
+
```
|
|
139
|
+
properties:
|
|
140
|
+
- name: type
|
|
141
|
+
type: "'tag'"
|
|
142
|
+
required: true
|
|
143
|
+
description: Specify the property to group files by. Required when `group` is defined.
|
|
144
|
+
note: |
|
|
145
|
+
`Required: true*` means this is required only when the `group` option is used. The `group` option itself is optional.
|
|
146
|
+
body: |
|
|
147
|
+
- `'tag'`: Uses the first tag from `operation.getTags().at(0)?.name`
|
|
148
|
+
- name: name
|
|
149
|
+
type: "(context: GroupContext) => string"
|
|
150
|
+
required: false
|
|
151
|
+
default: (ctx) => `${ctx.group}Controller`
|
|
152
|
+
description: Return the name of a group based on the group name, this will be used for the file and name generation.
|
|
153
|
+
- name: enumType
|
|
154
|
+
type: "'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral'"
|
|
155
|
+
required: false
|
|
156
|
+
default: "'asConst'"
|
|
157
|
+
description: Choose to use `enum` or `as const` for enums.
|
|
158
|
+
tip: |
|
|
159
|
+
The difference between `asConst` and `asPascalConst` is the casing of the constant variable name:
|
|
160
|
+
|
|
161
|
+
- `asConst`: generates a camelCase constant name (e.g., `petType`)
|
|
162
|
+
- `asPascalConst`: generates a PascalCase constant name (e.g., `PetType`)
|
|
163
|
+
examples:
|
|
164
|
+
- name: "'enum'"
|
|
165
|
+
files:
|
|
166
|
+
- lang: typescript
|
|
167
|
+
code: |
|
|
168
|
+
enum PetType {
|
|
169
|
+
Dog = 'dog',
|
|
170
|
+
Cat = 'cat',
|
|
171
|
+
}
|
|
172
|
+
twoslash: false
|
|
173
|
+
- name: "'asConst'"
|
|
174
|
+
files:
|
|
175
|
+
- lang: typescript
|
|
176
|
+
code: |
|
|
177
|
+
const petType = {
|
|
178
|
+
Dog: 'dog',
|
|
179
|
+
Cat: 'cat',
|
|
180
|
+
} as const;
|
|
181
|
+
twoslash: false
|
|
182
|
+
- name: "'asPascalConst'"
|
|
183
|
+
files:
|
|
184
|
+
- lang: typescript
|
|
185
|
+
code: |
|
|
186
|
+
const PetType = {
|
|
187
|
+
Dog: 'dog',
|
|
188
|
+
Cat: 'cat',
|
|
189
|
+
} as const;
|
|
190
|
+
twoslash: false
|
|
191
|
+
- name: "'constEnum'"
|
|
192
|
+
files:
|
|
193
|
+
- lang: typescript
|
|
194
|
+
code: |
|
|
195
|
+
const enum PetType {
|
|
196
|
+
Dog = 'dog',
|
|
197
|
+
Cat = 'cat',
|
|
198
|
+
}
|
|
199
|
+
twoslash: false
|
|
200
|
+
- name: "'literal'"
|
|
201
|
+
files:
|
|
202
|
+
- lang: typescript
|
|
203
|
+
code: |
|
|
204
|
+
type PetType = 'dog' | 'cat';
|
|
205
|
+
twoslash: false
|
|
206
|
+
- name: "'inlineLiteral'"
|
|
207
|
+
files:
|
|
208
|
+
- lang: typescript
|
|
209
|
+
code: |
|
|
210
|
+
// Enum values are inlined directly into the type
|
|
211
|
+
export interface Pet {
|
|
212
|
+
status?: 'available' | 'pending' | 'sold';
|
|
213
|
+
}
|
|
214
|
+
twoslash: false
|
|
215
|
+
body: |
|
|
216
|
+
> [!TIP]
|
|
217
|
+
> Consider `'inlineLiteral'` for the most idiomatic output — enum values are inlined directly into the property type instead of creating a separate named type.
|
|
218
|
+
- name: enumSuffix
|
|
219
|
+
required: false
|
|
220
|
+
description: ""
|
|
221
|
+
warning: |
|
|
222
|
+
This option has been moved to [`adapterOas`](/adapters/adapter-oas#enumSuffix). Use `adapterOas({ enumSuffix })` instead.
|
|
223
|
+
- name: enumTypeSuffix
|
|
224
|
+
type: string
|
|
225
|
+
required: false
|
|
226
|
+
default: "'Key'"
|
|
227
|
+
description: |
|
|
228
|
+
Suffix appended to the generated type alias name when `enumType` is `asConst` or `asPascalConst`.
|
|
229
|
+
|
|
230
|
+
Only the type alias is affected — the const object name stays unchanged.
|
|
231
|
+
examples:
|
|
232
|
+
- name: "'Key' (default)"
|
|
233
|
+
files:
|
|
234
|
+
- lang: typescript
|
|
235
|
+
code: |
|
|
236
|
+
const petType = {
|
|
237
|
+
Dog: 'dog',
|
|
238
|
+
Cat: 'cat',
|
|
239
|
+
} as const;
|
|
240
|
+
|
|
241
|
+
export type PetTypeKey = (typeof petType)[keyof typeof petType];
|
|
242
|
+
twoslash: false
|
|
243
|
+
- name: "'Value'"
|
|
244
|
+
files:
|
|
245
|
+
- lang: typescript
|
|
246
|
+
code: |
|
|
247
|
+
// enumTypeSuffix: 'Value'
|
|
248
|
+
const petType = {
|
|
249
|
+
Dog: 'dog',
|
|
250
|
+
Cat: 'cat',
|
|
251
|
+
} as const;
|
|
252
|
+
|
|
253
|
+
export type PetTypeValue = (typeof petType)[keyof typeof petType];
|
|
254
|
+
twoslash: false
|
|
255
|
+
- name: "'' (no suffix)"
|
|
256
|
+
files:
|
|
257
|
+
- lang: typescript
|
|
258
|
+
code: |
|
|
259
|
+
// enumTypeSuffix: ''
|
|
260
|
+
const petType = {
|
|
261
|
+
Dog: 'dog',
|
|
262
|
+
Cat: 'cat',
|
|
263
|
+
} as const;
|
|
264
|
+
|
|
265
|
+
export type PetType = (typeof petType)[keyof typeof petType];
|
|
266
|
+
twoslash: false
|
|
267
|
+
- name: enumKeyCasing
|
|
268
|
+
type: "'screamingSnakeCase' | 'snakeCase' | 'pascalCase' | 'camelCase' | 'none'"
|
|
269
|
+
required: false
|
|
270
|
+
default: "'none'"
|
|
271
|
+
description: Control the casing applied to enum key names in generated TypeScript. Use this to align generated enum keys with your project's naming conventions.
|
|
272
|
+
body: |
|
|
273
|
+
- `'screamingSnakeCase'`: `ENUM_VALUE`
|
|
274
|
+
- `'snakeCase'`: `enum_value`
|
|
275
|
+
- `'pascalCase'`: `EnumValue`
|
|
276
|
+
- `'camelCase'`: `enumValue`
|
|
277
|
+
- `'none'`: Uses the enum value as-is
|
|
278
|
+
- name: dateType
|
|
279
|
+
required: false
|
|
280
|
+
description: ""
|
|
281
|
+
warning: |
|
|
282
|
+
This option has been moved to [`adapterOas`](/adapters/adapter-oas#dateType). Use `adapterOas({ dateType })` instead.
|
|
283
|
+
- name: integerType
|
|
284
|
+
required: false
|
|
285
|
+
description: ""
|
|
286
|
+
warning: |
|
|
287
|
+
This option has been moved to [`adapterOas`](/adapters/adapter-oas#integerType). Use `adapterOas({ integerType })` instead.
|
|
288
|
+
- name: syntaxType
|
|
289
|
+
type: "'type' | 'interface'"
|
|
290
|
+
required: false
|
|
291
|
+
default: "'type'"
|
|
292
|
+
description: |
|
|
293
|
+
Control whether the TypeScript generator emits `type` aliases or `interface` declarations for object schemas.
|
|
294
|
+
See [Type vs Interface: Which Should You Use](https://www.totaltypescript.com/type-vs-interface-which-should-you-use).
|
|
295
|
+
examples:
|
|
296
|
+
- name: "'type'"
|
|
297
|
+
files:
|
|
298
|
+
- lang: typescript
|
|
299
|
+
code: |
|
|
300
|
+
type Pet = {
|
|
301
|
+
name: string;
|
|
302
|
+
};
|
|
303
|
+
twoslash: false
|
|
304
|
+
- name: "'interface'"
|
|
305
|
+
files:
|
|
306
|
+
- lang: typescript
|
|
307
|
+
code: |
|
|
308
|
+
interface Pet {
|
|
309
|
+
name: string;
|
|
310
|
+
}
|
|
311
|
+
twoslash: false
|
|
312
|
+
- name: unknownType
|
|
313
|
+
required: false
|
|
314
|
+
description: ""
|
|
315
|
+
warning: |
|
|
316
|
+
This option has been moved to [`adapterOas`](/adapters/adapter-oas#unknownType). Use `adapterOas({ unknownType })` instead.
|
|
317
|
+
- name: emptySchemaType
|
|
318
|
+
required: false
|
|
319
|
+
description: ""
|
|
320
|
+
warning: |
|
|
321
|
+
This option has been moved to [`adapterOas`](/adapters/adapter-oas#emptySchemaType). Use `adapterOas({ emptySchemaType })` instead.
|
|
322
|
+
- name: optionalType
|
|
323
|
+
type: "'questionToken' | 'undefined' | 'questionTokenAndUndefined'"
|
|
324
|
+
required: false
|
|
325
|
+
default: "'questionToken'"
|
|
326
|
+
description: Control how optional properties are represented in generated TypeScript types.
|
|
327
|
+
examples:
|
|
328
|
+
- name: "'questionToken'"
|
|
329
|
+
files:
|
|
330
|
+
- lang: typescript
|
|
331
|
+
code: |
|
|
332
|
+
type Pet = {
|
|
333
|
+
type?: string;
|
|
334
|
+
};
|
|
335
|
+
twoslash: false
|
|
336
|
+
- name: "'undefined'"
|
|
337
|
+
files:
|
|
338
|
+
- lang: typescript
|
|
339
|
+
code: |
|
|
340
|
+
type Pet = {
|
|
341
|
+
type: string | undefined;
|
|
342
|
+
};
|
|
343
|
+
twoslash: false
|
|
344
|
+
- name: "'questionTokenAndUndefined'"
|
|
345
|
+
files:
|
|
346
|
+
- lang: typescript
|
|
347
|
+
code: |
|
|
348
|
+
type Pet = {
|
|
349
|
+
type?: string | undefined;
|
|
350
|
+
};
|
|
351
|
+
twoslash: false
|
|
352
|
+
- name: arrayType
|
|
353
|
+
type: "'array' | 'generic'"
|
|
354
|
+
required: false
|
|
355
|
+
default: "'array'"
|
|
356
|
+
description: Choose between `Array<Type>` or `Type[]` syntax for array types.
|
|
357
|
+
examples:
|
|
358
|
+
- name: "'array'"
|
|
359
|
+
files:
|
|
360
|
+
- lang: typescript
|
|
361
|
+
code: |
|
|
362
|
+
type Pet = {
|
|
363
|
+
tags: string[];
|
|
364
|
+
};
|
|
365
|
+
twoslash: false
|
|
366
|
+
- name: "'generic'"
|
|
367
|
+
files:
|
|
368
|
+
- lang: typescript
|
|
369
|
+
code: |
|
|
370
|
+
type Pet = {
|
|
371
|
+
tags: Array<string>;
|
|
372
|
+
};
|
|
373
|
+
twoslash: false
|
|
374
|
+
- name: paramsCasing
|
|
375
|
+
type: "'camelcase'"
|
|
376
|
+
required: false
|
|
377
|
+
default: undefined
|
|
378
|
+
description: Transform parameter names to a specific casing format for path, query, and header parameters.
|
|
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.
|
|
381
|
+
|
|
382
|
+
All plugins that reference parameters (like `@kubb/plugin-client`, `@kubb/plugin-react-query`, `@kubb/plugin-swr`, `@kubb/plugin-faker`, `@kubb/plugin-mcp`) should use the same `paramsCasing` setting to ensure type compatibility.
|
|
383
|
+
examples:
|
|
384
|
+
- name: Original API
|
|
385
|
+
files:
|
|
386
|
+
- lang: typescript
|
|
387
|
+
code: |
|
|
388
|
+
// OpenAPI spec has: step_id, X-Custom-Header, bool_param
|
|
389
|
+
|
|
390
|
+
// Without paramsCasing
|
|
391
|
+
type FindPetsByStatusPathParams = {
|
|
392
|
+
step_id: string;
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
type FindPetsByStatusQueryParams = {
|
|
396
|
+
bool_param?: boolean;
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
type FindPetsByStatusHeaderParams = {
|
|
400
|
+
'X-Custom-Header'?: string;
|
|
401
|
+
};
|
|
402
|
+
twoslash: false
|
|
403
|
+
- name: "With paramsCasing: 'camelcase'"
|
|
404
|
+
files:
|
|
405
|
+
- lang: typescript
|
|
406
|
+
code: |
|
|
407
|
+
// Properties are transformed to camelCase
|
|
408
|
+
|
|
409
|
+
type FindPetsByStatusPathParams = {
|
|
410
|
+
stepId: string; // ✓ camelCase
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
type FindPetsByStatusQueryParams = {
|
|
414
|
+
boolParam?: boolean; // ✓ camelCase
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
type FindPetsByStatusHeaderParams = {
|
|
418
|
+
xCustomHeader?: string; // ✓ camelCase
|
|
419
|
+
};
|
|
420
|
+
twoslash: false
|
|
421
|
+
- name: resolver
|
|
422
|
+
type: Partial<ResolverTs> & ThisType<ResolverTs>
|
|
423
|
+
required: false
|
|
424
|
+
description: |
|
|
425
|
+
A single resolver that overrides the naming and path-resolution conventions. Each method you provide replaces the corresponding built-in one. When a method returns `null` or `undefined`, the resolver's result is used as the fallback, so you only need to supply the methods you want to change.
|
|
426
|
+
|
|
427
|
+
`this` inside each method is bound to the **full** resolver, so you can call other resolver methods (e.g. `this.default(name, 'function')`) without losing context.
|
|
428
|
+
body: |
|
|
429
|
+
Each plugin ships a built-in resolver:
|
|
430
|
+
|
|
431
|
+
| Plugin | Default resolver |
|
|
432
|
+
| --- | --- |
|
|
433
|
+
| `@kubb/plugin-ts` | `resolverTs` |
|
|
434
|
+
| `@kubb/plugin-zod` | `resolverZod` |
|
|
435
|
+
| `@kubb/plugin-cypress` | `resolverCypress` |
|
|
436
|
+
| `@kubb/plugin-mcp` | `resolverMcp` |
|
|
437
|
+
codeBlock:
|
|
438
|
+
lang: typescript
|
|
439
|
+
title: Custom resolver (plugin-ts example)
|
|
440
|
+
code: |
|
|
441
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
442
|
+
|
|
443
|
+
pluginTs({
|
|
444
|
+
resolver: {
|
|
445
|
+
resolveName(name) {
|
|
446
|
+
// Prefix every operation-derived name; falls back for names where
|
|
447
|
+
// this returns null/undefined.
|
|
448
|
+
return `Api${this.default(name, 'function')}`
|
|
449
|
+
},
|
|
450
|
+
},
|
|
451
|
+
})
|
|
452
|
+
tip: |
|
|
453
|
+
Use `resolver` for fine-grained control over naming conventions.
|
|
454
|
+
examples:
|
|
455
|
+
- name: Custom prefix for operation names
|
|
456
|
+
files:
|
|
457
|
+
- lang: typescript
|
|
458
|
+
code: |
|
|
459
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
460
|
+
|
|
461
|
+
pluginTs({
|
|
462
|
+
resolver: {
|
|
463
|
+
resolveName(name) {
|
|
464
|
+
return `Custom${this.default(name, 'function')}`
|
|
465
|
+
},
|
|
466
|
+
},
|
|
467
|
+
})
|
|
468
|
+
twoslash: false
|
|
469
|
+
- name: include
|
|
470
|
+
type: Array<Include>
|
|
471
|
+
required: false
|
|
472
|
+
description: Array containing include parameters to include tags, operations, methods, paths, or content types.
|
|
473
|
+
codeBlock:
|
|
474
|
+
lang: typescript
|
|
475
|
+
title: Include
|
|
476
|
+
code: |
|
|
477
|
+
export type Include = {
|
|
478
|
+
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
479
|
+
pattern: string | RegExp
|
|
480
|
+
}
|
|
481
|
+
- name: exclude
|
|
482
|
+
type: Array<Exclude>
|
|
483
|
+
required: false
|
|
484
|
+
description: Array containing exclude parameters to exclude or skip tags, operations, methods, paths, or content types.
|
|
485
|
+
codeBlock:
|
|
486
|
+
lang: typescript
|
|
487
|
+
title: Exclude
|
|
488
|
+
code: |
|
|
489
|
+
export type Exclude = {
|
|
490
|
+
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
491
|
+
pattern: string | RegExp
|
|
492
|
+
}
|
|
493
|
+
- name: override
|
|
494
|
+
type: Array<Override>
|
|
495
|
+
required: false
|
|
496
|
+
description: Array containing override parameters to override `options` based on tags, operations, methods, paths, or content types.
|
|
497
|
+
codeBlock:
|
|
498
|
+
lang: typescript
|
|
499
|
+
title: Override
|
|
500
|
+
code: |
|
|
501
|
+
export type Override = {
|
|
502
|
+
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
503
|
+
pattern: string | RegExp
|
|
504
|
+
options: PluginOptions
|
|
505
|
+
}
|
|
506
|
+
- name: generators
|
|
507
|
+
type: Array<Generator<PluginTs>>
|
|
508
|
+
required: false
|
|
509
|
+
experimental: true
|
|
510
|
+
description: |
|
|
511
|
+
Define additional generators next to the built-in generators.
|
|
512
|
+
|
|
513
|
+
See [Generators](https://kubb.dev/docs/5.x/guides/creating-plugins) for more information on how to use generators.
|
|
514
|
+
- name: transformer
|
|
515
|
+
type: Visitor
|
|
516
|
+
required: false
|
|
517
|
+
description: |
|
|
518
|
+
A single AST visitor applied to every node before code is printed. Each method you provide replaces the corresponding built-in one. When a method returns `null` or `undefined`, the preset transformer's result is used as the fallback — so you only need to supply the methods you want to change.
|
|
519
|
+
|
|
520
|
+
Visitor methods receive the node and a context object. Return a modified node to replace it, or return `undefined`/`void` to leave it unchanged.
|
|
521
|
+
examples:
|
|
522
|
+
- name: Strip descriptions before printing
|
|
523
|
+
files:
|
|
524
|
+
- lang: typescript
|
|
525
|
+
code: |
|
|
526
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
527
|
+
|
|
528
|
+
pluginTs({
|
|
529
|
+
transformer: {
|
|
530
|
+
schema(node) {
|
|
531
|
+
return { ...node, description: undefined }
|
|
532
|
+
},
|
|
533
|
+
},
|
|
534
|
+
})
|
|
535
|
+
twoslash: false
|
|
536
|
+
- name: Prefix every operationId
|
|
537
|
+
files:
|
|
538
|
+
- lang: typescript
|
|
539
|
+
code: |
|
|
540
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
541
|
+
|
|
542
|
+
pluginTs({
|
|
543
|
+
transformer: {
|
|
544
|
+
operation(node) {
|
|
545
|
+
return { ...node, operationId: `api_${node.operationId}` }
|
|
546
|
+
},
|
|
547
|
+
},
|
|
548
|
+
})
|
|
549
|
+
twoslash: false
|
|
550
|
+
tip: |
|
|
551
|
+
Use `transformer` to rewrite node properties before printing. For output naming customization, use `resolver` instead.
|
|
552
|
+
note: |
|
|
553
|
+
`@kubb/plugin-ts` uses AST `Visitor` transformers for schema/operation node transforms. For output naming customization, use `resolver` instead.
|
|
554
|
+
- name: printer
|
|
555
|
+
type: "{ nodes?: PrinterTsNodes }"
|
|
556
|
+
required: false
|
|
557
|
+
description: |
|
|
558
|
+
Override individual printer node handlers to customize how specific schema types are rendered.
|
|
559
|
+
|
|
560
|
+
Each key is a `SchemaType` (e.g. `'integer'`, `'date'`). The function you provide replaces the built-in handler for that type. Use `this.transform` to recurse into nested schema nodes and `this.options` to read printer options.
|
|
561
|
+
examples:
|
|
562
|
+
- name: Override the date node to use the Date object
|
|
563
|
+
files:
|
|
564
|
+
- lang: typescript
|
|
565
|
+
code: |
|
|
566
|
+
import ts from 'typescript'
|
|
567
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
568
|
+
|
|
569
|
+
pluginTs({
|
|
570
|
+
printer: {
|
|
571
|
+
nodes: {
|
|
572
|
+
date(node) {
|
|
573
|
+
return ts.factory.createTypeReferenceNode('Date', [])
|
|
574
|
+
},
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
})
|
|
578
|
+
twoslash: false
|
|
579
|
+
- name: Override integer to bigint
|
|
580
|
+
files:
|
|
581
|
+
- lang: typescript
|
|
582
|
+
code: |
|
|
583
|
+
import ts from 'typescript'
|
|
584
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
585
|
+
|
|
586
|
+
pluginTs({
|
|
587
|
+
printer: {
|
|
588
|
+
nodes: {
|
|
589
|
+
integer() {
|
|
590
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword)
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
},
|
|
594
|
+
})
|
|
595
|
+
twoslash: false
|
|
596
|
+
examples:
|
|
597
|
+
- name: kubb.config.ts
|
|
598
|
+
files:
|
|
599
|
+
- lang: typescript
|
|
600
|
+
code: |
|
|
601
|
+
import { defineConfig } from 'kubb';
|
|
602
|
+
import { pluginTs } from '@kubb/plugin-ts';
|
|
603
|
+
|
|
604
|
+
export default defineConfig({
|
|
605
|
+
input: {
|
|
606
|
+
path: './petStore.yaml',
|
|
607
|
+
},
|
|
608
|
+
output: {
|
|
609
|
+
path: './src/gen',
|
|
610
|
+
},
|
|
611
|
+
plugins: [
|
|
612
|
+
pluginTs({
|
|
613
|
+
output: {
|
|
614
|
+
path: './types',
|
|
615
|
+
},
|
|
616
|
+
exclude: [
|
|
617
|
+
{
|
|
618
|
+
type: 'tag',
|
|
619
|
+
pattern: 'store',
|
|
620
|
+
},
|
|
621
|
+
],
|
|
622
|
+
group: {
|
|
623
|
+
type: 'tag',
|
|
624
|
+
name: ({ group }) => `${group}Controller`,
|
|
625
|
+
},
|
|
626
|
+
enumType: 'asConst',
|
|
627
|
+
optionalType: 'questionTokenAndUndefined',
|
|
628
|
+
paramsCasing: 'camelcase',
|
|
629
|
+
}),
|
|
630
|
+
],
|
|
631
|
+
})
|
|
632
|
+
twoslash: false
|