@kubb/plugin-msw 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.
Files changed (39) hide show
  1. package/README.md +39 -22
  2. package/dist/{components-vO0FIb2i.js → components--3aER8iM.js} +65 -64
  3. package/dist/components--3aER8iM.js.map +1 -0
  4. package/dist/{components-CLQ77DVn.cjs → components-BW5VCVSO.cjs} +67 -72
  5. package/dist/components-BW5VCVSO.cjs.map +1 -0
  6. package/dist/components.cjs +1 -1
  7. package/dist/components.d.ts +6 -6
  8. package/dist/components.js +1 -1
  9. package/dist/{generators-CrmMwWE4.cjs → generators-B6qnyBTW.cjs} +61 -31
  10. package/dist/generators-B6qnyBTW.cjs.map +1 -0
  11. package/dist/{generators-BPJCs1x1.js → generators-CLKAZMic.js} +64 -34
  12. package/dist/generators-CLKAZMic.js.map +1 -0
  13. package/dist/generators.cjs +1 -1
  14. package/dist/generators.d.ts +16 -5
  15. package/dist/generators.js +1 -1
  16. package/dist/index.cjs +87 -15
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.ts +33 -4
  19. package/dist/index.js +88 -16
  20. package/dist/index.js.map +1 -1
  21. package/dist/types-CwhQTrui.d.ts +103 -0
  22. package/extension.yaml +594 -102
  23. package/package.json +11 -13
  24. package/src/components/Handlers.tsx +1 -1
  25. package/src/components/Mock.tsx +5 -4
  26. package/src/components/MockWithFaker.tsx +5 -4
  27. package/src/components/Response.tsx +1 -1
  28. package/src/generators/handlersGenerator.tsx +18 -12
  29. package/src/generators/mswGenerator.tsx +29 -18
  30. package/src/plugin.ts +34 -18
  31. package/src/resolvers/resolverMsw.ts +19 -3
  32. package/src/types.ts +35 -21
  33. package/src/utils.ts +26 -61
  34. package/dist/components-CLQ77DVn.cjs.map +0 -1
  35. package/dist/components-vO0FIb2i.js.map +0 -1
  36. package/dist/generators-BPJCs1x1.js.map +0 -1
  37. package/dist/generators-CrmMwWE4.cjs.map +0 -1
  38. package/dist/types-Dxu0KMQ4.d.ts +0 -89
  39. /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-msw
4
4
  name: MSW
5
- description: Generate Mock Service Worker (MSW) handlers from OpenAPI specifications.
5
+ description: Generate MSW request handlers from OpenAPI so you can mock the entire API in tests and during local development.
6
6
  category: mocks
7
7
  type: official
8
- npmPackage: "@kubb/plugin-msw"
8
+ npmPackage: '@kubb/plugin-msw'
9
9
  docsPath: /plugins/plugin-msw
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: ">=5.0.0"
16
- node: ">=22"
15
+ kubb: '>=5.0.0'
16
+ node: '>=22'
17
17
  tags:
18
18
  - msw
19
19
  - mock-service-worker
@@ -37,218 +37,711 @@ icon:
37
37
  intro: |
38
38
  # @kubb/plugin-msw
39
39
 
