diff --git a/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts index 9a68cd3c6..db8ab9086 100644 --- a/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts +++ b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts @@ -30,41 +30,15 @@ import type { import type { CreatePetsBody, Error, + ListPetsNestedArrayParams, ListPetsParams, Pet, - Pets, + PetsArray, + PetsNestedArray, } from '../model'; import { customInstance } from '../mutator/custom-instance'; import type { ErrorType } from '../mutator/custom-instance'; -// https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir/49579497#49579497 -type IfEquals = - (() => T extends X ? 1 : 2) extends () => T extends Y ? 1 : 2 ? A : B; - -type WritableKeys = { - [P in keyof T]-?: IfEquals< - { [Q in P]: T[P] }, - { -readonly [Q in P]: T[P] }, - P - >; -}[keyof T]; - -type UnionToIntersection = (U extends any ? (k: U) => void : never) extends ( - k: infer I, -) => void - ? I - : never; -type DistributeReadOnlyOverUnions = T extends any ? NonReadonly : never; - -type Writable = Pick>; -type NonReadonly = [T] extends [UnionToIntersection] - ? { - [P in keyof Writable]: T[P] extends object - ? NonReadonly> - : T[P]; - } - : DistributeReadOnlyOverUnions; - type AwaitedInput = PromiseLike | T; type Awaited = O extends AwaitedInput ? T : never; @@ -73,7 +47,7 @@ type Awaited = O extends AwaitedInput ? T : never; * @summary List all pets */ const listPets = (params?: ListPetsParams, version: number = 1) => { - return customInstance({ + return customInstance({ url: `/v${version}/pets`, method: 'GET', params, @@ -417,7 +391,7 @@ export const createPets = ( createPetsBody: CreatePetsBody, version: number = 1, ) => { - return customInstance({ + return customInstance({ url: `/v${version}/pets`, method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -486,75 +460,105 @@ export const useCreatePets = < }; /** - * @summary Update a pet + * @summary List all pets as nested array */ -export const updatePets = (pet: NonReadonly, version: number = 1) => { - return customInstance({ - url: `/v${version}/pets`, - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - data: pet, +export const listPetsNestedArray = ( + params?: ListPetsNestedArrayParams, + version: number = 1, + signal?: AbortSignal, +) => { + return customInstance({ + url: `/v${version}/pets-nested-array`, + method: 'GET', + params, + signal, }); }; -export const getUpdatePetsMutationOptions = < +export const getListPetsNestedArrayQueryKey = ( + params?: ListPetsNestedArrayParams, + version: number = 1, +) => { + return [ + `/v${version}/pets-nested-array`, + ...(params ? [params] : []), + ] as const; +}; + +export const getListPetsNestedArrayQueryOptions = < + TData = Awaited>, TError = ErrorType, - TContext = unknown, ->(options?: { - mutation?: UseMutationOptions< - Awaited>, - TError, - { data: NonReadonly; version?: number }, - TContext - >; -}): UseMutationOptions< - Awaited>, - TError, - { data: NonReadonly; version?: number }, - TContext -> => { - const { mutation: mutationOptions } = options ?? {}; +>( + params?: ListPetsNestedArrayParams, + version: number = 1, + options?: { + query?: Partial< + UseQueryOptions< + Awaited>, + TError, + TData + > + >; + }, +) => { + const { query: queryOptions } = options ?? {}; - const mutationFn: MutationFunction< - Awaited>, - { data: NonReadonly; version?: number } - > = (props) => { - const { data, version } = props ?? {}; + const queryKey = + queryOptions?.queryKey ?? getListPetsNestedArrayQueryKey(params, version); - return updatePets(data, version); - }; + const queryFn: QueryFunction< + Awaited> + > = ({ signal }) => listPetsNestedArray(params, version, signal); - return { mutationFn, ...mutationOptions }; + return { + queryKey, + queryFn, + enabled: !!version, + ...queryOptions, + } as UseQueryOptions< + Awaited>, + TError, + TData + > & { queryKey: QueryKey }; }; -export type UpdatePetsMutationResult = NonNullable< - Awaited> +export type ListPetsNestedArrayQueryResult = NonNullable< + Awaited> >; -export type UpdatePetsMutationBody = NonReadonly; -export type UpdatePetsMutationError = ErrorType; +export type ListPetsNestedArrayQueryError = ErrorType; /** - * @summary Update a pet + * @summary List all pets as nested array */ -export const useUpdatePets = < +export const useListPetsNestedArray = < + TData = Awaited>, TError = ErrorType, - TContext = unknown, ->(options?: { - mutation?: UseMutationOptions< - Awaited>, - TError, - { data: NonReadonly; version?: number }, - TContext - >; -}): UseMutationResult< - Awaited>, - TError, - { data: NonReadonly; version?: number }, - TContext -> => { - const mutationOptions = getUpdatePetsMutationOptions(options); +>( + params?: ListPetsNestedArrayParams, + version: number = 1, + options?: { + query?: Partial< + UseQueryOptions< + Awaited>, + TError, + TData + > + >; + }, +): UseQueryResult & { queryKey: QueryKey } => { + const queryOptions = getListPetsNestedArrayQueryOptions( + params, + version, + options, + ); - return useMutation(mutationOptions); + const query = useQuery(queryOptions) as UseQueryResult & { + queryKey: QueryKey; + }; + + query.queryKey = queryOptions.queryKey; + + return query; }; /**