Skip to content

Commit

Permalink
Support mutate with params (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle authored Mar 12, 2024
1 parent 1979d0b commit efa7c75
Show file tree
Hide file tree
Showing 84 changed files with 2,115 additions and 518 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-starfishes-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-tanstack-query": minor
---

use of `mutate.variablesType` to override mutation behaviour(params in the generated hook or mutate function with extra params)
1 change: 1 addition & 0 deletions configs/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineConfig({
'**/packages/swagger-client/client.ts',
'**/e2e/**',
'**/coverage/**',
'**/__snapshots__/**',
'**/packages/*/test?(s)/**',
'**/*.d.ts',
'test?(s)/**',
Expand Down
18 changes: 18 additions & 0 deletions docs/plugins/react/hooks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ function Component() {

:::

## useLanguage

`useLanguage` will return the current language set by the parent `Editor` component.

::: code-group

```typescript
import { useLanguage } from '@kubb/react'

function Component() {
const language = useLanguage()

return null
}
```

:::

## usePluginManager

`usePluginManager` will return the PluginManager instance.
Expand Down
86 changes: 86 additions & 0 deletions docs/plugins/swagger-tanstack-query/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,92 @@ export default defineConfig({

:::

### variablesType

Define the way of passing thought the queryParams, headerParams and data.

`'mutate'` will use the `mutate` or `mutateAsync` function. <br/>
`'hook'` will use the `useMutation` hook.

::: info type

::: code-group

```typescript ['mutate']
const { mutate } = useDeletePet()

mutate({
petId: 1,
})
```

```typescript ['hook']
const { mutate } = useDeletePet(1)

mutate()
```

:::

::: info

Type: `'mutate' | 'hook'` <br/>
Default: `'hook'`

::: code-group

```typescript ['mutate']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
import createSwaggerTS from '@kubb/swagger-ts'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerTanstackQuery(
{
variablesType: 'mutate',
},
),
],
})
```

```typescript ['hook']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
import createSwaggerTS from '@kubb/swagger-ts'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerTanstackQuery(
{
variablesType: 'hook',
},
),
],
})
```

:::

### parser

Which parser can be used before returning the data to `@tanstack/query`.
Expand Down
38 changes: 37 additions & 1 deletion docs/plugins/swagger/hooks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ See [Oas](https://github.com/readmeio/oas) to understand how to use the `Oas` in
import { useOas } from '@kubb/react'

function Component() {
const { oas } = useOas()
const oas = useOas()

return null
}
Expand All @@ -46,6 +46,24 @@ function Component() {

:::

## useOperations

`useOperations` will return all the Operations.<br/>

::: code-group

```typescript
import { useOperations } from '@kubb/react'

function Component() {
const operations = useOperations()

return null
}
```

:::

## useSchemas

`useSchemas` will return the schemas of the current `Operation`.<br/>
Expand All @@ -64,6 +82,24 @@ function Component() {

:::

## useOperationHelpers

`useOperationHelpers` will return some helper functions that can be used to get the operation file, get the operation name.<br/>

::: code-group

```typescript
import { useOperationHelpers } from '@kubb/react'

function Component() {
const { getName, getFile } = useOperationHelpers()

return null
}
```

:::

## useOperationName

`useOperationName` will return the name based on the current operation and plugin(when `pluginKey` is not provided).<br/>
Expand Down
3 changes: 3 additions & 0 deletions e2e/kubb.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const baseConfig = {
path: './clients/hooks',
},
group: { type: 'tag' },
mutate: {
variablesType: 'mutate',
},
}],
['@kubb/swagger-swr', {
output: {
Expand Down
3 changes: 3 additions & 0 deletions examples/advanced/configs/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export default defineConfig(async () => {
queryParam: 'test',
initialPageParam: '0',
},
mutate: {
variablesType: 'mutate',
},
},
},
],
Expand Down
14 changes: 10 additions & 4 deletions examples/advanced/src/gen/clients/hooks/petController/useAddPet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ type AddPet = {
* @summary Add a new pet to the store
* @link /pet */
export function useAddPet(options: {
mutation?: UseMutationOptions<AddPet['response'], AddPet['error'], AddPet['request']>
mutation?: UseMutationOptions<AddPet['response'], AddPet['error'], {
data: AddPet['request']
}>
client?: AddPet['client']['parameters']
} = {}): UseMutationResult<AddPet['response'], AddPet['error'], AddPet['request']> {
} = {}): UseMutationResult<AddPet['response'], AddPet['error'], {
data: AddPet['request']
}> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
return useMutation<AddPet['response'], AddPet['error'], AddPet['request']>({
mutationFn: async (data) => {
return useMutation<AddPet['response'], AddPet['error'], {
data: AddPet['request']
}>({
mutationFn: async ({ data }) => {
const res = await client<AddPet['data'], AddPet['error'], AddPet['request']>({
method: 'post',
url: `/pet`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ type DeletePet = {
* @description delete a pet
* @summary Deletes a pet
* @link /pet/:petId */
export function useDeletePet(petId: DeletePetPathParams['petId'], headers?: DeletePet['headerParams'], options: {
mutation?: UseMutationOptions<DeletePet['response'], DeletePet['error'], void>
export function useDeletePet(options: {
mutation?: UseMutationOptions<DeletePet['response'], DeletePet['error'], {
petId: DeletePetPathParams['petId']
headers?: DeletePet['headerParams']
}>
client?: DeletePet['client']['parameters']
} = {}): UseMutationResult<DeletePet['response'], DeletePet['error'], void> {
} = {}): UseMutationResult<DeletePet['response'], DeletePet['error'], {
petId: DeletePetPathParams['petId']
headers?: DeletePet['headerParams']
}> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
return useMutation<DeletePet['response'], DeletePet['error'], void>({
mutationFn: async () => {
const res = await client<DeletePet['data'], DeletePet['error'], void>({
return useMutation<DeletePet['response'], DeletePet['error'], {
petId: DeletePetPathParams['petId']
headers?: DeletePet['headerParams']
}>({
mutationFn: async ({ petId, headers }) => {
const res = await client<DeletePet['data'], DeletePet['error'], DeletePet['request']>({
method: 'delete',
url: `/pet/${petId}`,
headers: { ...headers, ...clientOptions.headers },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ type UpdatePet = {
* @summary Update an existing pet
* @link /pet */
export function useUpdatePet(options: {
mutation?: UseMutationOptions<UpdatePet['response'], UpdatePet['error'], UpdatePet['request']>
mutation?: UseMutationOptions<UpdatePet['response'], UpdatePet['error'], {
data: UpdatePet['request']
}>
client?: UpdatePet['client']['parameters']
} = {}): UseMutationResult<UpdatePet['response'], UpdatePet['error'], UpdatePet['request']> {
} = {}): UseMutationResult<UpdatePet['response'], UpdatePet['error'], {
data: UpdatePet['request']
}> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
return useMutation<UpdatePet['response'], UpdatePet['error'], UpdatePet['request']>({
mutationFn: async (data) => {
return useMutation<UpdatePet['response'], UpdatePet['error'], {
data: UpdatePet['request']
}>({
mutationFn: async ({ data }) => {
const res = await client<UpdatePet['data'], UpdatePet['error'], UpdatePet['request']>({
method: 'put',
url: `/pet`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@ type UpdatePetWithForm = {
/**
* @summary Updates a pet in the store with form data
* @link /pet/:petId */
export function useUpdatePetWithForm(petId: UpdatePetWithFormPathParams['petId'], params?: UpdatePetWithForm['queryParams'], options: {
mutation?: UseMutationOptions<UpdatePetWithForm['response'], UpdatePetWithForm['error'], void>
export function useUpdatePetWithForm(options: {
mutation?: UseMutationOptions<UpdatePetWithForm['response'], UpdatePetWithForm['error'], {
petId: UpdatePetWithFormPathParams['petId']
params?: UpdatePetWithForm['queryParams']
}>
client?: UpdatePetWithForm['client']['parameters']
} = {}): UseMutationResult<UpdatePetWithForm['response'], UpdatePetWithForm['error'], void> {
} = {}): UseMutationResult<UpdatePetWithForm['response'], UpdatePetWithForm['error'], {
petId: UpdatePetWithFormPathParams['petId']
params?: UpdatePetWithForm['queryParams']
}> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
return useMutation<UpdatePetWithForm['response'], UpdatePetWithForm['error'], void>({
mutationFn: async () => {
const res = await client<UpdatePetWithForm['data'], UpdatePetWithForm['error'], void>({
return useMutation<UpdatePetWithForm['response'], UpdatePetWithForm['error'], {
petId: UpdatePetWithFormPathParams['petId']
params?: UpdatePetWithForm['queryParams']
}>({
mutationFn: async ({ petId, params }) => {
const res = await client<UpdatePetWithForm['data'], UpdatePetWithForm['error'], UpdatePetWithForm['request']>({
method: 'post',
url: `/pet/${petId}`,
params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ type UploadFile = {
/**
* @summary uploads an image
* @link /pet/:petId/uploadImage */
export function useUploadFile(petId: UploadFilePathParams['petId'], params?: UploadFile['queryParams'], options: {
mutation?: UseMutationOptions<UploadFile['response'], UploadFile['error'], UploadFile['request']>
export function useUploadFile(options: {
mutation?: UseMutationOptions<UploadFile['response'], UploadFile['error'], {
petId: UploadFilePathParams['petId']
params?: UploadFile['queryParams']
data?: UploadFile['request']
}>
client?: UploadFile['client']['parameters']
} = {}): UseMutationResult<UploadFile['response'], UploadFile['error'], UploadFile['request']> {
} = {}): UseMutationResult<UploadFile['response'], UploadFile['error'], {
petId: UploadFilePathParams['petId']
params?: UploadFile['queryParams']
data?: UploadFile['request']
}> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
return useMutation<UploadFile['response'], UploadFile['error'], UploadFile['request']>({
mutationFn: async (data) => {
return useMutation<UploadFile['response'], UploadFile['error'], {
petId: UploadFilePathParams['petId']
params?: UploadFile['queryParams']
data?: UploadFile['request']
}>({
mutationFn: async ({ petId, params, data }) => {
const res = await client<UploadFile['data'], UploadFile['error'], UploadFile['request']>({
method: 'post',
url: `/pet/${petId}/uploadImage`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,28 @@ type CreatePets = {
/**
* @summary Create a pet
* @link /pets/:uuid */
export function useCreatePets(uuid: CreatePetsPathParams['uuid'], headers: CreatePets['headerParams'], params?: CreatePets['queryParams'], options: {
mutation?: UseMutationOptions<CreatePets['response'], CreatePets['error'], CreatePets['request']>
export function useCreatePets(options: {
mutation?: UseMutationOptions<CreatePets['response'], CreatePets['error'], {
uuid: CreatePetsPathParams['uuid']
params?: CreatePets['queryParams']
headers: CreatePets['headerParams']
data: CreatePets['request']
}>
client?: CreatePets['client']['parameters']
} = {}): UseMutationResult<CreatePets['response'], CreatePets['error'], CreatePets['request']> {
} = {}): UseMutationResult<CreatePets['response'], CreatePets['error'], {
uuid: CreatePetsPathParams['uuid']
params?: CreatePets['queryParams']
headers: CreatePets['headerParams']
data: CreatePets['request']
}> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
return useMutation<CreatePets['response'], CreatePets['error'], CreatePets['request']>({
mutationFn: async (data) => {
return useMutation<CreatePets['response'], CreatePets['error'], {
uuid: CreatePetsPathParams['uuid']
params?: CreatePets['queryParams']
headers: CreatePets['headerParams']
data: CreatePets['request']
}>({
mutationFn: async ({ uuid, headers, data, params }) => {
const res = await client<CreatePets['data'], CreatePets['error'], CreatePets['request']>({
method: 'post',
url: `/pets/${uuid}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type DeleteUser = {
* @summary Delete user
* @link /user/:username */
export function useDeleteUser(username: DeleteUserPathParams['username'], options: {
mutation?: UseMutationOptions<DeleteUser['response'], DeleteUser['error'], void>
mutation?: UseMutationOptions<DeleteUser['response'], DeleteUser['error'], DeleteUser['request']>
client?: DeleteUser['client']['parameters']
} = {}): UseMutationResult<DeleteUser['response'], DeleteUser['error'], void> {
} = {}): UseMutationResult<DeleteUser['response'], DeleteUser['error'], DeleteUser['request']> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
return useMutation<DeleteUser['response'], DeleteUser['error'], void>({
return useMutation<DeleteUser['response'], DeleteUser['error'], never>({
mutationFn: async () => {
const res = await client<DeleteUser['data'], DeleteUser['error'], void>({
const res = await client<DeleteUser['data'], DeleteUser['error'], DeleteUser['request']>({
method: 'delete',
url: `/user/${username}`,
...clientOptions,
Expand Down
Loading

0 comments on commit efa7c75

Please sign in to comment.