diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index a4f436e78..55306c172 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -167,7 +167,7 @@ export default defineConfig({ }, { text: '@kubb/react-templates under construction', - link: '/plugins/react-templates', + link: '/plugins/react-template', }, { text: 'Swagger plugins', diff --git a/docs/plugins/react-template.md b/docs/plugins/react-template.md index aecebdaa7..304a1671b 100644 --- a/docs/plugins/react-template.md +++ b/docs/plugins/react-template.md @@ -6,8 +6,11 @@ outline: deep --- # @kubb/react-template +Use React to create templates/variants for any plugin. +
+ ## Installation ::: code-group diff --git a/docs/plugins/swagger-client/index.md b/docs/plugins/swagger-client/index.md index b5b80e412..0b5710188 100644 --- a/docs/plugins/swagger-client/index.md +++ b/docs/plugins/swagger-client/index.md @@ -70,6 +70,15 @@ Relative to the root Type: `string`
Default: `"@kubb/swagger-client/ts-client"` +### dataReturnType +ReturnType that needs to be used when calling client(). + +`Data` will return ResponseConfig[data].
+`Full` will return ResponseConfig. + +Type: `string`
+Default: `"data"` + ### skipBy Array containing skipBy paramaters to exclude/skip tags/operations/methods/paths. @@ -86,6 +95,7 @@ Override the name of the client that is getting generated, this will also overri Type: `(name: string) => string`
+ ## Depended - [`@kubb/swagger`](/plugins/swagger) diff --git a/docs/plugins/swagger-swr.md b/docs/plugins/swagger-swr.md index d8397e213..65b150233 100644 --- a/docs/plugins/swagger-swr.md +++ b/docs/plugins/swagger-swr.md @@ -30,7 +30,6 @@ yarn add @kubb/swagger-swr ::: - ## Options @@ -70,6 +69,15 @@ Relative to the root Type: `string`
Default: `"@kubb/swagger-client/ts-client"` +### dataReturnType +ReturnType that needs to be used when calling client(). + +`Data` will return ResponseConfig[data].
+`Full` will return ResponseConfig. + +Type: `string`
+Default: `"data"` + ### skipBy Array containing skipBy paramaters to exclude/skip tags/operations/methods/paths. diff --git a/docs/plugins/swagger-tanstack-query.md b/docs/plugins/swagger-tanstack-query.md index 5ae09a3bc..97691ee53 100644 --- a/docs/plugins/swagger-tanstack-query.md +++ b/docs/plugins/swagger-tanstack-query.md @@ -75,6 +75,15 @@ Relative to the root Type: `string`
Default: `"@kubb/swagger-client/ts-client"` +### dataReturnType +ReturnType that needs to be used when calling client(). + +`Data` will return ResponseConfig[data].
+`Full` will return ResponseConfig. + +Type: `string`
+Default: `"data"` + ### framework Framework to be generated for. diff --git a/docs/plugins/swagger-ts.md b/docs/plugins/swagger-ts.md index 21b58e401..1bf0a7f41 100644 --- a/docs/plugins/swagger-ts.md +++ b/docs/plugins/swagger-ts.md @@ -81,7 +81,6 @@ date: Date Type: `'string' | 'date'`
Default: `'string'` - ### optionalType Choose what to use as mode for an optional value.
diff --git a/examples/advanced/kubb.config.ts b/examples/advanced/kubb.config.ts index 1b4a4998a..438aaa7ad 100644 --- a/examples/advanced/kubb.config.ts +++ b/examples/advanced/kubb.config.ts @@ -46,6 +46,7 @@ export default defineConfig(async () => { groupBy: { type: 'tag' }, client: './src/client.ts', infinite: {}, + dataReturnType: 'full', }, ], [ @@ -60,6 +61,7 @@ export default defineConfig(async () => { ], groupBy: { type: 'tag' }, client: './src/client.ts', + dataReturnType: 'full', }, ], [ @@ -74,6 +76,7 @@ export default defineConfig(async () => { ], groupBy: { type: 'tag', output: './clients/axios/{{tag}}Service' }, client: './src/client.ts', + dataReturnType: 'full', }, ], [ diff --git a/examples/advanced/src/gen/clients/axios/petService/addPet.ts b/examples/advanced/src/gen/clients/axios/petService/addPet.ts index 0281cc40a..59bd56483 100644 --- a/examples/advanced/src/gen/clients/axios/petService/addPet.ts +++ b/examples/advanced/src/gen/clients/axios/petService/addPet.ts @@ -10,13 +10,11 @@ import type { AddPetMutationRequest, AddPetMutationResponse } from '../../../mod export async function addPet( data: TVariables, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'post', url: `/pet`, data, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petService/deletePet.ts b/examples/advanced/src/gen/clients/axios/petService/deletePet.ts index 78773ad4c..203de3fd0 100644 --- a/examples/advanced/src/gen/clients/axios/petService/deletePet.ts +++ b/examples/advanced/src/gen/clients/axios/petService/deletePet.ts @@ -11,13 +11,11 @@ export async function deletePet( petId: DeletePetPathParams['petId'], headers?: DeletePetHeaderParams, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'delete', url: `/pet/${petId}`, headers: { ...headers, ...options.headers }, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts b/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts index 26831e0a8..583153934 100644 --- a/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts @@ -10,13 +10,11 @@ import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams } from export async function findPetsByStatus( params?: FindPetsByStatusQueryParams, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'get', url: `/pet/findByStatus`, params, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts b/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts index 5acc83b54..0e7210703 100644 --- a/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts +++ b/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts @@ -11,14 +11,12 @@ export async function findPetsByTags( headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'get', url: `/pet/findByTags`, params, headers: { ...headers, ...options.headers }, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petService/getPetById.ts b/examples/advanced/src/gen/clients/axios/petService/getPetById.ts index c15ef61da..86c11721f 100644 --- a/examples/advanced/src/gen/clients/axios/petService/getPetById.ts +++ b/examples/advanced/src/gen/clients/axios/petService/getPetById.ts @@ -10,12 +10,10 @@ import type { GetPetByIdQueryResponse, GetPetByIdPathParams } from '../../../mod export async function getPetById( petId: GetPetByIdPathParams['petId'], options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'get', url: `/pet/${petId}`, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petService/updatePet.ts b/examples/advanced/src/gen/clients/axios/petService/updatePet.ts index bddaf9b77..3e8e972b4 100644 --- a/examples/advanced/src/gen/clients/axios/petService/updatePet.ts +++ b/examples/advanced/src/gen/clients/axios/petService/updatePet.ts @@ -10,13 +10,11 @@ import type { UpdatePetMutationRequest, UpdatePetMutationResponse } from '../../ export async function updatePet( data: TVariables, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'put', url: `/pet`, data, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts b/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts index 082c4d70c..9aef29da9 100644 --- a/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts +++ b/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts @@ -14,13 +14,11 @@ export async function updatePetWithForm[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'post', url: `/pet/${petId}`, params, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts b/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts index f13b380d5..334b0f69e 100644 --- a/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts +++ b/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts @@ -16,14 +16,12 @@ export async function uploadFile[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'post', url: `/pet/${petId}/uploadImage`, params, data, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/petsService/createPets.ts b/examples/advanced/src/gen/clients/axios/petsService/createPets.ts index ce210e5c8..b21bc8e25 100644 --- a/examples/advanced/src/gen/clients/axios/petsService/createPets.ts +++ b/examples/advanced/src/gen/clients/axios/petsService/createPets.ts @@ -18,8 +18,8 @@ export async function createPets[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'post', url: `/pets/${uuid}`, params, @@ -27,6 +27,4 @@ export async function createPets( data?: TVariables, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'post', url: `/user`, data, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/userService/createUsersWithListInput.ts b/examples/advanced/src/gen/clients/axios/userService/createUsersWithListInput.ts index cf69a376d..e452c36f8 100644 --- a/examples/advanced/src/gen/clients/axios/userService/createUsersWithListInput.ts +++ b/examples/advanced/src/gen/clients/axios/userService/createUsersWithListInput.ts @@ -13,13 +13,11 @@ import type { export async function createUsersWithListInput( data?: TVariables, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'post', url: `/user/createWithList`, data, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/userService/deleteUser.ts b/examples/advanced/src/gen/clients/axios/userService/deleteUser.ts index 1fd85b5f7..287503988 100644 --- a/examples/advanced/src/gen/clients/axios/userService/deleteUser.ts +++ b/examples/advanced/src/gen/clients/axios/userService/deleteUser.ts @@ -10,12 +10,10 @@ import type { DeleteUserMutationResponse, DeleteUserPathParams } from '../../../ export async function deleteUser( username: DeleteUserPathParams['username'], options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'delete', url: `/user/${username}`, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/userService/getUserByName.ts b/examples/advanced/src/gen/clients/axios/userService/getUserByName.ts index 70791c301..2b1046cf5 100644 --- a/examples/advanced/src/gen/clients/axios/userService/getUserByName.ts +++ b/examples/advanced/src/gen/clients/axios/userService/getUserByName.ts @@ -9,12 +9,10 @@ import type { GetUserByNameQueryResponse, GetUserByNamePathParams } from '../../ export async function getUserByName( username: GetUserByNamePathParams['username'], options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'get', url: `/user/${username}`, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/userService/loginUser.ts b/examples/advanced/src/gen/clients/axios/userService/loginUser.ts index c67386564..0e62a0556 100644 --- a/examples/advanced/src/gen/clients/axios/userService/loginUser.ts +++ b/examples/advanced/src/gen/clients/axios/userService/loginUser.ts @@ -9,13 +9,11 @@ import type { LoginUserQueryResponse, LoginUserQueryParams } from '../../../mode export async function loginUser( params?: LoginUserQueryParams, options: Partial[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'get', url: `/user/login`, params, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/userService/logoutUser.ts b/examples/advanced/src/gen/clients/axios/userService/logoutUser.ts index e5dc557bf..2eb677777 100644 --- a/examples/advanced/src/gen/clients/axios/userService/logoutUser.ts +++ b/examples/advanced/src/gen/clients/axios/userService/logoutUser.ts @@ -6,12 +6,10 @@ import type { LogoutUserQueryResponse } from '../../../models/ts/userController/ * @summary Logs out current logged in user session * @link /user/logout */ -export async function logoutUser(options: Partial[0]> = {}): Promise['data']> { - const { data: resData } = await client({ +export async function logoutUser(options: Partial[0]> = {}): Promise> { + return client({ method: 'get', url: `/user/logout`, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/axios/userService/updateUser.ts b/examples/advanced/src/gen/clients/axios/userService/updateUser.ts index a41f5f31b..de85310a1 100644 --- a/examples/advanced/src/gen/clients/axios/userService/updateUser.ts +++ b/examples/advanced/src/gen/clients/axios/userService/updateUser.ts @@ -11,13 +11,11 @@ export async function updateUser[0]> = {}, -): Promise['data']> { - const { data: resData } = await client({ +): Promise> { + return client({ method: 'put', url: `/user/${username}`, data, ...options, }) - - return resData } diff --git a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts index 684ac3d82..63ecaa100 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts @@ -1,6 +1,7 @@ import type { QueryKey, UseQueryResult, UseQueryOptions, QueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult } from '@tanstack/react-query' import { useQuery, useInfiniteQuery } from '@tanstack/react-query' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus' export const findPetsByStatusQueryKey = (params?: FindPetsByStatusQueryParams) => [`/pet/findByStatus`, ...(params ? [params] : [])] as const @@ -8,7 +9,7 @@ export const findPetsByStatusQueryKey = (params?: FindPetsByStatusQueryParams) = export function findPetsByStatusQueryOptions( params?: FindPetsByStatusQueryParams, options: Partial[0]> = {}, -): UseQueryOptions { +): UseQueryOptions, TError> { const queryKey = findPetsByStatusQueryKey(params) return { @@ -20,7 +21,7 @@ export function findPetsByStatusQueryOptions res.data) + }).then((res) => res) }, } } @@ -34,17 +35,17 @@ export function findPetsByStatusQueryOptions( params?: FindPetsByStatusQueryParams, options: { - query?: UseQueryOptions + query?: UseQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseQueryResult & { queryKey: QueryKey } { +): UseQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey(params) - const query = useQuery({ + const query = useQuery, TError>({ ...findPetsByStatusQueryOptions(params, clientOptions), ...queryOptions, - }) as UseQueryResult & { queryKey: QueryKey } + }) as UseQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey @@ -54,7 +55,7 @@ export function useFindPetsByStatus( params?: FindPetsByStatusQueryParams, options: Partial[0]> = {}, -): UseInfiniteQueryOptions { +): UseInfiniteQueryOptions, TError> { const queryKey = findPetsByStatusQueryKey(params) return { @@ -70,7 +71,7 @@ export function findPetsByStatusQueryOptionsInfinite res.data) + }).then((res) => res) }, } } @@ -84,17 +85,17 @@ export function findPetsByStatusQueryOptionsInfinite( params?: FindPetsByStatusQueryParams, options: { - query?: UseInfiniteQueryOptions + query?: UseInfiniteQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseInfiniteQueryResult & { queryKey: QueryKey } { +): UseInfiniteQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey(params) - const query = useInfiniteQuery({ + const query = useInfiniteQuery, TError>({ ...findPetsByStatusQueryOptionsInfinite(params, clientOptions), ...queryOptions, - }) as UseInfiniteQueryResult & { queryKey: QueryKey } + }) as UseInfiniteQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey diff --git a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts index f8b112daa..ff2d156d8 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts @@ -1,6 +1,7 @@ import type { QueryKey, UseQueryResult, UseQueryOptions, QueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult } from '@tanstack/react-query' import { useQuery, useInfiniteQuery } from '@tanstack/react-query' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { FindPetsByTagsQueryResponse, FindPetsByTagsQueryParams, @@ -14,7 +15,7 @@ export function findPetsByTagsQueryOptions[0]> = {}, -): UseQueryOptions { +): UseQueryOptions, TError> { const queryKey = findPetsByTagsQueryKey(params) return { @@ -26,7 +27,7 @@ export function findPetsByTagsQueryOptions res.data) + }).then((res) => res) }, } } @@ -41,17 +42,17 @@ export function useFindPetsByTags + query?: UseQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseQueryResult & { queryKey: QueryKey } { +): UseQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params) - const query = useQuery({ + const query = useQuery, TError>({ ...findPetsByTagsQueryOptions(headers, params, clientOptions), ...queryOptions, - }) as UseQueryResult & { queryKey: QueryKey } + }) as UseQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey @@ -62,7 +63,7 @@ export function findPetsByTagsQueryOptionsInfinite[0]> = {}, -): UseInfiniteQueryOptions { +): UseInfiniteQueryOptions, TError> { const queryKey = findPetsByTagsQueryKey(params) return { @@ -78,7 +79,7 @@ export function findPetsByTagsQueryOptionsInfinite res.data) + }).then((res) => res) }, } } @@ -93,17 +94,17 @@ export function useFindPetsByTagsInfinite + query?: UseInfiniteQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseInfiniteQueryResult & { queryKey: QueryKey } { +): UseInfiniteQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params) - const query = useInfiniteQuery({ + const query = useInfiniteQuery, TError>({ ...findPetsByTagsQueryOptionsInfinite(headers, params, clientOptions), ...queryOptions, - }) as UseInfiniteQueryResult & { queryKey: QueryKey } + }) as UseInfiniteQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey diff --git a/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts b/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts index b7cc61cc3..52b467f77 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts @@ -1,6 +1,7 @@ import type { QueryKey, UseQueryResult, UseQueryOptions, QueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult } from '@tanstack/react-query' import { useQuery, useInfiniteQuery } from '@tanstack/react-query' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../../../models/ts/petController/GetPetById' export const getPetByIdQueryKey = (petId: GetPetByIdPathParams['petId']) => [`/pet/${petId}`] as const @@ -8,7 +9,7 @@ export const getPetByIdQueryKey = (petId: GetPetByIdPathParams['petId']) => [`/p export function getPetByIdQueryOptions( petId: GetPetByIdPathParams['petId'], options: Partial[0]> = {}, -): UseQueryOptions { +): UseQueryOptions, TError> { const queryKey = getPetByIdQueryKey(petId) return { @@ -19,7 +20,7 @@ export function getPetByIdQueryOptions res.data) + }).then((res) => res) }, } } @@ -33,17 +34,17 @@ export function getPetByIdQueryOptions( petId: GetPetByIdPathParams['petId'], options: { - query?: UseQueryOptions + query?: UseQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseQueryResult & { queryKey: QueryKey } { +): UseQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? getPetByIdQueryKey(petId) - const query = useQuery({ + const query = useQuery, TError>({ ...getPetByIdQueryOptions(petId, clientOptions), ...queryOptions, - }) as UseQueryResult & { queryKey: QueryKey } + }) as UseQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey @@ -53,7 +54,7 @@ export function useGetPetById( petId: GetPetByIdPathParams['petId'], options: Partial[0]> = {}, -): UseInfiniteQueryOptions { +): UseInfiniteQueryOptions, TError> { const queryKey = getPetByIdQueryKey(petId) return { @@ -64,7 +65,7 @@ export function getPetByIdQueryOptionsInfinite res.data) + }).then((res) => res) }, } } @@ -78,17 +79,17 @@ export function getPetByIdQueryOptionsInfinite( petId: GetPetByIdPathParams['petId'], options: { - query?: UseInfiniteQueryOptions + query?: UseInfiniteQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseInfiniteQueryResult & { queryKey: QueryKey } { +): UseInfiniteQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? getPetByIdQueryKey(petId) - const query = useInfiniteQuery({ + const query = useInfiniteQuery, TError>({ ...getPetByIdQueryOptionsInfinite(petId, clientOptions), ...queryOptions, - }) as UseInfiniteQueryResult & { queryKey: QueryKey } + }) as UseInfiniteQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey diff --git a/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts b/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts index ac31e6057..1bf1b63c4 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts @@ -1,6 +1,7 @@ import type { QueryKey, UseQueryResult, UseQueryOptions, QueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult } from '@tanstack/react-query' import { useQuery, useInfiniteQuery } from '@tanstack/react-query' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { GetUserByNameQueryResponse, GetUserByNamePathParams, GetUserByName400, GetUserByName404 } from '../../../models/ts/userController/GetUserByName' export const getUserByNameQueryKey = (username: GetUserByNamePathParams['username']) => [`/user/${username}`] as const @@ -8,7 +9,7 @@ export const getUserByNameQueryKey = (username: GetUserByNamePathParams['usernam export function getUserByNameQueryOptions( username: GetUserByNamePathParams['username'], options: Partial[0]> = {}, -): UseQueryOptions { +): UseQueryOptions, TError> { const queryKey = getUserByNameQueryKey(username) return { @@ -19,7 +20,7 @@ export function getUserByNameQueryOptions res.data) + }).then((res) => res) }, } } @@ -32,17 +33,17 @@ export function getUserByNameQueryOptions( username: GetUserByNamePathParams['username'], options: { - query?: UseQueryOptions + query?: UseQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseQueryResult & { queryKey: QueryKey } { +): UseQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? getUserByNameQueryKey(username) - const query = useQuery({ + const query = useQuery, TError>({ ...getUserByNameQueryOptions(username, clientOptions), ...queryOptions, - }) as UseQueryResult & { queryKey: QueryKey } + }) as UseQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey @@ -52,7 +53,7 @@ export function useGetUserByName( username: GetUserByNamePathParams['username'], options: Partial[0]> = {}, -): UseInfiniteQueryOptions { +): UseInfiniteQueryOptions, TError> { const queryKey = getUserByNameQueryKey(username) return { @@ -63,7 +64,7 @@ export function getUserByNameQueryOptionsInfinite res.data) + }).then((res) => res) }, } } @@ -76,17 +77,17 @@ export function getUserByNameQueryOptionsInfinite( username: GetUserByNamePathParams['username'], options: { - query?: UseInfiniteQueryOptions + query?: UseInfiniteQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseInfiniteQueryResult & { queryKey: QueryKey } { +): UseInfiniteQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? getUserByNameQueryKey(username) - const query = useInfiniteQuery({ + const query = useInfiniteQuery, TError>({ ...getUserByNameQueryOptionsInfinite(username, clientOptions), ...queryOptions, - }) as UseInfiniteQueryResult & { queryKey: QueryKey } + }) as UseInfiniteQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey diff --git a/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts b/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts index e9b110942..5650b3462 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts @@ -1,6 +1,7 @@ import type { QueryKey, UseQueryResult, UseQueryOptions, QueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult } from '@tanstack/react-query' import { useQuery, useInfiniteQuery } from '@tanstack/react-query' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { LoginUserQueryResponse, LoginUserQueryParams, LoginUser400 } from '../../../models/ts/userController/LoginUser' export const loginUserQueryKey = (params?: LoginUserQueryParams) => [`/user/login`, ...(params ? [params] : [])] as const @@ -8,7 +9,7 @@ export const loginUserQueryKey = (params?: LoginUserQueryParams) => [`/user/logi export function loginUserQueryOptions( params?: LoginUserQueryParams, options: Partial[0]> = {}, -): UseQueryOptions { +): UseQueryOptions, TError> { const queryKey = loginUserQueryKey(params) return { @@ -20,7 +21,7 @@ export function loginUserQueryOptions res.data) + }).then((res) => res) }, } } @@ -33,17 +34,17 @@ export function loginUserQueryOptions( params?: LoginUserQueryParams, options: { - query?: UseQueryOptions + query?: UseQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseQueryResult & { queryKey: QueryKey } { +): UseQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? loginUserQueryKey(params) - const query = useQuery({ + const query = useQuery, TError>({ ...loginUserQueryOptions(params, clientOptions), ...queryOptions, - }) as UseQueryResult & { queryKey: QueryKey } + }) as UseQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey @@ -53,7 +54,7 @@ export function useLoginUser( params?: LoginUserQueryParams, options: Partial[0]> = {}, -): UseInfiniteQueryOptions { +): UseInfiniteQueryOptions, TError> { const queryKey = loginUserQueryKey(params) return { @@ -69,7 +70,7 @@ export function loginUserQueryOptionsInfinite res.data) + }).then((res) => res) }, } } @@ -82,17 +83,17 @@ export function loginUserQueryOptionsInfinite( params?: LoginUserQueryParams, options: { - query?: UseInfiniteQueryOptions + query?: UseInfiniteQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseInfiniteQueryResult & { queryKey: QueryKey } { +): UseInfiniteQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? loginUserQueryKey(params) - const query = useInfiniteQuery({ + const query = useInfiniteQuery, TError>({ ...loginUserQueryOptionsInfinite(params, clientOptions), ...queryOptions, - }) as UseInfiniteQueryResult & { queryKey: QueryKey } + }) as UseInfiniteQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey diff --git a/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts b/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts index f42aeba57..725e00035 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts @@ -1,13 +1,14 @@ import type { QueryKey, UseQueryResult, UseQueryOptions, QueryOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult } from '@tanstack/react-query' import { useQuery, useInfiniteQuery } from '@tanstack/react-query' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { LogoutUserQueryResponse } from '../../../models/ts/userController/LogoutUser' export const logoutUserQueryKey = () => [`/user/logout`] as const export function logoutUserQueryOptions( options: Partial[0]> = {}, -): UseQueryOptions { +): UseQueryOptions, TError> { const queryKey = logoutUserQueryKey() return { @@ -18,7 +19,7 @@ export function logoutUserQueryOptions res.data) + }).then((res) => res) }, } } @@ -30,17 +31,17 @@ export function logoutUserQueryOptions( options: { - query?: UseQueryOptions + query?: UseQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseQueryResult & { queryKey: QueryKey } { +): UseQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? logoutUserQueryKey() - const query = useQuery({ + const query = useQuery, TError>({ ...logoutUserQueryOptions(clientOptions), ...queryOptions, - }) as UseQueryResult & { queryKey: QueryKey } + }) as UseQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey @@ -49,7 +50,7 @@ export function useLogoutUser export function logoutUserQueryOptionsInfinite( options: Partial[0]> = {}, -): UseInfiniteQueryOptions { +): UseInfiniteQueryOptions, TError> { const queryKey = logoutUserQueryKey() return { @@ -60,7 +61,7 @@ export function logoutUserQueryOptionsInfinite res.data) + }).then((res) => res) }, } } @@ -72,17 +73,17 @@ export function logoutUserQueryOptionsInfinite( options: { - query?: UseInfiniteQueryOptions + query?: UseInfiniteQueryOptions, TError> client?: Partial>[0]> } = {}, -): UseInfiniteQueryResult & { queryKey: QueryKey } { +): UseInfiniteQueryResult, TError> & { queryKey: QueryKey } { const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? logoutUserQueryKey() - const query = useInfiniteQuery({ + const query = useInfiniteQuery, TError>({ ...logoutUserQueryOptionsInfinite(clientOptions), ...queryOptions, - }) as UseInfiniteQueryResult & { queryKey: QueryKey } + }) as UseInfiniteQueryResult, TError> & { queryKey: QueryKey } query.queryKey = queryKey as QueryKey diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts index 33208d0fe..7cff7fee2 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByStatus.ts @@ -1,12 +1,13 @@ import useSWR from 'swr' import type { SWRConfiguration, SWRResponse } from 'swr' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus' export function findPetsByStatusQueryOptions( params?: FindPetsByStatusQueryParams, options: Partial[0]> = {}, -): SWRConfiguration { +): SWRConfiguration, TError> { return { fetcher: () => { return client({ @@ -16,7 +17,7 @@ export function findPetsByStatusQueryOptions res.data) + }).then((res) => res) }, } } @@ -30,13 +31,13 @@ export function findPetsByStatusQueryOptions( params?: FindPetsByStatusQueryParams, options?: { - query?: SWRConfiguration + query?: SWRConfiguration, TError> client?: Partial>[0]> }, -): SWRResponse { +): SWRResponse, TError> { const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const query = useSWR(`/pet/findByStatus`, { + const query = useSWR, TError, string>(`/pet/findByStatus`, { ...findPetsByStatusQueryOptions(params, clientOptions), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts index 366b1e1e2..f2d79ca60 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useFindPetsByTags.ts @@ -1,6 +1,7 @@ import useSWR from 'swr' import type { SWRConfiguration, SWRResponse } from 'swr' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { FindPetsByTagsQueryResponse, FindPetsByTagsQueryParams, @@ -12,7 +13,7 @@ export function findPetsByTagsQueryOptions[0]> = {}, -): SWRConfiguration { +): SWRConfiguration, TError> { return { fetcher: () => { return client({ @@ -22,7 +23,7 @@ export function findPetsByTagsQueryOptions res.data) + }).then((res) => res) }, } } @@ -37,13 +38,13 @@ export function useFindPetsByTags + query?: SWRConfiguration, TError> client?: Partial>[0]> }, -): SWRResponse { +): SWRResponse, TError> { const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const query = useSWR(`/pet/findByTags`, { + const query = useSWR, TError, string>(`/pet/findByTags`, { ...findPetsByTagsQueryOptions(headers, params, clientOptions), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts b/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts index a0222f51a..b4b190c22 100644 --- a/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts +++ b/examples/advanced/src/gen/clients/swr/petSWRController/useGetPetById.ts @@ -1,12 +1,13 @@ import useSWR from 'swr' import type { SWRConfiguration, SWRResponse } from 'swr' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../../../models/ts/petController/GetPetById' export function getPetByIdQueryOptions( petId: GetPetByIdPathParams['petId'], options: Partial[0]> = {}, -): SWRConfiguration { +): SWRConfiguration, TError> { return { fetcher: () => { return client({ @@ -14,7 +15,7 @@ export function getPetByIdQueryOptions res.data) + }).then((res) => res) }, } } @@ -28,13 +29,13 @@ export function getPetByIdQueryOptions( petId: GetPetByIdPathParams['petId'], options?: { - query?: SWRConfiguration + query?: SWRConfiguration, TError> client?: Partial>[0]> }, -): SWRResponse { +): SWRResponse, TError> { const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const query = useSWR(`/pet/${petId}`, { + const query = useSWR, TError, string>(`/pet/${petId}`, { ...getPetByIdQueryOptions(petId, clientOptions), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts index f944cfc52..05463d3f0 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useGetUserByName.ts @@ -1,12 +1,13 @@ import useSWR from 'swr' import type { SWRConfiguration, SWRResponse } from 'swr' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { GetUserByNameQueryResponse, GetUserByNamePathParams, GetUserByName400, GetUserByName404 } from '../../../models/ts/userController/GetUserByName' export function getUserByNameQueryOptions( username: GetUserByNamePathParams['username'], options: Partial[0]> = {}, -): SWRConfiguration { +): SWRConfiguration, TError> { return { fetcher: () => { return client({ @@ -14,7 +15,7 @@ export function getUserByNameQueryOptions res.data) + }).then((res) => res) }, } } @@ -27,13 +28,13 @@ export function getUserByNameQueryOptions( username: GetUserByNamePathParams['username'], options?: { - query?: SWRConfiguration + query?: SWRConfiguration, TError> client?: Partial>[0]> }, -): SWRResponse { +): SWRResponse, TError> { const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const query = useSWR(`/user/${username}`, { + const query = useSWR, TError, string>(`/user/${username}`, { ...getUserByNameQueryOptions(username, clientOptions), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts index b1310e7ff..b4eef076e 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useLoginUser.ts @@ -1,12 +1,13 @@ import useSWR from 'swr' import type { SWRConfiguration, SWRResponse } from 'swr' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { LoginUserQueryResponse, LoginUserQueryParams, LoginUser400 } from '../../../models/ts/userController/LoginUser' export function loginUserQueryOptions( params?: LoginUserQueryParams, options: Partial[0]> = {}, -): SWRConfiguration { +): SWRConfiguration, TError> { return { fetcher: () => { return client({ @@ -16,7 +17,7 @@ export function loginUserQueryOptions res.data) + }).then((res) => res) }, } } @@ -29,13 +30,13 @@ export function loginUserQueryOptions( params?: LoginUserQueryParams, options?: { - query?: SWRConfiguration + query?: SWRConfiguration, TError> client?: Partial>[0]> }, -): SWRResponse { +): SWRResponse, TError> { const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const query = useSWR(`/user/login`, { + const query = useSWR, TError, string>(`/user/login`, { ...loginUserQueryOptions(params, clientOptions), ...queryOptions, }) diff --git a/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts b/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts index 2f8ab3fda..134848378 100644 --- a/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts +++ b/examples/advanced/src/gen/clients/swr/userSWRController/useLogoutUser.ts @@ -1,11 +1,12 @@ import useSWR from 'swr' import type { SWRConfiguration, SWRResponse } from 'swr' import client from '../../../../client' +import type { ResponseConfig } from '../../../../client' import type { LogoutUserQueryResponse } from '../../../models/ts/userController/LogoutUser' export function logoutUserQueryOptions( options: Partial[0]> = {}, -): SWRConfiguration { +): SWRConfiguration, TError> { return { fetcher: () => { return client({ @@ -13,7 +14,7 @@ export function logoutUserQueryOptions res.data) + }).then((res) => res) }, } } @@ -24,12 +25,12 @@ export function logoutUserQueryOptions(options?: { - query?: SWRConfiguration + query?: SWRConfiguration, TError> client?: Partial>[0]> -}): SWRResponse { +}): SWRResponse, TError> { const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const query = useSWR(`/user/logout`, { + const query = useSWR, TError, string>(`/user/logout`, { ...logoutUserQueryOptions(clientOptions), ...queryOptions, }) diff --git a/packages/swagger-client/src/builders/ClientBuilder.tsx b/packages/swagger-client/src/builders/ClientBuilder.tsx index bd248c5bc..ec97fdb44 100644 --- a/packages/swagger-client/src/builders/ClientBuilder.tsx +++ b/packages/swagger-client/src/builders/ClientBuilder.tsx @@ -6,8 +6,10 @@ import { render } from '@kubb/react-template' import { URLPath } from '@kubb/core' import type { Operation, OperationSchemas } from '@kubb/swagger' import { ClientFunction } from '../components/index.ts' +import type { Options as PluginOptions } from '../types' type Config = { + dataReturnType: PluginOptions['dataReturnType'] operation: Operation schemas: OperationSchemas name: string @@ -17,7 +19,7 @@ type ClientResult = { code: string; name: string } export class ClientBuilder extends OasBuilder { private get client(): ClientResult { - const { name, operation, schemas } = this.config + const { name, operation, schemas, dataReturnType } = this.config const codes: string[] = [] const comments = getComments(operation) @@ -58,8 +60,9 @@ export class ClientBuilder extends OasBuilder { name={name} generics={generics} clientGenerics={clientGenerics} + dataReturnType={dataReturnType} params={params} - returnType={`ResponseConfig<${clientGenerics[0]}>["data"]`} + returnType={dataReturnType === 'data' ? `ResponseConfig<${clientGenerics[0]}>["data"]` : `ResponseConfig<${clientGenerics[0]}>`} method={method} url={new URLPath(operation.path).template} withParams={!!schemas.queryParams?.name} diff --git a/packages/swagger-client/src/components/ClientFunction.tsx b/packages/swagger-client/src/components/ClientFunction.tsx index 2c9ff99dc..db9128fc0 100644 --- a/packages/swagger-client/src/components/ClientFunction.tsx +++ b/packages/swagger-client/src/components/ClientFunction.tsx @@ -1,8 +1,8 @@ import React from 'react' import type { ReactNode } from 'react' -import { Function } from '@kubb/react-template' +import { Function, createIndent } from '@kubb/react-template' import type { HttpMethod } from '@kubb/swagger' -import { createIndent } from '../../../react-template/src/components/Text' +import type { Options as PluginOptions } from '../types' type Props = { name: string @@ -12,6 +12,7 @@ type Props = { method: HttpMethod url: string clientGenerics: string[] + dataReturnType: PluginOptions['dataReturnType'] withParams?: boolean withData?: boolean withHeaders?: boolean @@ -32,6 +33,7 @@ export function ClientFunction({ withHeaders, comments, children, + dataReturnType, }: Props): React.ReactNode { const clientParams = [ `method: "${method}"`, @@ -44,12 +46,19 @@ export function ClientFunction({ return ( - {` + {dataReturnType === 'data' && + ` const { data: resData } = await client<${clientGenerics.join(', ')}>({ ${createIndent(4)}${clientParams.join(`,\n${createIndent(4)}`)} }); return resData;`} + + {dataReturnType === 'full' && + ` +return client<${clientGenerics.join(', ')}>({ +${createIndent(4)}${clientParams.join(`,\n${createIndent(4)}`)} +});`} {children} ) diff --git a/packages/swagger-client/src/generators/OperationGenerator.ts b/packages/swagger-client/src/generators/OperationGenerator.ts index f1b5b83d9..3cf3c2432 100644 --- a/packages/swagger-client/src/generators/OperationGenerator.ts +++ b/packages/swagger-client/src/generators/OperationGenerator.ts @@ -8,9 +8,11 @@ import type { File, OptionalPath, PluginContext } from '@kubb/core' import type { ContentType, HttpMethod, Oas, Operation, OperationSchemas, Resolver, SkipBy } from '@kubb/swagger' import type { ResolvePathOptions, FileMeta } from '../types.ts' import { ClientBuilder } from '../builders/ClientBuilder.tsx' +import type { Options as PluginOptions } from '../types' type Options = { clientPath?: OptionalPath + dataReturnType: PluginOptions['dataReturnType'] oas: Oas contentType?: ContentType skipBy: SkipBy[] @@ -114,12 +116,12 @@ export class OperationGenerator extends Generator { } async get(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { oas, clientPath } = this.options + const { oas, clientPath, dataReturnType } = this.options const controller = this.resolve(operation) const type = this.resolveType(operation) - const source = new ClientBuilder(oas).configure({ name: controller.name, operation, schemas }).print() + const source = new ClientBuilder(oas).configure({ name: controller.name, operation, schemas, dataReturnType }).print() return { path: controller.filePath, @@ -149,12 +151,12 @@ export class OperationGenerator extends Generator { } async post(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { oas, clientPath } = this.options + const { oas, clientPath, dataReturnType } = this.options const controller = this.resolve(operation) const type = this.resolveType(operation) - const source = new ClientBuilder(oas).configure({ name: controller.name, operation, schemas }).print() + const source = new ClientBuilder(oas).configure({ name: controller.name, operation, schemas, dataReturnType }).print() return { path: controller.filePath, diff --git a/packages/swagger-client/src/plugin.ts b/packages/swagger-client/src/plugin.ts index 17772c7b7..b2c8a12be 100644 --- a/packages/swagger-client/src/plugin.ts +++ b/packages/swagger-client/src/plugin.ts @@ -15,7 +15,7 @@ import type { FileMeta } from './types' export const pluginName: PluginOptions['name'] = 'swagger-client' as const export const definePlugin = createPlugin((options) => { - const { output = 'clients', groupBy, skipBy = [], transformers = {} } = options + const { output = 'clients', groupBy, skipBy = [], transformers = {}, dataReturnType = 'data' } = options const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller` let pluginsOptions: [SwaggerPluginOptions] @@ -62,6 +62,7 @@ export const definePlugin = createPlugin((options) => { const operationGenerator = new OperationGenerator({ contentType: swaggerPlugin.api.contentType, + dataReturnType, clientPath, oas, skipBy, diff --git a/packages/swagger-client/src/types.ts b/packages/swagger-client/src/types.ts index 12796a0b0..992791098 100644 --- a/packages/swagger-client/src/types.ts +++ b/packages/swagger-client/src/types.ts @@ -59,6 +59,18 @@ export type Options = { * @default '@kubb/swagger-client/ts-client' */ client?: string + /** + * Experimental + * + * ReturnType that needs to be used when calling client(). + * + * `Data` will return ResponseConfig[data]. + * + * `Full` will return ResponseConfig. + * @default `'data'` + * @private + */ + dataReturnType?: 'data' | 'full' transformers?: { /** * Override the name of the client that is getting generated, this will also override the name of the file. diff --git a/packages/swagger-swr/src/builders/QueryBuilder.ts b/packages/swagger-swr/src/builders/QueryBuilder.ts index 6fd1afb5f..94c6f8450 100644 --- a/packages/swagger-swr/src/builders/QueryBuilder.ts +++ b/packages/swagger-swr/src/builders/QueryBuilder.ts @@ -6,8 +6,10 @@ import { OasBuilder, getComments, getDataParams } from '@kubb/swagger' import { URLPath, combineCodes } from '@kubb/core' import type { Operation, OperationSchemas } from '@kubb/swagger' import { camelCase } from 'change-case' +import type { Options as PluginOptions } from '../types' type Config = { + dataReturnType: PluginOptions['dataReturnType'] operation: Operation schemas: OperationSchemas errors: Resolver[] @@ -18,14 +20,14 @@ type QueryResult = { code: string; name: string } export class QueryBuilder extends OasBuilder { private get queryOptions(): QueryResult { - const { operation, schemas, errors } = this.config + const { operation, schemas, errors, dataReturnType } = this.config const codes: string[] = [] const name = camelCase(`${operation.getOperationId()}QueryOptions`) const generics = [`TData = ${schemas.response.name}`, `TError = ${errors.map((error) => error.name).join(' | ') || 'unknown'}`] const clientGenerics = ['TData', 'TError'] - const queryGenerics = ['TData', 'TError'] + const queryGenerics = [dataReturnType === 'data' ? 'TData' : 'ResponseConfig', 'TError'] const params = createFunctionParams([ ...getDataParams(schemas.pathParams, { typed: true }), { @@ -58,7 +60,7 @@ export function ${name} <${generics.join(', ')}>(${params}): SWRConfiguration<${ ${schemas.queryParams?.name ? 'params,' : ''} ${schemas.headerParams?.name ? 'headers: { ...headers, ...options.headers },' : ''} ...options, - }).then(res => res.data); + }).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'}); }, }; }; @@ -68,7 +70,7 @@ export function ${name} <${generics.join(', ')}>(${params}): SWRConfiguration<${ } private get query(): QueryResult { - const { name, errors, operation, schemas } = this.config + const { name, errors, operation, schemas, dataReturnType } = this.config const codes: string[] = [] const queryOptionsName = this.queryOptions.name @@ -77,7 +79,7 @@ export function ${name} <${generics.join(', ')}>(${params}): SWRConfiguration<${ const generics = [`TData = ${schemas.response.name}`, `TError = ${errors.map((error) => error.name).join(' | ') || 'unknown'}`] const clientGenerics = ['TData', 'TError'] - const queryGenerics = ['TData', 'TError'] + const queryGenerics = [dataReturnType === 'data' ? 'TData' : 'ResponseConfig', 'TError'] const params = createFunctionParams([ ...getDataParams(schemas.pathParams, { typed: true }), { diff --git a/packages/swagger-swr/src/generators/OperationGenerator.ts b/packages/swagger-swr/src/generators/OperationGenerator.ts index 6e042b092..32ffbfdc0 100644 --- a/packages/swagger-swr/src/generators/OperationGenerator.ts +++ b/packages/swagger-swr/src/generators/OperationGenerator.ts @@ -9,9 +9,11 @@ import type { ContentType, Oas, Operation, OperationSchemas, Resolver, SkipBy } import type { ResolvePathOptions } from '../types.ts' import { QueryBuilder } from '../builders/QueryBuilder.ts' import type { FileMeta } from '../types.ts' +import type { Options as PluginOptions } from '../types' type Options = { clientPath?: OptionalPath + dataReturnType: PluginOptions['dataReturnType'] oas: Oas contentType?: ContentType skipBy?: SkipBy[] @@ -105,7 +107,7 @@ export class OperationGenerator extends Generator { } async get(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { oas, clientPath } = this.options + const { oas, clientPath, dataReturnType } = this.options const hook = this.resolve(operation) const type = this.resolveType(operation) @@ -116,7 +118,7 @@ export class OperationGenerator extends Generator { errors = this.resolveErrors(schemas.errors?.map((item) => item.statusCode && { operation, statusCode: item.statusCode }).filter(Boolean)) } - const source = new QueryBuilder(oas).configure({ name: hook.name, errors, operation, schemas }).print('query') + const source = new QueryBuilder(oas).configure({ name: hook.name, errors, operation, schemas, dataReturnType }).print('query') return { path: hook.filePath, @@ -161,7 +163,7 @@ export class OperationGenerator extends Generator { } async post(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { oas, clientPath } = this.options + const { oas, clientPath, dataReturnType } = this.options const hook = this.resolve(operation) const type = this.resolveType(operation) @@ -172,7 +174,7 @@ export class OperationGenerator extends Generator { errors = this.resolveErrors(schemas.errors?.map((item) => item.statusCode && { operation, statusCode: item.statusCode }).filter(Boolean)) } - const source = new QueryBuilder(oas).configure({ name: hook.name, errors, operation, schemas }).print('mutation') + const source = new QueryBuilder(oas).configure({ name: hook.name, errors, operation, schemas, dataReturnType }).print('mutation') return { path: hook.filePath, diff --git a/packages/swagger-swr/src/plugin.ts b/packages/swagger-swr/src/plugin.ts index dbabd0188..5571a0622 100644 --- a/packages/swagger-swr/src/plugin.ts +++ b/packages/swagger-swr/src/plugin.ts @@ -15,7 +15,7 @@ import type { PluginOptions as SwaggerPluginOptions } from '@kubb/swagger' export const pluginName: PluginOptions['name'] = 'swagger-swr' as const export const definePlugin = createPlugin((options) => { - const { output = 'hooks', groupBy, skipBy = [], transformers = {} } = options + const { output = 'hooks', groupBy, skipBy = [], transformers = {}, dataReturnType = 'data' } = options const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}SWRController` let pluginsOptions: [SwaggerPluginOptions] @@ -62,6 +62,7 @@ export const definePlugin = createPlugin((options) => { const operationGenerator = new OperationGenerator({ contentType: swaggerPlugin.api.contentType, + dataReturnType, clientPath, oas, skipBy, diff --git a/packages/swagger-swr/src/types.ts b/packages/swagger-swr/src/types.ts index 538bed984..def4e4115 100644 --- a/packages/swagger-swr/src/types.ts +++ b/packages/swagger-swr/src/types.ts @@ -36,6 +36,18 @@ export type Options = { * @default '@kubb/swagger-client/ts-client' */ client?: string + /** + * Experimental + * + * ReturnType that needs to be used when calling client(). + * + * `Data` will return ResponseConfig[data]. + * + * `Full` will return ResponseConfig. + * @default `'data'` + * @private + */ + dataReturnType?: 'data' | 'full' transformers?: { /** * Override the name of the hook that is getting generated, this will also override the name of the file. diff --git a/packages/swagger-tanstack-query/src/builders/QueryBuilder.ts b/packages/swagger-tanstack-query/src/builders/QueryBuilder.ts index 70792696e..5b0ba0112 100644 --- a/packages/swagger-tanstack-query/src/builders/QueryBuilder.ts +++ b/packages/swagger-tanstack-query/src/builders/QueryBuilder.ts @@ -8,9 +8,11 @@ import type { Operation, OperationSchemas } from '@kubb/swagger' import { getParams } from '@kubb/swagger' import { camelCase } from 'change-case' import type { Framework, FrameworkImports } from '../types.ts' -import { createFunctionParams } from '../../../core/src/utils/createFunctionParams' +import { createFunctionParams } from '@kubb/core' +import type { Options as PluginOptions } from '../types' type BaseConfig = { + dataReturnType: PluginOptions['dataReturnType'] operation: Operation schemas: OperationSchemas framework: Framework @@ -53,7 +55,7 @@ export class QueryBuilder extends OasBuilder { } private get queryOptions(): QueryResult { - const { operation, schemas, framework, frameworkImports, errors } = this.config + const { operation, schemas, framework, frameworkImports, errors, dataReturnType } = this.config const codes: string[] = [] const name = camelCase(`${operation.getOperationId()}QueryOptions`) @@ -63,7 +65,7 @@ export class QueryBuilder extends OasBuilder { const generics = [`TData = ${schemas.response.name}`, `TError = ${errors.map((error) => error.name).join(' | ') || 'unknown'}`] const clientGenerics = ['TData', 'TError'] - const queryGenerics = ['TData', 'TError'] + const queryGenerics = [dataReturnType === 'data' ? 'TData' : 'ResponseConfig', 'TError'] const params = createFunctionParams([ ...getDataParams(schemas.pathParams, { typed: true }), { @@ -103,7 +105,7 @@ export function ${name} <${generics.join(', ')}>(${params}): ${frameworkImports. ${schemas.queryParams?.name ? 'params,' : ''} ${schemas.headerParams?.name ? 'headers: { ...headers, ...options.headers },' : ''} ...options, - }).then(res => res.data); + }).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'}); }, }; }; @@ -113,7 +115,7 @@ export function ${name} <${generics.join(', ')}>(${params}): ${frameworkImports. } private get query(): QueryResult { - const { framework, frameworkImports, errors, operation, schemas } = this.config + const { framework, frameworkImports, errors, operation, schemas, dataReturnType } = this.config const codes: string[] = [] const queryKeyName = this.queryKey.name @@ -124,7 +126,7 @@ export function ${name} <${generics.join(', ')}>(${params}): ${frameworkImports. const generics = [`TData = ${schemas.response.name}`, `TError = ${errors.map((error) => error.name).join(' | ') || 'unknown'}`] const clientGenerics = ['TData', 'TError'] - const queryGenerics = ['TData', 'TError'] + const queryGenerics = [dataReturnType === 'data' ? 'TData' : 'ResponseConfig', 'TError'] const params = createFunctionParams([ ...getDataParams(schemas.pathParams, { typed: true }), { @@ -178,7 +180,7 @@ export function ${name} <${generics.join(',')}>(${params}): ${frameworkImports.q const query = ${frameworkImports.query.useQuery}<${queryGenerics.join(', ')}>({ ...${queryOptions}, ...queryOptions - }) as ${frameworkImports.query.UseQueryResult}<${clientGenerics.join(', ')}> & { queryKey: QueryKey }; + }) as ${frameworkImports.query.UseQueryResult}<${queryGenerics.join(', ')}> & { queryKey: QueryKey }; query.queryKey = queryKey as QueryKey; @@ -191,7 +193,7 @@ export function ${name} <${generics.join(',')}>(${params}): ${frameworkImports.q //infinite private get queryOptionsInfinite(): QueryResult { - const { framework, frameworkImports, errors, operation, schemas, infinite: { queryParam = 'id' } = {} } = this.config as QueryConfig + const { framework, frameworkImports, errors, operation, schemas, infinite: { queryParam = 'id' } = {}, dataReturnType } = this.config as QueryConfig const codes: string[] = [] const name = camelCase(`${operation.getOperationId()}QueryOptionsInfinite`) @@ -202,7 +204,7 @@ export function ${name} <${generics.join(',')}>(${params}): ${frameworkImports.q const generics = [`TData = ${schemas.response.name}`, `TError = ${errors.map((error) => error.name).join(' | ') || 'unknown'}`] const clientGenerics = ['TData', 'TError'] - const queryGenerics = ['TData', 'TError'] + const queryGenerics = [dataReturnType === 'data' ? 'TData' : 'ResponseConfig', 'TError'] const params = createFunctionParams([ ...getDataParams(schemas.pathParams, { typed: true }), { @@ -250,7 +252,7 @@ export function ${name} <${generics.join(', ')}>(${params}): ${frameworkImports. }` : '' } - }).then(res => res.data); + }).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'}); }, }; }; @@ -260,7 +262,7 @@ export function ${name} <${generics.join(', ')}>(${params}): ${frameworkImports. } private get queryInfinite(): QueryResult { - const { framework, frameworkImports, errors, operation, schemas } = this.config + const { framework, frameworkImports, errors, operation, schemas, dataReturnType } = this.config const codes: string[] = [] const queryKeyName = this.queryKey.name @@ -271,7 +273,7 @@ export function ${name} <${generics.join(', ')}>(${params}): ${frameworkImports. const generics = [`TData = ${schemas.response.name}`, `TError = ${errors.map((error) => error.name).join(' | ') || 'unknown'}`] const clientGenerics = ['TData', 'TError'] - const queryGenerics = ['TData', 'TError'] + const queryGenerics = [dataReturnType === 'data' ? 'TData' : 'ResponseConfig', 'TError'] const params = createFunctionParams([ ...getDataParams(schemas.pathParams, { typed: true }), { @@ -327,7 +329,7 @@ export function ${name} <${generics.join(',')}>(${params}): ${frameworkImports.q const query = ${frameworkImports.query.useInfiniteQuery}<${queryGenerics.join(', ')}>({ ...${queryOptions}, ...queryOptions - }) as ${frameworkImports.query.UseInfiniteQueryResult}<${clientGenerics.join(', ')}> & { queryKey: QueryKey }; + }) as ${frameworkImports.query.UseInfiniteQueryResult}<${queryGenerics.join(', ')}> & { queryKey: QueryKey }; query.queryKey = queryKey as QueryKey; diff --git a/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts b/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts index f800c6125..981164c1d 100644 --- a/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts +++ b/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts @@ -8,10 +8,12 @@ import type { ResolvePathOptions, Framework, FrameworkImports } from '../types.t import { QueryBuilder } from '../builders/QueryBuilder.ts' import type { FileMeta } from '../types.ts' import { pluginName } from '../plugin.ts' +import type { Options as PluginOptions } from '../types' type Options = { framework: Framework clientPath?: OptionalPath + dataReturnType: PluginOptions['dataReturnType'] oas: Oas contentType?: ContentType skipBy: SkipBy[] @@ -270,7 +272,7 @@ export class OperationGenerator extends Generator { } async get(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { oas, clientPath, framework, infinite } = this.options + const { oas, clientPath, framework, infinite, dataReturnType } = this.options const hook = this.resolve(operation) const type = this.resolveType(operation) @@ -282,7 +284,7 @@ export class OperationGenerator extends Generator { errors = this.resolveErrors(schemas.errors?.map((item) => item.statusCode && { operation, statusCode: item.statusCode }).filter(Boolean)) } - const source = new QueryBuilder(oas).configure({ errors, framework, frameworkImports, operation, schemas, infinite }).print('query') + const source = new QueryBuilder(oas).configure({ errors, framework, frameworkImports, operation, schemas, infinite, dataReturnType }).print('query') return { path: hook.filePath, @@ -319,7 +321,7 @@ export class OperationGenerator extends Generator { } async post(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { oas, clientPath, framework } = this.options + const { oas, clientPath, framework, dataReturnType } = this.options const hook = this.resolve(operation) const type = this.resolveType(operation) @@ -331,7 +333,7 @@ export class OperationGenerator extends Generator { errors = this.resolveErrors(schemas.errors?.map((item) => item.statusCode && { operation, statusCode: item.statusCode }).filter(Boolean)) } - const source = new QueryBuilder(oas).configure({ errors, framework, frameworkImports, operation, schemas }).print('mutation') + const source = new QueryBuilder(oas).configure({ errors, framework, frameworkImports, operation, schemas, dataReturnType }).print('mutation') return { path: hook.filePath, diff --git a/packages/swagger-tanstack-query/src/plugin.ts b/packages/swagger-tanstack-query/src/plugin.ts index 7dc0bcaef..65217f9c6 100644 --- a/packages/swagger-tanstack-query/src/plugin.ts +++ b/packages/swagger-tanstack-query/src/plugin.ts @@ -14,7 +14,7 @@ import type { PluginOptions as SwaggerPluginOptions } from '@kubb/swagger' export const pluginName: PluginOptions['name'] = 'swagger-tanstack-query' as const export const definePlugin = createPlugin((options) => { - const { output = 'hooks', groupBy, skipBy = [], framework = 'react', infinite, transformers = {} } = options + const { output = 'hooks', groupBy, skipBy = [], framework = 'react', infinite, transformers = {}, dataReturnType = 'data' } = options const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller` let pluginsOptions: [SwaggerPluginOptions] @@ -60,6 +60,7 @@ export const definePlugin = createPlugin((options) => { const operationGenerator = new OperationGenerator({ contentType: swaggerPlugin.api.contentType, + dataReturnType, infinite: infinite, framework, skipBy, diff --git a/packages/swagger-tanstack-query/src/types.ts b/packages/swagger-tanstack-query/src/types.ts index 98168437c..b54aa863c 100644 --- a/packages/swagger-tanstack-query/src/types.ts +++ b/packages/swagger-tanstack-query/src/types.ts @@ -30,6 +30,18 @@ export type Options = { exportAs?: string } client?: string + /** + * Experimental + * + * ReturnType that needs to be used when calling client(). + * + * `Data` will return ResponseConfig[data]. + * + * `Full` will return ResponseConfig. + * @default `'data'` + * @private + */ + dataReturnType?: 'data' | 'full' /** * Framework to be generated for * @default 'react'