@kubb/plugin-react-query 5.10.0 → 5.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +10 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -4
- package/dist/index.d.ts +1 -4
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/docs/guides/calling-operations.md +9 -7
- package/docs/options.md +38 -20
- package/docs/overview.md +5 -3
- package/kubb-extension.json +8 -5
- package/package.json +6 -6
|
@@ -12,16 +12,18 @@ A query hook takes the operation's grouped request config (`path`, `query`, `hea
|
|
|
12
12
|
```typescript
|
|
13
13
|
import { useGetPetById } from './gen/hooks/useGetPetById'
|
|
14
14
|
|
|
15
|
-
const { data, error, isLoading } = useGetPetById({ path: { petId:
|
|
15
|
+
const { data, error, isLoading } = useGetPetById({ path: { petId: 1 } })
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
+
The examples set [`integerType: 'number'`](https://kubb.dev/extensions/kubb-labs/adapter-oas/options#integertype) on the adapter, so an `int64` id such as `petId` is a `number`. With the default it is a `bigint` and the call takes `1n`.
|
|
19
|
+
|
|
18
20
|
The second argument has two optional groups. `query` takes any TanStack Query option, and `query.client` sets the `QueryClient` to use. The top-level `client` takes request config for this call, such as `baseURL` or `signal`:
|
|
19
21
|
|
|
20
22
|
```typescript
|
|
21
23
|
import { useGetPetById } from './gen/hooks/useGetPetById'
|
|
22
24
|
|
|
23
25
|
useGetPetById(
|
|
24
|
-
{ path: { petId:
|
|
26
|
+
{ path: { petId: 1 } },
|
|
25
27
|
{
|
|
26
28
|
query: { staleTime: 60_000 },
|
|
27
29
|
client: { baseURL: 'https://api.example.com/v1' },
|
|
@@ -39,8 +41,8 @@ import { getPetByIdQueryKey, getPetByIdQueryOptions } from './gen/hooks/useGetPe
|
|
|
39
41
|
|
|
40
42
|
const queryClient = useQueryClient()
|
|
41
43
|
|
|
42
|
-
await queryClient.prefetchQuery(getPetByIdQueryOptions({ path: { petId:
|
|
43
|
-
queryClient.invalidateQueries({ queryKey: getPetByIdQueryKey({ path: { petId:
|
|
44
|
+
await queryClient.prefetchQuery(getPetByIdQueryOptions({ path: { petId: 1 } }))
|
|
45
|
+
queryClient.invalidateQueries({ queryKey: getPetByIdQueryKey({ path: { petId: 1 } }) })
|
|
44
46
|
```
|
|
45
47
|
|
|
46
48
|
## Mutations
|
|
@@ -57,7 +59,7 @@ const { mutate } = useDeletePet({
|
|
|
57
59
|
mutation: { onSuccess: () => queryClient.invalidateQueries() },
|
|
58
60
|
})
|
|
59
61
|
|
|
60
|
-
mutate({ path: { petId:
|
|
62
|
+
mutate({ path: { petId: 1 } })
|
|
61
63
|
```
|
|
62
64
|
|
|
63
65
|
Each mutation also exports a `mutationOptions` factory and a `mutationKey` helper.
|
|
@@ -111,7 +113,7 @@ const { data, fetchNextPage, hasNextPage } = useInfiniteQuery(
|
|
|
111
113
|
|
|
112
114
|
::
|
|
113
115
|
|
|
114
|
-
`nextParam` takes dot notation or an array of keys. Change it to match your API's response. Without `getNextPageParam`, `nextParam` or `cursorParam`, the generated options increment the page number until a page returns an empty array.
|
|
116
|
+
`nextParam` takes dot notation or an array of keys. Change it to match your API's response, and give `initialPageParam` the same type as the query parameter. Without `getNextPageParam`, `nextParam` or `cursorParam`, the generated options increment the page number until a page returns an empty array.
|
|
115
117
|
|
|
116
118
|
## Use suspense
|
|
117
119
|
|
|
@@ -128,7 +130,7 @@ pluginReactQuery({ hooks: true, suspense: {} })
|
|
|
128
130
|
```typescript [usage.ts]
|
|
129
131
|
import { useGetPetByIdSuspense } from './gen/hooks/useGetPetByIdSuspense'
|
|
130
132
|
|
|
131
|
-
const { data } = useGetPetByIdSuspense({ path: { petId:
|
|
133
|
+
const { data } = useGetPetByIdSuspense({ path: { petId: 1 } })
|
|
132
134
|
```
|
|
133
135
|
|
|
134
136
|
::
|
package/docs/options.md
CHANGED
|
@@ -16,7 +16,7 @@ Pass these options to `pluginReactQuery()`. Shared options link to [Shared plugi
|
|
|
16
16
|
| [`queryKey`](#querykey) | Build the `queryKey` for each query hook. | Exported `queryKeyTransformer` |
|
|
17
17
|
| [`mutation`](#mutation) | Configure the mutation hooks. | `{ methods: ['POST', 'PUT', 'PATCH', 'DELETE'], importPath: '@tanstack/react-query' }` |
|
|
18
18
|
| [`mutationKey`](#mutationkey) | Build the `mutationKey` for each mutation hook. | Exported `mutationKeyTransformer` |
|
|
19
|
-
| [`customOptions`](#customoptions) | Route every hook through
|
|
19
|
+
| [`customOptions`](#customoptions) | Route every hook through a custom options function. | None |
|
|
20
20
|
| [`hooks`](#hooks) | Emit `use*` hook functions on top of the factories. | `false` |
|
|
21
21
|
| [`include`](https://kubb.dev/docs/5.x/reference/plugin-options#include) | Keep only operations that match. | None |
|
|
22
22
|
| [`exclude`](https://kubb.dev/docs/5.x/reference/plugin-options#exclude) | Skip operations that match. | `[]` |
|
|
@@ -46,7 +46,7 @@ Calls the operations generated by [`@kubb/plugin-axios`](https://kubb.dev/extens
|
|
|
46
46
|
:::
|
|
47
47
|
|
|
48
48
|
:::field{name="'client'"}
|
|
49
|
-
Calls the operations generated by [`@kubb/plugin-client`](https://kubb.dev/extensions/kubb-labs/plugin-client).
|
|
49
|
+
Calls the operations generated by [`@kubb/plugin-client`](https://kubb.dev/extensions/kubb-labs/plugin-client). The client module must also export the `RequestConfig` and `ResponseErrorConfig` types.
|
|
50
50
|
:::
|
|
51
51
|
|
|
52
52
|
::
|
|
@@ -64,7 +64,7 @@ Shape of the value the generated hooks resolve to.
|
|
|
64
64
|
::field-group
|
|
65
65
|
|
|
66
66
|
:::field{name="'full'"}
|
|
67
|
-
Returns the client's whole `RequestResult` (`{ status, data, error, contentType, headers, request, response }`), so a hook can read the status and the response headers next to `data`. Infinite queries read the page cursor from `lastPage.data`. Needs a client plugin whose own `returnType` is `'full'` or `'plain'
|
|
67
|
+
Returns the client's whole `RequestResult` (`{ status, data, error, contentType, headers, request, response }`), so a hook can read the status and the response headers next to `data`. Infinite queries read the page cursor from `lastPage.data`. Needs a client plugin whose own `returnType` is `'full'` or `'plain'`. With a client `returnType: 'data'`, generation stops with an error.
|
|
68
68
|
:::
|
|
69
69
|
|
|
70
70
|
:::field{name="'data'"}
|
|
@@ -82,6 +82,8 @@ const etag = result?.response.headers.get('ETag')
|
|
|
82
82
|
const pet = result?.data
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
The examples set [`integerType: 'number'`](https://kubb.dev/extensions/kubb-labs/adapter-oas/options#integertype) on the adapter, so an `int64` id such as `petId` is a `number`. With the default it is a `bigint` and the call takes `1n`.
|
|
86
|
+
|
|
85
87
|
### infinite
|
|
86
88
|
|
|
87
89
|
Adds `infiniteQueryOptions` and `useInfiniteQuery` for operations that declare the query parameter named in `infinite.queryParam`. Needs [`hooks: true`](#hooks).
|
|
@@ -134,7 +136,7 @@ Path to the previous-page cursor on the response, in the same dot or array form.
|
|
|
134
136
|
|
|
135
137
|
#### infinite.cursorParam
|
|
136
138
|
|
|
137
|
-
Deprecated
|
|
139
|
+
Deprecated name of a top-level response field that holds the cursor, read for both `getNextPageParam` and `getPreviousPageParam` when set. Dot paths are not supported. Use `nextParam` and `previousParam` instead.
|
|
138
140
|
|
|
139
141
|
| | |
|
|
140
142
|
| -------- | ---------------- |
|
|
@@ -144,13 +146,13 @@ Deprecated path to the cursor field, read for both `getNextPageParam` and `getPr
|
|
|
144
146
|
|
|
145
147
|
#### infinite.getNextPageParam
|
|
146
148
|
|
|
147
|
-
Source code for TanStack Query's `getNextPageParam`, copied into the generated code as written. Use it when the next page must be computed instead of read from a field. Takes precedence over `nextParam` and `cursorParam`.
|
|
149
|
+
Source code for TanStack Query's `getNextPageParam`, copied into the generated code as written. Pass a string or a function. Use it when the next page must be computed instead of read from a field. Takes precedence over `nextParam` and `cursorParam`.
|
|
148
150
|
|
|
149
|
-
| |
|
|
150
|
-
| -------- |
|
|
151
|
-
| Type | `string \| null` |
|
|
152
|
-
| Required | `false`
|
|
153
|
-
| Default | `null`
|
|
151
|
+
| | |
|
|
152
|
+
| -------- | ------------------------------- |
|
|
153
|
+
| Type | `string \| PageParamFn \| null` |
|
|
154
|
+
| Required | `false` |
|
|
155
|
+
| Default | `null` |
|
|
154
156
|
|
|
155
157
|
```typescript
|
|
156
158
|
infinite: {
|
|
@@ -160,17 +162,33 @@ infinite: {
|
|
|
160
162
|
}
|
|
161
163
|
```
|
|
162
164
|
|
|
163
|
-
|
|
165
|
+
`definePageParam` types the page and the page parameter, and returns the function's source. A plain function works too. Only the source is copied, so the function cannot reference variables from the config file.
|
|
166
|
+
|
|
167
|
+
```typescript [kubb.config.ts]
|
|
168
|
+
import { definePageParam, pluginReactQuery } from '@kubb/plugin-react-query'
|
|
169
|
+
|
|
170
|
+
type PetsPage = { hasNext: boolean; number: number }
|
|
171
|
+
|
|
172
|
+
pluginReactQuery({
|
|
173
|
+
hooks: true,
|
|
174
|
+
infinite: {
|
|
175
|
+
queryParam: 'page',
|
|
176
|
+
getNextPageParam: definePageParam<PetsPage, number>((lastPage) => (lastPage.hasNext ? lastPage.number + 1 : undefined)),
|
|
177
|
+
},
|
|
178
|
+
})
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Without `getNextPageParam`, `nextParam` or `cursorParam`, the hook counts pages up from `initialPageParam` until a page is an empty array. Counting works only for array responses, so generation warns for any other response.
|
|
164
182
|
|
|
165
183
|
#### infinite.getPreviousPageParam
|
|
166
184
|
|
|
167
|
-
Source code for TanStack Query's `getPreviousPageParam`, copied into the generated code as written. Takes precedence over `previousParam` and `cursorParam`.
|
|
185
|
+
Source code for TanStack Query's `getPreviousPageParam`, copied into the generated code as written. Takes a string or a function, like `getNextPageParam`. Takes precedence over `previousParam` and `cursorParam`.
|
|
168
186
|
|
|
169
|
-
| |
|
|
170
|
-
| -------- |
|
|
171
|
-
| Type | `string \| null` |
|
|
172
|
-
| Required | `false`
|
|
173
|
-
| Default | `null`
|
|
187
|
+
| | |
|
|
188
|
+
| -------- | ------------------------------- |
|
|
189
|
+
| Type | `string \| PageParamFn \| null` |
|
|
190
|
+
| Required | `false` |
|
|
191
|
+
| Default | `null` |
|
|
174
192
|
|
|
175
193
|
### suspense
|
|
176
194
|
|
|
@@ -288,7 +306,7 @@ mutationKey: (props) => [JSON.stringify(props.node.operationId), ...mutationKeyT
|
|
|
288
306
|
|
|
289
307
|
### customOptions
|
|
290
308
|
|
|
291
|
-
Routes every hook through
|
|
309
|
+
Routes every hook through a custom function that returns extra options such as `onSuccess` or `select`. Also emits a `HookOptions` type that maps each hook name to its options type. Needs [`hooks: true`](#hooks).
|
|
292
310
|
|
|
293
311
|
| | |
|
|
294
312
|
| -------- | --------------------------------------- |
|
|
@@ -297,7 +315,7 @@ Routes every hook through your own function that returns extra options such as `
|
|
|
297
315
|
|
|
298
316
|
#### customOptions.importPath
|
|
299
317
|
|
|
300
|
-
Module of
|
|
318
|
+
Module of the custom-options hook, imported as a named import. The path resolves from the generated hooks folder. When the module does not exist yet, Kubb writes a starter file there, but only when `importPath` stays inside the plugin's output folder. The starter file imports `HookOptions` from the generated `HookOptions.ts` file. A module outside the output folder is never created.
|
|
301
319
|
|
|
302
320
|
| | |
|
|
303
321
|
| -------- | ---------------------------------- |
|
|
@@ -306,7 +324,7 @@ Module of your custom-options hook, imported as a named import. It resolves from
|
|
|
306
324
|
|
|
307
325
|
#### customOptions.name
|
|
308
326
|
|
|
309
|
-
Exported name of
|
|
327
|
+
Exported name of the custom-options hook.
|
|
310
328
|
|
|
311
329
|
| | |
|
|
312
330
|
| -------- | ------------------------ |
|
package/docs/overview.md
CHANGED
|
@@ -12,12 +12,12 @@ The plugin ships no HTTP client. Each helper calls a function generated by a cli
|
|
|
12
12
|
|
|
13
13
|
## Options worth knowing
|
|
14
14
|
|
|
15
|
-
- `client` picks `fetch` or `
|
|
15
|
+
- `client` picks `fetch`, `axios`, or `client` when more than one client plugin is registered. A single client plugin is detected automatically.
|
|
16
16
|
- `hooks` adds the `use*` hooks on top of the option factories.
|
|
17
17
|
- `suspense: {}` adds `useSuspenseQuery` hooks. It needs `hooks: true`.
|
|
18
18
|
- `infinite` adds `useInfiniteQuery` hooks for pagination. It needs `hooks: true` and covers the operations that take the query parameter named in `infinite.queryParam`, which defaults to `'id'`.
|
|
19
|
-
- `query` and `mutation` set which HTTP methods become queries (`GET`) and mutations (`POST`, `PUT`, `PATCH`, `DELETE`). They also set where the TanStack Query runtime is imported from. Set either to `false` to skip those helpers.
|
|
20
|
-
- `customOptions: { importPath }` routes every hook through your own function that returns extra options. It needs `hooks: true`.
|
|
19
|
+
- `query` and `mutation` set which HTTP methods become queries (`GET`, `QUERY`) and mutations (`POST`, `PUT`, `PATCH`, `DELETE`). They also set where the TanStack Query runtime is imported from. Set either to `false` to skip those helpers.
|
|
20
|
+
- `customOptions: { importPath }` routes every hook through your own function that returns extra options. It needs `hooks: true`. `importPath` resolves from the generated hooks folder. Kubb creates a starter file there when none exists and the path stays inside the plugin's output folder.
|
|
21
21
|
|
|
22
22
|
See [Options](https://kubb.dev/extensions/kubb-labs/plugin-react-query/options) for every option and its default.
|
|
23
23
|
|
|
@@ -27,6 +27,7 @@ The config below generates `useQuery` and `useMutation` hooks that call the Fetc
|
|
|
27
27
|
|
|
28
28
|
```typescript [kubb.config.ts]
|
|
29
29
|
import { defineConfig } from 'kubb/config'
|
|
30
|
+
import { adapterOas } from '@kubb/adapter-oas'
|
|
30
31
|
import { pluginTs } from '@kubb/plugin-ts'
|
|
31
32
|
import { pluginFetch } from '@kubb/plugin-fetch'
|
|
32
33
|
import { pluginReactQuery } from '@kubb/plugin-react-query'
|
|
@@ -34,6 +35,7 @@ import { pluginReactQuery } from '@kubb/plugin-react-query'
|
|
|
34
35
|
export default defineConfig({
|
|
35
36
|
input: './petStore.yaml',
|
|
36
37
|
output: { path: './src/gen' },
|
|
38
|
+
adapter: adapterOas({ integerType: 'number' }),
|
|
37
39
|
plugins: [
|
|
38
40
|
pluginTs(),
|
|
39
41
|
pluginFetch(),
|
package/kubb-extension.json
CHANGED
|
@@ -17,8 +17,7 @@
|
|
|
17
17
|
"github": "stijnvanhulle"
|
|
18
18
|
}
|
|
19
19
|
],
|
|
20
|
-
"
|
|
21
|
-
"optionalDependencies": ["plugin-zod"],
|
|
20
|
+
"optionalDependencies": ["plugin-ts", "plugin-zod"],
|
|
22
21
|
"compatibility": {
|
|
23
22
|
"kubb": ">=5.18.4",
|
|
24
23
|
"node": ">=22.22"
|
|
@@ -97,7 +96,7 @@
|
|
|
97
96
|
"key": "client",
|
|
98
97
|
"path": ["client"],
|
|
99
98
|
"label": "Client",
|
|
100
|
-
"description": "HTTP client the generated hooks call. Only needed to disambiguate when
|
|
99
|
+
"description": "HTTP client the generated hooks call. Only needed to disambiguate when more than one of plugin-fetch, plugin-axios and plugin-client is enabled",
|
|
101
100
|
"type": "select",
|
|
102
101
|
"default": null,
|
|
103
102
|
"choices": [
|
|
@@ -112,6 +111,10 @@
|
|
|
112
111
|
{
|
|
113
112
|
"label": "axios",
|
|
114
113
|
"value": "axios"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"label": "client",
|
|
117
|
+
"value": "client"
|
|
115
118
|
}
|
|
116
119
|
]
|
|
117
120
|
},
|
|
@@ -192,9 +195,9 @@
|
|
|
192
195
|
"key": "queryMethods",
|
|
193
196
|
"path": ["query", "methods"],
|
|
194
197
|
"label": "Query Methods",
|
|
195
|
-
"description": "Comma-separated HTTP methods treated as queries (default: GET)",
|
|
198
|
+
"description": "Comma-separated HTTP methods treated as queries (default: GET, QUERY)",
|
|
196
199
|
"type": "list",
|
|
197
|
-
"default": ["GET"],
|
|
200
|
+
"default": ["GET", "QUERY"],
|
|
198
201
|
"itemCase": "upper"
|
|
199
202
|
},
|
|
200
203
|
{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-react-query",
|
|
3
|
-
"version": "5.10.
|
|
3
|
+
"version": "5.10.1",
|
|
4
4
|
"description": "Generate type-safe TanStack Query (React Query) hooks with Kubb. Covers useQuery, useMutation, useInfiniteQuery, and queryOptions with full TypeScript support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-generation",
|
|
@@ -81,17 +81,17 @@
|
|
|
81
81
|
"registry": "https://registry.npmjs.org/"
|
|
82
82
|
},
|
|
83
83
|
"dependencies": {
|
|
84
|
-
"@kubb/kit": "^5.18.
|
|
85
|
-
"@kubb/plugin-ts": "^5.5.
|
|
86
|
-
"@kubb/renderer-jsx": "^5.18.
|
|
84
|
+
"@kubb/kit": "^5.18.8",
|
|
85
|
+
"@kubb/plugin-ts": "^5.5.1",
|
|
86
|
+
"@kubb/renderer-jsx": "^5.18.8"
|
|
87
87
|
},
|
|
88
88
|
"devDependencies": {
|
|
89
89
|
"@internals/client": "0.0.0",
|
|
90
90
|
"@internals/shared": "0.0.0",
|
|
91
91
|
"@internals/tanstack-query": "0.0.0",
|
|
92
92
|
"@internals/utils": "0.0.0",
|
|
93
|
-
"@kubb/plugin-client": "6.0.
|
|
94
|
-
"@tanstack/react-query": "^5.
|
|
93
|
+
"@kubb/plugin-client": "6.0.1",
|
|
94
|
+
"@tanstack/react-query": "^5.107.1"
|
|
95
95
|
},
|
|
96
96
|
"peerDependencies": {
|
|
97
97
|
"@tanstack/react-query": "^5.82.0"
|