Skip to content

Commit

Permalink
fix: using unref to http function prop when fetch with vue (#1518)
Browse files Browse the repository at this point in the history
* fix: using `unref` to http function prop when `fetch` with `vue`

* chore: update sample app
  • Loading branch information
soartec-lab authored Jul 15, 2024
1 parent 84d36c1 commit 88052ee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
14 changes: 14 additions & 0 deletions packages/query/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import {
import { generateRequestFunction as generateFetchRequestFunction } from '@orval/fetch';

import {
isVue,
makeRouteSafe,
vueUnRefParams,
vueWrapTypeWithMaybeRef,
} from './utils';
import exp from 'constants';

export const AXIOS_DEPENDENCIES: GeneratorDependency[] = [
{
Expand Down Expand Up @@ -376,3 +378,15 @@ export const getMutationRequestArgs = (
: ''
: '';
};

export const getHttpFunctionQueryProps = (
isVue: boolean,
httpClient: OutputHttpClient,
queryProperties: string,
) => {
if (isVue && httpClient === OutputHttpClient.FETCH && queryProperties) {
return `unref(${queryProperties})`;
}

return queryProperties;
};
7 changes: 6 additions & 1 deletion packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
getHooksOptionImplementation,
getMutationRequestArgs,
generateQueryRequestFunction,
getHttpFunctionQueryProps,
} from './client';

const REACT_DEPENDENCIES: GeneratorDependency[] = [
Expand Down Expand Up @@ -749,7 +750,11 @@ const generateQueryImplementation = ({
: param.name;
})
.join(',')
: queryProperties;
: getHttpFunctionQueryProps(
isVue(outputClient),
httpClient,
queryProperties,
);

const definedInitialDataReturnType = generateQueryReturnType({
outputClient,
Expand Down
18 changes: 10 additions & 8 deletions samples/vue-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const getListPetsQueryOptions = <

const queryFn: QueryFunction<Awaited<ReturnType<typeof listPets>>> = ({
signal,
}) => listPets(params, { signal, ...requestOptions });
}) => listPets(unref(params), { signal, ...requestOptions });

return { queryKey, queryFn, ...queryOptions } as UseQueryOptions<
Awaited<ReturnType<typeof listPets>>,
Expand All @@ -131,7 +131,8 @@ export type ListPetsQueryError = unknown;
/**
* @summary List all pets
*/
export const useListPets = <

export function useListPets<
TData = Awaited<ReturnType<typeof listPets>>,
TError = unknown,
>(
Expand All @@ -142,7 +143,7 @@ export const useListPets = <
>;
request?: SecondParameter<typeof customFetch>;
},
): UseQueryReturnType<TData, TError> & { queryKey: QueryKey } => {
): UseQueryReturnType<TData, TError> & { queryKey: QueryKey } {
const queryOptions = getListPetsQueryOptions(params, options);

const query = useQuery(queryOptions) as UseQueryReturnType<TData, TError> & {
Expand All @@ -152,7 +153,7 @@ export const useListPets = <
query.queryKey = unref(queryOptions).queryKey as QueryKey;

return query;
};
}

/**
* @summary Create a pet
Expand Down Expand Up @@ -360,7 +361,7 @@ export const getShowPetByIdQueryOptions = <

const queryFn: QueryFunction<Awaited<ReturnType<typeof showPetById>>> = ({
signal,
}) => showPetById(petId, { signal, ...requestOptions });
}) => showPetById(unref(petId), { signal, ...requestOptions });

return {
queryKey,
Expand All @@ -378,7 +379,8 @@ export type ShowPetByIdQueryError = Error;
/**
* @summary Info for a specific pet
*/
export const useShowPetById = <

export function useShowPetById<
TData = Awaited<ReturnType<typeof showPetById>>,
TError = Error,
>(
Expand All @@ -389,7 +391,7 @@ export const useShowPetById = <
>;
request?: SecondParameter<typeof customFetch>;
},
): UseQueryReturnType<TData, TError> & { queryKey: QueryKey } => {
): UseQueryReturnType<TData, TError> & { queryKey: QueryKey } {
const queryOptions = getShowPetByIdQueryOptions(petId, options);

const query = useQuery(queryOptions) as UseQueryReturnType<TData, TError> & {
Expand All @@ -399,4 +401,4 @@ export const useShowPetById = <
query.queryKey = unref(queryOptions).queryKey as QueryKey;

return query;
};
}

0 comments on commit 88052ee

Please sign in to comment.