From 90ad72bf8bd9f80d3f06f2577d315e3765f3f868 Mon Sep 17 00:00:00 2001 From: Stijn Van Hulle Date: Sun, 8 Sep 2024 16:51:55 +0200 Subject: [PATCH] chore: humanize swr output --- .../clients/swr/petSWRController/useAddPet.ts | 60 ++++--- .../swr/petSWRController/useDeletePet.ts | 60 ++++--- .../petSWRController/useFindPetsByStatus.ts | 12 +- .../swr/petSWRController/useFindPetsByTags.ts | 12 +- .../swr/petSWRController/useGetPetById.ts | 12 +- .../swr/petSWRController/useUpdatePet.ts | 60 ++++--- .../petSWRController/useUpdatePetWithForm.ts | 60 ++++--- .../swr/petSWRController/useUploadFile.ts | 68 ++++---- .../swr/petsSWRController/useCreatePets.ts | 72 ++++---- .../swr/userSWRController/useCreateUser.ts | 60 ++++--- .../useCreateUsersWithListInput.ts | 63 +++---- .../swr/userSWRController/useDeleteUser.ts | 56 +++---- .../swr/userSWRController/useGetUserByName.ts | 12 +- .../swr/userSWRController/useLoginUser.ts | 12 +- .../swr/userSWRController/useLogoutUser.ts | 12 +- .../swr/userSWRController/useUpdateUser.ts | 62 +++---- examples/swr/src/gen/hooks/useAddPet.ts | 59 ++++--- examples/swr/src/gen/hooks/useCreateUser.ts | 59 ++++--- .../gen/hooks/useCreateUsersWithListInput.ts | 62 +++---- examples/swr/src/gen/hooks/useDeleteOrder.ts | 55 +++---- examples/swr/src/gen/hooks/useDeletePet.ts | 59 +++---- examples/swr/src/gen/hooks/useDeleteUser.ts | 55 +++---- .../swr/src/gen/hooks/useFindPetsByStatus.ts | 10 +- .../swr/src/gen/hooks/useFindPetsByTags.ts | 10 +- examples/swr/src/gen/hooks/useGetInventory.ts | 10 +- examples/swr/src/gen/hooks/useGetOrderById.ts | 10 +- examples/swr/src/gen/hooks/useGetPetById.ts | 10 +- .../swr/src/gen/hooks/useGetUserByName.ts | 10 +- examples/swr/src/gen/hooks/useLoginUser.ts | 10 +- examples/swr/src/gen/hooks/useLogoutUser.ts | 10 +- examples/swr/src/gen/hooks/usePlaceOrder.ts | 59 ++++--- .../swr/src/gen/hooks/usePlaceOrderPatch.ts | 59 ++++--- examples/swr/src/gen/hooks/useUpdatePet.ts | 59 ++++--- .../swr/src/gen/hooks/useUpdatePetWithForm.ts | 59 +++---- examples/swr/src/gen/hooks/useUpdateUser.ts | 61 ++++--- examples/swr/src/gen/hooks/useUploadFile.ts | 67 ++++---- examples/zod/src/gen/zodClients.ts | 10 +- packages/core/mocks/index.ts | 10 +- .../plugin-client/src/components/Client.tsx | 13 +- .../src/generators/__snapshots__/deletePet.ts | 9 +- .../__snapshots__/deletePetObject.ts | 13 +- .../generators/__snapshots__/findByTags.ts | 4 +- .../__snapshots__/findByTagsFull.ts | 12 ++ .../__snapshots__/findByTagsWithZod.ts | 6 +- .../__snapshots__/findByTagsWithZodFull.ts | 12 ++ .../generators/__snapshots__/importPath.ts | 4 +- .../generators/__snapshots__/updatePetById.ts | 8 +- .../src/generators/clientGenerator.test.tsx | 19 +++ .../generators/operationsGenerator.test.tsx | 1 + .../src/generators/__snapshots__/createPet.ts | 12 +- .../generators/__snapshots__/createPetSeed.ts | 12 +- .../__snapshots__/createPetUnknownTypeAny.ts | 12 +- .../src/generators/__snapshots__/deletePet.ts | 2 +- .../src/generators/__snapshots__/enumNames.ts | 4 +- .../generators/__snapshots__/enumVarNames.ts | 4 +- .../src/generators/__snapshots__/getPets.ts | 16 +- .../src/generators/__snapshots__/pet.ts | 2 + .../__snapshots__/petWithDateString.ts | 4 +- .../generators/__snapshots__/petWithDayjs.ts | 5 +- .../generators/__snapshots__/petWithMapper.ts | 4 +- .../__snapshots__/petWithRandExp.ts | 5 +- .../src/generators/__snapshots__/pets.ts | 4 +- .../generators/__snapshots__/showPetById.ts | 16 +- .../src/generators/__snapshots__/createPet.ts | 6 +- .../src/generators/__snapshots__/deletePet.ts | 6 +- .../src/generators/__snapshots__/getPets.ts | 6 +- .../src/generators/__snapshots__/handlers.ts | 4 +- .../generators/__snapshots__/showPetById.ts | 4 +- .../__snapshots__/gen/showPetById.ts | 94 +++++------ .../__snapshots__/gen/useCreatePets.ts | 68 ++++---- .../__snapshots__/gen/useCreatePetsMutate.ts | 68 ++++---- .../src/generators/queryGenerator.test.tsx | 81 --------- .../plugin-swr/src/components/Mutation.tsx | 154 ++++++++---------- packages/plugin-swr/src/components/Query.tsx | 21 ++- .../src/components/QueryOptions.tsx | 3 +- .../__snapshots__/clientDataReturnTypeFull.ts | 50 ++---- .../__snapshots__/clientGetImportPath.ts | 48 ++---- .../__snapshots__/clientPostImportPath.ts | 52 ++---- .../generators/__snapshots__/findByTags.ts | 48 ++---- .../findByTagsPathParamsObject.ts | 48 ++---- .../__snapshots__/findByTagsWithZod.ts | 48 ++---- .../generators/__snapshots__/getAsMutation.ts | 50 ++---- .../generators/__snapshots__/postAsQuery.ts | 54 +++--- .../generators/__snapshots__/updatePetById.ts | 52 ++---- .../updatePetByIdPathParamsObject.ts | 56 +++---- .../src/generators/mutationGenerator.tsx | 27 ++- .../src/generators/queryGenerator.tsx | 10 +- .../__snapshots__/catTypeAsConst.ts | 2 +- .../src/generators/__snapshots__/createPet.ts | 18 +- .../createPetWithUnknownTypeUnknown.ts | 18 +- .../src/generators/__snapshots__/deletePet.ts | 6 +- .../__snapshots__/dogTypeAsConst.ts | 2 +- .../src/generators/__snapshots__/enumAllOf.ts | 4 +- .../src/generators/__snapshots__/enumArray.ts | 4 +- .../generators/__snapshots__/enumInObject.ts | 4 +- .../src/generators/__snapshots__/enumItems.ts | 4 +- .../src/generators/__snapshots__/enumNames.ts | 4 +- .../__snapshots__/enumNamesConst.ts | 4 +- .../__snapshots__/enumNamesLiteral.ts | 4 +- .../__snapshots__/enumNamesPascalConst.ts | 6 +- .../generators/__snapshots__/enumNamesType.ts | 4 +- .../__snapshots__/enumNullableMember.ts | 4 +- .../__snapshots__/enumNullableType.ts | 4 +- .../generators/__snapshots__/enumString.ts | 4 +- .../__snapshots__/enumVarNamesType.ts | 4 +- .../src/generators/__snapshots__/getPets.ts | 14 +- .../generators/__snapshots__/nullConstNull.ts | 2 +- .../src/generators/__snapshots__/petMapper.ts | 4 +- .../__snapshots__/petQuestionToken.ts | 4 +- .../petQuestionTokenAndUndefined.ts | 4 +- .../generators/__snapshots__/petUndefined.ts | 4 +- .../__snapshots__/petsStoreDiscriminator.ts | 4 +- .../generators/__snapshots__/petsStoreRef.ts | 2 +- .../generators/__snapshots__/plainDateDate.ts | 2 +- .../__snapshots__/plainDateString.ts | 2 +- .../generators/__snapshots__/plainTimeDate.ts | 2 +- .../generators/__snapshots__/showPetById.ts | 12 +- .../src/generators/__snapshots__/anyof.ts | 4 +- .../src/generators/__snapshots__/createPet.ts | 10 +- .../createPetWithUnknownTypeUnknown.ts | 10 +- .../src/generators/__snapshots__/deletePet.ts | 4 +- .../generators/__snapshots__/enumNamesType.ts | 2 + .../generators/__snapshots__/enumNullable.ts | 2 + .../__snapshots__/enumVarNamesType.ts | 4 +- .../src/generators/__snapshots__/example.ts | 2 + .../src/generators/__snapshots__/getPets.ts | 10 +- .../__snapshots__/mixedValueTypeConst.ts | 2 + .../__snapshots__/nullableString.ts | 4 +- .../__snapshots__/nullableStringUuid.ts | 4 +- .../__snapshots__/nullableStringWithAnyOf.ts | 4 +- .../__snapshots__/numberValueConst.ts | 2 + .../src/generators/__snapshots__/oneof.ts | 4 +- .../generators/__snapshots__/operations.ts | 27 ++- .../__snapshots__/optionalPetInfer.ts | 6 +- .../__snapshots__/optionalPetTyped.ts | 8 +- .../src/generators/__snapshots__/order.ts | 2 + .../__snapshots__/orderDateTyeString.ts | 4 +- .../__snapshots__/orderDateTypeFalse.ts | 4 +- .../__snapshots__/orderDateTypeString.ts | 10 ++ .../src/generators/__snapshots__/pet.ts | 2 + .../src/generators/__snapshots__/petArray.ts | 4 +- .../generators/__snapshots__/petCoercion.ts | 4 +- .../__snapshots__/petTupleObject.ts | 2 + .../generators/__snapshots__/petWithMapper.ts | 4 +- .../src/generators/__snapshots__/pets.ts | 2 + .../src/generators/__snapshots__/recursive.ts | 4 +- .../generators/__snapshots__/showPetById.ts | 10 +- .../__snapshots__/stringValueConst.ts | 2 + .../generators/__snapshots__/uuidSchema.ts | 4 +- .../src/generators/zodGenerator.test.tsx | 2 +- 150 files changed, 1466 insertions(+), 1626 deletions(-) create mode 100644 packages/plugin-client/src/generators/__snapshots__/findByTagsFull.ts create mode 100644 packages/plugin-client/src/generators/__snapshots__/findByTagsWithZodFull.ts delete mode 100644 packages/plugin-react-query/src/generators/queryGenerator.test.tsx create mode 100644 packages/plugin-zod/src/generators/__snapshots__/orderDateTypeString.ts diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useAddPet.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useAddPet.ts index 98d406e42..dc6068127 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useAddPet.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useAddPet.ts @@ -1,23 +1,25 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { AddPetMutationRequest, AddPetMutationResponse, AddPet405 } from '../../../models/ts/petController/AddPet.ts' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { addPetMutationResponseSchema } from '../../../zod/petController/addPetSchema.ts' -type AddPetClient = typeof client - -type AddPet = { - data: AddPetMutationResponse - error: AddPet405 - request: AddPetMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: AddPetMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Add a new pet to the store + * @summary Add a new pet to the store + * @link /pet + */ +async function addPet(data: AddPetMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'post', + url: '/pet', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return addPetMutationResponseSchema.parse(res.data) } /** @@ -25,23 +27,19 @@ type AddPet = { * @summary Add a new pet to the store * @link /pet */ -export function useAddPet(options?: { - mutation?: SWRMutationConfiguration - client?: AddPet['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/pet' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useAddPet( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/pet'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - data, - ...clientOptions, - }) - return res.data + return addPet(data, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useDeletePet.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useDeletePet.ts index b0e91fdc9..0509e65ff 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useDeletePet.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useDeletePet.ts @@ -1,23 +1,25 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { DeletePetMutationResponse, DeletePetPathParams, DeletePetHeaderParams, DeletePet400 } from '../../../models/ts/petController/DeletePet.ts' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { deletePetMutationResponseSchema } from '../../../zod/petController/deletePetSchema.ts' -type DeletePetClient = typeof client - -type DeletePet = { - data: DeletePetMutationResponse - error: DeletePet400 - request: never - pathParams: DeletePetPathParams - queryParams: never - headerParams: DeletePetHeaderParams - response: DeletePetMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description delete a pet + * @summary Deletes a pet + * @link /pet/:petId + */ +async function deletePet(petId: DeletePetPathParams['petId'], headers?: DeletePetHeaderParams, config: Partial = {}) { + const res = await client({ + method: 'delete', + url: `/pet/${petId}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + headers: { ...headers, ...config.headers }, + ...config, + }) + return deletePetMutationResponseSchema.parse(res.data) } /** @@ -27,25 +29,19 @@ type DeletePet = { */ export function useDeletePet( petId: DeletePetPathParams['petId'], - headers?: DeletePet['headerParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: DeletePet['client']['parameters'] + headers?: DeletePetHeaderParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` as const - return useSWRMutation( - shouldFetch ? url : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'delete', - url, - headers: { ...headers, ...clientOptions.headers }, - ...clientOptions, - }) - return res.data + return deletePet(petId, headers, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts index 5e5c020f1..c38c96fbd 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts @@ -2,7 +2,7 @@ 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 { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts' /** @@ -18,7 +18,7 @@ async function findPetsByStatus(params?: FindPetsByStatusQueryParams, config: Pa params, ...config, }) - return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) } + return findPetsByStatusQueryResponseSchema.parse(res.data) } export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParams, config: Partial = {}) { @@ -34,17 +34,17 @@ export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParam * @summary Finds Pets by status * @link /pet/findByStatus */ -export function useFindPetsByStatus( +export function useFindPetsByStatus( params?: FindPetsByStatusQueryParams, options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/pet/findByStatus' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/pet/findByStatus', params] as const + return useSWR(shouldFetch ? swrKey : null, { ...findPetsByStatusQueryOptions(params, config), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts index 98702610a..36a90fe24 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts @@ -7,7 +7,7 @@ import type { FindPetsByTagsHeaderParams, FindPetsByTags400, } from '../../../models/ts/petController/FindPetsByTags.ts' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' import { findPetsByTagsQueryResponseSchema } from '../../../zod/petController/findPetsByTagsSchema.ts' /** @@ -24,7 +24,7 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find headers: { ...headers, ...config.headers }, ...config, }) - return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) } + return findPetsByTagsQueryResponseSchema.parse(res.data) } export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, config: Partial = {}) { @@ -40,18 +40,18 @@ export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams, * @summary Finds Pets by tags * @link /pet/findByTags */ -export function useFindPetsByTags( +export function useFindPetsByTags( headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/pet/findByTags' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/pet/findByTags', params] as const + return useSWR(shouldFetch ? swrKey : null, { ...findPetsByTagsQueryOptions(headers, params, config), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts index efdcc73db..c0b74e326 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts @@ -2,7 +2,7 @@ import client from '../../../../swr-client.ts' import useSWR from 'swr' import type { RequestConfig } from '../../../../swr-client.ts' import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../../../models/ts/petController/GetPetById.ts' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' import { getPetByIdQueryResponseSchema } from '../../../zod/petController/getPetByIdSchema.ts' /** @@ -17,7 +17,7 @@ async function getPetById(petId: GetPetByIdPathParams['petId'], config: Partial< baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return { ...res, data: getPetByIdQueryResponseSchema.parse(res.data) } + return getPetByIdQueryResponseSchema.parse(res.data) } export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], config: Partial = {}) { @@ -33,17 +33,17 @@ export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], con * @summary Find pet by ID * @link /pet/:petId */ -export function useGetPetById( +export function useGetPetById( petId: GetPetByIdPathParams['petId'], options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` - return useSWR(shouldFetch ? url : null, { + const swrKey = [`/pet/${petId}`] as const + return useSWR(shouldFetch ? swrKey : null, { ...getPetByIdQueryOptions(petId, config), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePet.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePet.ts index 95ea76c3e..a7da35d0d 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePet.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePet.ts @@ -1,5 +1,6 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { UpdatePetMutationRequest, UpdatePetMutationResponse, @@ -8,22 +9,23 @@ import type { UpdatePet405, } from '../../../models/ts/petController/UpdatePet.ts' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { updatePetMutationResponseSchema } from '../../../zod/petController/updatePetSchema.ts' -type UpdatePetClient = typeof client - -type UpdatePet = { - data: UpdatePetMutationResponse - error: UpdatePet400 | UpdatePet404 | UpdatePet405 - request: UpdatePetMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: UpdatePetMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Update an existing pet by Id + * @summary Update an existing pet + * @link /pet + */ +async function updatePet(data: UpdatePetMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'put', + url: '/pet', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return updatePetMutationResponseSchema.parse(res.data) } /** @@ -31,23 +33,19 @@ type UpdatePet = { * @summary Update an existing pet * @link /pet */ -export function useUpdatePet(options?: { - mutation?: SWRMutationConfiguration - client?: UpdatePet['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/pet' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useUpdatePet( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/pet'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'put', - url, - data, - ...clientOptions, - }) - return res.data + return updatePet(data, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePetWithForm.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePetWithForm.ts index 76e4da3fc..917cadc34 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePetWithForm.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useUpdatePetWithForm.ts @@ -1,27 +1,29 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { UpdatePetWithFormMutationResponse, UpdatePetWithFormPathParams, UpdatePetWithFormQueryParams, UpdatePetWithForm405, } from '../../../models/ts/petController/UpdatePetWithForm.ts' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { updatePetWithFormMutationResponseSchema } from '../../../zod/petController/updatePetWithFormSchema.ts' -type UpdatePetWithFormClient = typeof client - -type UpdatePetWithForm = { - data: UpdatePetWithFormMutationResponse - error: UpdatePetWithForm405 - request: never - pathParams: UpdatePetWithFormPathParams - queryParams: UpdatePetWithFormQueryParams - headerParams: never - response: UpdatePetWithFormMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @summary Updates a pet in the store with form data + * @link /pet/:petId + */ +async function updatePetWithForm(petId: UpdatePetWithFormPathParams['petId'], params?: UpdatePetWithFormQueryParams, config: Partial = {}) { + const res = await client({ + method: 'post', + url: `/pet/${petId}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + params, + ...config, + }) + return updatePetWithFormMutationResponseSchema.parse(res.data) } /** @@ -30,25 +32,19 @@ type UpdatePetWithForm = { */ export function useUpdatePetWithForm( petId: UpdatePetWithFormPathParams['petId'], - params?: UpdatePetWithForm['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: UpdatePetWithForm['client']['parameters'] + params?: UpdatePetWithFormQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'post', - url, - params, - ...clientOptions, - }) - return res.data + return updatePetWithForm(petId, params, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useUploadFile.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useUploadFile.ts index 29a1b3aed..559587024 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useUploadFile.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useUploadFile.ts @@ -1,27 +1,36 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { UploadFileMutationRequest, UploadFileMutationResponse, UploadFilePathParams, UploadFileQueryParams, } from '../../../models/ts/petController/UploadFile.ts' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { uploadFileMutationResponseSchema } from '../../../zod/petController/uploadFileSchema.ts' -type UploadFileClient = typeof client - -type UploadFile = { - data: UploadFileMutationResponse - error: never - request: UploadFileMutationRequest - pathParams: UploadFilePathParams - queryParams: UploadFileQueryParams - headerParams: never - response: UploadFileMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @summary uploads an image + * @link /pet/:petId/uploadImage + */ +async function uploadFile( + petId: UploadFilePathParams['petId'], + data?: UploadFileMutationRequest, + params?: UploadFileQueryParams, + config: Partial> = {}, +) { + const res = await client({ + method: 'post', + url: `/pet/${petId}/uploadImage`, + baseURL: 'https://petstore3.swagger.io/api/v3', + params, + data, + headers: { 'Content-Type': 'application/octet-stream', ...config.headers }, + ...config, + }) + return uploadFileMutationResponseSchema.parse(res.data) } /** @@ -30,26 +39,19 @@ type UploadFile = { */ export function useUploadFile( petId: UploadFilePathParams['petId'], - params?: UploadFile['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: UploadFile['client']['parameters'] + params?: UploadFileQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial> shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}/uploadImage` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}/uploadImage`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - params, - data, - ...clientOptions, - }) - return res.data + return uploadFile(petId, data, params, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/petsSWRController/useCreatePets.ts b/examples/advanced/src/gen/clients/swr/petsSWRController/useCreatePets.ts index 39282ee9d..77d3e3dd4 100644 --- a/examples/advanced/src/gen/clients/swr/petsSWRController/useCreatePets.ts +++ b/examples/advanced/src/gen/clients/swr/petsSWRController/useCreatePets.ts @@ -1,5 +1,6 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { CreatePetsMutationRequest, CreatePetsMutationResponse, @@ -7,22 +8,31 @@ import type { CreatePetsQueryParams, CreatePetsHeaderParams, } from '../../../models/ts/petsController/CreatePets.ts' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { createPetsMutationResponseSchema } from '../../../zod/petsController/createPetsSchema.ts' -type CreatePetsClient = typeof client - -type CreatePets = { - data: CreatePetsMutationResponse - error: never - request: CreatePetsMutationRequest - pathParams: CreatePetsPathParams - queryParams: CreatePetsQueryParams - headerParams: CreatePetsHeaderParams - response: CreatePetsMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @summary Create a pet + * @link /pets/:uuid + */ +async function createPets( + uuid: CreatePetsPathParams['uuid'], + data: CreatePetsMutationRequest, + headers: CreatePetsHeaderParams, + params?: CreatePetsQueryParams, + config: Partial> = {}, +) { + const res = await client({ + method: 'post', + url: `/pets/${uuid}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + params, + data, + headers: { ...headers, ...config.headers }, + ...config, + }) + return createPetsMutationResponseSchema.parse(res.data) } /** @@ -31,28 +41,20 @@ type CreatePets = { */ export function useCreatePets( uuid: CreatePetsPathParams['uuid'], - params?: CreatePets['queryParams'], - headers?: CreatePets['headerParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: CreatePets['client']['parameters'] + headers: CreatePetsHeaderParams, + params?: CreatePetsQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial> shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pets/${uuid}` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pets/${uuid}`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - params, - data, - headers: { ...headers, ...clientOptions.headers }, - ...clientOptions, - }) - return res.data + return createPets(uuid, data, headers, params, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUser.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUser.ts index 45dea754d..2e2e76162 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUser.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUser.ts @@ -1,23 +1,25 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { CreateUserMutationRequest, CreateUserMutationResponse } from '../../../models/ts/userController/CreateUser.ts' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { createUserMutationResponseSchema } from '../../../zod/userController/createUserSchema.ts' -type CreateUserClient = typeof client - -type CreateUser = { - data: CreateUserMutationResponse - error: never - request: CreateUserMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: CreateUserMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description This can only be done by the logged in user. + * @summary Create user + * @link /user + */ +async function createUser(data?: CreateUserMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'post', + url: '/user', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return createUserMutationResponseSchema.parse(res.data) } /** @@ -25,23 +27,19 @@ type CreateUser = { * @summary Create user * @link /user */ -export function useCreateUser(options?: { - mutation?: SWRMutationConfiguration - client?: CreateUser['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/user' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useCreateUser( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/user'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - data, - ...clientOptions, - }) - return res.data + return createUser(data, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUsersWithListInput.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUsersWithListInput.ts index 9d5e1f727..af29e6a58 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUsersWithListInput.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useCreateUsersWithListInput.ts @@ -1,26 +1,31 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { CreateUsersWithListInputMutationRequest, CreateUsersWithListInputMutationResponse, } from '../../../models/ts/userController/CreateUsersWithListInput.ts' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { createUsersWithListInputMutationResponseSchema } from '../../../zod/userController/createUsersWithListInputSchema.ts' -type CreateUsersWithListInputClient = typeof client - -type CreateUsersWithListInput = { - data: CreateUsersWithListInputMutationResponse - error: never - request: CreateUsersWithListInputMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: CreateUsersWithListInputMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Creates list of users with given input array + * @summary Creates list of users with given input array + * @link /user/createWithList + */ +async function createUsersWithListInput( + data?: CreateUsersWithListInputMutationRequest, + config: Partial> = {}, +) { + const res = await client({ + method: 'post', + url: '/user/createWithList', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return createUsersWithListInputMutationResponseSchema.parse(res.data) } /** @@ -28,23 +33,19 @@ type CreateUsersWithListInput = { * @summary Creates list of users with given input array * @link /user/createWithList */ -export function useCreateUsersWithListInput(options?: { - mutation?: SWRMutationConfiguration - client?: CreateUsersWithListInput['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/user/createWithList' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useCreateUsersWithListInput( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/user/createWithList'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - data, - ...clientOptions, - }) - return res.data + return createUsersWithListInput(data, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useDeleteUser.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useDeleteUser.ts index 94f0e929a..ae92662fa 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useDeleteUser.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useDeleteUser.ts @@ -1,23 +1,24 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { DeleteUserMutationResponse, DeleteUserPathParams, DeleteUser400, DeleteUser404 } from '../../../models/ts/userController/DeleteUser.ts' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { deleteUserMutationResponseSchema } from '../../../zod/userController/deleteUserSchema.ts' -type DeleteUserClient = typeof client - -type DeleteUser = { - data: DeleteUserMutationResponse - error: DeleteUser400 | DeleteUser404 - request: never - pathParams: DeleteUserPathParams - queryParams: never - headerParams: never - response: DeleteUserMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description This can only be done by the logged in user. + * @summary Delete user + * @link /user/:username + */ +async function deleteUser(username: DeleteUserPathParams['username'], config: Partial = {}) { + const res = await client({ + method: 'delete', + url: `/user/${username}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + ...config, + }) + return deleteUserMutationResponseSchema.parse(res.data) } /** @@ -27,23 +28,18 @@ type DeleteUser = { */ export function useDeleteUser( username: DeleteUserPathParams['username'], - options?: { - mutation?: SWRMutationConfiguration - client?: DeleteUser['client']['parameters'] + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/user/${username}` as const - return useSWRMutation( - shouldFetch ? url : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/user/${username}`] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'delete', - url, - ...clientOptions, - }) - return res.data + return deleteUser(username, config) }, mutationOptions, ) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts index a6a3d270e..31082310f 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts @@ -7,7 +7,7 @@ import type { GetUserByName400, GetUserByName404, } from '../../../models/ts/userController/GetUserByName.ts' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' import { getUserByNameQueryResponseSchema } from '../../../zod/userController/getUserByNameSchema.ts' /** @@ -21,7 +21,7 @@ async function getUserByName(username: GetUserByNamePathParams['username'], conf baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return { ...res, data: getUserByNameQueryResponseSchema.parse(res.data) } + return getUserByNameQueryResponseSchema.parse(res.data) } export function getUserByNameQueryOptions(username: GetUserByNamePathParams['username'], config: Partial = {}) { @@ -36,17 +36,17 @@ export function getUserByNameQueryOptions(username: GetUserByNamePathParams['use * @summary Get user by user name * @link /user/:username */ -export function useGetUserByName( +export function useGetUserByName( username: GetUserByNamePathParams['username'], options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = `/user/${username}` - return useSWR(shouldFetch ? url : null, { + const swrKey = [`/user/${username}`] as const + return useSWR(shouldFetch ? swrKey : null, { ...getUserByNameQueryOptions(username, config), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts index 12c434a9a..cfac9bd18 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts @@ -2,7 +2,7 @@ import client from '../../../../swr-client.ts' import useSWR from 'swr' import type { RequestConfig } from '../../../../swr-client.ts' import type { LoginUserQueryResponse, LoginUserQueryParams, LoginUser400 } from '../../../models/ts/userController/LoginUser.ts' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' import { loginUserQueryResponseSchema } from '../../../zod/userController/loginUserSchema.ts' /** @@ -17,7 +17,7 @@ async function loginUser(params?: LoginUserQueryParams, config: Partial = {}) { @@ -32,17 +32,17 @@ export function loginUserQueryOptions(params?: LoginUserQueryParams, config: Par * @summary Logs user into the system * @link /user/login */ -export function useLoginUser( +export function useLoginUser( params?: LoginUserQueryParams, options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/user/login' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/user/login', params] as const + return useSWR(shouldFetch ? swrKey : null, { ...loginUserQueryOptions(params, config), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts index a22bfa819..5bb1d7e32 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts @@ -2,7 +2,7 @@ import client from '../../../../swr-client.ts' import useSWR from 'swr' import type { RequestConfig } from '../../../../swr-client.ts' import type { LogoutUserQueryResponse } from '../../../models/ts/userController/LogoutUser.ts' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' import { logoutUserQueryResponseSchema } from '../../../zod/userController/logoutUserSchema.ts' /** @@ -16,7 +16,7 @@ async function logoutUser(config: Partial = {}) { baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return { ...res, data: logoutUserQueryResponseSchema.parse(res.data) } + return logoutUserQueryResponseSchema.parse(res.data) } export function logoutUserQueryOptions(config: Partial = {}) { @@ -31,16 +31,16 @@ export function logoutUserQueryOptions(config: Partial = {}) { * @summary Logs out current logged in user session * @link /user/logout */ -export function useLogoutUser( +export function useLogoutUser( options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/user/logout' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/user/logout'] as const + return useSWR(shouldFetch ? swrKey : null, { ...logoutUserQueryOptions(config), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useUpdateUser.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useUpdateUser.ts index 50f9f86db..a91c6e85d 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useUpdateUser.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useUpdateUser.ts @@ -1,23 +1,29 @@ import client from '../../../../swr-client.ts' import useSWRMutation from 'swr/mutation' +import type { RequestConfig } from '../../../../swr-client.ts' import type { UpdateUserMutationRequest, UpdateUserMutationResponse, UpdateUserPathParams } from '../../../models/ts/userController/UpdateUser.ts' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' +import { updateUserMutationResponseSchema } from '../../../zod/userController/updateUserSchema.ts' -type UpdateUserClient = typeof client - -type UpdateUser = { - data: UpdateUserMutationResponse - error: never - request: UpdateUserMutationRequest - pathParams: UpdateUserPathParams - queryParams: never - headerParams: never - response: UpdateUserMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description This can only be done by the logged in user. + * @summary Update user + * @link /user/:username + */ +async function updateUser( + username: UpdateUserPathParams['username'], + data?: UpdateUserMutationRequest, + config: Partial> = {}, +) { + const res = await client({ + method: 'put', + url: `/user/${username}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return updateUserMutationResponseSchema.parse(res.data) } /** @@ -27,24 +33,18 @@ type UpdateUser = { */ export function useUpdateUser( username: UpdateUserPathParams['username'], - options?: { - mutation?: SWRMutationConfiguration - client?: UpdateUser['client']['parameters'] + options: { + mutation?: SWRMutationConfiguration + client?: Partial> shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/user/${username}` as const - return useSWRMutation( - shouldFetch ? url : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/user/${username}`] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'put', - url, - data, - ...clientOptions, - }) - return res.data + return updateUser(username, data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useAddPet.ts b/examples/swr/src/gen/hooks/useAddPet.ts index 8a0adfd5e..9112c236a 100644 --- a/examples/swr/src/gen/hooks/useAddPet.ts +++ b/examples/swr/src/gen/hooks/useAddPet.ts @@ -1,23 +1,24 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { AddPetMutationRequest, AddPetMutationResponse, AddPet405 } from '../models/AddPet.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type AddPetClient = typeof client - -type AddPet = { - data: AddPetMutationResponse - error: AddPet405 - request: AddPetMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: AddPetMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Add a new pet to the store + * @summary Add a new pet to the store + * @link /pet + */ +async function addPet(data: AddPetMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'post', + url: '/pet', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return res.data } /** @@ -25,23 +26,19 @@ type AddPet = { * @summary Add a new pet to the store * @link /pet */ -export function useAddPet(options?: { - mutation?: SWRMutationConfiguration - client?: AddPet['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/pet' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useAddPet( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/pet'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - data, - ...clientOptions, - }) - return res.data + return addPet(data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useCreateUser.ts b/examples/swr/src/gen/hooks/useCreateUser.ts index 8fe7824b7..21cec8db0 100644 --- a/examples/swr/src/gen/hooks/useCreateUser.ts +++ b/examples/swr/src/gen/hooks/useCreateUser.ts @@ -1,23 +1,24 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { CreateUserMutationRequest, CreateUserMutationResponse } from '../models/CreateUser.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type CreateUserClient = typeof client - -type CreateUser = { - data: CreateUserMutationResponse - error: never - request: CreateUserMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: CreateUserMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description This can only be done by the logged in user. + * @summary Create user + * @link /user + */ +async function createUser(data?: CreateUserMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'post', + url: '/user', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return res.data } /** @@ -25,23 +26,19 @@ type CreateUser = { * @summary Create user * @link /user */ -export function useCreateUser(options?: { - mutation?: SWRMutationConfiguration - client?: CreateUser['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/user' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useCreateUser( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/user'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - data, - ...clientOptions, - }) - return res.data + return createUser(data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useCreateUsersWithListInput.ts b/examples/swr/src/gen/hooks/useCreateUsersWithListInput.ts index de91d057d..9620aac51 100644 --- a/examples/swr/src/gen/hooks/useCreateUsersWithListInput.ts +++ b/examples/swr/src/gen/hooks/useCreateUsersWithListInput.ts @@ -1,23 +1,27 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { CreateUsersWithListInputMutationRequest, CreateUsersWithListInputMutationResponse } from '../models/CreateUsersWithListInput.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type CreateUsersWithListInputClient = typeof client - -type CreateUsersWithListInput = { - data: CreateUsersWithListInputMutationResponse - error: never - request: CreateUsersWithListInputMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: CreateUsersWithListInputMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Creates list of users with given input array + * @summary Creates list of users with given input array + * @link /user/createWithList + */ +async function createUsersWithListInput( + data?: CreateUsersWithListInputMutationRequest, + config: Partial> = {}, +) { + const res = await client({ + method: 'post', + url: '/user/createWithList', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return res.data } /** @@ -25,23 +29,19 @@ type CreateUsersWithListInput = { * @summary Creates list of users with given input array * @link /user/createWithList */ -export function useCreateUsersWithListInput(options?: { - mutation?: SWRMutationConfiguration - client?: CreateUsersWithListInput['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/user/createWithList' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useCreateUsersWithListInput( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/user/createWithList'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - data, - ...clientOptions, - }) - return res.data + return createUsersWithListInput(data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useDeleteOrder.ts b/examples/swr/src/gen/hooks/useDeleteOrder.ts index 1837b6a3f..e0d3673a7 100644 --- a/examples/swr/src/gen/hooks/useDeleteOrder.ts +++ b/examples/swr/src/gen/hooks/useDeleteOrder.ts @@ -1,23 +1,23 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { DeleteOrderMutationResponse, DeleteOrderPathParams, DeleteOrder400, DeleteOrder404 } from '../models/DeleteOrder.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type DeleteOrderClient = typeof client - -type DeleteOrder = { - data: DeleteOrderMutationResponse - error: DeleteOrder400 | DeleteOrder404 - request: never - pathParams: DeleteOrderPathParams - queryParams: never - headerParams: never - response: DeleteOrderMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * @summary Delete purchase order by ID + * @link /store/order/:orderId + */ +async function deleteOrder(orderId: DeleteOrderPathParams['orderId'], config: Partial = {}) { + const res = await client({ + method: 'delete', + url: `/store/order/${orderId}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + ...config, + }) + return res.data } /** @@ -27,23 +27,18 @@ type DeleteOrder = { */ export function useDeleteOrder( orderId: DeleteOrderPathParams['orderId'], - options?: { - mutation?: SWRMutationConfiguration - client?: DeleteOrder['client']['parameters'] + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/store/order/${orderId}` as const - return useSWRMutation( - shouldFetch ? url : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/store/order/${orderId}`] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'delete', - url, - ...clientOptions, - }) - return res.data + return deleteOrder(orderId, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useDeletePet.ts b/examples/swr/src/gen/hooks/useDeletePet.ts index cef835b9c..345f8c35a 100644 --- a/examples/swr/src/gen/hooks/useDeletePet.ts +++ b/examples/swr/src/gen/hooks/useDeletePet.ts @@ -1,23 +1,24 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { DeletePetMutationResponse, DeletePetPathParams, DeletePetHeaderParams, DeletePet400 } from '../models/DeletePet.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type DeletePetClient = typeof client - -type DeletePet = { - data: DeletePetMutationResponse - error: DeletePet400 - request: never - pathParams: DeletePetPathParams - queryParams: never - headerParams: DeletePetHeaderParams - response: DeletePetMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description delete a pet + * @summary Deletes a pet + * @link /pet/:petId + */ +async function deletePet(petId: DeletePetPathParams['petId'], headers?: DeletePetHeaderParams, config: Partial = {}) { + const res = await client({ + method: 'delete', + url: `/pet/${petId}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + headers: { ...headers, ...config.headers }, + ...config, + }) + return res.data } /** @@ -27,25 +28,19 @@ type DeletePet = { */ export function useDeletePet( petId: DeletePetPathParams['petId'], - headers?: DeletePet['headerParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: DeletePet['client']['parameters'] + headers?: DeletePetHeaderParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` as const - return useSWRMutation( - shouldFetch ? url : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'delete', - url, - headers: { ...headers, ...clientOptions.headers }, - ...clientOptions, - }) - return res.data + return deletePet(petId, headers, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useDeleteUser.ts b/examples/swr/src/gen/hooks/useDeleteUser.ts index 367d5c127..fc2a1ff77 100644 --- a/examples/swr/src/gen/hooks/useDeleteUser.ts +++ b/examples/swr/src/gen/hooks/useDeleteUser.ts @@ -1,23 +1,23 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { DeleteUserMutationResponse, DeleteUserPathParams, DeleteUser400, DeleteUser404 } from '../models/DeleteUser.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type DeleteUserClient = typeof client - -type DeleteUser = { - data: DeleteUserMutationResponse - error: DeleteUser400 | DeleteUser404 - request: never - pathParams: DeleteUserPathParams - queryParams: never - headerParams: never - response: DeleteUserMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description This can only be done by the logged in user. + * @summary Delete user + * @link /user/:username + */ +async function deleteUser(username: DeleteUserPathParams['username'], config: Partial = {}) { + const res = await client({ + method: 'delete', + url: `/user/${username}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + ...config, + }) + return res.data } /** @@ -27,23 +27,18 @@ type DeleteUser = { */ export function useDeleteUser( username: DeleteUserPathParams['username'], - options?: { - mutation?: SWRMutationConfiguration - client?: DeleteUser['client']['parameters'] + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/user/${username}` as const - return useSWRMutation( - shouldFetch ? url : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/user/${username}`] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'delete', - url, - ...clientOptions, - }) - return res.data + return deleteUser(username, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useFindPetsByStatus.ts b/examples/swr/src/gen/hooks/useFindPetsByStatus.ts index 72c523e4e..1cf3a6e76 100644 --- a/examples/swr/src/gen/hooks/useFindPetsByStatus.ts +++ b/examples/swr/src/gen/hooks/useFindPetsByStatus.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../models/FindPetsByStatus.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @description Multiple status values can be provided with comma separated strings @@ -33,17 +33,17 @@ export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParam * @summary Finds Pets by status * @link /pet/findByStatus */ -export function useFindPetsByStatus( +export function useFindPetsByStatus( params?: FindPetsByStatusQueryParams, options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/pet/findByStatus' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/pet/findByStatus', params] as const + return useSWR(shouldFetch ? swrKey : null, { ...findPetsByStatusQueryOptions(params, config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/useFindPetsByTags.ts b/examples/swr/src/gen/hooks/useFindPetsByTags.ts index f5446bf80..953e79a85 100644 --- a/examples/swr/src/gen/hooks/useFindPetsByTags.ts +++ b/examples/swr/src/gen/hooks/useFindPetsByTags.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { FindPetsByTagsQueryResponse, FindPetsByTagsQueryParams, FindPetsByTags400 } from '../models/FindPetsByTags.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -33,17 +33,17 @@ export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, c * @summary Finds Pets by tags * @link /pet/findByTags */ -export function useFindPetsByTags( +export function useFindPetsByTags( params?: FindPetsByTagsQueryParams, options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/pet/findByTags' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/pet/findByTags', params] as const + return useSWR(shouldFetch ? swrKey : null, { ...findPetsByTagsQueryOptions(params, config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/useGetInventory.ts b/examples/swr/src/gen/hooks/useGetInventory.ts index 17f04bfbe..ac85f264e 100644 --- a/examples/swr/src/gen/hooks/useGetInventory.ts +++ b/examples/swr/src/gen/hooks/useGetInventory.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { GetInventoryQueryResponse } from '../models/GetInventory.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @description Returns a map of status codes to quantities @@ -32,16 +32,16 @@ export function getInventoryQueryOptions(config: Partial = {}) { * @summary Returns pet inventories by status * @link /store/inventory */ -export function useGetInventory( +export function useGetInventory( options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/store/inventory' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/store/inventory'] as const + return useSWR(shouldFetch ? swrKey : null, { ...getInventoryQueryOptions(config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/useGetOrderById.ts b/examples/swr/src/gen/hooks/useGetOrderById.ts index ff40b93db..942b492b2 100644 --- a/examples/swr/src/gen/hooks/useGetOrderById.ts +++ b/examples/swr/src/gen/hooks/useGetOrderById.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { GetOrderByIdQueryResponse, GetOrderByIdPathParams, GetOrderById400, GetOrderById404 } from '../models/GetOrderById.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -32,17 +32,17 @@ export function getOrderByIdQueryOptions(orderId: GetOrderByIdPathParams['orderI * @summary Find purchase order by ID * @link /store/order/:orderId */ -export function useGetOrderById( +export function useGetOrderById( orderId: GetOrderByIdPathParams['orderId'], options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = `/store/order/${orderId}` - return useSWR(shouldFetch ? url : null, { + const swrKey = [`/store/order/${orderId}`] as const + return useSWR(shouldFetch ? swrKey : null, { ...getOrderByIdQueryOptions(orderId, config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/useGetPetById.ts b/examples/swr/src/gen/hooks/useGetPetById.ts index 26768b74e..9552b4cfa 100644 --- a/examples/swr/src/gen/hooks/useGetPetById.ts +++ b/examples/swr/src/gen/hooks/useGetPetById.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../models/GetPetById.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @description Returns a single pet @@ -32,17 +32,17 @@ export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], con * @summary Find pet by ID * @link /pet/:petId */ -export function useGetPetById( +export function useGetPetById( petId: GetPetByIdPathParams['petId'], options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` - return useSWR(shouldFetch ? url : null, { + const swrKey = [`/pet/${petId}`] as const + return useSWR(shouldFetch ? swrKey : null, { ...getPetByIdQueryOptions(petId, config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/useGetUserByName.ts b/examples/swr/src/gen/hooks/useGetUserByName.ts index d50412da2..bec66d811 100644 --- a/examples/swr/src/gen/hooks/useGetUserByName.ts +++ b/examples/swr/src/gen/hooks/useGetUserByName.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { GetUserByNameQueryResponse, GetUserByNamePathParams, GetUserByName400, GetUserByName404 } from '../models/GetUserByName.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @summary Get user by user name @@ -30,17 +30,17 @@ export function getUserByNameQueryOptions(username: GetUserByNamePathParams['use * @summary Get user by user name * @link /user/:username */ -export function useGetUserByName( +export function useGetUserByName( username: GetUserByNamePathParams['username'], options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = `/user/${username}` - return useSWR(shouldFetch ? url : null, { + const swrKey = [`/user/${username}`] as const + return useSWR(shouldFetch ? swrKey : null, { ...getUserByNameQueryOptions(username, config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/useLoginUser.ts b/examples/swr/src/gen/hooks/useLoginUser.ts index 3ea14ebb9..44a3a8a2c 100644 --- a/examples/swr/src/gen/hooks/useLoginUser.ts +++ b/examples/swr/src/gen/hooks/useLoginUser.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { LoginUserQueryResponse, LoginUserQueryParams, LoginUser400 } from '../models/LoginUser.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @summary Logs user into the system @@ -31,17 +31,17 @@ export function loginUserQueryOptions(params?: LoginUserQueryParams, config: Par * @summary Logs user into the system * @link /user/login */ -export function useLoginUser( +export function useLoginUser( params?: LoginUserQueryParams, options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/user/login' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/user/login', params] as const + return useSWR(shouldFetch ? swrKey : null, { ...loginUserQueryOptions(params, config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/useLogoutUser.ts b/examples/swr/src/gen/hooks/useLogoutUser.ts index 49acc4e7e..fefb0f6f6 100644 --- a/examples/swr/src/gen/hooks/useLogoutUser.ts +++ b/examples/swr/src/gen/hooks/useLogoutUser.ts @@ -2,7 +2,7 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { LogoutUserQueryResponse } from '../models/LogoutUser.ts' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration } from 'swr' +import type { Key, SWRConfiguration } from 'swr' /** * @summary Logs out current logged in user session @@ -30,16 +30,16 @@ export function logoutUserQueryOptions(config: Partial = {}) { * @summary Logs out current logged in user session * @link /user/logout */ -export function useLogoutUser( +export function useLogoutUser( options: { - query?: SWRConfiguration + query?: SWRConfiguration client?: Partial shouldFetch?: boolean } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const url = '/user/logout' - return useSWR(shouldFetch ? url : null, { + const swrKey = ['/user/logout'] as const + return useSWR(shouldFetch ? swrKey : null, { ...logoutUserQueryOptions(config), ...queryOptions, }) diff --git a/examples/swr/src/gen/hooks/usePlaceOrder.ts b/examples/swr/src/gen/hooks/usePlaceOrder.ts index c3c0f9785..1024d2f83 100644 --- a/examples/swr/src/gen/hooks/usePlaceOrder.ts +++ b/examples/swr/src/gen/hooks/usePlaceOrder.ts @@ -1,23 +1,24 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { PlaceOrderMutationRequest, PlaceOrderMutationResponse, PlaceOrder405 } from '../models/PlaceOrder.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type PlaceOrderClient = typeof client - -type PlaceOrder = { - data: PlaceOrderMutationResponse - error: PlaceOrder405 - request: PlaceOrderMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: PlaceOrderMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Place a new order in the store + * @summary Place an order for a pet + * @link /store/order + */ +async function placeOrder(data?: PlaceOrderMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'post', + url: '/store/order', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return res.data } /** @@ -25,23 +26,19 @@ type PlaceOrder = { * @summary Place an order for a pet * @link /store/order */ -export function usePlaceOrder(options?: { - mutation?: SWRMutationConfiguration - client?: PlaceOrder['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/store/order' as const - return useSWRMutation( - shouldFetch ? url : null, +export function usePlaceOrder( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/store/order'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - data, - ...clientOptions, - }) - return res.data + return placeOrder(data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/usePlaceOrderPatch.ts b/examples/swr/src/gen/hooks/usePlaceOrderPatch.ts index 4744f8391..4e50cc59b 100644 --- a/examples/swr/src/gen/hooks/usePlaceOrderPatch.ts +++ b/examples/swr/src/gen/hooks/usePlaceOrderPatch.ts @@ -1,23 +1,24 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { PlaceOrderPatchMutationRequest, PlaceOrderPatchMutationResponse, PlaceOrderPatch405 } from '../models/PlaceOrderPatch.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type PlaceOrderPatchClient = typeof client - -type PlaceOrderPatch = { - data: PlaceOrderPatchMutationResponse - error: PlaceOrderPatch405 - request: PlaceOrderPatchMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: PlaceOrderPatchMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Place a new order in the store with patch + * @summary Place an order for a pet with patch + * @link /store/order + */ +async function placeOrderPatch(data?: PlaceOrderPatchMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'patch', + url: '/store/order', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return res.data } /** @@ -25,23 +26,19 @@ type PlaceOrderPatch = { * @summary Place an order for a pet with patch * @link /store/order */ -export function usePlaceOrderPatch(options?: { - mutation?: SWRMutationConfiguration - client?: PlaceOrderPatch['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/store/order' as const - return useSWRMutation( - shouldFetch ? url : null, +export function usePlaceOrderPatch( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/store/order'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'patch', - url, - data, - ...clientOptions, - }) - return res.data + return placeOrderPatch(data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useUpdatePet.ts b/examples/swr/src/gen/hooks/useUpdatePet.ts index 197434a26..99056bf10 100644 --- a/examples/swr/src/gen/hooks/useUpdatePet.ts +++ b/examples/swr/src/gen/hooks/useUpdatePet.ts @@ -1,23 +1,24 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { UpdatePetMutationRequest, UpdatePetMutationResponse, UpdatePet400, UpdatePet404, UpdatePet405 } from '../models/UpdatePet.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type UpdatePetClient = typeof client - -type UpdatePet = { - data: UpdatePetMutationResponse - error: UpdatePet400 | UpdatePet404 | UpdatePet405 - request: UpdatePetMutationRequest - pathParams: never - queryParams: never - headerParams: never - response: UpdatePetMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description Update an existing pet by Id + * @summary Update an existing pet + * @link /pet + */ +async function updatePet(data: UpdatePetMutationRequest, config: Partial> = {}) { + const res = await client({ + method: 'put', + url: '/pet', + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return res.data } /** @@ -25,23 +26,19 @@ type UpdatePet = { * @summary Update an existing pet * @link /pet */ -export function useUpdatePet(options?: { - mutation?: SWRMutationConfiguration - client?: UpdatePet['client']['parameters'] - shouldFetch?: boolean -}): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = '/pet' as const - return useSWRMutation( - shouldFetch ? url : null, +export function useUpdatePet( + options: { + mutation?: SWRMutationConfiguration + client?: Partial> + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = ['/pet'] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'put', - url, - data, - ...clientOptions, - }) - return res.data + return updatePet(data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useUpdatePetWithForm.ts b/examples/swr/src/gen/hooks/useUpdatePetWithForm.ts index e1d3d739d..dbb8783ba 100644 --- a/examples/swr/src/gen/hooks/useUpdatePetWithForm.ts +++ b/examples/swr/src/gen/hooks/useUpdatePetWithForm.ts @@ -6,22 +6,23 @@ import type { UpdatePetWithFormQueryParams, UpdatePetWithForm405, } from '../models/UpdatePetWithForm.ts' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { RequestConfig } from '@kubb/plugin-client/client' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' -type UpdatePetWithFormClient = typeof client - -type UpdatePetWithForm = { - data: UpdatePetWithFormMutationResponse - error: UpdatePetWithForm405 - request: never - pathParams: UpdatePetWithFormPathParams - queryParams: UpdatePetWithFormQueryParams - headerParams: never - response: UpdatePetWithFormMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @summary Updates a pet in the store with form data + * @link /pet/:petId + */ +async function updatePetWithForm(petId: UpdatePetWithFormPathParams['petId'], params?: UpdatePetWithFormQueryParams, config: Partial = {}) { + const res = await client({ + method: 'post', + url: `/pet/${petId}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + params, + ...config, + }) + return res.data } /** @@ -30,25 +31,19 @@ type UpdatePetWithForm = { */ export function useUpdatePetWithForm( petId: UpdatePetWithFormPathParams['petId'], - params?: UpdatePetWithForm['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: UpdatePetWithForm['client']['parameters'] + params?: UpdatePetWithFormQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'post', - url, - params, - ...clientOptions, - }) - return res.data + return updatePetWithForm(petId, params, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useUpdateUser.ts b/examples/swr/src/gen/hooks/useUpdateUser.ts index 730683b73..1a488684f 100644 --- a/examples/swr/src/gen/hooks/useUpdateUser.ts +++ b/examples/swr/src/gen/hooks/useUpdateUser.ts @@ -1,23 +1,28 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { UpdateUserMutationRequest, UpdateUserMutationResponse, UpdateUserPathParams } from '../models/UpdateUser.ts' +import type { RequestConfig } from '@kubb/plugin-client/client' import type { Key } from 'swr' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { SWRMutationConfiguration } from 'swr/mutation' -type UpdateUserClient = typeof client - -type UpdateUser = { - data: UpdateUserMutationResponse - error: never - request: UpdateUserMutationRequest - pathParams: UpdateUserPathParams - queryParams: never - headerParams: never - response: UpdateUserMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @description This can only be done by the logged in user. + * @summary Update user + * @link /user/:username + */ +async function updateUser( + username: UpdateUserPathParams['username'], + data?: UpdateUserMutationRequest, + config: Partial> = {}, +) { + const res = await client({ + method: 'put', + url: `/user/${username}`, + baseURL: 'https://petstore3.swagger.io/api/v3', + data, + ...config, + }) + return res.data } /** @@ -27,24 +32,18 @@ type UpdateUser = { */ export function useUpdateUser( username: UpdateUserPathParams['username'], - options?: { - mutation?: SWRMutationConfiguration - client?: UpdateUser['client']['parameters'] + options: { + mutation?: SWRMutationConfiguration + client?: Partial> shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/user/${username}` as const - return useSWRMutation( - shouldFetch ? url : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/user/${username}`] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'put', - url, - data, - ...clientOptions, - }) - return res.data + return updateUser(username, data, config) }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/useUploadFile.ts b/examples/swr/src/gen/hooks/useUploadFile.ts index 5e6abea5d..605062d1d 100644 --- a/examples/swr/src/gen/hooks/useUploadFile.ts +++ b/examples/swr/src/gen/hooks/useUploadFile.ts @@ -1,22 +1,30 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' import type { UploadFileMutationRequest, UploadFileMutationResponse, UploadFilePathParams, UploadFileQueryParams } from '../models/UploadFile.ts' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' +import type { RequestConfig } from '@kubb/plugin-client/client' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' -type UploadFileClient = typeof client - -type UploadFile = { - data: UploadFileMutationResponse - error: never - request: UploadFileMutationRequest - pathParams: UploadFilePathParams - queryParams: UploadFileQueryParams - headerParams: never - response: UploadFileMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } +/** + * @summary uploads an image + * @link /pet/:petId/uploadImage + */ +async function uploadFile( + petId: UploadFilePathParams['petId'], + data?: UploadFileMutationRequest, + params?: UploadFileQueryParams, + config: Partial> = {}, +) { + const res = await client({ + method: 'post', + url: `/pet/${petId}/uploadImage`, + baseURL: 'https://petstore3.swagger.io/api/v3', + params, + data, + headers: { 'Content-Type': 'application/octet-stream', ...config.headers }, + ...config, + }) + return res.data } /** @@ -25,26 +33,19 @@ type UploadFile = { */ export function useUploadFile( petId: UploadFilePathParams['petId'], - params?: UploadFile['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: UploadFile['client']['parameters'] + params?: UploadFileQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial> shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}/uploadImage` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}/uploadImage`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url, { arg: data }) => { - const res = await client({ - method: 'post', - url, - params, - data, - ...clientOptions, - }) - return res.data + return uploadFile(petId, data, params, config) }, mutationOptions, ) diff --git a/examples/zod/src/gen/zodClients.ts b/examples/zod/src/gen/zodClients.ts index 7b41365ae..90e0f1d17 100644 --- a/examples/zod/src/gen/zodClients.ts +++ b/examples/zod/src/gen/zodClients.ts @@ -23,7 +23,7 @@ export async function getInventory(config: Partial = {}) { baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return { ...res, data: getInventoryQueryResponseSchema.parse(res.data) } + return getInventoryQueryResponseSchema.parse(res.data) } /** @@ -39,7 +39,7 @@ export async function placeOrder(data?: PlaceOrderMutationRequestType, config: P data, ...config, }) - return { ...res, data: placeOrderMutationResponseSchema.parse(res.data) } + return placeOrderMutationResponseSchema.parse(res.data) } /** @@ -55,7 +55,7 @@ export async function placeOrderPatch(data?: PlaceOrderPatchMutationRequestType, data, ...config, }) - return { ...res, data: placeOrderPatchMutationResponseSchema.parse(res.data) } + return placeOrderPatchMutationResponseSchema.parse(res.data) } /** @@ -77,7 +77,7 @@ export async function getOrderById( baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return { ...res, data: getOrderByIdQueryResponseSchema.parse(res.data) } + return getOrderByIdQueryResponseSchema.parse(res.data) } /** @@ -99,5 +99,5 @@ export async function deleteOrder( baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return { ...res, data: deleteOrderMutationResponseSchema.parse(res.data) } + return deleteOrderMutationResponseSchema.parse(res.data) } diff --git a/packages/core/mocks/index.ts b/packages/core/mocks/index.ts index a06dfb404..7ca50c3a1 100644 --- a/packages/core/mocks/index.ts +++ b/packages/core/mocks/index.ts @@ -17,15 +17,19 @@ export const mockedLogger = { export const createMockedPluginManager = (name?: string) => ({ resolveName: (result) => { + if (result.type === 'file') { + return camelCase(name || result.name) + } + if (result.type === 'type') { - return pascalCase(name || result.name) + return pascalCase(result.name) } if (result.type === 'function') { - return camelCase(name || result.name) + return camelCase(result.name) } - return camelCase(name || result.name) + return camelCase(result.name) }, config: { output: { diff --git a/packages/plugin-client/src/components/Client.tsx b/packages/plugin-client/src/components/Client.tsx index 846056c42..70abcd241 100644 --- a/packages/plugin-client/src/components/Client.tsx +++ b/packages/plugin-client/src/components/Client.tsx @@ -133,10 +133,6 @@ export function Client({ ` : '' - if (dataReturnType === 'full' && parser !== 'client') { - throw new Error("Set dataReturnType to 'data' if you want to use the Zod parser") - } - return ( (${clientParams.toCall()})`}
- {dataReturnType === 'data' - ? parser === 'zod' && zodSchemas - ? `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}` - : 'return res.data' - : 'return res'} + {dataReturnType === 'full' && parser === 'zod' && zodSchemas && `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`} + {dataReturnType === 'full' && parser === 'client' && 'return res'} + {dataReturnType === 'data' && parser === 'client' && 'return res.data'} + {dataReturnType === 'data' && parser === 'zod' && zodSchemas && `return ${zodSchemas.response.name}.parse(res.data)`}
) diff --git a/packages/plugin-client/src/generators/__snapshots__/deletePet.ts b/packages/plugin-client/src/generators/__snapshots__/deletePet.ts index 5186e0758..27a3589ed 100644 --- a/packages/plugin-client/src/generators/__snapshots__/deletePet.ts +++ b/packages/plugin-client/src/generators/__snapshots__/deletePet.ts @@ -6,7 +6,12 @@ import type { RequestConfig } from '@kubb/plugin-client/client' * @summary Deletes a pet * @link /pet/:petId */ -export async function deletePet(petId: DeletePet['petId'], headers?: DeletePet, config: Partial = {}) { - const res = await client({ method: 'delete', url: `/pet/${petId}`, headers: { ...headers, ...config.headers }, ...config }) +export async function deletePet(petId: DeletePetPathParams['petId'], headers?: DeletePetHeaderParams, config: Partial = {}) { + const res = await client({ + method: 'delete', + url: `/pet/${petId}`, + headers: { ...headers, ...config.headers }, + ...config, + }) return res.data } diff --git a/packages/plugin-client/src/generators/__snapshots__/deletePetObject.ts b/packages/plugin-client/src/generators/__snapshots__/deletePetObject.ts index 915b74b64..dd4ac3541 100644 --- a/packages/plugin-client/src/generators/__snapshots__/deletePetObject.ts +++ b/packages/plugin-client/src/generators/__snapshots__/deletePetObject.ts @@ -6,15 +6,20 @@ import type { RequestConfig } from '@kubb/plugin-client/client' * @summary Deletes a pet * @link /pet/:petId */ -export async function deletePetObject( +export async function deletePet( { petId, }: { - petId: DeletePetObject['petId'] + petId: DeletePetPathParams['petId'] }, - headers?: DeletePetObject, + headers?: DeletePetHeaderParams, config: Partial = {}, ) { - const res = await client({ method: 'delete', url: `/pet/${petId}`, headers: { ...headers, ...config.headers }, ...config }) + const res = await client({ + method: 'delete', + url: `/pet/${petId}`, + headers: { ...headers, ...config.headers }, + ...config, + }) return res.data } diff --git a/packages/plugin-client/src/generators/__snapshots__/findByTags.ts b/packages/plugin-client/src/generators/__snapshots__/findByTags.ts index 53f61103f..5e7cfa4c6 100644 --- a/packages/plugin-client/src/generators/__snapshots__/findByTags.ts +++ b/packages/plugin-client/src/generators/__snapshots__/findByTags.ts @@ -6,7 +6,7 @@ import type { RequestConfig } from '@kubb/plugin-client/client' * @summary Finds Pets by tags * @link /pet/findByTags */ -export async function findByTags(params?: FindByTags, config: Partial = {}) { - const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) +export async function findPetsByTags(params?: FindPetsByTagsQueryParams, config: Partial = {}) { + const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) return res.data } diff --git a/packages/plugin-client/src/generators/__snapshots__/findByTagsFull.ts b/packages/plugin-client/src/generators/__snapshots__/findByTagsFull.ts new file mode 100644 index 000000000..fc73b7fde --- /dev/null +++ b/packages/plugin-client/src/generators/__snapshots__/findByTagsFull.ts @@ -0,0 +1,12 @@ +import client from '@kubb/plugin-client/client' +import type { RequestConfig } from '@kubb/plugin-client/client' + +/** + * @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 async function findPetsByTags(params?: FindPetsByTagsQueryParams, config: Partial = {}) { + const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) + return res +} diff --git a/packages/plugin-client/src/generators/__snapshots__/findByTagsWithZod.ts b/packages/plugin-client/src/generators/__snapshots__/findByTagsWithZod.ts index 549206312..220ad16ee 100644 --- a/packages/plugin-client/src/generators/__snapshots__/findByTagsWithZod.ts +++ b/packages/plugin-client/src/generators/__snapshots__/findByTagsWithZod.ts @@ -6,7 +6,7 @@ import type { RequestConfig } from '@kubb/plugin-client/client' * @summary Finds Pets by tags * @link /pet/findByTags */ -export async function findByTagsWithZod(params?: FindByTagsWithZod, config: Partial = {}) { - const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) - return { ...res, data: findByTagsWithZod.parse(res.data) } +export async function findPetsByTags(params?: FindPetsByTagsQueryParams, config: Partial = {}) { + const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) + return findPetsByTagsQueryResponse.parse(res.data) } diff --git a/packages/plugin-client/src/generators/__snapshots__/findByTagsWithZodFull.ts b/packages/plugin-client/src/generators/__snapshots__/findByTagsWithZodFull.ts new file mode 100644 index 000000000..1f2cccb73 --- /dev/null +++ b/packages/plugin-client/src/generators/__snapshots__/findByTagsWithZodFull.ts @@ -0,0 +1,12 @@ +import client from '@kubb/plugin-client/client' +import type { RequestConfig } from '@kubb/plugin-client/client' + +/** + * @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 async function findPetsByTags(params?: FindPetsByTagsQueryParams, config: Partial = {}) { + const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) + return { ...res, data: findPetsByTagsQueryResponse.parse(res.data) } +} diff --git a/packages/plugin-client/src/generators/__snapshots__/importPath.ts b/packages/plugin-client/src/generators/__snapshots__/importPath.ts index 7a2c41ea9..9048de7f6 100644 --- a/packages/plugin-client/src/generators/__snapshots__/importPath.ts +++ b/packages/plugin-client/src/generators/__snapshots__/importPath.ts @@ -6,7 +6,7 @@ import type { RequestConfig } from 'axios' * @summary Finds Pets by tags * @link /pet/findByTags */ -export async function importPath(params?: ImportPath, config: Partial = {}) { - const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) +export async function findPetsByTags(params?: FindPetsByTagsQueryParams, config: Partial = {}) { + const res = await client({ method: 'get', url: `/pet/findByTags`, params, ...config }) return res.data } diff --git a/packages/plugin-client/src/generators/__snapshots__/updatePetById.ts b/packages/plugin-client/src/generators/__snapshots__/updatePetById.ts index 884892afa..db0baa6f5 100644 --- a/packages/plugin-client/src/generators/__snapshots__/updatePetById.ts +++ b/packages/plugin-client/src/generators/__snapshots__/updatePetById.ts @@ -5,7 +5,11 @@ import type { RequestConfig } from '@kubb/plugin-client/client' * @summary Updates a pet in the store with form data * @link /pet/:petId */ -export async function updatePetById(petId: UpdatePetById['petId'], params?: UpdatePetById, config: Partial = {}) { - const res = await client({ method: 'post', url: `/pet/${petId}`, params, ...config }) +export async function updatePetWithForm( + petId: UpdatePetWithFormPathParams['petId'], + params?: UpdatePetWithFormQueryParams, + config: Partial = {}, +) { + const res = await client({ method: 'post', url: `/pet/${petId}`, params, ...config }) return res.data } diff --git a/packages/plugin-client/src/generators/clientGenerator.test.tsx b/packages/plugin-client/src/generators/clientGenerator.test.tsx index 729b16f65..d94ae452e 100644 --- a/packages/plugin-client/src/generators/clientGenerator.test.tsx +++ b/packages/plugin-client/src/generators/clientGenerator.test.tsx @@ -26,6 +26,25 @@ describe('clientGenerator operation', async () => { parser: 'zod', }, }, + { + name: 'findByTagsFull', + input: '../../mocks/petStore.yaml', + path: '/pet/findByTags', + method: 'get', + options: { + dataReturnType: 'full', + }, + }, + { + name: 'findByTagsWithZodFull', + input: '../../mocks/petStore.yaml', + path: '/pet/findByTags', + method: 'get', + options: { + parser: 'zod', + dataReturnType: 'full', + }, + }, { name: 'importPath', input: '../../mocks/petStore.yaml', diff --git a/packages/plugin-client/src/generators/operationsGenerator.test.tsx b/packages/plugin-client/src/generators/operationsGenerator.test.tsx index d96630d23..3425331a0 100644 --- a/packages/plugin-client/src/generators/operationsGenerator.test.tsx +++ b/packages/plugin-client/src/generators/operationsGenerator.test.tsx @@ -33,6 +33,7 @@ describe('operationsGenerator operations', async () => { pathParamsType: 'inline', importPath: '@kubb/plugin-client/client', baseURL: '', + parser: 'client', ...props.options, } const plugin = { options } as Plugin diff --git a/packages/plugin-faker/src/generators/__snapshots__/createPet.ts b/packages/plugin-faker/src/generators/__snapshots__/createPet.ts index 5a8c1cb11..03185354e 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/createPet.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/createPet.ts @@ -1,24 +1,26 @@ +import { faker } from '@faker-js/faker' + /** * @description Null response */ -export function createPet() { +export function createPets201() { return unknown } /** * @description unexpected error */ -export function createPet() { - return createPet() +export function createPetsError() { + return error() } -export function createPet(data: NonNullable> = {}) { +export function createPetsMutationRequest(data: NonNullable> = {}) { return { ...{ name: faker.string.alpha(), tag: faker.string.alpha() }, ...data, } } -export function createPet() { +export function createPetsMutationResponse() { return unknown } diff --git a/packages/plugin-faker/src/generators/__snapshots__/createPetSeed.ts b/packages/plugin-faker/src/generators/__snapshots__/createPetSeed.ts index 0e542f730..79f441cec 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/createPetSeed.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/createPetSeed.ts @@ -1,7 +1,9 @@ +import { faker } from '@faker-js/faker' + /** * @description Null response */ -export function createPetSeed() { +export function createPets201() { faker.seed([222]) return unknown } @@ -9,12 +11,12 @@ export function createPetSeed() { /** * @description unexpected error */ -export function createPetSeed() { +export function createPetsError() { faker.seed([222]) - return createPetSeed() + return error() } -export function createPetSeed(data: NonNullable> = {}) { +export function createPetsMutationRequest(data: NonNullable> = {}) { faker.seed([222]) return { ...{ name: faker.string.alpha(), tag: faker.string.alpha() }, @@ -22,7 +24,7 @@ export function createPetSeed(data: NonNullable> = {}) { } } -export function createPetSeed() { +export function createPetsMutationResponse() { faker.seed([222]) return unknown } diff --git a/packages/plugin-faker/src/generators/__snapshots__/createPetUnknownTypeAny.ts b/packages/plugin-faker/src/generators/__snapshots__/createPetUnknownTypeAny.ts index fd930839b..a333fe805 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/createPetUnknownTypeAny.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/createPetUnknownTypeAny.ts @@ -1,24 +1,26 @@ +import { faker } from '@faker-js/faker' + /** * @description Null response */ -export function createPetUnknownTypeAny() { +export function createPets201() { return undefined } /** * @description unexpected error */ -export function createPetUnknownTypeAny() { - return createPetUnknownTypeAny() +export function createPetsError() { + return error() } -export function createPetUnknownTypeAny(data: NonNullable> = {}) { +export function createPetsMutationRequest(data: NonNullable> = {}) { return { ...{ name: faker.string.alpha(), tag: faker.string.alpha() }, ...data, } } -export function createPetUnknownTypeAny() { +export function createPetsMutationResponse() { return undefined } diff --git a/packages/plugin-faker/src/generators/__snapshots__/deletePet.ts b/packages/plugin-faker/src/generators/__snapshots__/deletePet.ts index ea37d8aa1..e46f94787 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/deletePet.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/deletePet.ts @@ -1,3 +1,3 @@ -export function deletePet() { +export function deletePetsPetidMutationResponse() { return unknown } diff --git a/packages/plugin-faker/src/generators/__snapshots__/enumNames.ts b/packages/plugin-faker/src/generators/__snapshots__/enumNames.ts index f1c0cfbe7..55d589f93 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/enumNames.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/enumNames.ts @@ -1,3 +1,5 @@ -export function enumNames() { +import { faker } from '@faker-js/faker' + +export function enumNamesType() { return faker.helpers.arrayElement(['Pending', 'Received']) } diff --git a/packages/plugin-faker/src/generators/__snapshots__/enumVarNames.ts b/packages/plugin-faker/src/generators/__snapshots__/enumVarNames.ts index 408335557..437e3d901 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/enumVarNames.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/enumVarNames.ts @@ -1,3 +1,5 @@ -export function enumVarNames() { +import { faker } from '@faker-js/faker' + +export function enumVarNamesType() { return faker.helpers.arrayElement(['Pending', 'Received']) } diff --git a/packages/plugin-faker/src/generators/__snapshots__/getPets.ts b/packages/plugin-faker/src/generators/__snapshots__/getPets.ts index 181cf8dfa..68b799f1c 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/getPets.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/getPets.ts @@ -1,4 +1,6 @@ -export function getPets(data: NonNullable> = {}) { +import { faker } from '@faker-js/faker' + +export function listPetsQueryParams(data: NonNullable> = {}) { return { ...{ limit: faker.string.alpha() }, ...data, @@ -8,20 +10,20 @@ export function getPets(data: NonNullable> = {}) { /** * @description A paged array of pets */ -export function getPets() { - return getPets() +export function listPets200() { + return pets() } /** * @description unexpected error */ -export function getPets() { - return getPets() +export function listPetsError() { + return error() } /** * @description A paged array of pets */ -export function getPets() { - return getPets() +export function listPetsQueryResponse() { + return pets() } diff --git a/packages/plugin-faker/src/generators/__snapshots__/pet.ts b/packages/plugin-faker/src/generators/__snapshots__/pet.ts index d7c064dd4..de1b7e8a6 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/pet.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/pet.ts @@ -1,3 +1,5 @@ +import { faker } from '@faker-js/faker' + export function pet(data: NonNullable> = {}) { return { ...{ diff --git a/packages/plugin-faker/src/generators/__snapshots__/petWithDateString.ts b/packages/plugin-faker/src/generators/__snapshots__/petWithDateString.ts index 44fac06fb..a9502ae6d 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/petWithDateString.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/petWithDateString.ts @@ -1,4 +1,6 @@ -export function petWithDateString(data: NonNullable> = {}) { +import { faker } from '@faker-js/faker' + +export function pet(data: NonNullable> = {}) { return { ...{ id: faker.number.int(), diff --git a/packages/plugin-faker/src/generators/__snapshots__/petWithDayjs.ts b/packages/plugin-faker/src/generators/__snapshots__/petWithDayjs.ts index 423e6ca40..edb4866c0 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/petWithDayjs.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/petWithDayjs.ts @@ -1,4 +1,7 @@ -export function petWithDayjs(data: NonNullable> = {}) { +import dayjs from 'dayjs' +import { faker } from '@faker-js/faker' + +export function pet(data: NonNullable> = {}) { return { ...{ id: faker.number.int(), diff --git a/packages/plugin-faker/src/generators/__snapshots__/petWithMapper.ts b/packages/plugin-faker/src/generators/__snapshots__/petWithMapper.ts index 39f572a93..df20b5aa6 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/petWithMapper.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/petWithMapper.ts @@ -1,4 +1,6 @@ -export function petWithMapper(data: NonNullable> = {}) { +import { faker } from '@faker-js/faker' + +export function pet(data: NonNullable> = {}) { return { ...{ id: faker.string.fromCharacters('abc'), diff --git a/packages/plugin-faker/src/generators/__snapshots__/petWithRandExp.ts b/packages/plugin-faker/src/generators/__snapshots__/petWithRandExp.ts index 5d992b0f0..9495b3088 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/petWithRandExp.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/petWithRandExp.ts @@ -1,4 +1,7 @@ -export function petWithRandExp(data: NonNullable> = {}) { +import RandExp from 'randexp' +import { faker } from '@faker-js/faker' + +export function pet(data: NonNullable> = {}) { return { ...{ id: faker.number.int(), diff --git a/packages/plugin-faker/src/generators/__snapshots__/pets.ts b/packages/plugin-faker/src/generators/__snapshots__/pets.ts index bc81603a2..35479b9b8 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/pets.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/pets.ts @@ -1,3 +1,5 @@ +import { faker } from '@faker-js/faker' + export function pets(data: NonNullable> = []) { - return [...(faker.helpers.arrayElements([pets()]) as any), ...data] + return [...(faker.helpers.arrayElements([pet()]) as any), ...data] } diff --git a/packages/plugin-faker/src/generators/__snapshots__/showPetById.ts b/packages/plugin-faker/src/generators/__snapshots__/showPetById.ts index 3c849e160..c44722cb1 100644 --- a/packages/plugin-faker/src/generators/__snapshots__/showPetById.ts +++ b/packages/plugin-faker/src/generators/__snapshots__/showPetById.ts @@ -1,4 +1,6 @@ -export function showPetById(data: NonNullable> = {}) { +import { faker } from '@faker-js/faker' + +export function showPetByIdPathParams(data: NonNullable> = {}) { return { ...{ petId: faker.string.alpha(), testId: faker.string.alpha() }, ...data, @@ -8,20 +10,20 @@ export function showPetById(data: NonNullable> = {}) { /** * @description Expected response to a valid request */ -export function showPetById() { - return showPetById() +export function showPetById200() { + return pet() } /** * @description unexpected error */ -export function showPetById() { - return showPetById() +export function showPetByIdError() { + return error() } /** * @description Expected response to a valid request */ -export function showPetById() { - return showPetById() +export function showPetByIdQueryResponse() { + return pet() } diff --git a/packages/plugin-msw/src/generators/__snapshots__/createPet.ts b/packages/plugin-msw/src/generators/__snapshots__/createPet.ts index bdc57eac7..ed972c789 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/createPet.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/createPet.ts @@ -1,5 +1,7 @@ -export const createPet = http.post('*/pets', function handler(info) { - return new Response(JSON.stringify(createPet()), { +import { http } from 'msw' + +export const createPets = http.post('*/pets', function handler(info) { + return new Response(JSON.stringify(createPetsMutationResponse()), { headers: { 'Content-Type': 'application/json', }, diff --git a/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts b/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts index 6759fa082..0efd01689 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts @@ -1,5 +1,7 @@ -export const deletePet = http.delete('*/pets/:petId', function handler(info) { - return new Response(JSON.stringify(deletePet()), { +import { http } from 'msw' + +export const deletePetsPetid = http.delete('*/pets/:petId', function handler(info) { + return new Response(JSON.stringify(deletePetsPetidMutationResponse()), { headers: { 'Content-Type': 'application/json', }, diff --git a/packages/plugin-msw/src/generators/__snapshots__/getPets.ts b/packages/plugin-msw/src/generators/__snapshots__/getPets.ts index f99041428..b6149d5be 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/getPets.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/getPets.ts @@ -1,5 +1,7 @@ -export const getPets = http.get('*/pets', function handler(info) { - return new Response(JSON.stringify(getPets()), { +import { http } from 'msw' + +export const listPets = http.get('*/pets', function handler(info) { + return new Response(JSON.stringify(listPetsQueryResponse()), { headers: { 'Content-Type': 'application/json', }, diff --git a/packages/plugin-msw/src/generators/__snapshots__/handlers.ts b/packages/plugin-msw/src/generators/__snapshots__/handlers.ts index 7fb824534..429fa9bcd 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/handlers.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/handlers.ts @@ -1 +1,3 @@ -export const handlers = [findByTags, findByTags, findByTags] as const +import { listPets, createPets, showPetById } from './findByTags.ts' + +export const handlers = [listPets, createPets, showPetById] as const diff --git a/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts b/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts index 7199745e8..dc5c934d8 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts @@ -1,5 +1,7 @@ +import { http } from 'msw' + export const showPetById = http.get('*/pets/:petId', function handler(info) { - return new Response(JSON.stringify(showPetById()), { + return new Response(JSON.stringify(showPetByIdQueryResponse()), { headers: { 'Content-Type': 'application/json', }, diff --git a/packages/plugin-react-query/src/components/__snapshots__/gen/showPetById.ts b/packages/plugin-react-query/src/components/__snapshots__/gen/showPetById.ts index 656fad6fd..f25f6e704 100644 --- a/packages/plugin-react-query/src/components/__snapshots__/gen/showPetById.ts +++ b/packages/plugin-react-query/src/components/__snapshots__/gen/showPetById.ts @@ -1,19 +1,19 @@ type GetPetsUuidClient = typeof client type GetPetsUuid = - { - data: GetPetsUuidQueryResponse - error: never - request: never - pathParams: never - queryParams: never - headerParams: never - response: GetPetsUuidQueryResponse - client: { - parameters: Partial[0]> - return: Awaited> - } + { + data: GetPetsUuidQueryResponse + error: never + request: never + pathParams: never + queryParams: never + headerParams: never + response: GetPetsUuidQueryResponse + client: { + parameters: Partial[0]> + return: Awaited> } + } export const GetPetsUuidQueryKey = () => [{ url: '/pets/:uuid', params: {uuid:uuid} }] as const @@ -21,47 +21,47 @@ export type GetPetsUuidQueryKey = ReturnType export function GetPetsUuidQueryOptions(options: GetPetsUuid['client']['parameters'] = {}) { - const queryKey = GetPetsUuidQueryKey() - - return { - queryKey, - queryFn: async () => { - + const queryKey = GetPetsUuidQueryKey() + + return { + queryKey, + queryFn: async () => { + + + const res = await client({ + method: "get", + url: `/pets/${uuid}`, + ...options + }) + + return res.data + }, - const res = await client({ - method: "get", - url: `/pets/${uuid}`, - ...options - }) - - return res.data - }, - - } - - + } + + } /** * @link /pets/:uuid */ export function useGetPetsUuid(options: { - query?: Partial>, - client?: GetPetsUuid['client']['parameters'] - } = {}): UseQueryResult & { queryKey: TQueryKey } { + query?: Partial>, + client?: GetPetsUuid['client']['parameters'] + } = {}): UseQueryResult & { queryKey: TQueryKey } { + + const { query: queryOptions, client: clientOptions = {} } = options ?? {} + const queryKey = queryOptions?.queryKey ?? GetPetsUuidQueryKey() + + const query = useQuery({ + ...GetPetsUuidQueryOptions(clientOptions), + queryKey, + ...queryOptions + }) as UseQueryResult & { queryKey: TQueryKey } + + query.queryKey = queryKey as TQueryKey + + return query - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? GetPetsUuidQueryKey() - - const query = useQuery({ - ...GetPetsUuidQueryOptions(clientOptions), - queryKey, - ...queryOptions - }) as UseQueryResult & { queryKey: TQueryKey } - - query.queryKey = queryKey as TQueryKey - - return query - - + } \ No newline at end of file diff --git a/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePets.ts b/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePets.ts index a80206a0a..3bd0e1fd9 100644 --- a/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePets.ts +++ b/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePets.ts @@ -1,46 +1,46 @@ type CreatePetsClient = typeof client type CreatePets = - { - data: CreatePetsMutationResponse - error: never - request: CreatePetsMutationRequest - pathParams: CreatePetsPathParams - queryParams: CreatePetsQueryParams - headerParams: CreatePetsHeaderParams - response: CreatePetsMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } + { + data: CreatePetsMutationResponse + error: never + request: CreatePetsMutationRequest + pathParams: CreatePetsPathParams + queryParams: CreatePetsQueryParams + headerParams: CreatePetsHeaderParams + response: CreatePetsMutationResponse + client: { + parameters: Partial[0]> + return: Awaited> } + } /** * @summary Create a pet * @link /pets/:uuid */ export function useCreatePets(uuid: CreatePetsPathParams["uuid"], headers: CreatePets['headerParams'], params?: CreatePets['queryParams'], options: { - mutation?: UseMutationOptions, - client?: CreatePets['client']['parameters'] - } = {}) { + mutation?: UseMutationOptions, + client?: CreatePets['client']['parameters'] + } = {}) { - const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {} - - return useMutation({ - mutationFn: async(data) => { - - - const res = await client({ - method: "post", - url: `/pets/${uuid}`, - params, - data, - headers: { ...headers, ...clientOptions.headers }, - ...clientOptions - }) - - return res.data - }, - ...mutationOptions - }) + const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {} + + return useMutation({ + mutationFn: async(data) => { + + + const res = await client({ + method: "post", + url: `/pets/${uuid}`, + params, + data, + headers: { ...headers, ...clientOptions.headers }, + ...clientOptions + }) + + return res.data + }, + ...mutationOptions + }) } \ No newline at end of file diff --git a/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePetsMutate.ts b/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePetsMutate.ts index 6f916b254..4bfac6b09 100644 --- a/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePetsMutate.ts +++ b/packages/plugin-react-query/src/components/__snapshots__/gen/useCreatePetsMutate.ts @@ -1,46 +1,46 @@ type CreatePetsClient = typeof client type CreatePets = - { - data: CreatePetsMutationResponse - error: never - request: CreatePetsMutationRequest - pathParams: CreatePetsPathParams - queryParams: CreatePetsQueryParams - headerParams: CreatePetsHeaderParams - response: CreatePetsMutationResponse - client: { - parameters: Partial[0]> - return: Awaited> - } + { + data: CreatePetsMutationResponse + error: never + request: CreatePetsMutationRequest + pathParams: CreatePetsPathParams + queryParams: CreatePetsQueryParams + headerParams: CreatePetsHeaderParams + response: CreatePetsMutationResponse + client: { + parameters: Partial[0]> + return: Awaited> } + } /** * @summary Create a pet * @link /pets/:uuid */ export function useCreatePets(options: { - mutation?: UseMutationOptions, - client?: CreatePets['client']['parameters'] - } = {}) { + mutation?: UseMutationOptions, + client?: CreatePets['client']['parameters'] + } = {}) { - const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {} - - return useMutation({ - mutationFn: async({ uuid, headers, data, params }) => { - - - const res = await client({ - method: "post", - url: `/pets/${uuid}`, - params, - data, - headers: { ...headers, ...clientOptions.headers }, - ...clientOptions - }) - - return res.data - }, - ...mutationOptions - }) + const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {} + + return useMutation({ + mutationFn: async({ uuid, headers, data, params }) => { + + + const res = await client({ + method: "post", + url: `/pets/${uuid}`, + params, + data, + headers: { ...headers, ...clientOptions.headers }, + ...clientOptions + }) + + return res.data + }, + ...mutationOptions + }) } \ No newline at end of file diff --git a/packages/plugin-react-query/src/generators/queryGenerator.test.tsx b/packages/plugin-react-query/src/generators/queryGenerator.test.tsx deleted file mode 100644 index 34b0d72ea..000000000 --- a/packages/plugin-react-query/src/generators/queryGenerator.test.tsx +++ /dev/null @@ -1,81 +0,0 @@ -// import { createMockedPluginManager, matchFiles, mockedPluginManager } from '@kubb/core/mocks' -// -// import path from 'node:path' -// import type { Plugin } from '@kubb/core' -// import type { HttpMethod } from '@kubb/oas' -// import { parse } from '@kubb/oas/parser' -// import { OperationGenerator } from '@kubb/plugin-oas' -// import { queryGenerator } from './queryGenerator.tsx' -// import type { PluginReactQuery } from '../types.ts'; -// -// describe('queryGenerator operation', async () => { -// const testData = [ -// { -// name: 'showPetById', -// input: '../../mocks/petStore.yaml', -// path: '/pets/{petId}', -// method: 'get', -// options: {}, -// }, -// { -// name: 'getPets', -// input: '../../mocks/petStore.yaml', -// path: '/pets', -// method: 'get', -// options: {}, -// }, -// { -// name: 'createPet', -// input: '../../mocks/petStore.yaml', -// path: '/pets', -// method: 'post', -// options: {}, -// }, -// ] as const satisfies Array<{ -// input: string -// name: string -// path: string -// method: HttpMethod -// options: Partial -// }> -// -// test.each(testData)('$name', async (props) => { -// const oas = await parse(path.resolve(__dirname, props.input)) -// -// const options: PluginReactQuery['resolvedOptions'] = { -// client: { -// dataReturnType: 'data', -// importPath: '@kubb/plugin-client/client', -// }, -// parser: 'zod', -// query: { -// methods: ['get'], -// }, -// mutation: { -// methods: ['post'], -// }, -// ...props.options, -// } -// const plugin = { options } as Plugin -// const instance = new OperationGenerator(options, { -// oas, -// include: undefined, -// pluginManager: createMockedPluginManager(props.name), -// plugin, -// contentType: undefined, -// override: undefined, -// mode: 'split', -// exclude: [], -// }) -// await instance.build(queryGenerator) -// -// const operation = oas.operation(props.path, props.method) -// const files = await queryGenerator.operation?.({ -// operation, -// options, -// instance, -// }) -// -// await matchFiles(files) -// }) -// }) diff --git a/packages/plugin-swr/src/components/Mutation.tsx b/packages/plugin-swr/src/components/Mutation.tsx index 3a85a4e18..36ea04d01 100644 --- a/packages/plugin-swr/src/components/Mutation.tsx +++ b/packages/plugin-swr/src/components/Mutation.tsx @@ -1,10 +1,10 @@ -import transformers from '@kubb/core/transformers' -import { FunctionParams, URLPath } from '@kubb/core/utils' -import { File, Function } from '@kubb/react' +import { URLPath } from '@kubb/core/utils' +import { File, Function, FunctionParams } from '@kubb/react' -import type { Operation } from '@kubb/oas' +import { type Operation, isOptional } from '@kubb/oas' +import { Client } from '@kubb/plugin-client/components' import type { OperationSchemas } from '@kubb/plugin-oas' -import { getASTParams, getComments } from '@kubb/plugin-oas/utils' +import { getComments, getPathParams } from '@kubb/plugin-oas/utils' import type { ReactNode } from 'react' import type { PluginSwr } from '../types.ts' @@ -14,115 +14,89 @@ type Props = { */ name: string typeName: string - typedSchemas: OperationSchemas + clientName: string + typeSchemas: OperationSchemas operation: Operation dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType'] + pathParamsType: PluginSwr['resolvedOptions']['pathParamsType'] } -export function Mutation({ name, typeName, dataReturnType, typedSchemas, operation }: Props): ReactNode { - const path = new URLPath(operation.path) - const returnType = `SWRMutationResponse<${[`${typeName}["response"]`, `${typeName}["error"]`].join(', ')}>` - // TODO use params type - const clientOptions = [ - `method: "${operation.method}"`, - 'url', - typedSchemas.queryParams?.name ? 'params' : undefined, - typedSchemas.request?.name ? 'data' : undefined, - typedSchemas.headerParams?.name ? 'headers: { ...headers, ...clientOptions.headers }' : undefined, - '...clientOptions', - ].filter(Boolean) +type GetParamsProps = { + pathParamsType: PluginSwr['resolvedOptions']['pathParamsType'] + dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType'] + typeSchemas: OperationSchemas +} - const params = new FunctionParams() +function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) { + const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>` - params.add([ - ...getASTParams(typedSchemas.pathParams, { typed: true }), - { - name: 'params', - type: `${typeName}['queryParams']`, - enabled: !!typedSchemas.queryParams?.name, - required: false, + return FunctionParams.factory({ + pathParams: { + mode: pathParamsType === 'object' ? 'object' : 'inlineSpread', + children: getPathParams(typeSchemas.pathParams, { typed: true }), }, - { - name: 'headers', - type: `${typeName}['headerParams']`, - enabled: !!typedSchemas.headerParams?.name, - required: false, - }, - { - name: 'options', - required: false, - type: `{ - mutation?: SWRMutationConfiguration<${[`${typeName}["response"]`, `${typeName}["error"]`].join(', ')}>, - client?: ${typeName}['client']['parameters'], - shouldFetch?: boolean, - }`, + params: typeSchemas.queryParams?.name + ? { + type: typeSchemas.queryParams?.name, + optional: isOptional(typeSchemas.queryParams?.schema), + } + : undefined, + headers: typeSchemas.headerParams?.name + ? { + type: typeSchemas.headerParams?.name, + optional: isOptional(typeSchemas.headerParams?.schema), + } + : undefined, + options: { + type: ` +{ + mutation?: SWRMutationConfiguration<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}>, + client?: ${typeSchemas.request?.name ? `Partial>` : 'Partial'}, + shouldFetch?: boolean, +} +`, default: '{}', }, - ]) - - const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,\n${transformers.createIndent(4)}`)}` - const clientGenerics = [`${typeName}["data"]`, `${typeName}["error"]`, typedSchemas.request?.name ? `${typeName}["request"]` : ''].filter(Boolean).join(', ') - - if (typedSchemas.queryParams?.name) { - const hookGenerics = [`${typeName}["response"]`, `${typeName}["error"]`, '[typeof url, typeof params] | null'].join(', ') + }) +} - return ( - - - {` - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} +export function Mutation({ name, clientName, pathParamsType, dataReturnType, typeSchemas, operation }: Props): ReactNode { + const path = new URLPath(operation.path) + const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>` + const hookGenerics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'Key'] - const url = ${path.template} as const - return useSWRMutation<${hookGenerics}>( - shouldFetch ? [url, params]: null, - async (_url${typedSchemas.request?.name ? ', { arg: data }' : ''}) => { - const res = await client<${clientGenerics}>({ - ${resolvedClientOptions} - }) + const params = getParams({ + pathParamsType, + dataReturnType, + typeSchemas, + }) - return ${dataReturnType === 'data' ? 'res.data' : 'res'} - }, - mutationOptions - ) - `} - - - ) - } + const clientParams = Client.getParams({ + typeSchemas, + pathParamsType, + }) - const hookGenerics = [`${typeName}["response"]`, `${typeName}["error"]`, 'Key'].join(', ') + //fixed name, see Query.getParams and params + const swrKey = [path.template, typeSchemas.queryParams?.name ? 'params' : undefined].filter(Boolean) return ( {` - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - - const url = ${path.template} as const - return useSWRMutation<${hookGenerics}>( - shouldFetch ? url : null, - async (_url${typedSchemas.request?.name ? ', { arg: data }' : ''}) => { - const res = await client<${clientGenerics}>({ - ${resolvedClientOptions} - }) + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} - return ${dataReturnType === 'data' ? 'res.data' : 'res'} + const swrKey = [${swrKey.join(', ')}] as const + return useSWRMutation<${hookGenerics}>( + shouldFetch ? swrKey : null, + async (_url${typeSchemas.request?.name ? ', { arg: data }' : ''}) => { + return ${clientName}(${clientParams.toCall()}) }, mutationOptions ) diff --git a/packages/plugin-swr/src/components/Query.tsx b/packages/plugin-swr/src/components/Query.tsx index feee9174a..99664c70c 100644 --- a/packages/plugin-swr/src/components/Query.tsx +++ b/packages/plugin-swr/src/components/Query.tsx @@ -22,10 +22,14 @@ type Props = { type GetParamsProps = { pathParamsType: PluginSwr['resolvedOptions']['pathParamsType'] + dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType'] + typeSchemas: OperationSchemas } -function getParams({ pathParamsType, typeSchemas }: GetParamsProps) { +function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) { + const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>` + return FunctionParams.factory({ pathParams: { mode: pathParamsType === 'object' ? 'object' : 'inlineSpread', @@ -52,7 +56,7 @@ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) { options: { type: ` { - query?: SWRConfiguration<${['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}>, + query?: SWRConfiguration<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}>, client?: ${typeSchemas.request?.name ? `Partial>` : 'Partial'}, shouldFetch?: boolean, } @@ -64,10 +68,11 @@ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) { export function Query({ name, typeSchemas, queryOptionsName, operation, dataReturnType, pathParamsType }: Props): ReactNode { const path = new URLPath(operation.path) - const generics = [`TData = ${dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`}`] - const hookGenerics = ['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'typeof url | null'] + const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>` + const hookGenerics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'Key'] const params = getParams({ pathParamsType, + dataReturnType, typeSchemas, }) @@ -76,12 +81,14 @@ export function Query({ name, typeSchemas, queryOptionsName, operation, dataRetu typeSchemas, }) + //fixed name, see Query.getParams and params + const swrKey = [path.template, typeSchemas.queryParams?.name ? 'params' : undefined].filter(Boolean) + return ( ( - shouldFetch ? url : null, + shouldFetch ? swrKey : null, { ...${queryOptionsName}(${queryOptionsParams.toCall()}) ...queryOptions diff --git a/packages/plugin-swr/src/components/QueryOptions.tsx b/packages/plugin-swr/src/components/QueryOptions.tsx index 6d879d561..0f6f3946b 100644 --- a/packages/plugin-swr/src/components/QueryOptions.tsx +++ b/packages/plugin-swr/src/components/QueryOptions.tsx @@ -10,7 +10,6 @@ import type { OperationSchemas } from '@kubb/plugin-oas' type Props = { name: string - queryTypeName: string clientName: string typeSchemas: OperationSchemas pathParamsType: PluginSwr['resolvedOptions']['pathParamsType'] @@ -52,7 +51,7 @@ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) { }) } -export function QueryOptions({ name, clientName, queryTypeName, typeSchemas, pathParamsType }: Props): ReactNode { +export function QueryOptions({ name, clientName, typeSchemas, pathParamsType }: Props): ReactNode { const params = getParams({ pathParamsType, typeSchemas }) const clientParams = Client.getParams({ typeSchemas, diff --git a/packages/plugin-swr/src/generators/__snapshots__/clientDataReturnTypeFull.ts b/packages/plugin-swr/src/generators/__snapshots__/clientDataReturnTypeFull.ts index 8281936cf..fc18b92de 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/clientDataReturnTypeFull.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/clientDataReturnTypeFull.ts @@ -1,53 +1,33 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' -import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration, SWRResponse } from 'swr' - -type ClientDataReturnTypeFullClient = typeof client - -type ClientDataReturnTypeFull = { - data: ClientDataReturnTypeFull - error: ClientDataReturnTypeFull - request: never - pathParams: never - queryParams: ClientDataReturnTypeFull - headerParams: never - response: Awaited> - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { RequestConfig, ResponseConfig } from '@kubb/plugin-client/client' +import type { Key, SWRConfiguration } from 'swr' /** * @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 clientDataReturnTypeFull( - params?: ClientDataReturnTypeFull['queryParams'], - options?: { - query?: SWRConfiguration - client?: ClientDataReturnTypeFull['client']['parameters'] +export function findPetsByTags( + params?: FindPetsByTagsQueryParams, + options: { + query?: SWRConfiguration, FindPetsByTags400> + client?: Partial shouldFetch?: boolean - }, -): SWRResponse { - const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/findByTags` - const query = useSWR(shouldFetch ? url : null, { - ...findPetsByTagsQueryOptions(params, clientOptions), + } = {}, +) { + const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/findByTags`, params] as const + return useSWR, FindPetsByTags400, Key>(shouldFetch ? swrKey : null, { + ...findPetsByTagsQueryOptions(params, config), ...queryOptions, }) - return query } -export function findPetsByTagsQueryOptions( - params?: ClientDataReturnTypeFull, - config: Partial = {}, -): SWRConfiguration { +export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, config: Partial = {}) { return { fetcher: async () => { - return clientDataReturnTypeFull(params, config) + return findPetsByTags(params, config) }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/clientGetImportPath.ts b/packages/plugin-swr/src/generators/__snapshots__/clientGetImportPath.ts index 7ce88f04f..b4124ea42 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/clientGetImportPath.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/clientGetImportPath.ts @@ -1,53 +1,33 @@ import client from 'axios' import useSWR from 'swr' import type { RequestConfig } from 'axios' -import type { SWRConfiguration, SWRResponse } from 'swr' - -type ClientGetImportPathClient = typeof client - -type ClientGetImportPath = { - data: ClientGetImportPath - error: ClientGetImportPath - request: never - pathParams: never - queryParams: ClientGetImportPath - headerParams: never - response: ClientGetImportPath - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { Key, SWRConfiguration } from 'swr' /** * @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 clientGetImportPath( - params?: ClientGetImportPath['queryParams'], - options?: { - query?: SWRConfiguration - client?: ClientGetImportPath['client']['parameters'] +export function findPetsByTags( + params?: FindPetsByTagsQueryParams, + options: { + query?: SWRConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRResponse { - const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/findByTags` - const query = useSWR(shouldFetch ? url : null, { - ...findPetsByTagsQueryOptions(params, clientOptions), + } = {}, +) { + const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/findByTags`, params] as const + return useSWR(shouldFetch ? swrKey : null, { + ...findPetsByTagsQueryOptions(params, config), ...queryOptions, }) - return query } -export function findPetsByTagsQueryOptions( - params?: ClientGetImportPath, - config: Partial = {}, -): SWRConfiguration { +export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, config: Partial = {}) { return { fetcher: async () => { - return clientGetImportPath(params, config) + return findPetsByTags(params, config) }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/clientPostImportPath.ts b/packages/plugin-swr/src/generators/__snapshots__/clientPostImportPath.ts index 3929dec61..0c20030fb 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/clientPostImportPath.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/clientPostImportPath.ts @@ -1,48 +1,28 @@ import client from 'axios' import useSWRMutation from 'swr/mutation' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' - -type ClientPostImportPathClient = typeof client - -type ClientPostImportPath = { - data: ClientPostImportPath - error: ClientPostImportPath - request: never - pathParams: ClientPostImportPath - queryParams: ClientPostImportPath - headerParams: never - response: ClientPostImportPath - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { RequestConfig } from 'axios' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' /** * @summary Updates a pet in the store with form data * @link /pet/:petId */ -export function clientPostImportPath( - petId: ClientPostImportPath['petId'], - params?: ClientPostImportPath['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: ClientPostImportPath['client']['parameters'] +export function updatePetWithForm( + petId: UpdatePetWithFormPathParams['petId'], + params?: UpdatePetWithFormQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'post', - url, - params, - ...clientOptions, - }) - return res.data + return updatePetWithForm(petId, params, config) }, mutationOptions, ) diff --git a/packages/plugin-swr/src/generators/__snapshots__/findByTags.ts b/packages/plugin-swr/src/generators/__snapshots__/findByTags.ts index 6caa1a451..1bdded0ab 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/findByTags.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/findByTags.ts @@ -1,53 +1,33 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration, SWRResponse } from 'swr' - -type FindByTagsClient = typeof client - -type FindByTags = { - data: FindByTags - error: FindByTags - request: never - pathParams: never - queryParams: FindByTags - headerParams: never - response: FindByTags - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { Key, SWRConfiguration } from 'swr' /** * @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 findByTags( - params?: FindByTags['queryParams'], - options?: { - query?: SWRConfiguration - client?: FindByTags['client']['parameters'] +export function findPetsByTags( + params?: FindPetsByTagsQueryParams, + options: { + query?: SWRConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRResponse { - const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/findByTags` - const query = useSWR(shouldFetch ? url : null, { - ...findPetsByTagsQueryOptions(params, clientOptions), + } = {}, +) { + const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/findByTags`, params] as const + return useSWR(shouldFetch ? swrKey : null, { + ...findPetsByTagsQueryOptions(params, config), ...queryOptions, }) - return query } -export function findPetsByTagsQueryOptions( - params?: FindByTags, - config: Partial = {}, -): SWRConfiguration { +export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, config: Partial = {}) { return { fetcher: async () => { - return findByTags(params, config) + return findPetsByTags(params, config) }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/findByTagsPathParamsObject.ts b/packages/plugin-swr/src/generators/__snapshots__/findByTagsPathParamsObject.ts index 2b3c976f4..1bdded0ab 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/findByTagsPathParamsObject.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/findByTagsPathParamsObject.ts @@ -1,53 +1,33 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration, SWRResponse } from 'swr' - -type FindByTagsPathParamsObjectClient = typeof client - -type FindByTagsPathParamsObject = { - data: FindByTagsPathParamsObject - error: FindByTagsPathParamsObject - request: never - pathParams: never - queryParams: FindByTagsPathParamsObject - headerParams: never - response: FindByTagsPathParamsObject - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { Key, SWRConfiguration } from 'swr' /** * @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 findByTagsPathParamsObject( - params?: FindByTagsPathParamsObject['queryParams'], - options?: { - query?: SWRConfiguration - client?: FindByTagsPathParamsObject['client']['parameters'] +export function findPetsByTags( + params?: FindPetsByTagsQueryParams, + options: { + query?: SWRConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRResponse { - const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/findByTags` - const query = useSWR(shouldFetch ? url : null, { - ...findPetsByTagsQueryOptions(params, clientOptions), + } = {}, +) { + const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/findByTags`, params] as const + return useSWR(shouldFetch ? swrKey : null, { + ...findPetsByTagsQueryOptions(params, config), ...queryOptions, }) - return query } -export function findPetsByTagsQueryOptions( - params?: FindByTagsPathParamsObject, - config: Partial = {}, -): SWRConfiguration { +export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, config: Partial = {}) { return { fetcher: async () => { - return findByTagsPathParamsObject(params, config) + return findPetsByTags(params, config) }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/findByTagsWithZod.ts b/packages/plugin-swr/src/generators/__snapshots__/findByTagsWithZod.ts index 0d8460064..1bdded0ab 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/findByTagsWithZod.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/findByTagsWithZod.ts @@ -1,53 +1,33 @@ import client from '@kubb/plugin-client/client' import useSWR from 'swr' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration, SWRResponse } from 'swr' - -type FindByTagsWithZodClient = typeof client - -type FindByTagsWithZod = { - data: FindByTagsWithZod - error: FindByTagsWithZod - request: never - pathParams: never - queryParams: FindByTagsWithZod - headerParams: never - response: FindByTagsWithZod - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { Key, SWRConfiguration } from 'swr' /** * @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 findByTagsWithZod( - params?: FindByTagsWithZod['queryParams'], - options?: { - query?: SWRConfiguration - client?: FindByTagsWithZod['client']['parameters'] +export function findPetsByTags( + params?: FindPetsByTagsQueryParams, + options: { + query?: SWRConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRResponse { - const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/findByTags` - const query = useSWR(shouldFetch ? url : null, { - ...findPetsByTagsQueryOptions(params, clientOptions), + } = {}, +) { + const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/findByTags`, params] as const + return useSWR(shouldFetch ? swrKey : null, { + ...findPetsByTagsQueryOptions(params, config), ...queryOptions, }) - return query } -export function findPetsByTagsQueryOptions( - params?: FindByTagsWithZod, - config: Partial = {}, -): SWRConfiguration { +export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, config: Partial = {}) { return { fetcher: async () => { - return findByTagsWithZod(params, config) + return findPetsByTags(params, config) }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/getAsMutation.ts b/packages/plugin-swr/src/generators/__snapshots__/getAsMutation.ts index 0301d3d9d..d0be460cc 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/getAsMutation.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/getAsMutation.ts @@ -1,48 +1,28 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'custom-swr/mutation' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'custom-swr/mutation' - -type GetAsMutationClient = typeof client - -type GetAsMutation = { - data: GetAsMutation - error: GetAsMutation - request: never - pathParams: never - queryParams: GetAsMutation - headerParams: never - response: GetAsMutation - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { RequestConfig } from '@kubb/plugin-client/client' +import type { SWRMutationConfiguration } from 'custom-swr/mutation' +import type { Key } from 'swr' /** * @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 getAsMutation( - params?: GetAsMutation['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: GetAsMutation['client']['parameters'] +export function findPetsByTags( + params?: FindPetsByTagsQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/findByTags` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/findByTags`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'get', - url, - params, - ...clientOptions, - }) - return res.data + return findPetsByTags(params, config) }, mutationOptions, ) diff --git a/packages/plugin-swr/src/generators/__snapshots__/postAsQuery.ts b/packages/plugin-swr/src/generators/__snapshots__/postAsQuery.ts index 0af844751..e7fe78839 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/postAsQuery.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/postAsQuery.ts @@ -1,54 +1,38 @@ import client from '@kubb/plugin-client/client' import useSWR from 'custom-swr' import type { RequestConfig } from '@kubb/plugin-client/client' -import type { SWRConfiguration, SWRResponse } from 'custom-swr' - -type PostAsQueryClient = typeof client - -type PostAsQuery = { - data: PostAsQuery - error: PostAsQuery - request: never - pathParams: PostAsQuery - queryParams: PostAsQuery - headerParams: never - response: PostAsQuery - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { SWRConfiguration } from 'custom-swr' +import type { Key } from 'swr' /** * @summary Updates a pet in the store with form data * @link /pet/:petId */ -export function postAsQuery( - petId: PostAsQuery['petId'], - params?: PostAsQuery['queryParams'], - options?: { - query?: SWRConfiguration - client?: PostAsQuery['client']['parameters'] +export function updatePetWithForm( + petId: UpdatePetWithFormPathParams['petId'], + params?: UpdatePetWithFormQueryParams, + options: { + query?: SWRConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRResponse { - const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` - const query = useSWR(shouldFetch ? url : null, { - ...updatePetWithFormQueryOptions(petId, params, clientOptions), + } = {}, +) { + const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`, params] as const + return useSWR(shouldFetch ? swrKey : null, { + ...updatePetWithFormQueryOptions(petId, params, config), ...queryOptions, }) - return query } -export function updatePetWithFormQueryOptions( - petId: PostAsQuery['petId'], - params?: PostAsQuery, +export function updatePetWithFormQueryOptions( + petId: UpdatePetWithFormPathParams['petId'], + params?: UpdatePetWithFormQueryParams, config: Partial = {}, -): SWRConfiguration { +) { return { fetcher: async () => { - return postAsQuery(petId, params, config) + return updatePetWithForm(petId, params, config) }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/updatePetById.ts b/packages/plugin-swr/src/generators/__snapshots__/updatePetById.ts index 0302115da..286f41802 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/updatePetById.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/updatePetById.ts @@ -1,48 +1,28 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' - -type UpdatePetByIdClient = typeof client - -type UpdatePetById = { - data: UpdatePetById - error: UpdatePetById - request: never - pathParams: UpdatePetById - queryParams: UpdatePetById - headerParams: never - response: UpdatePetById - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { RequestConfig } from '@kubb/plugin-client/client' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' /** * @summary Updates a pet in the store with form data * @link /pet/:petId */ -export function updatePetById( - petId: UpdatePetById['petId'], - params?: UpdatePetById['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: UpdatePetById['client']['parameters'] +export function updatePetWithForm( + petId: UpdatePetWithFormPathParams['petId'], + params?: UpdatePetWithFormQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial shouldFetch?: boolean - }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'post', - url, - params, - ...clientOptions, - }) - return res.data + return updatePetWithForm(petId, params, config) }, mutationOptions, ) diff --git a/packages/plugin-swr/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts b/packages/plugin-swr/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts index cc807bc6b..b412fe73a 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts @@ -1,48 +1,32 @@ import client from '@kubb/plugin-client/client' import useSWRMutation from 'swr/mutation' -import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation' - -type UpdatePetByIdPathParamsObjectClient = typeof client - -type UpdatePetByIdPathParamsObject = { - data: UpdatePetByIdPathParamsObject - error: UpdatePetByIdPathParamsObject - request: never - pathParams: UpdatePetByIdPathParamsObject - queryParams: UpdatePetByIdPathParamsObject - headerParams: never - response: UpdatePetByIdPathParamsObject - client: { - parameters: Partial[0]> - return: Awaited> - } -} +import type { RequestConfig } from '@kubb/plugin-client/client' +import type { Key } from 'swr' +import type { SWRMutationConfiguration } from 'swr/mutation' /** * @summary Updates a pet in the store with form data * @link /pet/:petId */ -export function updatePetByIdPathParamsObject( - petId: UpdatePetByIdPathParamsObject['petId'], - params?: UpdatePetByIdPathParamsObject['queryParams'], - options?: { - mutation?: SWRMutationConfiguration - client?: UpdatePetByIdPathParamsObject['client']['parameters'] - shouldFetch?: boolean +export function updatePetWithForm( + { + petId, + }: { + petId: UpdatePetWithFormPathParams['petId'] }, -): SWRMutationResponse { - const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {} - const url = `/pet/${petId}` as const - return useSWRMutation( - shouldFetch ? [url, params] : null, + params?: UpdatePetWithFormQueryParams, + options: { + mutation?: SWRMutationConfiguration + client?: Partial + shouldFetch?: boolean + } = {}, +) { + const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {} + const swrKey = [`/pet/${petId}`, params] as const + return useSWRMutation( + shouldFetch ? swrKey : null, async (_url) => { - const res = await client({ - method: 'post', - url, - params, - ...clientOptions, - }) - return res.data + return updatePetWithForm({ petId }, params, config) }, mutationOptions, ) diff --git a/packages/plugin-swr/src/generators/mutationGenerator.tsx b/packages/plugin-swr/src/generators/mutationGenerator.tsx index d9f7d8b36..e9180c2fd 100644 --- a/packages/plugin-swr/src/generators/mutationGenerator.tsx +++ b/packages/plugin-swr/src/generators/mutationGenerator.tsx @@ -1,10 +1,11 @@ +import { pluginClientName } from '@kubb/plugin-client' +import { Client } from '@kubb/plugin-client/components' import { createReactGenerator } from '@kubb/plugin-oas' import { useOperationManager } from '@kubb/plugin-oas/hooks' import { pluginTsName } from '@kubb/plugin-ts' import { pluginZodName } from '@kubb/plugin-zod' import { File, useApp } from '@kubb/react' import { Mutation } from '../components' -import { SchemaType } from '../components/SchemaType.tsx' import type { PluginSwr } from '../types' export const mutationGenerator = createReactGenerator({ @@ -34,6 +35,10 @@ export const mutationGenerator = createReactGenerator({ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }), } + const client = { + name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }), + } + if (!isMutate) { return null } @@ -47,7 +52,7 @@ export const mutationGenerator = createReactGenerator({ - + ({ path={type.file.path} isTypeOnly /> - - + ) diff --git a/packages/plugin-swr/src/generators/queryGenerator.tsx b/packages/plugin-swr/src/generators/queryGenerator.tsx index 0a8ffd55a..45b0c635f 100644 --- a/packages/plugin-swr/src/generators/queryGenerator.tsx +++ b/packages/plugin-swr/src/generators/queryGenerator.tsx @@ -7,7 +7,6 @@ import { pluginTsName } from '@kubb/plugin-ts' import { pluginZodName } from '@kubb/plugin-zod' import { File, useApp } from '@kubb/react' import { Query, QueryOptions } from '../components' -import { SchemaType } from '../components/SchemaType.tsx' import type { PluginSwr } from '../types' export const queryGenerator = createReactGenerator({ @@ -52,6 +51,7 @@ export const queryGenerator = createReactGenerator({ return ( {options.parser === 'zod' && } + @@ -84,13 +84,7 @@ export const queryGenerator = createReactGenerator({ pathParamsType={options.pathParamsType} parser={options.parser} /> - + createPet) +export const createPetsError = z.lazy(() => error) -export const createPet = z.object({ name: z.string(), tag: z.string() }) +export const createPetsMutationRequest = z.object({ name: z.string(), tag: z.string() }) -export const createPet = z.any() +export const createPetsMutationResponse = z.any() diff --git a/packages/plugin-zod/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts b/packages/plugin-zod/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts index 3ffea8b32..542e434b9 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts @@ -1,13 +1,15 @@ +import { z } from 'zod' + /** * @description Null response */ -export const createPetWithUnknownTypeUnknown = z.unknown() +export const createPets201 = z.unknown() /** * @description unexpected error */ -export const createPetWithUnknownTypeUnknown = z.lazy(() => createPetWithUnknownTypeUnknown) +export const createPetsError = z.lazy(() => error) -export const createPetWithUnknownTypeUnknown = z.object({ name: z.string(), tag: z.string() }) +export const createPetsMutationRequest = z.object({ name: z.string(), tag: z.string() }) -export const createPetWithUnknownTypeUnknown = z.unknown() +export const createPetsMutationResponse = z.unknown() diff --git a/packages/plugin-zod/src/generators/__snapshots__/deletePet.ts b/packages/plugin-zod/src/generators/__snapshots__/deletePet.ts index ac42a7f9a..c58b055d4 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/deletePet.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/deletePet.ts @@ -1 +1,3 @@ -export const deletePet = z.any() \ No newline at end of file +import { z } from 'zod' + +export const deletePetsPetidMutationResponse = z.any() diff --git a/packages/plugin-zod/src/generators/__snapshots__/enumNamesType.ts b/packages/plugin-zod/src/generators/__snapshots__/enumNamesType.ts index fafaa170e..0d04ed8c4 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/enumNamesType.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/enumNamesType.ts @@ -1 +1,3 @@ +import { z } from 'zod' + export const enumNamesType = z.enum(['0', '1']) diff --git a/packages/plugin-zod/src/generators/__snapshots__/enumNullable.ts b/packages/plugin-zod/src/generators/__snapshots__/enumNullable.ts index f3ac64a83..3cf0d64f6 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/enumNullable.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/enumNullable.ts @@ -1 +1,3 @@ +import { z } from 'zod' + export const enumNullable = z.enum(['Pending', 'Received']).nullable() diff --git a/packages/plugin-zod/src/generators/__snapshots__/enumVarNamesType.ts b/packages/plugin-zod/src/generators/__snapshots__/enumVarNamesType.ts index 29a647729..ae4c22334 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/enumVarNamesType.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/enumVarNamesType.ts @@ -1 +1,3 @@ -export const enumVarNamesType = z.union([z.literal(0), z.literal(1)]) \ No newline at end of file +import { z } from 'zod' + +export const enumVarNamesType = z.union([z.literal(0), z.literal(1)]) diff --git a/packages/plugin-zod/src/generators/__snapshots__/example.ts b/packages/plugin-zod/src/generators/__snapshots__/example.ts index 63cb26475..846730be4 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/example.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/example.ts @@ -1 +1,3 @@ +import { z } from 'zod' + export const example = z.object({ nestedExamples: z.lazy(() => example).optional() }) diff --git a/packages/plugin-zod/src/generators/__snapshots__/getPets.ts b/packages/plugin-zod/src/generators/__snapshots__/getPets.ts index 6a4d9f998..0a73dc829 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/getPets.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/getPets.ts @@ -1,16 +1,18 @@ -export const getPets = z.object({ limit: z.string().describe('How many items to return at one time (max 100)').optional() }) +import { z } from 'zod' + +export const listPetsQueryParams = z.object({ limit: z.string().describe('How many items to return at one time (max 100)').optional() }).optional() /** * @description A paged array of pets */ -export const getPets = z.lazy(() => getPets) +export const listPets200 = z.lazy(() => pets) /** * @description unexpected error */ -export const getPets = z.lazy(() => getPets) +export const listPetsError = z.lazy(() => error) /** * @description A paged array of pets */ -export const getPets = z.lazy(() => getPets) +export const listPetsQueryResponse = z.lazy(() => pets) diff --git a/packages/plugin-zod/src/generators/__snapshots__/mixedValueTypeConst.ts b/packages/plugin-zod/src/generators/__snapshots__/mixedValueTypeConst.ts index f14fa582f..9ec8a2d1f 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/mixedValueTypeConst.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/mixedValueTypeConst.ts @@ -1,3 +1,5 @@ +import { z } from 'zod' + /** * @description This probably should fail miserably */ diff --git a/packages/plugin-zod/src/generators/__snapshots__/nullableString.ts b/packages/plugin-zod/src/generators/__snapshots__/nullableString.ts index 2c83e729f..ab6a30471 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/nullableString.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/nullableString.ts @@ -1 +1,3 @@ -export const nullableString = z.string().nullable() \ No newline at end of file +import { z } from 'zod' + +export const nullableString = z.string().nullable() diff --git a/packages/plugin-zod/src/generators/__snapshots__/nullableStringUuid.ts b/packages/plugin-zod/src/generators/__snapshots__/nullableStringUuid.ts index a14b69984..d92961b4e 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/nullableStringUuid.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/nullableStringUuid.ts @@ -1 +1,3 @@ -export const nullableStringUuid = z.string().uuid().nullable() \ No newline at end of file +import { z } from 'zod' + +export const nullableStringUuid = z.string().uuid().nullable() diff --git a/packages/plugin-zod/src/generators/__snapshots__/nullableStringWithAnyOf.ts b/packages/plugin-zod/src/generators/__snapshots__/nullableStringWithAnyOf.ts index edfec7e5e..03091f1e9 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/nullableStringWithAnyOf.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/nullableStringWithAnyOf.ts @@ -1 +1,3 @@ -export const nullableStringWithAnyOf = z.union([z.string(), z.null()]) \ No newline at end of file +import { z } from 'zod' + +export const nullableStringWithAnyOf = z.union([z.string(), z.null()]) diff --git a/packages/plugin-zod/src/generators/__snapshots__/numberValueConst.ts b/packages/plugin-zod/src/generators/__snapshots__/numberValueConst.ts index 6ae885351..4c59371a0 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/numberValueConst.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/numberValueConst.ts @@ -1,3 +1,5 @@ +import { z } from 'zod' + /** * @description its value is equal to the value of the keyword */ diff --git a/packages/plugin-zod/src/generators/__snapshots__/oneof.ts b/packages/plugin-zod/src/generators/__snapshots__/oneof.ts index 5792f19fb..d215a4a4f 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/oneof.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/oneof.ts @@ -1 +1,3 @@ -export const oneof = z.union([z.object({ propertyA: z.string().optional() }), z.object({ propertyA: z.string().optional() })]) +import { z } from 'zod' + +export const test = z.union([z.object({ propertyA: z.string().optional() }), z.object({ propertyA: z.string().optional() })]) diff --git a/packages/plugin-zod/src/generators/__snapshots__/operations.ts b/packages/plugin-zod/src/generators/__snapshots__/operations.ts index aca4487ff..32539647a 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/operations.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/operations.ts @@ -1,40 +1,49 @@ +import { + listPetsQueryResponse, + listPetsQueryParams, + createPetsMutationRequest, + createPetsMutationResponse, + showPetByIdQueryResponse, + showPetByIdPathParams, +} from './showPetById.ts' + export const operations = { listPets: { request: undefined, parameters: { path: undefined, - query: showPetById, + query: listPetsQueryParams, header: undefined, }, responses: { - 200: showPetById, - default: showPetById, + 200: listPetsQueryResponse, + default: listPetsQueryResponse, }, errors: {}, }, createPets: { - request: showPetById, + request: createPetsMutationRequest, parameters: { path: undefined, query: undefined, header: undefined, }, responses: { - 201: showPetById, - default: showPetById, + 201: createPetsMutationResponse, + default: createPetsMutationResponse, }, errors: {}, }, showPetById: { request: undefined, parameters: { - path: showPetById, + path: showPetByIdPathParams, query: undefined, header: undefined, }, responses: { - 200: showPetById, - default: showPetById, + 200: showPetByIdQueryResponse, + default: showPetByIdQueryResponse, }, errors: {}, }, diff --git a/packages/plugin-zod/src/generators/__snapshots__/optionalPetInfer.ts b/packages/plugin-zod/src/generators/__snapshots__/optionalPetInfer.ts index 23adbaf1f..94a9133cc 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/optionalPetInfer.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/optionalPetInfer.ts @@ -1,3 +1,5 @@ -export const optionalPetInfer = z.object({ id: z.number().int().optional(), name: z.string().optional(), tag: z.string().optional() }) +import { z } from 'zod' -export type OptionalPetInfer = z.infer +export const optionalPet = z.object({ id: z.number().int().optional(), name: z.string().optional(), tag: z.string().optional() }) + +export type OptionalPet = z.infer diff --git a/packages/plugin-zod/src/generators/__snapshots__/optionalPetTyped.ts b/packages/plugin-zod/src/generators/__snapshots__/optionalPetTyped.ts index 8488c09fb..c71e6bc74 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/optionalPetTyped.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/optionalPetTyped.ts @@ -1,5 +1,3 @@ -export const optionalPetTyped = z.object({ - id: z.number().int().optional(), - name: z.string().optional(), - tag: z.string().optional(), -}) as z.ZodType +import { z } from 'zod' + +export const optionalPet = z.object({ id: z.number().int().optional(), name: z.string().optional(), tag: z.string().optional() }) as z.ZodType diff --git a/packages/plugin-zod/src/generators/__snapshots__/order.ts b/packages/plugin-zod/src/generators/__snapshots__/order.ts index 393398c99..51778bf4a 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/order.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/order.ts @@ -1,3 +1,5 @@ +import { z } from 'zod' + export const order = z.object({ id: z.number().int().optional(), petId: z.number().int().optional(), diff --git a/packages/plugin-zod/src/generators/__snapshots__/orderDateTyeString.ts b/packages/plugin-zod/src/generators/__snapshots__/orderDateTyeString.ts index a787f3f70..e0cf7af5a 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/orderDateTyeString.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/orderDateTyeString.ts @@ -1,4 +1,6 @@ -export const orderDateTyestring = z.object({ +import { z } from 'zod' + +export const order = z.object({ id: z.number().int().optional(), petId: z.number().int().optional(), quantity: z.number().int().optional(), diff --git a/packages/plugin-zod/src/generators/__snapshots__/orderDateTypeFalse.ts b/packages/plugin-zod/src/generators/__snapshots__/orderDateTypeFalse.ts index e15677612..855140409 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/orderDateTypeFalse.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/orderDateTypeFalse.ts @@ -1,4 +1,6 @@ -export const orderDateTypeFalse = z.object({ +import { z } from 'zod' + +export const order = z.object({ id: z.number().int().optional(), petId: z.number().int().optional(), quantity: z.number().int().optional(), diff --git a/packages/plugin-zod/src/generators/__snapshots__/orderDateTypeString.ts b/packages/plugin-zod/src/generators/__snapshots__/orderDateTypeString.ts new file mode 100644 index 000000000..e0cf7af5a --- /dev/null +++ b/packages/plugin-zod/src/generators/__snapshots__/orderDateTypeString.ts @@ -0,0 +1,10 @@ +import { z } from 'zod' + +export const order = z.object({ + id: z.number().int().optional(), + petId: z.number().int().optional(), + quantity: z.number().int().optional(), + shipDate: z.string().datetime().optional(), + status: z.enum(['placed', 'approved', 'delivered']).describe('Order Status').optional(), + complete: z.boolean().optional(), +}) diff --git a/packages/plugin-zod/src/generators/__snapshots__/pet.ts b/packages/plugin-zod/src/generators/__snapshots__/pet.ts index 3212ef678..56342a123 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/pet.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/pet.ts @@ -1 +1,3 @@ +import { z } from 'zod' + export const pet = z.object({ id: z.number().int(), name: z.string(), date: z.date().optional(), tag: z.string().min(5).max(100).optional() }) diff --git a/packages/plugin-zod/src/generators/__snapshots__/petArray.ts b/packages/plugin-zod/src/generators/__snapshots__/petArray.ts index f6027b7da..b908dcbbf 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/petArray.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/petArray.ts @@ -1,8 +1,10 @@ +import { z } from 'zod' + /** * @description List of Pet object */ export const petArray = z - .array(z.lazy(() => petArray)) + .array(z.lazy(() => pet)) .min(1) .max(3) .describe('List of Pet object') diff --git a/packages/plugin-zod/src/generators/__snapshots__/petCoercion.ts b/packages/plugin-zod/src/generators/__snapshots__/petCoercion.ts index 662efb44d..8bf1f14d6 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/petCoercion.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/petCoercion.ts @@ -1,4 +1,6 @@ -export const petCoercion = z.object({ +import { z } from 'zod' + +export const pet = z.object({ id: z.coerce.number().int(), name: z.coerce.string(), date: z.coerce.date().optional(), diff --git a/packages/plugin-zod/src/generators/__snapshots__/petTupleObject.ts b/packages/plugin-zod/src/generators/__snapshots__/petTupleObject.ts index 8f4de35a8..e45a7802c 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/petTupleObject.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/petTupleObject.ts @@ -1,3 +1,5 @@ +import { z } from 'zod' + /** * @description Tuple of exact length 2 nested in an object */ diff --git a/packages/plugin-zod/src/generators/__snapshots__/petWithMapper.ts b/packages/plugin-zod/src/generators/__snapshots__/petWithMapper.ts index 0280717ca..90fdc76a3 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/petWithMapper.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/petWithMapper.ts @@ -1 +1,3 @@ -export const petWithMapper = z.object({ id: z.number().int(), name: z.string().email(), date: z.date().optional(), tag: z.string().min(5).max(100).optional() }) +import { z } from 'zod' + +export const pet = z.object({ id: z.number().int(), name: z.string().email(), date: z.date().optional(), tag: z.string().min(5).max(100).optional() }) diff --git a/packages/plugin-zod/src/generators/__snapshots__/pets.ts b/packages/plugin-zod/src/generators/__snapshots__/pets.ts index 83528ec87..559d8df70 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/pets.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/pets.ts @@ -1 +1,3 @@ +import { z } from 'zod' + export const pets = z.array(z.object({ id: z.number().int(), name: z.string(), tag: z.string().optional() })) as z.ZodType diff --git a/packages/plugin-zod/src/generators/__snapshots__/recursive.ts b/packages/plugin-zod/src/generators/__snapshots__/recursive.ts index 0aec6b3e9..f860ee3c3 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/recursive.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/recursive.ts @@ -1 +1,3 @@ -export const recursive = z.object({ name: z.string(), children: z.array(z.lazy(() => recursive)) }) +import { z } from 'zod' + +export const example = z.object({ name: z.string(), children: z.array(z.lazy(() => example)) }) diff --git a/packages/plugin-zod/src/generators/__snapshots__/showPetById.ts b/packages/plugin-zod/src/generators/__snapshots__/showPetById.ts index 671110bdf..99afd833c 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/showPetById.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/showPetById.ts @@ -1,4 +1,6 @@ -export const showPetById = z.object({ +import { z } from 'zod' + +export const showPetByIdPathParams = z.object({ petId: z.string().describe('The id of the pet to retrieve'), testId: z.string().describe('The id of the pet to retrieve'), }) @@ -6,14 +8,14 @@ export const showPetById = z.object({ /** * @description Expected response to a valid request */ -export const showPetById = z.lazy(() => showPetById) +export const showPetById200 = z.lazy(() => pet) /** * @description unexpected error */ -export const showPetById = z.lazy(() => showPetById) +export const showPetByIdError = z.lazy(() => error) /** * @description Expected response to a valid request */ -export const showPetById = z.lazy(() => showPetById) +export const showPetByIdQueryResponse = z.lazy(() => pet) diff --git a/packages/plugin-zod/src/generators/__snapshots__/stringValueConst.ts b/packages/plugin-zod/src/generators/__snapshots__/stringValueConst.ts index a12dcd298..36a84f27b 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/stringValueConst.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/stringValueConst.ts @@ -1,3 +1,5 @@ +import { z } from 'zod' + /** * @description its value is equal to the value of the keyword */ diff --git a/packages/plugin-zod/src/generators/__snapshots__/uuidSchema.ts b/packages/plugin-zod/src/generators/__snapshots__/uuidSchema.ts index 6288bb680..2436d7dc7 100644 --- a/packages/plugin-zod/src/generators/__snapshots__/uuidSchema.ts +++ b/packages/plugin-zod/src/generators/__snapshots__/uuidSchema.ts @@ -1 +1,3 @@ -export const uuidSchema = z.string().uuid() \ No newline at end of file +import { z } from 'zod' + +export const uuidSchema = z.string().uuid() diff --git a/packages/plugin-zod/src/generators/zodGenerator.test.tsx b/packages/plugin-zod/src/generators/zodGenerator.test.tsx index 2c0e0fbdd..6dd7fa236 100644 --- a/packages/plugin-zod/src/generators/zodGenerator.test.tsx +++ b/packages/plugin-zod/src/generators/zodGenerator.test.tsx @@ -78,7 +78,7 @@ describe('zodGenerator schema', async () => { options: {}, }, { - name: 'OrderDateTyestring', + name: 'OrderDateTypeString', path: 'Order', input: '../../mocks/petStore.yaml', options: {