Skip to content

Commit

Permalink
Fix/disable use query generation (#1374)
Browse files Browse the repository at this point in the history
* fix: allow disabling `useQuery`

* fix: InfiniteData for react-query v5(disabled for v4)

* chore: update examples

* chore: upgrade packages

* chore: update test cases
  • Loading branch information
stijnvanhulle authored Nov 6, 2024
1 parent e246cc0 commit 487d135
Show file tree
Hide file tree
Showing 60 changed files with 353 additions and 417 deletions.
6 changes: 6 additions & 0 deletions .changeset/serious-bottles-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kubb/plugin-react-query": patch
"@kubb/plugin-vue-query": patch
---

InfiniteData for react-query v5(disabled for v4)
5 changes: 5 additions & 0 deletions .changeset/stupid-trees-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-react-query": patch
---

allow disabling `useQuery`
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const knowledgeBaseSidebar = [
{
text: 'Kubb Plugins',
link: '/knowledge-base/plugins/',
}
},
// {
// text: 'Plugins',
// collapsed: false,
Expand Down
2 changes: 1 addition & 1 deletion docs/blog/v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ We’re excited to announce the release of Kubb v3, a major update that brings s
In this post, we’ll dive into the key features of Kubb v3 and explore what’s new.

## What is Kubb?
For those unfamiliar with Kubb, it’s a flexible framework built to automate code generation based on Swagger/OpenApi spec file. Kubb simplifies your workflow by creating API clients, generating type-safe code, Zod schema's, React-Query code, ...
For those unfamiliar with Kubb, it’s a flexible framework built to automate code generation based on Swagger/OpenAPI spec file. Kubb simplifies your workflow by creating API clients, generating type-safe code, Zod schema's, React-Query code, ...
By integrating with tools like OpenAPI, Kubb ensures that your code remains aligned with your API specifications, reducing the risk of errors and improving maintainability.
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ title: Changelog

# Changelog

## 3.0.1
- [`plugin-react-query`](/plugins/plugin-react-query/): allow disabling `useQuery`
- [`plugin-react-query`](/plugins/plugin-react-query/): use of `InfiniteData` TypeScript helper for infiniteQueries
- [`plugin-vue-query`](/plugins/plugin-vue-query/): use of `InfiniteData` TypeScript helper for infiniteQueries

## 3.0.0-beta.12
- [`plugin-react-query`](/plugins/plugin-react-query/): allow to disable the generation of useQuery or createQuery hooks.
- [`plugin-svelte-query`](/plugins/plugin-svelte-query/): allow to disable the generation of useQuery or createQuery hooks.
Expand Down
8 changes: 4 additions & 4 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"@kubb/plugin-ts": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/react-query": "^5.59.19",
"@tanstack/solid-query": "^5.59.17",
"@tanstack/svelte-query": "^5.59.17",
"@tanstack/vue-query": "^5.59.17",
"@tanstack/react-query": "^5.59.20",
"@tanstack/solid-query": "^5.59.20",
"@tanstack/svelte-query": "^5.59.20",
"@tanstack/vue-query": "^5.59.20",
"axios": "^1.7.7",
"msw": "^2.6.0",
"react": "^18.3.1",
Expand Down
10 changes: 5 additions & 5 deletions examples/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"@kubb/plugin-ts": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/query-core": "^5.59.17",
"@tanstack/react-query": "^5.59.19",
"@tanstack/solid-query": "^5.59.17",
"@tanstack/svelte-query": "^5.59.17",
"@tanstack/vue-query": "^5.59.17",
"@tanstack/query-core": "^5.59.20",
"@tanstack/react-query": "^5.59.20",
"@tanstack/solid-query": "^5.59.20",
"@tanstack/svelte-query": "^5.59.20",
"@tanstack/vue-query": "^5.59.20",
"axios": "^1.7.7",
"msw": "^2.6.0",
"react": "^18.3.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type { QueryKey, InfiniteQueryObserverOptions, UseInfiniteQueryResult } from '../../../../tanstack-query-hook.ts'
import type { InfiniteData, QueryKey, InfiniteQueryObserverOptions, UseInfiniteQueryResult } from '../../../../tanstack-query-hook.ts'
import type {
FindPetsByTagsQueryResponse,
FindPetsByTagsQueryParams,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function findPetsByTagsInfiniteQueryOptions(
* @link /pet/findByTags
*/
export function useFindPetsByTagsInfinite<
TData = ResponseConfig<FindPetsByTagsQueryResponse>,
TData = InfiniteData<ResponseConfig<FindPetsByTagsQueryResponse>>,
TQueryData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryKey extends QueryKey = FindPetsByTagsInfiniteQueryKey,
>(
Expand Down
4 changes: 4 additions & 0 deletions examples/react-query/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export const config = {
importPath: '@tanstack/react-query',
methods: ['post'],
},
mutation: {
importPath: '@tanstack/react-query',
methods: ['put', 'delete'],
},
pathParamsType: 'inline',
},
},
Expand Down
4 changes: 2 additions & 2 deletions examples/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"@kubb/plugin-react-query": "workspace:*",
"@kubb/plugin-ts": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/react-query": "^5.59.19",
"@tanstack/react-query-devtools": "^5.59.19",
"@tanstack/react-query": "^5.59.20",
"@tanstack/react-query-devtools": "^5.59.20",
"axios": "^1.7.7",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
2 changes: 2 additions & 0 deletions examples/react-query/src/gen/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type { FindPetsByTagsQueryKey } from './useFindPetsByTagsHook.ts'
export type { FindPetsByTagsInfiniteQueryKey } from './useFindPetsByTagsInfiniteHook.ts'
export type { FindPetsByTagsSuspenseQueryKey } from './useFindPetsByTagsSuspenseHook.ts'
export type { GetInventoryQueryKey } from './useGetInventoryHook.ts'
export type { GetInventorySuspenseQueryKey } from './useGetInventorySuspenseHook.ts'
export type { GetOrderByIdQueryKey } from './useGetOrderByIdHook.ts'
export type { GetOrderByIdSuspenseQueryKey } from './useGetOrderByIdSuspenseHook.ts'
export type { GetPetByIdQueryKey } from './useGetPetByIdHook.ts'
Expand Down Expand Up @@ -43,6 +44,7 @@ export { findPetsByTagsQueryKey, findPetsByTagsQueryOptionsHook, useFindPetsByTa
export { findPetsByTagsInfiniteQueryKey, findPetsByTagsInfiniteQueryOptionsHook, useFindPetsByTagsInfiniteHook } from './useFindPetsByTagsInfiniteHook.ts'
export { findPetsByTagsSuspenseQueryKey, findPetsByTagsSuspenseQueryOptionsHook, useFindPetsByTagsSuspenseHook } from './useFindPetsByTagsSuspenseHook.ts'
export { getInventoryQueryKey, getInventoryQueryOptionsHook } from './useGetInventoryHook.ts'
export { getInventorySuspenseQueryKey, getInventorySuspenseQueryOptionsHook, useGetInventorySuspenseHook } from './useGetInventorySuspenseHook.ts'
export { getOrderByIdQueryKey, getOrderByIdQueryOptionsHook, useGetOrderByIdHook } from './useGetOrderByIdHook.ts'
export { getOrderByIdSuspenseQueryKey, getOrderByIdSuspenseQueryOptionsHook, useGetOrderByIdSuspenseHook } from './useGetOrderByIdSuspenseHook.ts'
export { getPetByIdQueryKey, getPetByIdQueryOptionsHook, useGetPetByIdHook } from './useGetPetByIdHook.ts'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import client from '@kubb/plugin-client/client'
import type { FindPetsByTagsQueryResponse, FindPetsByTagsQueryParams, FindPetsByTags400 } from '../models/FindPetsByTags.ts'
import type { RequestConfig, ResponseConfig } from '@kubb/plugin-client/client'
import type { QueryKey, InfiniteQueryObserverOptions, UseInfiniteQueryResult } from '@tanstack/react-query'
import type { InfiniteData, QueryKey, InfiniteQueryObserverOptions, UseInfiniteQueryResult } from '@tanstack/react-query'
import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/react-query'

export const findPetsByTagsInfiniteQueryKey = (params?: FindPetsByTagsQueryParams) => ['v5', { url: '/pet/findByTags' }, ...(params ? [params] : [])] as const
Expand Down Expand Up @@ -41,7 +41,7 @@ export function findPetsByTagsInfiniteQueryOptionsHook(params?: FindPetsByTagsQu
* @link /pet/findByTags
*/
export function useFindPetsByTagsInfiniteHook<
TData = ResponseConfig<FindPetsByTagsQueryResponse>,
TData = InfiniteData<ResponseConfig<FindPetsByTagsQueryResponse>>,
TQueryData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryKey extends QueryKey = FindPetsByTagsInfiniteQueryKey,
>(
Expand Down
58 changes: 58 additions & 0 deletions examples/react-query/src/gen/hooks/useGetInventorySuspenseHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import client from '@kubb/plugin-client/client'
import type { GetInventoryQueryResponse } from '../models/GetInventory.ts'
import type { RequestConfig } from '@kubb/plugin-client/client'
import type { QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query'
import { queryOptions, useSuspenseQuery } from '@tanstack/react-query'

export const getInventorySuspenseQueryKey = () => ['v5', { url: '/store/inventory' }] as const

export type GetInventorySuspenseQueryKey = ReturnType<typeof getInventorySuspenseQueryKey>

/**
* @description Returns a map of status codes to quantities
* @summary Returns pet inventories by status
* @link /store/inventory
*/
async function getInventory(config: Partial<RequestConfig> = {}) {
const res = await client<GetInventoryQueryResponse, Error, unknown>({ method: 'GET', url: '/store/inventory', ...config })
return res.data
}

export function getInventorySuspenseQueryOptionsHook(config: Partial<RequestConfig> = {}) {
const queryKey = getInventorySuspenseQueryKey()
return queryOptions({
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return getInventory(config)
},
})
}

/**
* @description Returns a map of status codes to quantities
* @summary Returns pet inventories by status
* @link /store/inventory
*/
export function useGetInventorySuspenseHook<
TData = GetInventoryQueryResponse,
TQueryData = GetInventoryQueryResponse,
TQueryKey extends QueryKey = GetInventorySuspenseQueryKey,
>(
options: {
query?: Partial<UseSuspenseQueryOptions<GetInventoryQueryResponse, Error, TData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
const { query: queryOptions, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? getInventorySuspenseQueryKey()
const query = useSuspenseQuery({
...(getInventorySuspenseQueryOptionsHook(config) as unknown as UseSuspenseQueryOptions),
queryKey,
...(queryOptions as unknown as Omit<UseSuspenseQueryOptions, 'queryKey'>),
}) as UseSuspenseQueryResult<TData, Error> & {
queryKey: TQueryKey
}
query.queryKey = queryKey as TQueryKey
return query
}
2 changes: 2 additions & 0 deletions examples/react-query/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type { FindPetsByTagsQueryKey } from './hooks/useFindPetsByTagsHook.ts'
export type { FindPetsByTagsInfiniteQueryKey } from './hooks/useFindPetsByTagsInfiniteHook.ts'
export type { FindPetsByTagsSuspenseQueryKey } from './hooks/useFindPetsByTagsSuspenseHook.ts'
export type { GetInventoryQueryKey } from './hooks/useGetInventoryHook.ts'
export type { GetInventorySuspenseQueryKey } from './hooks/useGetInventorySuspenseHook.ts'
export type { GetOrderByIdQueryKey } from './hooks/useGetOrderByIdHook.ts'
export type { GetOrderByIdSuspenseQueryKey } from './hooks/useGetOrderByIdSuspenseHook.ts'
export type { GetPetByIdQueryKey } from './hooks/useGetPetByIdHook.ts'
Expand Down Expand Up @@ -134,6 +135,7 @@ export { findPetsByTagsQueryKey, findPetsByTagsQueryOptionsHook, useFindPetsByTa
export { findPetsByTagsInfiniteQueryKey, findPetsByTagsInfiniteQueryOptionsHook, useFindPetsByTagsInfiniteHook } from './hooks/useFindPetsByTagsInfiniteHook.ts'
export { findPetsByTagsSuspenseQueryKey, findPetsByTagsSuspenseQueryOptionsHook, useFindPetsByTagsSuspenseHook } from './hooks/useFindPetsByTagsSuspenseHook.ts'
export { getInventoryQueryKey, getInventoryQueryOptionsHook } from './hooks/useGetInventoryHook.ts'
export { getInventorySuspenseQueryKey, getInventorySuspenseQueryOptionsHook, useGetInventorySuspenseHook } from './hooks/useGetInventorySuspenseHook.ts'
export { getOrderByIdQueryKey, getOrderByIdQueryOptionsHook, useGetOrderByIdHook } from './hooks/useGetOrderByIdHook.ts'
export { getOrderByIdSuspenseQueryKey, getOrderByIdSuspenseQueryOptionsHook, useGetOrderByIdSuspenseHook } from './hooks/useGetOrderByIdSuspenseHook.ts'
export { getPetByIdQueryKey, getPetByIdQueryOptionsHook, useGetPetByIdHook } from './hooks/useGetPetByIdHook.ts'
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-single/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@kubb/plugin-redoc": "workspace:*",
"@kubb/plugin-ts": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@tanstack/react-query": "^5.59.19",
"@tanstack/react-query": "^5.59.20",
"axios": "^1.7.7",
"react": "^18.3.1",
"tsup": "^8.3.5",
Expand Down
1 change: 1 addition & 0 deletions examples/simple-single/src/gen2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ export const machinesListQueryParamsSchema = z
.object({
include_deleted: z.boolean().describe('Include deleted machines').optional(),
region: z.string().describe('Region filter').optional(),
state: z.string().describe('comma separated list of states to filter (created, started, stopped, suspended)').optional(),
summary: z.boolean().describe('Only return summary info about machines (omit config, checks, events, host_status, nonce, etc.)').optional(),
})
.optional()
Expand Down
1 change: 1 addition & 0 deletions examples/solid-query/kubb.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default defineConfig({
output: {
path: './hooks',
},
query: false,
override: [
{
type: 'operationId',
Expand Down
2 changes: 1 addition & 1 deletion examples/solid-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@kubb/plugin-solid-query": "workspace:*",
"@kubb/plugin-ts": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@tanstack/solid-query": "^5.59.17",
"@tanstack/solid-query": "^5.59.20",
"axios": "^1.7.7",
"solid-js": "^1.9.3",
"tsup": "^8.3.5"
Expand Down
33 changes: 1 addition & 32 deletions examples/solid-query/src/gen/hooks/createFindPetsByStatus.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import client from '@kubb/plugin-client/client'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../models/FindPetsByStatus.ts'
import type { RequestConfig } from '@kubb/plugin-client/client'
import type { QueryKey, CreateBaseQueryOptions, CreateQueryResult } from '@tanstack/solid-query'
import { queryOptions, createQuery } from '@tanstack/solid-query'
import { queryOptions } from '@tanstack/solid-query'

export const findPetsByStatusQueryKey = (params?: FindPetsByStatusQueryParams) => [{ url: '/pet/findByStatus' }, ...(params ? [params] : [])] as const

Expand All @@ -28,33 +27,3 @@ export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParam
},
})
}

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
* @link /pet/findByStatus
*/
export function createFindPetsByStatus<
TData = FindPetsByStatusQueryResponse,
TQueryData = FindPetsByStatusQueryResponse,
TQueryKey extends QueryKey = FindPetsByStatusQueryKey,
>(
params?: FindPetsByStatusQueryParams,
options: {
query?: Partial<CreateBaseQueryOptions<FindPetsByStatusQueryResponse, FindPetsByStatus400, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
const { query: queryOptions, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey(params)
const query = createQuery(() => ({
...(findPetsByStatusQueryOptions(params, config) as unknown as CreateBaseQueryOptions),
queryKey,
initialData: null,
...(queryOptions as unknown as Omit<CreateBaseQueryOptions, 'queryKey'>),
})) as CreateQueryResult<TData, FindPetsByStatus400> & {
queryKey: TQueryKey
}
query.queryKey = queryKey as TQueryKey
return query
}
35 changes: 2 additions & 33 deletions examples/solid-query/src/gen/hooks/createFindPetsByTags.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import client from '@kubb/plugin-client/client'
import type { FindPetsByTagsQueryResponse, FindPetsByTagsQueryParams, FindPetsByTags400 } from '../models/FindPetsByTags.ts'
import type { RequestConfig, ResponseConfig } from '@kubb/plugin-client/client'
import type { QueryKey, CreateBaseQueryOptions, CreateQueryResult } from '@tanstack/solid-query'
import { queryOptions, createQuery } from '@tanstack/solid-query'
import type { RequestConfig } from '@kubb/plugin-client/client'
import { queryOptions } from '@tanstack/solid-query'

export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: '/pet/findByTags' }, ...(params ? [params] : [])] as const

Expand All @@ -28,33 +27,3 @@ export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, c
},
})
}

/**
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @summary Finds Pets by tags
* @link /pet/findByTags
*/
export function createFindPetsByTags<
TData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryKey extends QueryKey = FindPetsByTagsQueryKey,
>(
params?: FindPetsByTagsQueryParams,
options: {
query?: Partial<CreateBaseQueryOptions<ResponseConfig<FindPetsByTagsQueryResponse>, FindPetsByTags400, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
const { query: queryOptions, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params)
const query = createQuery(() => ({
...(findPetsByTagsQueryOptions(params, config) as unknown as CreateBaseQueryOptions),
queryKey,
initialData: null,
...(queryOptions as unknown as Omit<CreateBaseQueryOptions, 'queryKey'>),
})) as CreateQueryResult<TData, FindPetsByTags400> & {
queryKey: TQueryKey
}
query.queryKey = queryKey as TQueryKey
return query
}
32 changes: 1 addition & 31 deletions examples/solid-query/src/gen/hooks/createGetInventory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import client from '@kubb/plugin-client/client'
import type { GetInventoryQueryResponse } from '../models/GetInventory.ts'
import type { RequestConfig } from '@kubb/plugin-client/client'
import type { QueryKey, CreateBaseQueryOptions, CreateQueryResult } from '@tanstack/solid-query'
import { queryOptions, createQuery } from '@tanstack/solid-query'
import { queryOptions } from '@tanstack/solid-query'

export const getInventoryQueryKey = () => [{ url: '/store/inventory' }] as const

Expand All @@ -28,32 +27,3 @@ export function getInventoryQueryOptions(config: Partial<RequestConfig> = {}) {
},
})
}

/**
* @description Returns a map of status codes to quantities
* @summary Returns pet inventories by status
* @link /store/inventory
*/
export function createGetInventory<
TData = GetInventoryQueryResponse,
TQueryData = GetInventoryQueryResponse,
TQueryKey extends QueryKey = GetInventoryQueryKey,
>(
options: {
query?: Partial<CreateBaseQueryOptions<GetInventoryQueryResponse, Error, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
const { query: queryOptions, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? getInventoryQueryKey()
const query = createQuery(() => ({
...(getInventoryQueryOptions(config) as unknown as CreateBaseQueryOptions),
queryKey,
initialData: null,
...(queryOptions as unknown as Omit<CreateBaseQueryOptions, 'queryKey'>),
})) as CreateQueryResult<TData, Error> & {
queryKey: TQueryKey
}
query.queryKey = queryKey as TQueryKey
return query
}
Loading

0 comments on commit 487d135

Please sign in to comment.