@kubb/plugin-react-query 5.0.0-beta.3 → 5.0.0-beta.30

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 (48) hide show
  1. package/README.md +34 -83
  2. package/dist/{components-DTGLu4UV.js → components-CDmg-RPi.js} +275 -255
  3. package/dist/components-CDmg-RPi.js.map +1 -0
  4. package/dist/{components-dAKJEn9b.cjs → components-MPBTffPl.cjs} +299 -255
  5. package/dist/components-MPBTffPl.cjs.map +1 -0
  6. package/dist/components.cjs +1 -1
  7. package/dist/components.d.ts +1 -75
  8. package/dist/components.js +1 -1
  9. package/dist/{generators-C_fbcjpG.js → generators-Bma51Uar.js} +301 -261
  10. package/dist/generators-Bma51Uar.js.map +1 -0
  11. package/dist/{generators-CWEQsdO9.cjs → generators-BtsWNz-6.cjs} +299 -259
  12. package/dist/generators-BtsWNz-6.cjs.map +1 -0
  13. package/dist/generators.cjs +1 -1
  14. package/dist/generators.d.ts +41 -1
  15. package/dist/generators.js +1 -1
  16. package/dist/index.cjs +143 -20
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.ts +30 -1
  19. package/dist/index.js +143 -20
  20. package/dist/index.js.map +1 -1
  21. package/dist/types-DiZPLTXl.d.ts +400 -0
  22. package/extension.yaml +1507 -0
  23. package/package.json +16 -18
  24. package/src/components/InfiniteQuery.tsx +19 -13
  25. package/src/components/InfiniteQueryOptions.tsx +29 -47
  26. package/src/components/Mutation.tsx +35 -15
  27. package/src/components/MutationOptions.tsx +14 -13
  28. package/src/components/Query.tsx +9 -10
  29. package/src/components/QueryOptions.tsx +6 -27
  30. package/src/components/SuspenseInfiniteQuery.tsx +19 -13
  31. package/src/components/SuspenseInfiniteQueryOptions.tsx +29 -47
  32. package/src/components/SuspenseQuery.tsx +9 -10
  33. package/src/generators/customHookOptionsFileGenerator.tsx +18 -14
  34. package/src/generators/hookOptionsGenerator.tsx +36 -33
  35. package/src/generators/infiniteQueryGenerator.tsx +46 -64
  36. package/src/generators/mutationGenerator.tsx +42 -50
  37. package/src/generators/queryGenerator.tsx +43 -49
  38. package/src/generators/suspenseInfiniteQueryGenerator.tsx +41 -51
  39. package/src/generators/suspenseQueryGenerator.tsx +44 -62
  40. package/src/plugin.ts +42 -16
  41. package/src/resolvers/resolverReactQuery.ts +102 -6
  42. package/src/types.ts +199 -61
  43. package/src/utils.ts +10 -33
  44. package/dist/components-DTGLu4UV.js.map +0 -1
  45. package/dist/components-dAKJEn9b.cjs.map +0 -1
  46. package/dist/generators-CWEQsdO9.cjs.map +0 -1
  47. package/dist/generators-C_fbcjpG.js.map +0 -1
  48. package/dist/types-DfaFRSBf.d.ts +0 -284