40
- Generate [MSW](https://mswjs.io/) API mock handlers from your OpenAPI schema. Mock API requests in development and tests with type-safe handlers.
40
+ Generate [MSW](https://mswjs.io/) handlers from your OpenAPI spec. Drop them into your test setup or service worker to mock the API end-to-end request path, method, status, and response body all stay in sync with the spec.
41
+
42
+ Combine with `@kubb/plugin-faker` to seed handlers with realistic data, or feed them custom payloads from tests.
41
43
  options:
42
44
  - name: output
43
45
  type: Output
44
46
  required: false
45
- default: "{ path: 'handlers', barrelType: 'named' }"
46
- description: Specify the export location for the files and define the behavior of the output.
47
+ default: "{ path: 'handlers', barrel: { type: 'named' } }"
48
+ description: Where the generated MSW handlers are written and how they are exported.
47
49
  properties:
48
50
  - name: path
49
51
  type: string
50
52
  required: true
51
- description: Output directory or file for the generated code, relative to the global `output.path`.
53
+ description: |
54
+ Folder (or single file) where the plugin writes its generated code. The path is resolved against the global `output.path` set on `defineConfig`.
55
+
56
+ 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'`.
52
57
  tip: |
53
- if `output.path` is a file, `group` cannot be used.
58
+ When `output.path` points to a single file, the `group` option cannot be used because every operation ends up in the same file.
59
+ examples:
60
+ - name: kubb.config.ts
61
+ files:
62
+ - lang: typescript
63
+ twoslash: false
64
+ code: |
65
+ import { defineConfig } from 'kubb'
66
+ import { pluginTs } from '@kubb/plugin-ts'
67
+
68
+ export default defineConfig({
69
+ input: { path: './petStore.yaml' },
70
+ output: { path: './src/gen' },
71
+ plugins: [
72
+ pluginTs({
73
+ output: { path: './types' },
74
+ }),
75
+ ],
76
+ })
77
+ - name: Resulting tree
78
+ files:
79
+ - lang: text
80
+ twoslash: false
81
+ code: |
82
+ src/
83
+ └── gen/
84
+ └── types/
85
+ ├── Pet.ts
86
+ └── Store.ts
54
87
  default: "'handlers'"
55
- - name: barrelType
56
- type: "'all' | 'named' | 'propagate' | false"
88
+ - name: barrel
89
+ type: "{ type: 'named' | 'all', nested?: boolean } | false"
57
90
  required: false
58
- default: "'named'"
59
- description: Specify what to export and optionally disable barrel-file generation.
91
+ default: "{ type: 'named' }"
92
+ description: |
93
+ Controls how the generated `index.ts` (barrel) file re-exports the plugin's output.
94
+
95
+ - `{ type: 'named' }` re-exports each symbol by name. Best for tree-shaking and explicit imports.
96
+ - `{ type: 'all' }` uses `export *`. Smaller barrel file, but exports everything.
97
+ - `{ nested: true }` creates a barrel in every subdirectory, so callers can import from any depth.
98
+ - `false` skips the barrel entirely. The plugin's files are also excluded from the root `index.ts`.
60
99
  tip: |
61
- 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.
100
+ 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.
62
101
  examples:
63
- - name: all
102
+ - name: "'named' (default)"
64
103
  files:
65
- - lang: typescript
104
+ - name: kubb.config.ts
105
+ lang: typescript
106
+ twoslash: false
66
107
  code: |
67
- export * from './gen/petService.ts'
108
+ import { defineConfig } from 'kubb'
109
+ import { pluginTs } from '@kubb/plugin-ts'
110
+
111
+ export default defineConfig({
112
+ input: { path: './petStore.yaml' },
113
+ output: { path: './src/gen' },
114
+ plugins: [
115
+ pluginTs({
116
+ output: { barrel: { type: 'named' } },
117
+ }),
118
+ ],
119
+ })
120
+ - name: src/gen/types/index.ts
121
+ lang: typescript
68
122
  twoslash: false
69
- - name: named
123
+ code: |
124
+ export { Pet, PetStatus } from './Pet'
125
+ export { Store } from './Store'
126
+ - name: "'all'"
70
127
  files:
71
- - lang: typescript
128
+ - name: kubb.config.ts
129
+ lang: typescript
130
+ twoslash: false
72
131
  code: |
73
- export { PetService } from './gen/petService.ts'
132
+ import { defineConfig } from 'kubb'
133
+ import { pluginTs } from '@kubb/plugin-ts'
134
+
135
+ export default defineConfig({
136
+ input: { path: './petStore.yaml' },
137
+ output: { path: './src/gen' },
138
+ plugins: [
139
+ pluginTs({
140
+ output: { barrel: { type: 'all' } },
141
+ }),
142
+ ],
143
+ })
144
+ - name: src/gen/types/index.ts
145
+ lang: typescript
74
146
  twoslash: false
75
- - name: propagate
147
+ code: |
148
+ export * from './Pet'
149
+ export * from './Store'
150
+ - name: nested
76
151
  files:
77
- - lang: typescript
78
- code: ""
152
+ - name: kubb.config.ts
153
+ lang: typescript
79
154
  twoslash: false
80
- - name: "false"
155
+ code: |
156
+ import { defineConfig } from 'kubb'
157
+ import { pluginTs } from '@kubb/plugin-ts'
158
+
159
+ export default defineConfig({
160
+ input: { path: './petStore.yaml' },
161
+ output: { path: './src/gen' },
162
+ plugins: [
163
+ pluginTs({
164
+ output: { barrel: { type: 'named', nested: true } },
165
+ }),
166
+ ],
167
+ })
168
+ - name: Generated tree
169
+ lang: text
170
+ twoslash: false
171
+ code: |
172
+ src/gen/types/
173
+ ├── index.ts # re-exports ./petController and ./storeController
174
+ ├── petController/
175
+ │ ├── index.ts # re-exports Pet, Store, ...
176
+ │ └── Pet.ts
177
+ └── storeController/
178
+ ├── index.ts
179
+ └── Store.ts
180
+ - name: 'false'
81
181
  files:
82
- - lang: typescript
83
- code: ""
182
+ - name: kubb.config.ts
183
+ lang: typescript
184
+ twoslash: false
185
+ code: |
186
+ import { defineConfig } from 'kubb'
187
+ import { pluginTs } from '@kubb/plugin-ts'
188
+
189
+ export default defineConfig({
190
+ input: { path: './petStore.yaml' },
191
+ output: { path: './src/gen' },
192
+ plugins: [
193
+ pluginTs({
194
+ output: { barrel: false },
195
+ }),
196
+ ],
197
+ })
198
+ - name: Result
199
+ lang: text
84
200
  twoslash: false
201
+ code: |
202
+ # No index.ts is generated for this plugin.
203
+ # Its files are also excluded from the root index.ts.
85
204
  - name: banner
86
- type: "string | ((node: RootNode) => string)"
205
+ type: 'string | ((node: RootNode) => string)'
87
206
  required: false
88
- 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.
207
+ description: |
208
+ Text prepended to every generated file. Useful for license headers, lint disables, or `@ts-nocheck` directives.
209
+
210
+ 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).
211
+ examples:
212
+ - name: Static banner
213
+ files:
214
+ - name: kubb.config.ts
215
+ lang: typescript
216
+ twoslash: false
217
+ code: |
218
+ import { defineConfig } from 'kubb'
219
+ import { pluginTs } from '@kubb/plugin-ts'
220
+
221
+ export default defineConfig({
222
+ input: { path: './petStore.yaml' },
223
+ output: { path: './src/gen' },
224
+ plugins: [
225
+ pluginTs({
226
+ output: {
227
+ banner: '/* eslint-disable */\n// @ts-nocheck',
228
+ },
229
+ }),
230
+ ],
231
+ })
232
+ - name: Generated file
233
+ lang: typescript
234
+ twoslash: false
235
+ code: |
236
+ /* eslint-disable */
237
+ // @ts-nocheck
238
+ export type Pet = {
239
+ id: number
240
+ name: string
241
+ }
242
+ - name: Dynamic banner
243
+ files:
244
+ - name: kubb.config.ts
245
+ lang: typescript
246
+ twoslash: false
247
+ code: |
248
+ import { defineConfig } from 'kubb'
249
+ import { pluginTs } from '@kubb/plugin-ts'
250
+
251
+ export default defineConfig({
252
+ input: { path: './petStore.yaml' },
253
+ output: { path: './src/gen' },
254
+ plugins: [
255
+ pluginTs({
256
+ output: {
257
+ banner: (node) => `// Source: ${node.path}\n// Generated at ${new Date().toISOString()}`,
258
+ },
259
+ }),
260
+ ],
261
+ })
89
262
  - name: footer
90
- type: "string | ((node: RootNode) => string)"
263
+ type: 'string | ((node: RootNode) => string)'
91
264
  required: false
92
- 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.
265
+ description: |
266
+ 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.
267
+
268
+ Pass a string for a static footer, or a function that receives the file's `RootNode` and returns the footer text.
269
+ examples:
270
+ - name: Re-enable lint after a banner disable
271
+ files:
272
+ - name: kubb.config.ts
273
+ lang: typescript
274
+ twoslash: false
275
+ code: |
276
+ import { defineConfig } from 'kubb'
277
+ import { pluginTs } from '@kubb/plugin-ts'
278
+
279
+ export default defineConfig({
280
+ input: { path: './petStore.yaml' },
281
+ output: { path: './src/gen' },
282
+ plugins: [
283
+ pluginTs({
284
+ output: {
285
+ banner: '/* eslint-disable */',
286
+ footer: '/* eslint-enable */',
287
+ },
288
+ }),
289
+ ],
290
+ })
93
291
  - name: override
94
292
  type: boolean
95
293
  required: false
96
- default: "false"
97
- description: Whether Kubb overrides existing external files that can be generated if they already exist.
294
+ default: 'false'
295
+ description: |
296
+ Allows the plugin to overwrite hand-written files that share a name with a generated file.
297
+
298
+ - `false` (default): Kubb skips a file if it already exists and is not marked as generated. This protects manual edits.
299
+ - `true`: Kubb overwrites any file at the target path, including hand-written ones.
300
+ warning: |
301
+ Enable this only when you are sure the target folder contains nothing you need to keep. Local edits are lost on the next generation.
302
+ examples:
303
+ - name: kubb.config.ts
304
+ files:
305
+ - lang: typescript
306
+ twoslash: false
307
+ code: |
308
+ import { defineConfig } from 'kubb'
309
+ import { pluginTs } from '@kubb/plugin-ts'
310
+
311
+ export default defineConfig({
312
+ input: { path: './petStore.yaml' },
313
+ output: { path: './src/gen' },
314
+ plugins: [
315
+ pluginTs({
316
+ output: { override: true },
317
+ }),
318
+ ],
319
+ })
98
320
  - name: handlers
99
321
  type: boolean
100
322
  required: false
101
- default: "false"
102
- description: Create a `handlers.ts` file with all handlers grouped by methods.
103
- - name: contentType
104
- type: "'application/json' | (string & {})"
105
- required: false
323
+ default: 'false'
106
324
  description: |
107
- Define which content type to use.
325
+ Emits a `handlers.ts` file that re-exports every generated handler grouped by HTTP method. Drop this file into your MSW `setupServer(...handlers)` or `setupWorker(...handlers)` call.
326
+ examples:
327
+ - name: setup.ts
328
+ files:
329
+ - lang: typescript
330
+ twoslash: false
331
+ code: |
332
+ import { setupServer } from 'msw/node'
333
+ import { handlersGet, handlersPost } from './gen/handlers'
108
334
 
109
- By default, Kubb uses the first JSON-valid media type.
335
+ export const server = setupServer(...handlersGet, ...handlersPost)
110
336
  - name: baseURL
111
337
  type: string
112
338
  required: false
113
- description: Sets a custom base URL for all generated calls. When not set, the base URL is automatically taken from the OAS spec via the adapter (e.g. the `servers[0].url` field).
339
+ description: |
340
+ Base URL prepended to every request URL in the generated client. When omitted, the URL comes from the OpenAPI spec's `servers[0].url` (or whichever index the adapter is configured to read).
341
+
342
+ Set this when the generated client should point at a different environment (staging, production) than the one written in the spec.
343
+ examples:
344
+ - name: Override the spec's server URL
345
+ files:
346
+ - lang: typescript
347
+ twoslash: false
348
+ code: |
349
+ import { defineConfig } from 'kubb'
350
+ import { pluginClient } from '@kubb/plugin-client'
351
+
352
+ export default defineConfig({
353
+ input: { path: './petStore.yaml' },
354
+ output: { path: './src/gen' },
355
+ plugins: [
356
+ pluginClient({
357
+ baseURL: 'https://petstore.swagger.io/v2',
358
+ }),
359
+ ],
360
+ })
114
361
  - name: group
115
362
  type: Group
116
363
  required: false
117
364
  description: |
118
- Grouping combines files in a folder based on a specific `type`.
365
+ Splits generated files into subfolders based on the operation's tag, so each tag in your OpenAPI spec gets its own directory.
366
+
367
+ 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.
368
+ tip: |
369
+ 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.
119
370
  examples:
120
371
  - name: kubb.config.ts
121
372
  files:
122
373
  - lang: typescript
123
- code: |
124
- group: {
125
- type: 'tag',
126
- name({ group }) {
127
- return `${group}Controller`
128
- }
129
- }
130
374
  twoslash: false
375
+ code: |
376
+ import { defineConfig } from 'kubb'
377
+ import { pluginTs } from '@kubb/plugin-ts'
378
+
379
+ export default defineConfig({
380
+ input: { path: './petStore.yaml' },
381
+ output: { path: './src/gen' },
382
+ plugins: [
383
+ pluginTs({
384
+ group: {
385
+ type: 'tag',
386
+ name: ({ group }) => `${group}Controller`,
387
+ },
388
+ }),
389
+ ],
390
+ })
131
391
  body: |
132
392
  With the configuration above, the generator emits:
133
393
 
134
394
  ```text
135
- .
136
- ├── src/
137
- └── petController/
138
- │ ├── addPet.ts
139
- │ │ └── getPet.ts
140
- │ └── storeController/
141
- │ ├── createStore.ts
142
- │ └── getStoreById.ts
143
- ├── petStore.yaml
144
- ├── kubb.config.ts
145
- └── package.json
395
+ src/gen/
396
+ ├── petController/
397
+ ├── AddPet.ts
398
+ └── GetPet.ts
399
+ └── storeController/
400
+ ├── CreateStore.ts
401
+ └── GetStoreById.ts
146
402
  ```
147
403
  properties:
148
404
  - name: type
149
405
  type: "'tag'"
150
406
  required: true
151
- description: Specify the property to group files by. Required when `group` is defined.
407
+ description: |
408
+ Property used to assign each operation to a group. Required whenever `group` is set.
409
+
410
+ 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.
152
411
  note: |
153
- `Required: true*` means this is required only when the `group` option is used. The `group` option itself is optional.
154
- body: |
155
- - `'tag'`: Uses the first tag from `operation.getTags().at(0)?.name`
412
+ `Required: true*` is conditional only required when the parent `group` option is used. `group` itself stays optional.
156
413
  - name: name
157
- type: "(context: GroupContext) => string"
414
+ type: '(context: GroupContext) => string'
158
415
  required: false
159
416
  default: (ctx) => `${ctx.group}Controller`
160
- description: Return the name of a group based on the group name. This is used for file and identifier generation.
417
+ description: Function that builds the folder/identifier name from a group key (the operation's first tag).
161
418
  - name: parser
162
419
  type: "'data' | 'faker'"
163
420
  required: false
164
421
  default: "'data'"
165
- description: Choose which parser to use when generating response data.
166
- body: |
167
- - `'faker'` will use `@kubb/plugin-faker` to generate the data for the response.
168
- - `'data'` will use your custom data to generate the data for the response.
422
+ description: |
423
+ Source of the response body returned by each generated handler.
424
+
425
+ - `'data'` (default) handlers return a typed empty/example payload, ready for you to fill in from tests.
426
+ - `'faker'` — handlers return a value produced by `@kubb/plugin-faker`. Requires the Faker plugin in the plugins array.
427
+ examples:
428
+ - name: "'faker'"
429
+ files:
430
+ - lang: typescript
431
+ twoslash: false
432
+ code: |
433
+ import { defineConfig } from 'kubb'
434
+ import { pluginTs } from '@kubb/plugin-ts'
435
+ import { pluginFaker } from '@kubb/plugin-faker'
436
+ import { pluginMsw } from '@kubb/plugin-msw'
437
+
438
+ export default defineConfig({
439
+ input: { path: './petStore.yaml' },
440
+ output: { path: './src/gen' },
441
+ plugins: [
442
+ pluginTs(),
443
+ pluginFaker(),
444
+ pluginMsw({ parser: 'faker' }),
445
+ ],
446
+ })
169
447
  - name: include
170
448
  type: Array<Include>
171
449
  required: false
172
- description: Array containing include parameters to include tags, operations, methods, paths, or content types.
450
+ description: |
451
+ Restricts generation to operations that match at least one entry in the list. Anything not matched is skipped.
452
+
453
+ Each entry filters by one of:
454
+
455
+ - `tag` — the operation's first tag in the OpenAPI spec.
456
+ - `operationId` — the operation's `operationId`.
457
+ - `path` — the URL pattern (`'/pet/{petId}'`).
458
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
459
+ - `contentType` — the media type of the request body.
460
+
461
+ `pattern` accepts either a string (exact match) or a `RegExp` for fuzzy matches.
173
462
  codeBlock:
174
463
  lang: typescript
175
- title: Include
464
+ title: Type definition
176
465
  code: |
177
466
  export type Include = {
178
467
  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
179
468
  pattern: string | RegExp
180
469
  }
470
+ examples:
471
+ - name: Only the pet tag
472
+ files:
473
+ - name: kubb.config.ts
474
+ lang: typescript
475
+ twoslash: false
476
+ code: |
477
+ import { defineConfig } from 'kubb'
478
+ import { pluginTs } from '@kubb/plugin-ts'
479
+
480
+ export default defineConfig({
481
+ input: { path: './petStore.yaml' },
482
+ output: { path: './src/gen' },
483
+ plugins: [
484
+ pluginTs({
485
+ include: [
486
+ { type: 'tag', pattern: 'pet' },
487
+ ],
488
+ }),
489
+ ],
490
+ })
491
+ - name: Only GET operations under /pet
492
+ files:
493
+ - name: kubb.config.ts
494
+ lang: typescript
495
+ twoslash: false
496
+ code: |
497
+ import { defineConfig } from 'kubb'
498
+ import { pluginTs } from '@kubb/plugin-ts'
499
+
500
+ export default defineConfig({
501
+ input: { path: './petStore.yaml' },
502
+ output: { path: './src/gen' },
503
+ plugins: [
504
+ pluginTs({
505
+ include: [
506
+ { type: 'method', pattern: 'get' },
507
+ { type: 'path', pattern: /^\/pet/ },
508
+ ],
509
+ }),
510
+ ],
511
+ })
181
512
  - name: exclude
182
513
  type: Array<Exclude>
183
514
  required: false
184
- description: Array containing exclude parameters to exclude or skip tags, operations, methods, paths, or content types.
515
+ description: |
516
+ Skips any operation that matches at least one entry in the list. The opposite of `include`.
517
+
518
+ Each entry filters by one of:
519
+
520
+ - `tag` — the operation's first tag.
521
+ - `operationId` — the operation's `operationId`.
522
+ - `path` — the URL pattern (`'/pet/{petId}'`).
523
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
524
+ - `contentType` — the media type of the request body.
525
+
526
+ `pattern` accepts a plain string or a `RegExp`. When both `include` and `exclude` are set, `exclude` wins.
185
527
  codeBlock:
186
528
  lang: typescript
187
- title: Exclude
529
+ title: Type definition
188
530
  code: |
189
531
  export type Exclude = {
190
532
  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
191
533
  pattern: string | RegExp
192
534
  }
535
+ examples:
536
+ - name: Skip everything under the store tag
537
+ files:
538
+ - name: kubb.config.ts
539
+ lang: typescript
540
+ twoslash: false
541
+ code: |
542
+ import { defineConfig } from 'kubb'
543
+ import { pluginTs } from '@kubb/plugin-ts'
544
+
545
+ export default defineConfig({
546
+ input: { path: './petStore.yaml' },
547
+ output: { path: './src/gen' },
548
+ plugins: [
549
+ pluginTs({
550
+ exclude: [
551
+ { type: 'tag', pattern: 'store' },
552
+ ],
553
+ }),
554
+ ],
555
+ })
556
+ - name: Skip a specific operation and all delete methods
557
+ files:
558
+ - name: kubb.config.ts
559
+ lang: typescript
560
+ twoslash: false
561
+ code: |
562
+ import { defineConfig } from 'kubb'
563
+ import { pluginTs } from '@kubb/plugin-ts'
564
+
565
+ export default defineConfig({
566
+ input: { path: './petStore.yaml' },
567
+ output: { path: './src/gen' },
568
+ plugins: [
569
+ pluginTs({
570
+ exclude: [
571
+ { type: 'operationId', pattern: 'deletePet' },
572
+ { type: 'method', pattern: 'delete' },
573
+ ],
574
+ }),
575
+ ],
576
+ })
193
577
  - name: override
194
578
  type: Array<Override>
195
579
  required: false
196
- description: Array containing override parameters to override `options` based on tags, operations, methods, paths, or content types.
580
+ description: |
581
+ 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.
582
+
583
+ 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.
584
+
585
+ Entries are evaluated top to bottom. The first matching entry's `options` is merged onto the plugin defaults; later entries do not stack.
197
586
  codeBlock:
198
587
  lang: typescript
199
- title: Override
588
+ title: Type definition
200
589
  code: |
201
590
  export type Override = {
202
591
  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
203
592
  pattern: string | RegExp
204
593
  options: PluginOptions
205
594
  }
595
+ examples:
596
+ - name: Use a different enum style for the user tag
597
+ files:
598
+ - name: kubb.config.ts
599
+ lang: typescript
600
+ twoslash: false
601
+ code: |
602
+ import { defineConfig } from 'kubb'
603
+ import { pluginTs } from '@kubb/plugin-ts'
604
+
605
+ export default defineConfig({
606
+ input: { path: './petStore.yaml' },
607
+ output: { path: './src/gen' },
608
+ plugins: [
609
+ pluginTs({
610
+ enumType: 'asConst',
611
+ override: [
612
+ {
613
+ type: 'tag',
614
+ pattern: 'user',
615
+ options: { enumType: 'literal' },
616
+ },
617
+ ],
618
+ }),
619
+ ],
620
+ })
206
621
  - name: generators
207
622
  type: Array<Generator<PluginMsw>>
208
623
  required: false
209
624
  experimental: true
210
625
  description: |
211
- Define additional generators next to the built-in generators.
626
+ 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.
212
627
 
213
- See [Generators](https://kubb.dev/docs/5.x/guides/creating-plugins) for more information on how to use generators.
214
- - name: transformers
215
- type: object
628
+ 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).
629
+ warning: |
630
+ Generators are an experimental, low-level API. The signature may change between minor releases.
631
+ - name: resolver
632
+ type: Partial<ResolverMsw> & ThisType<ResolverMsw>
216
633
  required: false
217
- description: Customize the generated names based on the type provided by the plugin.
218
- properties:
219
- - name: name
220
- type: "(name: string, type?: ResolveType) => string"
221
- required: false
222
- description: Customize the names based on the type that is provided by the plugin.
223
- codeBlock:
224
- lang: typescript
225
- code: |
226
- type ResolveType = 'file' | 'function' | 'type' | 'const'
634
+ description: |
635
+ 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.
636
+
637
+ 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.
638
+
639
+ 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.
640
+ body: |
641
+ Each plugin ships with a default resolver:
642
+
643
+ | Plugin | Default resolver |
644
+ | --- | --- |
645
+ | `@kubb/plugin-ts` | `resolverTs` |
646
+ | `@kubb/plugin-zod` | `resolverZod` |
647
+ | `@kubb/plugin-faker` | `resolverFaker` |
648
+ | `@kubb/plugin-cypress` | `resolverCypress` |
649
+ | `@kubb/plugin-msw` | `resolverMsw` |
650
+ | `@kubb/plugin-mcp` | `resolverMcp` |
651
+ | `@kubb/plugin-client` | `resolverClient` |
652
+ codeBlock:
653
+ lang: typescript
654
+ title: Prefix every handler name with "Mock"
655
+ code: |
656
+ import { defineConfig } from 'kubb'
657
+ import { pluginMsw } from '@kubb/plugin-msw'
658
+
659
+ export default defineConfig({
660
+ input: { path: './petStore.yaml' },
661
+ output: { path: './src/gen' },
662
+ plugins: [
663
+ pluginMsw({
664
+ resolver: {
665
+ resolveName(name) {
666
+ return `Mock${this.default(name, 'function')}`
667
+ },
668
+ },
669
+ }),
670
+ ],
671
+ })
672
+ tip: |
673
+ Use `resolver` for naming and file-location tweaks. For changing the AST nodes themselves (e.g. stripping descriptions), use `transformer` instead.
674
+ - name: transformer
675
+ type: Visitor
676
+ required: false
677
+ description: |
678
+ 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.
679
+
680
+ 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.
681
+ examples:
682
+ - name: Strip descriptions before printing
683
+ files:
684
+ - name: kubb.config.ts
685
+ lang: typescript
686
+ twoslash: false
687
+ code: |
688
+ import { defineConfig } from 'kubb'
689
+ import { pluginTs } from '@kubb/plugin-ts'
690
+
691
+ export default defineConfig({
692
+ input: { path: './petStore.yaml' },
693
+ output: { path: './src/gen' },
694
+ plugins: [
695
+ pluginTs({
696
+ transformer: {
697
+ schema(node) {
698
+ return { ...node, description: undefined }
699
+ },
700
+ },
701
+ }),
702
+ ],
703
+ })
704
+ - name: Prefix every operationId
705
+ files:
706
+ - name: kubb.config.ts
707
+ lang: typescript
708
+ twoslash: false
709
+ code: |
710
+ import { defineConfig } from 'kubb'
711
+ import { pluginTs } from '@kubb/plugin-ts'
712
+
713
+ export default defineConfig({
714
+ input: { path: './petStore.yaml' },
715
+ output: { path: './src/gen' },
716
+ plugins: [
717
+ pluginTs({
718
+ transformer: {
719
+ operation(node) {
720
+ return { ...node, operationId: `api_${node.operationId}` }
721
+ },
722
+ },
723
+ }),
724
+ ],
725
+ })
726
+ tip: |
727
+ Use `transformer` to rewrite node properties before printing. For changing the names of generated symbols and files, use `resolver` instead.
227
728
  examples:
228
729
  - name: kubb.config.ts
229
730
  files:
230
731
  - lang: typescript
732
+ twoslash: false
231
733
  code: |
232
734
  import { defineConfig } from 'kubb'
233
735
  import { pluginMsw } from '@kubb/plugin-msw'
234
736
  import { pluginTs } from '@kubb/plugin-ts'
235
737
 
236
738
  export default defineConfig({
237
- input: {
238
- path: './petStore.yaml',
239
- },
240
- output: {
241
- path: './src/gen',
242
- },
739
+ input: { path: './petStore.yaml' },
740
+ output: { path: './src/gen' },
243
741
  plugins: [
244
742
  pluginTs(),
245
743
  pluginMsw({
246
- output: {
247
- path: './mocks',
248
- barrelType: 'named',
249
- banner: '/* eslint-disable no-alert, no-console */',
250
- footer: '',
251
- },
744
+ output: { path: './mocks' },
252
745
  group: {
253
746
  type: 'tag',
254
747
  name: ({ group }) => `${group}Service`,
@@ -257,4 +750,3 @@ examples:
257
750
  }),
258
751
  ],
259
752
  })
260
- twoslash: false