Skip to content

Commit

Permalink
fix: Correct use of variables when a path/params contains _ or - (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle authored Nov 15, 2024
1 parent 0e6e2cb commit b634bc9
Show file tree
Hide file tree
Showing 63 changed files with 703 additions and 329 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-pets-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/oas": patch
---

Correct use of variables when a path/params contains \_ or -
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ title: Changelog
---

# Changelog

## 3.0.6
-[`plugin-oas`](/plugins/plugin-oas/): Correct use of variables when a path/params contains _ or -

## 3.0.5
- [`react`](/helpers/react//): Better error logging + wider range for `@kubb/react` peerDependency

Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"serve": "vitepress serve"
},
"dependencies": {
"@shikijs/vitepress-twoslash": "^1.22.2",
"@shikijs/vitepress-twoslash": "^1.23.0",
"mermaid": "^11.4.0",
"sitemap": "^8.0.0",
"vitepress": "^1.5.0",
Expand Down
2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@kubb/plugin-ts": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/react-query": "^5.59.20",
"@tanstack/react-query": "^5.60.2",
"@tanstack/solid-query": "^5.59.20",
"@tanstack/svelte-query": "^5.59.20",
"@tanstack/vue-query": "^5.59.20",
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/query-core": "^5.59.20",
"@tanstack/react-query": "^5.59.20",
"@tanstack/react-query": "^5.60.2",
"@tanstack/solid-query": "^5.59.20",
"@tanstack/svelte-query": "^5.59.20",
"@tanstack/vue-query": "^5.59.20",
Expand Down
27 changes: 16 additions & 11 deletions examples/advanced/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,31 @@ paths:
- petstore_auth:
- write:pets
- read:pets
/pet/findByStatus:
/pet/findByStatus/{step_id}:
get:
tags:
- pet
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPetsByStatus
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: false
explode: true
- name: step_id
in: path
required: true
schema:
type: string
default: available
enum:
- available
- pending
- sold
# - name: status
# in: query
# description: Status values that need to be considered for filter
# required: false
# explode: true
# schema:
# type: string
# default: available
# enum:
# - available
# - pending
# - sold
responses:
"200":
description: successful operation
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/clients/axios/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const operations = {
method: 'post',
},
findPetsByStatus: {
path: '/pet/findByStatus',
path: '/pet/findByStatus/:step_id',
method: 'get',
},
findPetsByTags: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import client from '../../../../axios-client.ts'
import type { RequestConfig } from '../../../../axios-client.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusPathParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
* @link /pet/findByStatus
* @link /pet/findByStatus/:step_id
*/
export async function findPetsByStatus(
{
params,
stepId,
}: {
params?: FindPetsByStatusQueryParams
stepId: FindPetsByStatusPathParams['step_id']
},
config: Partial<RequestConfig> = {},
) {
const res = await client<FindPetsByStatusQueryResponse, FindPetsByStatus400, unknown>({
method: 'GET',
url: '/pet/findByStatus',
url: `/pet/findByStatus/${stepId}`,
baseURL: 'https://petstore3.swagger.io/api/v3',
params,
...config,
})
return res
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusPathParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import { queryOptions, useQuery } from '../../../../tanstack-query-hook.ts'
import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts'

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

export type FindPetsByStatusQueryKey = ReturnType<typeof findPetsByStatusQueryKey>

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
* @link /pet/findByStatus
* @link /pet/findByStatus/:step_id
*/
async function findPetsByStatus(
{
params,
stepId,
}: {
params?: FindPetsByStatusQueryParams
stepId: FindPetsByStatusPathParams['step_id']
},
config: Partial<RequestConfig> = {},
) {
const res = await client<FindPetsByStatusQueryResponse, FindPetsByStatus400, unknown>({ method: 'GET', url: '/pet/findByStatus', params, ...config })
const res = await client<FindPetsByStatusQueryResponse, FindPetsByStatus400, unknown>({ method: 'GET', url: `/pet/findByStatus/${stepId}`, ...config })
return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) }
}

export function findPetsByStatusQueryOptions(
{
params,
stepId,
}: {
params?: FindPetsByStatusQueryParams
stepId: FindPetsByStatusPathParams['step_id']
},
config: Partial<RequestConfig> = {},
) {
const queryKey = findPetsByStatusQueryKey(params)
const queryKey = findPetsByStatusQueryKey({ stepId })
return queryOptions({
enabled: !!stepId,
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return findPetsByStatus({ params }, config)
return findPetsByStatus({ stepId }, config)
},
})
}

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
* @link /pet/findByStatus
* @link /pet/findByStatus/:step_id
*/
export function useFindPetsByStatus<
TData = ResponseConfig<FindPetsByStatusQueryResponse>,
TQueryData = ResponseConfig<FindPetsByStatusQueryResponse>,
TQueryKey extends QueryKey = FindPetsByStatusQueryKey,
>(
{
params,
stepId,
}: {
params?: FindPetsByStatusQueryParams
stepId: FindPetsByStatusPathParams['step_id']
},
options: {
query?: Partial<QueryObserverOptions<ResponseConfig<FindPetsByStatusQueryResponse>, FindPetsByStatus400, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
const { query: queryOptions, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey(params)
const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey({ stepId })
const query = useQuery({
...(findPetsByStatusQueryOptions({ params }, config) as unknown as QueryObserverOptions),
...(findPetsByStatusQueryOptions({ stepId }, config) as unknown as QueryObserverOptions),
queryKey,
...(queryOptions as unknown as Omit<QueryObserverOptions, 'queryKey'>),
}) as UseQueryResult<TData, FindPetsByStatus400> & {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
import client from '../../../../swr-client.ts'
import useSWR from 'swr'
import type { RequestConfig } from '../../../../swr-client.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusPathParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts'

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

export type FindPetsByStatusQueryKeySWR = ReturnType<typeof findPetsByStatusQueryKeySWR>

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
* @link /pet/findByStatus
* @link /pet/findByStatus/:step_id
*/
async function findPetsByStatusSWR(
{
params,
stepId,
}: {
params?: FindPetsByStatusQueryParams
stepId: FindPetsByStatusPathParams['step_id']
},
config: Partial<RequestConfig> = {},
) {
const res = await client<FindPetsByStatusQueryResponse, FindPetsByStatus400, unknown>({
method: 'GET',
url: '/pet/findByStatus',
url: `/pet/findByStatus/${stepId}`,
baseURL: 'https://petstore3.swagger.io/api/v3',
params,
...config,
})
return findPetsByStatusQueryResponseSchema.parse(res.data)
}

export function findPetsByStatusQueryOptionsSWR(
{
params,
stepId,
}: {
params?: FindPetsByStatusQueryParams
stepId: FindPetsByStatusPathParams['step_id']
},
config: Partial<RequestConfig> = {},
) {
return {
fetcher: async () => {
return findPetsByStatusSWR({ params }, config)
return findPetsByStatusSWR({ stepId }, config)
},
}
}

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
* @link /pet/findByStatus
* @link /pet/findByStatus/:step_id
*/
export function useFindPetsByStatusSWR(
{
params,
stepId,
}: {
params?: FindPetsByStatusQueryParams
stepId: FindPetsByStatusPathParams['step_id']
},
options: {
query?: Parameters<typeof useSWR<FindPetsByStatusQueryResponse, FindPetsByStatus400, FindPetsByStatusQueryKeySWR | null, any>>[2]
Expand All @@ -64,9 +67,9 @@ export function useFindPetsByStatusSWR(
} = {},
) {
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
const queryKey = findPetsByStatusQueryKeySWR(params)
const queryKey = findPetsByStatusQueryKeySWR({ stepId })
return useSWR<FindPetsByStatusQueryResponse, FindPetsByStatus400, FindPetsByStatusQueryKeySWR | null>(shouldFetch ? queryKey : null, {
...findPetsByStatusQueryOptionsSWR({ params }, config),
...findPetsByStatusQueryOptionsSWR({ stepId }, config),
...queryOptions,
})
}
Loading

0 comments on commit b634bc9

Please sign in to comment.