Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose queryKey and mutationKey for the SWR plugin #1259

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/modern-laws-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@kubb/plugin-svelte-query": minor
"@kubb/plugin-react-query": minor
"@kubb/plugin-solid-query": minor
"@kubb/plugin-vue-query": minor
"@kubb/plugin-client": minor
"@kubb/plugin-faker": minor
"@kubb/plugin-msw": minor
"@kubb/plugin-swr": minor
"@kubb/plugin-zod": minor
"@kubb/plugin-ts": minor
---

'generators' option for all plugins
5 changes: 5 additions & 0 deletions .changeset/warm-ligers-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-swr": minor
---

Expose queryKey and mutationKey for the SWR plugin
2 changes: 1 addition & 1 deletion docs/blog/benefits-of-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ If you compare the previous template with an example using JSX/React, you can al

## Overriding with templates

We've made a guide on how you can override templates in our plugins: [templates the guide](/guide/tutorial/templates).
We've made a guide on how you can override templates in our plugins: [templates the guide](/tutorials/templates).
7 changes: 3 additions & 4 deletions docs/blog/v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Kubb has been available for almost a year and we have learned a lot since v1. Mo

Another big change is that it will now also be possible to use multiple of the same plugins and with the introduction of `exclude/include/override` you can generate code for just one specific path.

The introduction of `React` and `JSX` was also a big change, in the background we can now 'translate' Swagger/OpenAPI files to generated code(TypeScript, ReactQuery, ...). More about why and how we did this can be found in the [Benefits of using JSX for templates](/blog/whyTemplates) blog.
The introduction of `React` and `JSX` was also a big change, in the background we can now 'translate' Swagger/OpenAPI files to generated code(TypeScript, ReactQuery, ...). More about why and how we did this can be found in the [Benefits of using JSX for templates](/blog/benefits-of-templates) blog.

Because of this change we can now use [templates](/reference/templates) to override the default behaviour of for example the `useQuery` of @tanstack/query.

Expand All @@ -54,9 +54,8 @@ In practice, most of the "breaking" changes should not have an actual effect, an

With templates you can override the default behaviour of our plugins.

- [What are templates](/reference/templates)
- [Benefits of using JSX for templates](/blog/whyTemplates)
- [Guide in create your own template](/guide/tutorial/templates)
- [Benefits of using JSX for templates](/blog/benefits-of-templates)
- [Guide in create your own template](/tutorials/templates)

### Exclude and include

Expand Down
24 changes: 14 additions & 10 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ title: Changelog

# Changelog

## 3.0.0-alpha.26
- [`plugin-swr`](/plugins/plugin-swr/): Expose queryKey and mutationKey for the SWR plugin
- 'generators' option for all plugins

## 3.0.0-alpha.25
- [`plugin-react-query`](/plugins/plugin-react-query): Use of MutationKeys for `useMutation`
- [`plugin-svelte-query`](/plugins/plugin-react-query): Use of MutationKeys for `createMutation`
- [`plugin-vue-query`](/plugins/plugin-vue-query): Use of MutationKeys for `useMutation`
- [`plugin-react-query`](/plugins/plugin-react-query/): Use of MutationKeys for `useMutation`
- [`plugin-svelte-query`](/plugins/plugin-react-query/): Use of MutationKeys for `createMutation`
- [`plugin-vue-query`](/plugins/plugin-vue-query/): Use of MutationKeys for `useMutation`