package/extension.yaml ADDED
@@ -0,0 +1,1507 @@
1
+ $schema: https://kubb.dev/schemas/extension.json
2
+ kind: plugin
3
+ id: plugin-react-query
4
+ name: React Query
5
+ description: Generate TanStack Query hooks for React (useQuery, useMutation, useSuspenseQuery, useInfiniteQuery) from OpenAPI.
6
+ category: framework
7
+ type: official
8
+ npmPackage: '@kubb/plugin-react-query'
9
+ docsPath: /plugins/plugin-react-query
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
+ - react-query
19
+ - tanstack-query
20
+ - react
21
+ - hooks
22
+ - data-fetching
23
+ - codegen
24
+ - openapi
25
+ dependencies:
26
+ - plugin-ts
27
+ - plugin-client
28
+ resources:
29
+ documentation: https://kubb.dev/plugins/plugin-react-query
30
+ repository: https://github.com/kubb-labs/plugins
31
+ issues: https://github.com/kubb-labs/plugins/issues
32
+ changelog: https://github.com/kubb-labs/plugins/blob/main/packages/plugin-react-query/CHANGELOG.md
33
+ codesandbox: https://codesandbox.io/p/github/kubb-labs/plugins/main/examples/react-query
34
+ featured: true
35
+ icon:
36
+ light: https://kubb.dev/feature/tanstack.svg
37
+ intro: |
38
+ # @kubb/plugin-react-query
39
+
40
+ Generate one [TanStack Query](https://tanstack.com/query) hook per OpenAPI operation. Queries become `useFooQuery`/`useFooSuspenseQuery`/`useFooInfiniteQuery`; mutations become `useFooMutation`. Each hook is fully typed: query keys, input variables, response data, and error shape all come from the spec.
41
+
42
+ Pairs with `@kubb/plugin-client` for the HTTP layer and `@kubb/plugin-ts` for types.
43
+ options:
44
+ - name: output
45
+ type: Output
46
+ required: false
47
+ default: "{ path: 'hooks', barrel: { type: 'named' } }"
48
+ description: Where the generated hooks are written and how they are exported.
49
+ properties:
50
+ - name: path
51
+ type: string
52
+ required: true
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'`.
57
+ tip: |
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
87
+ default: "'hooks'"
88
+ - name: barrel
89
+ type: "{ type: 'named' | 'all', nested?: boolean } | false"
90
+ required: false
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`.
99
+ tip: |
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.
101
+ examples:
102
+ - name: "'named' (default)"
103
+ files:
104
+ - name: kubb.config.ts
105
+ lang: typescript
106
+ twoslash: false
107
+ code: |
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
122
+ twoslash: false
123
+ code: |
124
+ export { Pet, PetStatus } from './Pet'
125
+ export { Store } from './Store'
126
+ - name: "'all'"
127
+ files:
128
+ - name: kubb.config.ts
129
+ lang: typescript
130
+ twoslash: false
131
+ code: |
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
146
+ twoslash: false
147
+ code: |
148
+ export * from './Pet'
149
+ export * from './Store'
150
+ - name: nested
151
+ files:
152
+ - name: kubb.config.ts
153
+ lang: typescript
154
+ twoslash: 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'
181
+ files:
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
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.
204
+ - name: banner
205
+ type: 'string | ((node: RootNode) => string)'
206
+ required: false
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
+ })
262
+ - name: footer
263
+ type: 'string | ((node: RootNode) => string)'
264
+ required: false
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
+ })
291
+ - name: override
292
+ type: boolean
293
+ required: false
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
+ })
320
+ - name: contentType
321
+ type: "'application/json' | (string & {})"
322
+ required: false
323
+ description: |
324
+ Selects which request/response media type the generator reads from the OpenAPI spec.
325
+
326
+ 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.
327
+ examples:
328
+ - name: JSON API media type
329
+ files:
330
+ - lang: typescript
331
+ twoslash: false
332
+ code: |
333
+ import { defineConfig } from 'kubb'
334
+ import { pluginTs } from '@kubb/plugin-ts'
335
+
336
+ export default defineConfig({
337
+ input: { path: './petStore.yaml' },
338
+ output: { path: './src/gen' },
339
+ plugins: [
340
+ pluginTs({
341
+ contentType: 'application/vnd.api+json',
342
+ }),
343
+ ],
344
+ })
345
+ - name: group
346
+ type: Group
347
+ required: false
348
+ description: |
349
+ Splits generated files into subfolders based on the operation's tag, so each tag in your OpenAPI spec gets its own directory.
350
+
351
+ 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.
352
+ tip: |
353
+ 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.
354
+ examples:
355
+ - name: kubb.config.ts
356
+ files:
357
+ - lang: typescript
358
+ twoslash: false
359
+ code: |
360
+ import { defineConfig } from 'kubb'
361
+ import { pluginTs } from '@kubb/plugin-ts'
362
+
363
+ export default defineConfig({
364
+ input: { path: './petStore.yaml' },
365
+ output: { path: './src/gen' },
366
+ plugins: [
367
+ pluginTs({
368
+ group: {
369
+ type: 'tag',
370
+ name: ({ group }) => `${group}Controller`,
371
+ },
372
+ }),
373
+ ],
374
+ })
375
+ body: |
376
+ With the configuration above, the generator emits:
377
+
378
+ ```text
379
+ src/gen/
380
+ ├── petController/
381
+ │ ├── AddPet.ts
382
+ │ └── GetPet.ts
383
+ └── storeController/
384
+ ├── CreateStore.ts
385
+ └── GetStoreById.ts
386
+ ```
387
+ properties:
388
+ - name: type
389
+ type: "'tag'"
390
+ required: true
391
+ description: |
392
+ Property used to assign each operation to a group. Required whenever `group` is set.
393
+
394
+ 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.
395
+ note: |
396
+ `Required: true*` is conditional — only required when the parent `group` option is used. `group` itself stays optional.
397
+ - name: name
398
+ type: '(context: GroupContext) => string'
399
+ required: false
400
+ default: (ctx) => `${ctx.group}Controller`
401
+ description: Function that builds the folder/identifier name from a group key (the operation's first tag).
402
+ - name: client
403
+ type: ClientImportPath & { clientType?, dataReturnType?, baseURL?, bundle? }
404
+ required: false
405
+ description: |
406
+ HTTP client used inside every generated hook. Each generated hook calls into this client to perform the actual request.
407
+
408
+ Mirrors a subset of `pluginClient` options. Set these here when the React Query hooks need different client behavior than the rest of your app (for example, a different base URL or full response objects).
409
+ properties:
410
+ - name: importPath
411
+ type: string
412
+ required: false
413
+ description: |
414
+ Path or module specifier of a custom client module. Generated code imports its HTTP runtime from here instead of `@kubb/plugin-client/clients/{client}`.
415
+
416
+ Use this when you need to inject auth headers, add interceptors, change the base URL at runtime, or wrap a different HTTP library (ky, ofetch, ...). Both relative paths (`./src/client.ts`) and bare specifiers (`@my-org/api-client`) work.
417
+ details:
418
+ - title: When to use `importPath`
419
+ body: |
420
+ Reach for a custom client when you need to:
421
+
422
+ - Add an auth token to every request.
423
+ - Plug in interceptors, retries, or logging.
424
+ - Configure `baseURL` and headers from environment variables.
425
+ - Wrap a library other than `axios`/`fetch`.
426
+ - title: Default behavior
427
+ body: |
428
+ Without `importPath`:
429
+
430
+ - `bundle: false` (default) — generated code imports from `@kubb/plugin-client/clients/{axios|fetch}`.
431
+ - `bundle: true` — Kubb writes `.kubb/client.ts` and generated code imports from there.
432
+ - title: Required exports
433
+ body: |
434
+ The module pointed to by `importPath` must satisfy the same shape as the built-in client. At minimum:
435
+
436
+ - A default export of the `client` function.
437
+ - A `RequestConfig` type.
438
+ - A `ResponseErrorConfig` type.
439
+
440
+ When used together with a query plugin (`@kubb/plugin-react-query`, `@kubb/plugin-vue-query`), it must also export a `Client` type alias.
441
+ - title: How generated files import it
442
+ body: |
443
+ Generated code imports the client as a default import (bound to the local name `client`) and the runtime types as named type imports:
444
+ codeBlock:
445
+ lang: typescript
446
+ code: |
447
+ import client from '${client.importPath}'
448
+ import type { RequestConfig, ResponseErrorConfig } from '${client.importPath}'
449
+ // ... rest of the generated file
450
+ important: |
451
+ When used with query plugins (`@kubb/plugin-react-query`, `@kubb/plugin-vue-query`), generated hooks also import a `Client` type alias. Your module **must** export `Client`, `RequestConfig`, and `ResponseErrorConfig` — TypeScript will fail the import otherwise.
452
+ codeBlock:
453
+ lang: typescript
454
+ title: src/client.ts
455
+ code: |
456
+ import axios from 'axios'
457
+
458
+ export type RequestConfig<TData = unknown> = {
459
+ url?: string
460
+ method: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE'
461
+ params?: object
462
+ data?: TData | FormData
463
+ responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
464
+ signal?: AbortSignal
465
+ headers?: HeadersInit
466
+ }
467
+
468
+ export type ResponseConfig<TData = unknown> = {
469
+ data: TData
470
+ status: number
471
+ statusText: string
472
+ }
473
+
474
+ export type ResponseErrorConfig<TError = unknown> = TError
475
+
476
+ // Required when used with @kubb/plugin-react-query or @kubb/plugin-vue-query
477
+ export type Client = <TData, _TError = unknown, TVariables = unknown>(
478
+ config: RequestConfig<TVariables>,
479
+ ) => Promise<ResponseConfig<TData>>
480
+
481
+ const client: Client = async (config) => {
482
+ const response = await axios.request<TData>({
483
+ ...config,
484
+ headers: {
485
+ Authorization: `Bearer ${process.env.API_TOKEN}`,
486
+ ...config.headers,
487
+ },
488
+ })
489
+
490
+ return response
491
+ }
492
+
493
+ export default client
494
+ examples:
495
+ - name: Wire up a custom client
496
+ files:
497
+ - name: kubb.config.ts
498
+ lang: typescript
499
+ twoslash: false
500
+ code: |
501
+ import { defineConfig } from 'kubb'
502
+ import { pluginClient } from '@kubb/plugin-client'
503
+
504
+ export default defineConfig({
505
+ input: { path: './petStore.yaml' },
506
+ output: { path: './src/gen' },
507
+ plugins: [
508
+ pluginClient({
509
+ importPath: './src/client.ts',
510
+ }),
511
+ ],
512
+ })
513
+ tip: |
514
+ See the [custom client guide](https://kubb.dev/plugins/plugin-client#importpath) for a worked example.
515
+ - name: dataReturnType
516
+ type: "'data' | 'full'"
517
+ required: false
518
+ default: "'data'"
519
+ description: |
520
+ Shape of the value returned from each generated client function.
521
+
522
+ - `'data'` returns only the response body (`response.data`). Concise and matches what most apps need.
523
+ - `'full'` returns the full response config — body, status code, headers, and the original request. Use this when callers need to inspect headers or status.
524
+ examples:
525
+ - name: "'data' (default)"
526
+ files:
527
+ - name: getPetById.ts
528
+ lang: typescript
529
+ twoslash: false
530
+ code: |
531
+ export async function getPetById<TData>(
532
+ petId: GetPetByIdPathParams,
533
+ ): Promise<ResponseConfig<TData>['data']> {
534
+ // ...
535
+ }
536
+ - name: usage.ts
537
+ lang: typescript
538
+ twoslash: false
539
+ code: |
540
+ const pet = await getPetById(1)
541
+ // ^? Pet
542
+ - name: "'full'"
543
+ files:
544
+ - name: getPetById.ts
545
+ lang: typescript
546
+ twoslash: false
547
+ code: |
548
+ export async function getPetById<TData>(
549
+ petId: GetPetByIdPathParams,
550
+ ): Promise<ResponseConfig<TData>> {
551
+ // ...
552
+ }
553
+ - name: usage.ts
554
+ lang: typescript
555
+ twoslash: false
556
+ code: |
557
+ const response = await getPetById(1)
558
+ // ^? ResponseConfig<Pet>
559
+ console.log(response.status, response.headers)
560
+ const pet = response.data
561
+ - name: baseURL
562
+ type: string
563
+ required: false
564
+ description: |
565
+ 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).
566
+
567
+ Set this when the generated client should point at a different environment (staging, production) than the one written in the spec.
568
+ examples:
569
+ - name: Override the spec's server URL
570
+ files:
571
+ - lang: typescript
572
+ twoslash: false
573
+ code: |
574
+ import { defineConfig } from 'kubb'
575
+ import { pluginClient } from '@kubb/plugin-client'
576
+
577
+ export default defineConfig({
578
+ input: { path: './petStore.yaml' },
579
+ output: { path: './src/gen' },
580
+ plugins: [
581
+ pluginClient({
582
+ baseURL: 'https://petstore.swagger.io/v2',
583
+ }),
584
+ ],
585
+ })
586
+ - name: clientType
587
+ type: "'function' | 'class'"
588
+ required: false
589
+ default: "'function'"
590
+ description: |
591
+ Style of the HTTP client that this plugin imports from `@kubb/plugin-client`.
592
+
593
+ - `'function'` — imports the function client (`getPetById(...)`). Required for query plugins.
594
+ - `'class'` — also generates a wrapper class on top, but only usable inside `@kubb/plugin-client`.
595
+ warning: |
596
+ Query plugins (`@kubb/plugin-react-query`, `@kubb/plugin-vue-query`, `@kubb/plugin-svelte-query`, `@kubb/plugin-solid-query`) work only with `clientType: 'function'`. If you set `clientType: 'class'` here, the plugin falls back to generating its own inline function-based client instead of importing from `@kubb/plugin-client`.
597
+ - name: bundle
598
+ type: boolean
599
+ required: false
600
+ default: 'false'
601
+ description: |
602
+ Copies the HTTP client runtime into the generated output, so the consuming app does not need `@kubb/plugin-client` installed at runtime.
603
+
604
+ - `false` (default) — generated files import from `@kubb/plugin-client/clients/{client}`. Smaller diff, but the package must be a runtime dependency.
605
+ - `true` — Kubb writes a `.kubb/client.ts` file with the client implementation. Generated code imports from that local file and the project no longer pulls `@kubb/plugin-client` at runtime.
606
+ - Setting `client.importPath` overrides both behaviors and uses your custom client instead.
607
+ examples:
608
+ - name: Bundle the runtime
609
+ files:
610
+ - lang: typescript
611
+ twoslash: false
612
+ code: |
613
+ import { defineConfig } from 'kubb'
614
+ import { pluginClient } from '@kubb/plugin-client'
615
+
616
+ export default defineConfig({
617
+ input: { path: './petStore.yaml' },
618
+ output: { path: './src/gen' },
619
+ plugins: [
620
+ pluginClient({
621
+ client: 'fetch',
622
+ bundle: true,
623
+ }),
624
+ ],
625
+ })
626
+ - name: paramsType
627
+ type: "'object' | 'inline'"
628
+ required: false
629
+ default: "'inline'"
630
+ description: |
631
+ How operation parameters (path, query, headers) are exposed in the generated function signature.
632
+
633
+ - `'inline'` (default) — each parameter is a separate positional argument. Compact for operations with one or two params.
634
+ - `'object'` — every parameter is wrapped in a single object argument. Easier to read for operations with many params and named at the call site.
635
+ tip: |
636
+ Setting `paramsType: 'object'` implicitly sets `pathParamsType: 'object'` as well, so call sites are consistent.
637
+ examples:
638
+ - name: "'inline' (default)"
639
+ files:
640
+ - name: Generated client
641
+ lang: typescript
642
+ twoslash: false
643
+ code: |
644
+ export async function deletePet(
645
+ petId: DeletePetPathParams['petId'],
646
+ headers?: DeletePetHeaderParams,
647
+ config: Partial<RequestConfig> = {},
648
+ ) {
649
+ // ...
650
+ }
651
+ - name: Caller
652
+ lang: typescript
653
+ twoslash: false
654
+ code: |
655
+ await deletePet(42)
656
+ - name: "'object'"
657
+ files:
658
+ - name: Generated client
659
+ lang: typescript
660
+ twoslash: false
661
+ code: |
662
+ export async function deletePet(
663
+ {
664
+ petId,
665
+ headers,
666
+ }: {
667
+ petId: DeletePetPathParams['petId']
668
+ headers?: DeletePetHeaderParams
669
+ },
670
+ config: Partial<RequestConfig> = {},
671
+ ) {
672
+ // ...
673
+ }
674
+ - name: Caller
675
+ lang: typescript
676
+ twoslash: false
677
+ code: |
678
+ await deletePet({ petId: 42, headers: { 'X-Api-Key': 'secret' } })
679
+ - name: paramsCasing
680
+ type: "'camelcase'"
681
+ required: false
682
+ description: |
683
+ Renames path, query, and header parameters in the generated client to the chosen casing. The HTTP request still uses the original names from the OpenAPI spec — Kubb writes the mapping for you.
684
+
685
+ - `'camelcase'` — turn `pet_id` and `X-Api-Key` into `petId` and `xApiKey` in your TypeScript code. The runtime URL still uses `/pet/{pet_id}` and the header is still sent as `X-Api-Key`.
686
+ important: |
687
+ Set the same `paramsCasing` on every plugin that touches operation parameters (`@kubb/plugin-ts`, `@kubb/plugin-client`, `@kubb/plugin-react-query`, `@kubb/plugin-faker`, `@kubb/plugin-mcp`). Mismatched casing causes type errors between generated layers.
688
+ examples:
689
+ - name: "With paramsCasing: 'camelcase'"
690
+ files:
691
+ - name: Generated client
692
+ lang: typescript
693
+ twoslash: false
694
+ code: |
695
+ // Function takes camelCase parameters
696
+ export async function deletePet(
697
+ petId: DeletePetPathParams['petId'],
698
+ headers?: DeletePetHeaderParams,
699
+ config: Partial<RequestConfig> = {},
700
+ ) {
701
+ // ...mapped back to the original API name internally
702
+ const pet_id = petId
703
+
704
+ return client({
705
+ method: 'DELETE',
706
+ url: `/pet/${pet_id}`,
707
+ ...config,
708
+ })
709
+ }
710
+ - name: Caller
711
+ lang: typescript
712
+ twoslash: false
713
+ code: |
714
+ await deletePet(42)
715
+ - name: Without paramsCasing
716
+ files:
717
+ - name: Generated client
718
+ lang: typescript
719
+ twoslash: false
720
+ code: |
721
+ // Function parameters mirror the OpenAPI spec
722
+ export async function deletePet(
723
+ pet_id: DeletePetPathParams['pet_id'],
724
+ headers?: DeletePetHeaderParams,
725
+ config: Partial<RequestConfig> = {},
726
+ ) {
727
+ return client({
728
+ method: 'DELETE',
729
+ url: `/pet/${pet_id}`,
730
+ ...config,
731
+ })
732
+ }
733
+ tip: |
734
+ Callers write friendly camelCase names. The generated client maps them back to whatever the API expects (snake_case path params, kebab-case headers, ...).
735
+ - name: pathParamsType
736
+ type: "'object' | 'inline'"
737
+ required: false
738
+ default: "'inline'"
739
+ description: |
740
+ How URL path parameters appear in the generated function signature. Affects only path params; query/header params follow `paramsType`.
741
+
742
+ - `'inline'` (default) — each path param is a positional argument: `getPetById(petId)`.
743
+ - `'object'` — path params are wrapped in a single object: `getPetById({ petId })`.
744
+ examples:
745
+ - name: "'inline' (default)"
746
+ files:
747
+ - lang: typescript
748
+ twoslash: false
749
+ code: |
750
+ export async function getPetById(
751
+ petId: GetPetByIdPathParams,
752
+ ) {
753
+ // ...
754
+ }
755
+ - name: "'object'"
756
+ files:
757
+ - lang: typescript
758
+ twoslash: false
759
+ code: |
760
+ export async function getPetById(
761
+ { petId }: GetPetByIdPathParams,
762
+ ) {
763
+ // ...
764
+ }
765
+ - name: parser
766
+ type: "'client' | 'zod'"
767
+ required: false
768
+ default: "'client'"
769
+ description: |
770
+ Runtime validator applied to the response body before it is returned to the caller.
771
+
772
+ - `'client'` (default) — no validation. The response is cast to the generated TypeScript type and returned as-is. Fastest path, trusts the API.
773
+ - `'zod'` — pipes the response through the Zod schema produced by `@kubb/plugin-zod`. Catches mismatches between spec and API at runtime, at the cost of a parse on every call.
774
+
775
+ Use `'zod'` when you want a defensive boundary against drift between your OpenAPI spec and the live API. Requires `@kubb/plugin-zod` in the plugins list.
776
+ examples:
777
+ - name: Validate responses with Zod
778
+ files:
779
+ - lang: typescript
780
+ twoslash: false
781
+ code: |
782
+ import { defineConfig } from 'kubb'
783
+ import { pluginClient } from '@kubb/plugin-client'
784
+ import { pluginTs } from '@kubb/plugin-ts'
785
+ import { pluginZod } from '@kubb/plugin-zod'
786
+
787
+ export default defineConfig({
788
+ input: { path: './petStore.yaml' },
789
+ output: { path: './src/gen' },
790
+ plugins: [
791
+ pluginTs(),
792
+ pluginZod(),
793
+ pluginClient({
794
+ parser: 'zod',
795
+ }),
796
+ ],
797
+ })
798
+ - name: infinite
799
+ type: Infinite | false
800
+ required: false
801
+ default: 'false'
802
+ description: |
803
+ Enables `useInfiniteQuery` hooks for cursor- or page-based pagination. Pass an object to configure how the cursor is read from the response; pass `false` (default) to skip infinite query generation.
804
+ codeBlock:
805
+ lang: typescript
806
+ title: Infinite type
807
+ code: |
808
+ type Infinite = {
809
+ /** Query-param key used as the page cursor. Defaults to `'id'`. */
810
+ queryParam: string
811
+ /** @deprecated Use `nextParam` / `previousParam` instead. */
812
+ cursorParam?: string
813
+ /** Path to the next-page cursor in the response. Dot or array form. */
814
+ nextParam?: string | string[]
815
+ /** Path to the previous-page cursor in the response. Dot or array form. */
816
+ previousParam?: string | string[]
817
+ /** Value of `pageParam` for the first page. Defaults to `0`. */
818
+ initialPageParam: unknown
819
+ } | false
820
+ properties:
821
+ - name: queryParam
822
+ type: string
823
+ required: false
824
+ default: "'id'"
825
+ description: Name of the query parameter that holds the page cursor (Kubb passes `pageParam` into this query key).
826
+ - name: initialPageParam
827
+ type: unknown
828
+ required: false
829
+ default: '0'
830
+ description: Initial value for `pageParam` on the first fetch.
831
+ - name: cursorParam
832
+ type: string | undefined
833
+ required: false
834
+ deprecated:
835
+ note: '`cursorParam` is deprecated. Use `nextParam` and `previousParam` for richer pagination control.'
836
+ description: Path to the cursor field on the response. Leave undefined when the cursor is not known.
837
+ - name: nextParam
838
+ type: string | string[] | undefined
839
+ required: false
840
+ description: |
841
+ Path to the next-page cursor on the response. Supports dot notation (`'pagination.next.id'`) or array form (`['pagination', 'next', 'id']`).
842
+ - name: previousParam
843
+ type: string | string[] | undefined
844
+ required: false
845
+ description: |
846
+ Path to the previous-page cursor on the response. Supports dot notation (`'pagination.prev.id'`) or array form (`['pagination', 'prev', 'id']`).
847
+ examples:
848
+ - name: Cursor pagination
849
+ files:
850
+ - lang: typescript
851
+ twoslash: false
852
+ code: |
853
+ import { defineConfig } from 'kubb'
854
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
855
+
856
+ export default defineConfig({
857
+ input: { path: './petStore.yaml' },
858
+ output: { path: './src/gen' },
859
+ plugins: [
860
+ pluginReactQuery({
861
+ infinite: {
862
+ queryParam: 'cursor',
863
+ initialPageParam: null,
864
+ nextParam: 'pagination.next.cursor',
865
+ previousParam: 'pagination.prev.cursor',
866
+ },
867
+ }),
868
+ ],
869
+ })
870
+ - name: query
871
+ type: Query
872
+ required: false
873
+ description: |
874
+ Configures the query hooks. Pass `false` to skip generating hooks entirely and only emit `queryOptions(...)` helpers — useful when you want to call `useQuery` yourself in app code.
875
+ codeBlock:
876
+ lang: typescript
877
+ title: Query type
878
+ code: |
879
+ type Query = {
880
+ methods: Array<HttpMethod>
881
+ importPath?: string
882
+ } | false
883
+ properties:
884
+ - name: methods
885
+ type: Array<HttpMethod>
886
+ required: false
887
+ default: "['get']"
888
+ description: |
889
+ HTTP methods treated as queries. Operations using one of these methods generate a `useQuery`-style hook (or `queryOptions` helper) instead of a mutation.
890
+
891
+ Defaults to `['get']`. Add other methods (for example `'head'`) only when your API uses them for cache-friendly reads.
892
+ examples:
893
+ - name: Allow HEAD as a query method
894
+ files:
895
+ - lang: typescript
896
+ twoslash: false
897
+ code: |
898
+ import { defineConfig } from 'kubb'
899
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
900
+
901
+ export default defineConfig({
902
+ input: { path: './petStore.yaml' },
903
+ output: { path: './src/gen' },
904
+ plugins: [
905
+ pluginReactQuery({
906
+ query: { methods: ['get', 'head'] },
907
+ }),
908
+ ],
909
+ })
910
+ - name: importPath
911
+ type: string
912
+ required: false
913
+ default: "'@tanstack/react-query'"
914
+ description: |
915
+ Module specifier used in the `import { useQuery } from '...'` statement at the top of every generated hook file. Use this to point at your own re-export of TanStack Query (for example a wrapper that injects a default `queryClient`).
916
+ - name: queryKey
917
+ type: '(props: { operation: Operation; schemas: OperationSchemas }) => unknown[]'
918
+ required: false
919
+ description: |
920
+ Builds the `queryKey` for each generated hook. Use this to add a version namespace, swap to operation IDs, or shape keys to match an existing `queryClient.invalidateQueries` strategy.
921
+
922
+ The callback receives:
923
+
924
+ - `operation` — the OpenAPI operation (`getTags()`, `getOperationId()`, ...).
925
+ - `schemas` — operation schemas including `pathParams`, `queryParams`, `request`, `response`.
926
+ warning: |
927
+ String values are inlined verbatim into generated code. Wrap any string you want emitted as a literal in `JSON.stringify(...)`.
928
+ details:
929
+ - title: Keys from tags and path parameters
930
+ body: "Build a key from the operation's first tag plus its path parameters. For a `GET /user/{username}` operation with the `user` tag, this generates:"
931
+ codeBlock:
932
+ - lang: typescript
933
+ code: |-
934
+ import { defineConfig } from 'kubb'
935
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
936
+
937
+ export default defineConfig({
938
+ input: { path: './petStore.yaml' },
939
+ output: { path: './src/gen' },
940
+ plugins: [
941
+ pluginReactQuery({
942
+ queryKey: ({ operation, schemas }) => {
943
+ const tags = operation.getTags().map((tag) => JSON.stringify(tag.name))
944
+ const pathParams = schemas.pathParams?.keys ?? []
945
+ return [...tags, ...pathParams]
946
+ },
947
+ }),
948
+ ],
949
+ })
950
+ - lang: typescript
951
+ code: |-
952
+ export const getUserByNameQueryKey = ({ username }: { username: GetUserByNamePathParams['username'] }) =>
953
+ ['user', username] as const
954
+ - title: Extend the default transformer
955
+ body: 'Prepend a version prefix to the default query key:'
956
+ codeBlock:
957
+ - lang: typescript
958
+ code: |-
959
+ import { defineConfig } from 'kubb'
960
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
961
+ import { QueryKey } from '@kubb/plugin-react-query/components'
962
+
963
+ export default defineConfig({
964
+ input: { path: './petStore.yaml' },
965
+ output: { path: './src/gen' },
966
+ plugins: [
967
+ pluginReactQuery({
968
+ queryKey: (props) => {
969
+ const defaultKeys = QueryKey.getTransformer(props)
970
+ return [JSON.stringify('v5'), ...defaultKeys]
971
+ },
972
+ }),
973
+ ],
974
+ })
975
+ - lang: typescript
976
+ code: |-
977
+ export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) =>
978
+ ['v5', { url: '/pet/findByTags' }, ...(params ? [params] : [])] as const
979
+ - title: Key from operationId
980
+ body: Use the operationId as the only key. Smallest possible key.
981
+ codeBlock:
982
+ lang: typescript
983
+ code: |-
984
+ import { defineConfig } from 'kubb'
985
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
986
+
987
+ export default defineConfig({
988
+ input: { path: './petStore.yaml' },
989
+ output: { path: './src/gen' },
990
+ plugins: [
991
+ pluginReactQuery({
992
+ queryKey: ({ operation }) => [JSON.stringify(operation.getOperationId())],
993
+ }),
994
+ ],
995
+ })
996
+ - title: Conditional keys based on params
997
+ body: 'Include query params in the key only when they are present:'
998
+ codeBlock:
999
+ lang: typescript
1000
+ code: |-
1001
+ import { defineConfig } from 'kubb'
1002
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
1003
+
1004
+ export default defineConfig({
1005
+ input: { path: './petStore.yaml' },
1006
+ output: { path: './src/gen' },
1007
+ plugins: [
1008
+ pluginReactQuery({
1009
+ queryKey: ({ operation, schemas }) => {
1010
+ const keys: unknown[] = [JSON.stringify(operation.getOperationId())]
1011
+
1012
+ if (schemas.pathParams?.keys) {
1013
+ keys.push(...schemas.pathParams.keys)
1014
+ }
1015
+
1016
+ if (schemas.queryParams?.name) {
1017
+ keys.push('...(params ? [params] : [])')
1018
+ }
1019
+
1020
+ return keys
1021
+ },
1022
+ }),
1023
+ ],
1024
+ })
1025
+ - name: suspense
1026
+ type: object | false
1027
+ required: false
1028
+ description: |
1029
+ Adds `useSuspenseQuery` hooks alongside the regular `useQuery` ones. Pass an empty object (`{}`) to enable; omit or set to `false` to skip.
1030
+
1031
+ Suspense queries throw promises while loading and require a `<Suspense>` boundary in the React tree. TanStack Query v5+ only.
1032
+ - name: mutation
1033
+ type: Mutation
1034
+ required: false
1035
+ description: |
1036
+ Configures mutation hooks. Set to `false` to skip mutation generation entirely.
1037
+ codeBlock:
1038
+ lang: typescript
1039
+ title: Mutation type
1040
+ code: |
1041
+ type Mutation = {
1042
+ methods: Array<HttpMethod>
1043
+ importPath?: string
1044
+ } | false
1045
+ properties:
1046
+ - name: methods
1047
+ type: Array<HttpMethod>
1048
+ required: false
1049
+ default: "['post', 'put', 'delete']"
1050
+ description: |
1051
+ HTTP methods treated as mutations. Operations using one of these methods generate a `useMutation`-style hook instead of a query.
1052
+
1053
+ Defaults to `['post', 'put', 'delete']`. Add `'patch'` if your API uses it for partial updates.
1054
+ examples:
1055
+ - name: Include PATCH as a mutation
1056
+ files:
1057
+ - lang: typescript
1058
+ twoslash: false
1059
+ code: |
1060
+ import { defineConfig } from 'kubb'
1061
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
1062
+
1063
+ export default defineConfig({
1064
+ input: { path: './petStore.yaml' },
1065
+ output: { path: './src/gen' },
1066
+ plugins: [
1067
+ pluginReactQuery({
1068
+ mutation: { methods: ['post', 'put', 'patch', 'delete'] },
1069
+ }),
1070
+ ],
1071
+ })
1072
+ - name: importPath
1073
+ type: string
1074
+ required: false
1075
+ default: "'@tanstack/react-query'"
1076
+ description: |
1077
+ Module specifier used in the `import { useMutation } from '...'` statement at the top of every generated hook file. Useful for routing through your own wrapper.
1078
+ - name: mutationKey
1079
+ type: '(props: { operation: Operation; schemas: OperationSchemas }) => unknown[]'
1080
+ required: false
1081
+ description: |
1082
+ Builds the `mutationKey` for each mutation hook. Useful when you batch invalidations or read mutation state via `useMutationState`.
1083
+ warning: |
1084
+ String values are inlined verbatim into generated code. Wrap literal strings in `JSON.stringify(...)`.
1085
+ - name: customOptions
1086
+ type: CustomOptions
1087
+ required: false
1088
+ description: |
1089
+ Wires every generated hook through a user-supplied function that returns extra options (`onSuccess`, `onError`, `select`, ...). The plugin also emits a `HookOptions` type so your wrapper stays in sync with the generated hooks.
1090
+
1091
+ Use this to centralize cache invalidation, error toasts, or analytics in one place instead of repeating them at every call site.
1092
+ codeBlock:
1093
+ lang: typescript
1094
+ title: CustomOptions type
1095
+ code: |
1096
+ type CustomOptions = {
1097
+ importPath: string
1098
+ name?: string
1099
+ }
1100
+ properties:
1101
+ - name: importPath
1102
+ type: string
1103
+ required: true
1104
+ description: |
1105
+ Module specifier of your custom-options hook. Imported as `import ${name} from '${importPath}'`. Use a relative path from the generated file or a bare specifier.
1106
+ - name: name
1107
+ type: string
1108
+ required: false
1109
+ default: "'useCustomHookOptions'"
1110
+ description: |
1111
+ Exported function name of your custom-options hook. Imported as `import ${name} from '${importPath}'`.
1112
+ details:
1113
+ - title: Centralised cache invalidation
1114
+ codeBlock:
1115
+ lang: typescript
1116
+ title: src/useCustomHookOptions.ts
1117
+ code: |-
1118
+ import { useQueryClient, type QueryClient } from '@tanstack/react-query'
1119
+ import type { HookOptions } from '../gen/hookOptions'
1120
+ import { getUserByNameQueryKey } from '../gen/hooks/userController/useGetUserByNameHook'
1121
+
1122
+ function getCustomHookOptions({ queryClient }: { queryClient: QueryClient }): Partial<HookOptions> {
1123
+ return {
1124
+ useUpdatePetHook: {
1125
+ onSuccess: () => {
1126
+ void queryClient.invalidateQueries({ queryKey: ['pet'] })
1127
+ },
1128
+ },
1129
+ useDeletePetHook: {
1130
+ onSuccess: (_data, variables) => {
1131
+ void queryClient.invalidateQueries({ queryKey: ['pet', variables.pet_id] })
1132
+ },
1133
+ },
1134
+ useUpdateUserHook: {
1135
+ onSuccess: (_data, variables) => {
1136
+ void queryClient.invalidateQueries({
1137
+ queryKey: getUserByNameQueryKey({ username: variables.username }),
1138
+ })
1139
+ },
1140
+ },
1141
+ }
1142
+ }
1143
+
1144
+ export function useCustomHookOptions<T extends keyof HookOptions>({
1145
+ hookName,
1146
+ }: {
1147
+ hookName: T
1148
+ operationId: string
1149
+ }): HookOptions[T] {
1150
+ const queryClient = useQueryClient()
1151
+ const customOptions = getCustomHookOptions({ queryClient })
1152
+ return customOptions[hookName] ?? {}
1153
+ }
1154
+ - title: App-wide error reporting
1155
+ codeBlock:
1156
+ lang: typescript
1157
+ title: src/useCustomHookOptions.ts
1158
+ code: |-
1159
+ import { toast } from 'sonner'
1160
+ import { useQueryClient, type QueryClient } from '@tanstack/react-query'
1161
+ import type { HookOptions } from '../gen/hookOptions'
1162
+
1163
+ function getCustomHookOptions({ queryClient }: { queryClient: QueryClient }): Partial<HookOptions> {
1164
+ return {
1165
+ useUpdatePetHook: {
1166
+ onError: (error) => {
1167
+ console.error('Failed to update pet:', error)
1168
+ },
1169
+ },
1170
+ useDeletePetHook: {
1171
+ onError: (_error, variables) => {
1172
+ toast.error(`Failed to delete pet with id '${variables.pet_id}'`)
1173
+ },
1174
+ },
1175
+ }
1176
+ }
1177
+
1178
+ export function useCustomHookOptions<T extends keyof HookOptions>({
1179
+ hookName,
1180
+ }: {
1181
+ hookName: T
1182
+ operationId: string
1183
+ }): HookOptions[T] {
1184
+ const queryClient = useQueryClient()
1185
+ const customOptions = getCustomHookOptions({ queryClient })
1186
+ return customOptions[hookName] ?? {}
1187
+ }
1188
+ - name: include
1189
+ type: Array<Include>
1190
+ required: false
1191
+ description: |
1192
+ Restricts generation to operations that match at least one entry in the list. Anything not matched is skipped.
1193
+
1194
+ Each entry filters by one of:
1195
+
1196
+ - `tag` — the operation's first tag in the OpenAPI spec.
1197
+ - `operationId` — the operation's `operationId`.
1198
+ - `path` — the URL pattern (`'/pet/{petId}'`).
1199
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
1200
+ - `contentType` — the media type of the request body.
1201
+
1202
+ `pattern` accepts either a string (exact match) or a `RegExp` for fuzzy matches.
1203
+ codeBlock:
1204
+ lang: typescript
1205
+ title: Type definition
1206
+ code: |
1207
+ export type Include = {
1208
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
1209
+ pattern: string | RegExp
1210
+ }
1211
+ examples:
1212
+ - name: Only the pet tag
1213
+ files:
1214
+ - name: kubb.config.ts
1215
+ lang: typescript
1216
+ twoslash: false
1217
+ code: |
1218
+ import { defineConfig } from 'kubb'
1219
+ import { pluginTs } from '@kubb/plugin-ts'
1220
+
1221
+ export default defineConfig({
1222
+ input: { path: './petStore.yaml' },
1223
+ output: { path: './src/gen' },
1224
+ plugins: [
1225
+ pluginTs({
1226
+ include: [
1227
+ { type: 'tag', pattern: 'pet' },
1228
+ ],
1229
+ }),
1230
+ ],
1231
+ })
1232
+ - name: Only GET operations under /pet
1233
+ files:
1234
+ - name: kubb.config.ts
1235
+ lang: typescript
1236
+ twoslash: false
1237
+ code: |
1238
+ import { defineConfig } from 'kubb'
1239
+ import { pluginTs } from '@kubb/plugin-ts'
1240
+
1241
+ export default defineConfig({
1242
+ input: { path: './petStore.yaml' },
1243
+ output: { path: './src/gen' },
1244
+ plugins: [
1245
+ pluginTs({
1246
+ include: [
1247
+ { type: 'method', pattern: 'get' },
1248
+ { type: 'path', pattern: /^\/pet/ },
1249
+ ],
1250
+ }),
1251
+ ],
1252
+ })
1253
+ - name: exclude
1254
+ type: Array<Exclude>
1255
+ required: false
1256
+ description: |
1257
+ Skips any operation that matches at least one entry in the list. The opposite of `include`.
1258
+
1259
+ Each entry filters by one of:
1260
+
1261
+ - `tag` — the operation's first tag.
1262
+ - `operationId` — the operation's `operationId`.
1263
+ - `path` — the URL pattern (`'/pet/{petId}'`).
1264
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
1265
+ - `contentType` — the media type of the request body.
1266
+
1267
+ `pattern` accepts a plain string or a `RegExp`. When both `include` and `exclude` are set, `exclude` wins.
1268
+ codeBlock:
1269
+ lang: typescript
1270
+ title: Type definition
1271
+ code: |
1272
+ export type Exclude = {
1273
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
1274
+ pattern: string | RegExp
1275
+ }
1276
+ examples:
1277
+ - name: Skip everything under the store tag
1278
+ files:
1279
+ - name: kubb.config.ts
1280
+ lang: typescript
1281
+ twoslash: false
1282
+ code: |
1283
+ import { defineConfig } from 'kubb'
1284
+ import { pluginTs } from '@kubb/plugin-ts'
1285
+
1286
+ export default defineConfig({
1287
+ input: { path: './petStore.yaml' },
1288
+ output: { path: './src/gen' },
1289
+ plugins: [
1290
+ pluginTs({
1291
+ exclude: [
1292
+ { type: 'tag', pattern: 'store' },
1293
+ ],
1294
+ }),
1295
+ ],
1296
+ })
1297
+ - name: Skip a specific operation and all delete methods
1298
+ files:
1299
+ - name: kubb.config.ts
1300
+ lang: typescript
1301
+ twoslash: false
1302
+ code: |
1303
+ import { defineConfig } from 'kubb'
1304
+ import { pluginTs } from '@kubb/plugin-ts'
1305
+
1306
+ export default defineConfig({
1307
+ input: { path: './petStore.yaml' },
1308
+ output: { path: './src/gen' },
1309
+ plugins: [
1310
+ pluginTs({
1311
+ exclude: [
1312
+ { type: 'operationId', pattern: 'deletePet' },
1313
+ { type: 'method', pattern: 'delete' },
1314
+ ],
1315
+ }),
1316
+ ],
1317
+ })
1318
+ - name: override
1319
+ type: Array<Override>
1320
+ required: false
1321
+ description: |
1322
+ 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.
1323
+
1324
+ 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.
1325
+
1326
+ Entries are evaluated top to bottom. The first matching entry's `options` is merged onto the plugin defaults; later entries do not stack.
1327
+ codeBlock:
1328
+ lang: typescript
1329
+ title: Type definition
1330
+ code: |
1331
+ export type Override = {
1332
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
1333
+ pattern: string | RegExp
1334
+ options: PluginOptions
1335
+ }
1336
+ examples:
1337
+ - name: Use a different enum style for the user tag
1338
+ files:
1339
+ - name: kubb.config.ts
1340
+ lang: typescript
1341
+ twoslash: false
1342
+ code: |
1343
+ import { defineConfig } from 'kubb'
1344
+ import { pluginTs } from '@kubb/plugin-ts'
1345
+
1346
+ export default defineConfig({
1347
+ input: { path: './petStore.yaml' },
1348
+ output: { path: './src/gen' },
1349
+ plugins: [
1350
+ pluginTs({
1351
+ enumType: 'asConst',
1352
+ override: [
1353
+ {
1354
+ type: 'tag',
1355
+ pattern: 'user',
1356
+ options: { enumType: 'literal' },
1357
+ },
1358
+ ],
1359
+ }),
1360
+ ],
1361
+ })
1362
+ - name: generators
1363
+ type: Array<Generator<PluginReactQuery>>
1364
+ required: false
1365
+ experimental: true
1366
+ description: |
1367
+ 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.
1368
+
1369
+ 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).
1370
+ warning: |
1371
+ Generators are an experimental, low-level API. The signature may change between minor releases.
1372
+ - name: resolver
1373
+ type: Partial<ResolverReactQuery> & ThisType<ResolverReactQuery>
1374
+ required: false
1375
+ description: |
1376
+ 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.
1377
+
1378
+ 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.
1379
+
1380
+ 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.
1381
+ body: |
1382
+ Each plugin ships with a default resolver:
1383
+
1384
+ | Plugin | Default resolver |
1385
+ | --- | --- |
1386
+ | `@kubb/plugin-ts` | `resolverTs` |
1387
+ | `@kubb/plugin-zod` | `resolverZod` |
1388
+ | `@kubb/plugin-cypress` | `resolverCypress` |
1389
+ | `@kubb/plugin-mcp` | `resolverMcp` |
1390
+ | `@kubb/plugin-client` | `resolverClient` |
1391
+ codeBlock:
1392
+ lang: typescript
1393
+ title: Add an Api prefix to every name
1394
+ code: |
1395
+ import { defineConfig } from 'kubb'
1396
+ import { pluginTs } from '@kubb/plugin-ts'
1397
+
1398
+ export default defineConfig({
1399
+ input: { path: './petStore.yaml' },
1400
+ output: { path: './src/gen' },
1401
+ plugins: [
1402
+ pluginTs({
1403
+ resolver: {
1404
+ resolveName(name) {
1405
+ return `Api${this.default(name, 'function')}`
1406
+ },
1407
+ },
1408
+ }),
1409
+ ],
1410
+ })
1411
+ tip: |
1412
+ Use `resolver` for naming and file-location tweaks. For changing the AST nodes themselves (e.g. stripping descriptions), use `transformer` instead.
1413
+ - name: transformer
1414
+ type: Visitor
1415
+ required: false
1416
+ description: |
1417
+ 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.
1418
+
1419
+ 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.
1420
+ examples:
1421
+ - name: Strip descriptions before printing
1422
+ files:
1423
+ - name: kubb.config.ts
1424
+ lang: typescript
1425
+ twoslash: false
1426
+ code: |
1427
+ import { defineConfig } from 'kubb'
1428
+ import { pluginTs } from '@kubb/plugin-ts'
1429
+
1430
+ export default defineConfig({
1431
+ input: { path: './petStore.yaml' },
1432
+ output: { path: './src/gen' },
1433
+ plugins: [
1434
+ pluginTs({
1435
+ transformer: {
1436
+ schema(node) {
1437
+ return { ...node, description: undefined }
1438
+ },
1439
+ },
1440
+ }),
1441
+ ],
1442
+ })
1443
+ - name: Prefix every operationId
1444
+ files:
1445
+ - name: kubb.config.ts
1446
+ lang: typescript
1447
+ twoslash: false
1448
+ code: |
1449
+ import { defineConfig } from 'kubb'
1450
+ import { pluginTs } from '@kubb/plugin-ts'
1451
+
1452
+ export default defineConfig({
1453
+ input: { path: './petStore.yaml' },
1454
+ output: { path: './src/gen' },
1455
+ plugins: [
1456
+ pluginTs({
1457
+ transformer: {
1458
+ operation(node) {
1459
+ return { ...node, operationId: `api_${node.operationId}` }
1460
+ },
1461
+ },
1462
+ }),
1463
+ ],
1464
+ })
1465
+ tip: |
1466
+ Use `transformer` to rewrite node properties before printing. For changing the names of generated symbols and files, use `resolver` instead.
1467
+ notes:
1468
+ - type: tip
1469
+ body: |
1470
+ Reference: [TanStack Query](https://tanstack.com/query) for cache, retries, and devtools.
1471
+ examples:
1472
+ - name: kubb.config.ts
1473
+ files:
1474
+ - lang: typescript
1475
+ twoslash: false
1476
+ code: |
1477
+ import { defineConfig } from 'kubb'
1478
+ import { pluginTs } from '@kubb/plugin-ts'
1479
+ import { pluginReactQuery } from '@kubb/plugin-react-query'
1480
+
1481
+ export default defineConfig({
1482
+ input: { path: './petStore.yaml' },
1483
+ output: { path: './src/gen' },
1484
+ plugins: [
1485
+ pluginTs(),
1486
+ pluginReactQuery({
1487
+ output: { path: './hooks' },
1488
+ group: {
1489
+ type: 'tag',
1490
+ name: ({ group }) => `${group}Hooks`,
1491
+ },
1492
+ client: { dataReturnType: 'full' },
1493
+ mutation: { methods: ['post', 'put', 'delete'] },
1494
+ infinite: {
1495
+ queryParam: 'next_page',
1496
+ initialPageParam: 0,
1497
+ nextParam: 'pagination.next.cursor',
1498
+ previousParam: ['pagination', 'prev', 'cursor'],
1499
+ },
1500
+ query: {
1501
+ methods: ['get'],
1502
+ importPath: '@tanstack/react-query',
1503
+ },
1504
+ suspense: {},
1505
+ }),
1506
+ ],
1507
+ })