Skip to content

Commit

Permalink
fix: post querykey
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Apr 1, 2024
1 parent 0b61a8c commit 0bb749a
Show file tree
Hide file tree
Showing 26 changed files with 315 additions and 242 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-ducks-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-tanstack-query": patch
---

queryKey should include POST request body
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type { DeletePetHeaderParams, DeletePetMutationResponse, DeletePetPathPar
* @link /pet/:petId
*/
export async function deletePet(
{ petId }: DeletePetPathParams,
{
petId,
}: {
petId: DeletePetPathParams['petId']
},
headers?: DeletePetHeaderParams,
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<DeletePetMutationResponse>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type { GetPetByIdPathParams, GetPetByIdQueryResponse } from '../../../mod
* @link /pet/:petId
*/
export async function getPetById(
{ petId }: GetPetByIdPathParams,
{
petId,
}: {
petId: GetPetByIdPathParams['petId']
},
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<GetPetByIdQueryResponse>> {
const res = await client<GetPetByIdQueryResponse>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import type {
* @link /pet/:petId
*/
export async function updatePetWithForm(
{ petId }: UpdatePetWithFormPathParams,
{
petId,
}: {
petId: UpdatePetWithFormPathParams['petId']
},
params?: UpdatePetWithFormQueryParams,
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<UpdatePetWithFormMutationResponse>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import type {
* @link /pet/:petId/uploadImage
*/
export async function uploadFile(
{ petId }: UploadFilePathParams,
{
petId,
}: {
petId: UploadFilePathParams['petId']
},
data?: UploadFileMutationRequest,
params?: UploadFileQueryParams,
options: Partial<Parameters<typeof client>[0]> = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import type {
* @link /pets/:uuid
*/
export async function createPets(
{ uuid }: CreatePetsPathParams,
{
uuid,
}: {
uuid: CreatePetsPathParams['uuid']
},
data: CreatePetsMutationRequest,
headers: CreatePetsHeaderParams,
params?: CreatePetsQueryParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type { DeleteUserMutationResponse, DeleteUserPathParams } from '../../../
* @link /user/:username
*/
export async function deleteUser(
{ username }: DeleteUserPathParams,
{
username,
}: {
username: DeleteUserPathParams['username']
},
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<DeleteUserMutationResponse>> {
const res = await client<DeleteUserMutationResponse>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type { GetUserByNamePathParams, GetUserByNameQueryResponse } from '../../
* @link /user/:username
*/
export async function getUserByName(
{ username }: GetUserByNamePathParams,
{
username,
}: {
username: GetUserByNamePathParams['username']
},
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<GetUserByNameQueryResponse>> {
const res = await client<GetUserByNameQueryResponse>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type { UpdateUserMutationRequest, UpdateUserMutationResponse, UpdateUserP
* @link /user/:username
*/
export async function updateUser(
{ username }: UpdateUserPathParams,
{
username,
}: {
username: UpdateUserPathParams['username']
},
data?: UpdateUserMutationRequest,
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<UpdateUserMutationResponse>> {
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/utils/FunctionParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ describe('objectToParameters', () => {
{ name: 'params', type: 'Params' },
])
.toString(),
).toEqual('{ id, data }?: { id: Id, data: Data }, params: Params')
).toEqual('{ id, data }?: { id: Id; data: Data }, params: Params')
})

test('if object is resolved to a string with empty array', () => {
expect(new FunctionParams().add([[{ name: 'id' }], { name: 'params' }]).toString()).toEqual('{ id }, params')
expect(new FunctionParams().add([[{ name: 'id' }, { name: 'data' }], { name: 'params' }]).toString()).toEqual('{ id, data }, params')
expect(new FunctionParams().add([[], { name: 'params', type: 'Params' }]).toString()).toEqual('params: Params')
})

test.todo('if static functionality works')
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/utils/FunctionParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class FunctionParams {

return {
name: `{ ${name.join(', ')} }`,
type: type.length ? `{ ${type.join(', ')} }` : undefined,
type: type.length ? `{ ${type.join('; ')} }` : undefined,
enabled,
required,
}
Expand All @@ -135,6 +135,9 @@ export class FunctionParams {
return sortedData
.reduce((acc, item) => {
if (Array.isArray(item)) {
if (item.length <= 0) {
return acc
}
const subItems = FunctionParams.#orderItems(item) as FunctionParamsAST[]
const objectItem = FunctionParams.toObject(subItems)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ShowPetByIdQueryResponse, ShowPetByIdPathParams } from "./";
* @summary Info for a specific pet
* @link /pets/:pet_id
*/
export async function showPetById(petId: ShowPetByIdPathParams["pet_id"], testId: ShowPetByIdPathParams["testId"], options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<ShowPetByIdQueryResponse>["data"]> {
export async function showPetById(petId: ShowPetByIdPathParams["petId"], testId: ShowPetByIdPathParams["testId"], options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<ShowPetByIdQueryResponse>["data"]> {
const res = await client<ShowPetByIdQueryResponse>({
method: "get",
url: \`/pets/\${petId}\`,
Expand All @@ -29,7 +29,10 @@ import type { ShowPetByIdQueryResponse, ShowPetByIdPathParams } from "./";
* @summary Info for a specific pet
* @link /pets/:pet_id
*/
export async function showPetById({ petId, testId }: ShowPetByIdPathParams, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<ShowPetByIdQueryResponse>["data"]> {
export async function showPetById({ petId, testId }: {
petId: ShowPetByIdPathParams["petId"];
testId: ShowPetByIdPathParams["testId"];
}, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<ShowPetByIdQueryResponse>["data"]> {
const res = await client<ShowPetByIdQueryResponse>({
method: "get",
url: \`/pets/\${petId}\`,
Expand All @@ -50,7 +53,10 @@ import type { ShowPetByIdQueryResponse, ShowPetByIdPathParams } from "./";
* @summary Info for a specific pet
* @link /pets/:pet_id
*/
export async function showPetById({ petId, testId }: ShowPetByIdPathParams, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<ShowPetByIdQueryResponse>["data"]> {
export async function showPetById({ petId, testId }: {
petId: ShowPetByIdPathParams["petId"];
testId: ShowPetByIdPathParams["testId"];
}, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<ShowPetByIdQueryResponse>["data"]> {
return axios.get(\`/pets/\${petId}\`, options);
}
"
Expand Down
5 changes: 1 addition & 4 deletions packages/swagger-client/src/components/Client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ export function Client({ Template = defaultTemplates.default }: ClientProps): Ku
clientGenerics.add([{ type: schemas.response.name }, { type: schemas.request?.name, enabled: !!schemas.request?.name }])

params.add([
...getASTParams(schemas.pathParams, {
typed: true,
asObject: pathParamsType === 'object',
}),
...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams, { typed: true })] : getASTParams(schemas.pathParams, { typed: true })),
{
name: 'data',
type: schemas.request?.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @link /pets/:pet_id
*/
export async function showPetById(
{ petId, testId }: ShowPetByIdPathParams,
{ petId, testId }: { petId: ShowPetByIdPathParams['petId']; testId: ShowPetByIdPathParams['testId'] },
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<ShowPetByIdQueryResponse>['data']> {
const res = await client<ShowPetByIdQueryResponse>({
Expand Down
8 changes: 8 additions & 0 deletions packages/swagger-tanstack-query/mocks/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ paths:
description: Status of pet that needs to be updated
schema:
type: string
requestBody:
content:
application/json:
schema:
type: object
properties:
offset:
type: number
responses:
"405":
description: Invalid input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import client from "@kubb/swagger-client/client";
import { useQuery } from "@tanstack/react-query";
import type { UpdatePetWithFormMutationResponse, UpdatePetWithFormPathParams, UpdatePetWithFormQueryParams, UpdatePetWithForm405 } from "./";
import type { UpdatePetWithFormMutationRequest, UpdatePetWithFormMutationResponse, UpdatePetWithFormPathParams, UpdatePetWithFormQueryParams, UpdatePetWithForm405 } from "./";
import type { UseBaseQueryOptions, UseQueryResult, QueryKey, WithRequired } from "@tanstack/react-query";

type UpdatePetWithFormClient = typeof client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, never>;
type UpdatePetWithFormClient = typeof client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationRequest>;
type UpdatePetWithForm = {
data: UpdatePetWithFormMutationResponse;
error: UpdatePetWithForm405;
request: never;
request: UpdatePetWithFormMutationRequest;
pathParams: UpdatePetWithFormPathParams;
queryParams: UpdatePetWithFormQueryParams;
headerParams: never;
Expand All @@ -17,17 +17,22 @@ type UpdatePetWithForm = {
return: Awaited<ReturnType<UpdatePetWithFormClient>>;
};
};
export const UpdatePetWithFormQueryKey = ({ petId }: UpdatePetWithFormPathParams, params?: UpdatePetWithForm["queryParams"]) => [{ url: "/pet/:petId", params: { petId: petId } }, ...(params ? [params] : [])] as const;
export const UpdatePetWithFormQueryKey = ({ petId }: {
petId: UpdatePetWithFormPathParams["petId"];
}, params?: UpdatePetWithForm["queryParams"], data?: UpdatePetWithForm["request"]) => [{ url: "/pet/:petId", params: { petId: petId } }, ...(params ? [params] : []), ...(data ? [data] : [])] as const;
export type UpdatePetWithFormQueryKey = ReturnType<typeof UpdatePetWithFormQueryKey>;
export function UpdatePetWithFormQueryOptions<TData = UpdatePetWithForm["response"], TQueryData = UpdatePetWithForm["response"]>({ petId }: UpdatePetWithFormPathParams, params?: UpdatePetWithForm["queryParams"], options: UpdatePetWithForm["client"]["parameters"] = {}): WithRequired<UseBaseQueryOptions<UpdatePetWithForm["response"], UpdatePetWithForm["error"], TData, TQueryData>, "queryKey"> {
const queryKey = UpdatePetWithFormQueryKey(petId, params);
export function UpdatePetWithFormQueryOptions<TData = UpdatePetWithForm["response"], TQueryData = UpdatePetWithForm["response"]>({ petId }: {
petId: UpdatePetWithFormPathParams["petId"];
}, params?: UpdatePetWithForm["queryParams"], data?: UpdatePetWithForm["request"], options: UpdatePetWithForm["client"]["parameters"] = {}): WithRequired<UseBaseQueryOptions<UpdatePetWithForm["response"], UpdatePetWithForm["error"], TData, TQueryData>, "queryKey"> {
const queryKey = UpdatePetWithFormQueryKey({ petId }, params, data);
return {
queryKey,
queryFn: async () => {
const res = await client<UpdatePetWithForm["data"], UpdatePetWithForm["error"]>({
method: "post",
url: `/pet/${petId}`,
params,
data,
...options
});
return res.data;
Expand All @@ -38,16 +43,18 @@ export function UpdatePetWithFormQueryOptions<TData = UpdatePetWithForm["respons
* @summary Updates a pet in the store with form data
* @link /pet/:petId
*/
export function updatePetWithForm<TData = UpdatePetWithForm["response"], TQueryData = UpdatePetWithForm["response"], TQueryKey extends QueryKey = UpdatePetWithFormQueryKey>({ petId }: UpdatePetWithFormPathParams, params?: UpdatePetWithForm["queryParams"], options: {
export function updatePetWithForm<TData = UpdatePetWithForm["response"], TQueryData = UpdatePetWithForm["response"], TQueryKey extends QueryKey = UpdatePetWithFormQueryKey>({ petId }: {
petId: UpdatePetWithFormPathParams["petId"];
}, params?: UpdatePetWithForm["queryParams"], data?: UpdatePetWithForm["request"], options: {
query?: Partial<UseBaseQueryOptions<UpdatePetWithForm["response"], UpdatePetWithForm["error"], TData, TQueryData, TQueryKey>>;
client?: UpdatePetWithForm["client"]["parameters"];
} = {}): UseQueryResult<TData, UpdatePetWithForm["error"]> & {
queryKey: TQueryKey;
} {
const { query: queryOptions, client: clientOptions = {} } = options ?? {};
const queryKey = queryOptions?.queryKey ?? UpdatePetWithFormQueryKey(petId, params);
const queryKey = queryOptions?.queryKey ?? UpdatePetWithFormQueryKey({ petId }, params, data);
const query = useQuery<UpdatePetWithForm["data"], UpdatePetWithForm["error"], TData, any>({
...UpdatePetWithFormQueryOptions<TData, TQueryData>({ petId }, UpdatePetWithFormPathParams, params, clientOptions),
...UpdatePetWithFormQueryOptions<TData, TQueryData>({ petId }, params, data, clientOptions),
queryKey,
...queryOptions
}) as UseQueryResult<TData, UpdatePetWithForm["error"]> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ type GetPetById = {
return: Awaited<ReturnType<GetPetByIdClient>>;
};
};
export const GetPetByIdQueryKey = ({ petId }: GetPetByIdPathParams) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export const GetPetByIdQueryKey = ({ petId }: {
petId: GetPetByIdPathParams["petId"];
}) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export type GetPetByIdQueryKey = ReturnType<typeof GetPetByIdQueryKey>;
export function GetPetByIdQueryOptions<TData = GetPetById["response"], TQueryData = GetPetById["response"]>({ petId }: GetPetByIdPathParams, options: GetPetById["client"]["parameters"] = {}): WithRequired<UseBaseQueryOptions<GetPetById["response"], GetPetById["error"], TData, TQueryData>, "queryKey"> {
const queryKey = GetPetByIdQueryKey(petId);
export function GetPetByIdQueryOptions<TData = GetPetById["response"], TQueryData = GetPetById["response"]>({ petId }: {
petId: GetPetByIdPathParams["petId"];
}, options: GetPetById["client"]["parameters"] = {}): WithRequired<UseBaseQueryOptions<GetPetById["response"], GetPetById["error"], TData, TQueryData>, "queryKey"> {
const queryKey = GetPetByIdQueryKey({ petId });
return {
queryKey,
queryFn: async () => {
Expand All @@ -38,16 +42,18 @@ export function GetPetByIdQueryOptions<TData = GetPetById["response"], TQueryDat
* @summary Find pet by ID
* @link /pet/:petId
*/
export function getPetById<TData = GetPetById["response"], TQueryData = GetPetById["response"], TQueryKey extends QueryKey = GetPetByIdQueryKey>({ petId }: GetPetByIdPathParams, options: {
export function getPetById<TData = GetPetById["response"], TQueryData = GetPetById["response"], TQueryKey extends QueryKey = GetPetByIdQueryKey>({ petId }: {
petId: GetPetByIdPathParams["petId"];
}, options: {
query?: Partial<UseBaseQueryOptions<GetPetById["response"], GetPetById["error"], TData, TQueryData, TQueryKey>>;
client?: GetPetById["client"]["parameters"];
} = {}): UseQueryResult<TData, GetPetById["error"]> & {
queryKey: TQueryKey;
} {
const { query: queryOptions, client: clientOptions = {} } = options ?? {};
const queryKey = queryOptions?.queryKey ?? GetPetByIdQueryKey(petId);
const queryKey = queryOptions?.queryKey ?? GetPetByIdQueryKey({ petId });
const query = useQuery<GetPetById["data"], GetPetById["error"], TData, any>({
...GetPetByIdQueryOptions<TData, TQueryData>({ petId }, GetPetByIdPathParams, clientOptions),
...GetPetByIdQueryOptions<TData, TQueryData>({ petId }, clientOptions),
queryKey,
...queryOptions
}) as UseQueryResult<TData, GetPetById["error"]> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ type GetPetById = {
return: Awaited<ReturnType<GetPetByIdClient>>;
};
};
export const GetPetByIdQueryKey = ({ petId }: GetPetByIdPathParams) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export const GetPetByIdQueryKey = ({ petId }: {
petId: GetPetByIdPathParams["petId"];
}) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export type GetPetByIdQueryKey = ReturnType<typeof GetPetByIdQueryKey>;
export function GetPetByIdQueryOptions<TData = GetPetById["response"], TQueryData = GetPetById["response"]>({ petId }: GetPetByIdPathParams, options: GetPetById["client"]["parameters"] = {}): WithRequired<UseBaseQueryOptions<GetPetById["response"], GetPetById["error"], TData, TQueryData>, "queryKey"> {
const queryKey = GetPetByIdQueryKey(petId);
export function GetPetByIdQueryOptions<TData = GetPetById["response"], TQueryData = GetPetById["response"]>({ petId }: {
petId: GetPetByIdPathParams["petId"];
}, options: GetPetById["client"]["parameters"] = {}): WithRequired<UseBaseQueryOptions<GetPetById["response"], GetPetById["error"], TData, TQueryData>, "queryKey"> {
const queryKey = GetPetByIdQueryKey({ petId });
return {
queryKey,
queryFn: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ type GetPetById = {
return: Awaited<ReturnType<GetPetByIdClient>>;
};
};
export const GetPetByIdQueryKey = ({ petId }: GetPetByIdPathParams) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export const GetPetByIdQueryKey = ({ petId }: {
petId: GetPetByIdPathParams["petId"];
}) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export type GetPetByIdQueryKey = ReturnType<typeof GetPetByIdQueryKey>;
/**
* @description Returns a single pet
* @summary Find pet by ID
* @link /pet/:petId
*/
export function getPetById<TData = GetPetById["response"], TQueryData = GetPetById["response"], TQueryKey extends QueryKey = GetPetByIdQueryKey>({ petId }: GetPetByIdPathParams, options: {
export function getPetById<TData = GetPetById["response"], TQueryData = GetPetById["response"], TQueryKey extends QueryKey = GetPetByIdQueryKey>({ petId }: {
petId: GetPetByIdPathParams["petId"];
}, options: {
query?: Partial<UseBaseQueryOptions<GetPetById["response"], GetPetById["error"], TData, TQueryData, TQueryKey>>;
client?: GetPetById["client"]["parameters"];
} = {}): UseQueryResult<TData, GetPetById["error"]> & {
queryKey: TQueryKey;
} {
const { query: queryOptions, client: clientOptions = {} } = options ?? {};
const queryKey = queryOptions?.queryKey ?? GetPetByIdQueryKey(petId);
const queryKey = queryOptions?.queryKey ?? GetPetByIdQueryKey({ petId });
const query = useQuery<GetPetById["data"], GetPetById["error"], TData, any>({
...GetPetByIdQueryOptions<TData, TQueryData>({ petId }, GetPetByIdPathParams, clientOptions),
...GetPetByIdQueryOptions<TData, TQueryData>({ petId }, clientOptions),
queryKey,
...queryOptions
}) as UseQueryResult<TData, GetPetById["error"]> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ type DeletePet = {
return: Awaited<ReturnType<DeletePetClient>>;
};
};
export const DeletePetQueryKey = ({ petId }: DeletePetPathParams) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export const DeletePetQueryKey = ({ petId }: {
petId: DeletePetPathParams["petId"];
}) => [{ url: "/pet/:petId", params: { petId: petId } }] as const;
export type DeletePetQueryKey = ReturnType<typeof DeletePetQueryKey>;
Loading

0 comments on commit 0bb749a

Please sign in to comment.