## 3.0.0-alpha.24
- [`plugin-oas`](/plugins/plugin-oas): Support for [discriminator](https://swagger.io/specification/?sbsearch=discriminator)
- [`plugin-oas`](/plugins/plugin-oas/): Support for [discriminator](https://swagger.io/specification/?sbsearch=discriminator)


## 3.0.0-alpha.23
- [`plugin-client`](/plugins/plugin-client): Use of uppercase for httpMethods, `GET` instead of `get`, `POST` instead of `post`, ...
- [`plugin-client`](/plugins/plugin-client/): Use of uppercase for httpMethods, `GET` instead of `get`, `POST` instead of `post`, ...

## 3.0.0-alpha.22
- [`plugin-faker`](/plugins/plugin-faker): Use of `faker.image.url()` instead of `faker.image.imageUrl()`
- [`plugin-zod`](/plugins/plugin-zod): Enums should use `z.literal` when format is set to number, string or boolean
- [`plugin-faker`](/plugins/plugin-faker/): Use of `faker.image.url()` instead of `faker.image.imageUrl()`
- [`plugin-zod`](/plugins/plugin-zod/): Enums should use `z.literal` when format is set to number, string or boolean

::: code-group

Expand All @@ -35,12 +39,12 @@ z.enum(["true", "false"]) // [!code --]
z.union([z.literal(true), z.literal(false)]) // [!code ++]
```
:::
- [`plugin-ts`](/plugins/plugin-ts): Use of `readonly` for references($ref)
- [`plugin-client`](/plugins/plugin-client): Use of type `Error` when no errors are set for an operation
- [`plugin-ts`](/plugins/plugin-ts/): Use of `readonly` for references($ref)
- [`plugin-client`](/plugins/plugin-client/): Use of type `Error` when no errors are set for an operation


## 3.0.0-alpha.21
- [`plugin-zod`](/plugins/plugin-zod): Use of `x-nullable` and `nullable` for additionalProperties.
- [`plugin-zod`](/plugins/plugin-zod/): Use of `x-nullable` and `nullable` for additionalProperties.

## 3.0.0-alpha.20

Expand Down
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"@kubb/plugin-vue-query": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@types/node": "^20.16.5",
"@types/react": "^18.3.8",
"@types/node": "^20.16.6",
"@types/react": "^18.3.9",
"cross-env": "^7.0.3",
"react": "^18.3.1",
"unplugin-kubb": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { addPetMutationResponseSchema } from '../../../zod/petController/addPetSchema.ts'

export const addPetMutationKey = () => [{ url: '/pet' }] as const

export type AddPetMutationKey = ReturnType<typeof addPetMutationKey>

/**
* @description Add a new pet to the store
* @summary Add a new pet to the store
Expand Down Expand Up @@ -35,9 +39,9 @@ export function useAddPet(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/pet'] as const
const mutationKey = addPetMutationKey()
return useSWRMutation<AddPetMutationResponse, AddPet405, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url, { arg: data }) => {
return addPet(data, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { deletePetMutationResponseSchema } from '../../../zod/petController/deletePetSchema.ts'

export const deletePetMutationKey = () => [{ url: '/pet/{petId}' }] as const

export type DeletePetMutationKey = ReturnType<typeof deletePetMutationKey>

/**
* @description delete a pet
* @summary Deletes a pet
Expand Down Expand Up @@ -37,9 +41,9 @@ export function useDeletePet(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = [`/pet/${petId}`] as const
const mutationKey = deletePetMutationKey()
return useSWRMutation<DeletePetMutationResponse, DeletePet400, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url) => {
return deletePet(petId, headers, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPe
import type { Key, SWRConfiguration } from 'swr'
import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts'

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

export type FindPetsByStatusQueryKey = ReturnType<typeof findPetsByStatusQueryKey>

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
Expand Down Expand Up @@ -43,8 +47,8 @@ export function useFindPetsByStatus(
} = {},
) {
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/pet/findByStatus', params] as const
return useSWR<FindPetsByStatusQueryResponse, FindPetsByStatus400, Key>(shouldFetch ? swrKey : null, {
const queryKey = findPetsByStatusQueryKey(params)
return useSWR<FindPetsByStatusQueryResponse, FindPetsByStatus400, Key>(shouldFetch ? queryKey : null, {
...findPetsByStatusQueryOptions(params, config),
...queryOptions,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import type {
import type { Key, SWRConfiguration } from 'swr'
import { findPetsByTagsQueryResponseSchema } from '../../../zod/petController/findPetsByTagsSchema.ts'

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

export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>

/**
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @summary Finds Pets by tags
Expand Down Expand Up @@ -50,8 +54,8 @@ export function useFindPetsByTags(
} = {},
) {
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/pet/findByTags', params] as const
return useSWR<FindPetsByTagsQueryResponse, FindPetsByTags400, Key>(shouldFetch ? swrKey : null, {
const queryKey = findPetsByTagsQueryKey(params)
return useSWR<FindPetsByTagsQueryResponse, FindPetsByTags400, Key>(shouldFetch ? queryKey : null, {
...findPetsByTagsQueryOptions(headers, params, config),
...queryOptions,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetP
import type { Key, SWRConfiguration } from 'swr'
import { getPetByIdQueryResponseSchema } from '../../../zod/petController/getPetByIdSchema.ts'

export const getPetByIdQueryKey = (petId: GetPetByIdPathParams['petId']) => [{ url: '/pet/:petId', params: { petId: petId } }] as const

export type GetPetByIdQueryKey = ReturnType<typeof getPetByIdQueryKey>

/**
* @description Returns a single pet
* @summary Find pet by ID
Expand Down Expand Up @@ -42,8 +46,8 @@ export function useGetPetById(
} = {},
) {
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = [`/pet/${petId}`] as const
return useSWR<GetPetByIdQueryResponse, GetPetById400 | GetPetById404, Key>(shouldFetch ? swrKey : null, {
const queryKey = getPetByIdQueryKey(petId)
return useSWR<GetPetByIdQueryResponse, GetPetById400 | GetPetById404, Key>(shouldFetch ? queryKey : null, {
...getPetByIdQueryOptions(petId, config),
...queryOptions,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { updatePetMutationResponseSchema } from '../../../zod/petController/updatePetSchema.ts'

export const updatePetMutationKey = () => [{ url: '/pet' }] as const

export type UpdatePetMutationKey = ReturnType<typeof updatePetMutationKey>

/**
* @description Update an existing pet by Id
* @summary Update an existing pet
Expand Down Expand Up @@ -41,9 +45,9 @@ export function useUpdatePet(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/pet'] as const
const mutationKey = updatePetMutationKey()
return useSWRMutation<UpdatePetMutationResponse, UpdatePet400 | UpdatePet404 | UpdatePet405, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url, { arg: data }) => {
return updatePet(data, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { updatePetWithFormMutationResponseSchema } from '../../../zod/petController/updatePetWithFormSchema.ts'

export const updatePetWithFormMutationKey = () => [{ url: '/pet/{petId}' }] as const

export type UpdatePetWithFormMutationKey = ReturnType<typeof updatePetWithFormMutationKey>

/**
* @summary Updates a pet in the store with form data
* @link /pet/:petId
Expand Down Expand Up @@ -40,9 +44,9 @@ export function useUpdatePetWithForm(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = [`/pet/${petId}`, params] as const
const mutationKey = updatePetWithFormMutationKey()
return useSWRMutation<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url) => {
return updatePetWithForm(petId, params, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { uploadFileMutationResponseSchema } from '../../../zod/petController/uploadFileSchema.ts'

export const uploadFileMutationKey = () => [{ url: '/pet/{petId}/uploadImage' }] as const

export type UploadFileMutationKey = ReturnType<typeof uploadFileMutationKey>

/**
* @summary uploads an image
* @link /pet/:petId/uploadImage
Expand Down Expand Up @@ -47,9 +51,9 @@ export function useUploadFile(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = [`/pet/${petId}/uploadImage`, params] as const
const mutationKey = uploadFileMutationKey()
return useSWRMutation<UploadFileMutationResponse, Error, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url, { arg: data }) => {
return uploadFile(petId, data, params, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { createPetsMutationResponseSchema } from '../../../zod/petsController/createPetsSchema.ts'

export const createPetsMutationKey = () => [{ url: '/pets/{uuid}' }] as const

export type CreatePetsMutationKey = ReturnType<typeof createPetsMutationKey>

/**
* @summary Create a pet
* @link /pets/:uuid
Expand Down Expand Up @@ -50,9 +54,9 @@ export function useCreatePets(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = [`/pets/${uuid}`, params] as const
const mutationKey = createPetsMutationKey()
return useSWRMutation<CreatePetsMutationResponse, Error, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url, { arg: data }) => {
return createPets(uuid, data, headers, params, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { createUserMutationResponseSchema } from '../../../zod/userController/createUserSchema.ts'

export const createUserMutationKey = () => [{ url: '/user' }] as const

export type CreateUserMutationKey = ReturnType<typeof createUserMutationKey>

/**
* @description This can only be done by the logged in user.
* @summary Create user
Expand Down Expand Up @@ -35,9 +39,9 @@ export function useCreateUser(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/user'] as const
const mutationKey = createUserMutationKey()
return useSWRMutation<CreateUserMutationResponse, Error, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url, { arg: data }) => {
return createUser(data, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { createUsersWithListInputMutationResponseSchema } from '../../../zod/userController/createUsersWithListInputSchema.ts'

export const createUsersWithListInputMutationKey = () => [{ url: '/user/createWithList' }] as const

export type CreateUsersWithListInputMutationKey = ReturnType<typeof createUsersWithListInputMutationKey>

/**
* @description Creates list of users with given input array
* @summary Creates list of users with given input array
Expand Down Expand Up @@ -41,9 +45,9 @@ export function useCreateUsersWithListInput(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/user/createWithList'] as const
const mutationKey = createUsersWithListInputMutationKey()
return useSWRMutation<CreateUsersWithListInputMutationResponse, Error, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url, { arg: data }) => {
return createUsersWithListInput(data, config)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type { Key } from 'swr'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { deleteUserMutationResponseSchema } from '../../../zod/userController/deleteUserSchema.ts'

export const deleteUserMutationKey = () => [{ url: '/user/{username}' }] as const

export type DeleteUserMutationKey = ReturnType<typeof deleteUserMutationKey>

/**
* @description This can only be done by the logged in user.
* @summary Delete user
Expand Down Expand Up @@ -35,9 +39,9 @@ export function useDeleteUser(
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = [`/user/${username}`] as const
const mutationKey = deleteUserMutationKey()
return useSWRMutation<DeleteUserMutationResponse, DeleteUser400 | DeleteUser404, Key>(
shouldFetch ? swrKey : null,
shouldFetch ? mutationKey : null,
async (_url) => {
return deleteUser(username, config)
},
Expand Down
Loading
Loading