Skip to content

Commit

Permalink
regenerate samples
Browse files Browse the repository at this point in the history
  • Loading branch information
karlismelderis-mckinsey committed May 14, 2024
1 parent 42ddff4 commit 9182fdf
Showing 1 changed file with 88 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<X, Y, A = X, B = never> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? A : B;

type WritableKeys<T> = {
[P in keyof T]-?: IfEquals<
{ [Q in P]: T[P] },
{ -readonly [Q in P]: T[P] },
P
>;
}[keyof T];

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I,
) => void
? I
: never;
type DistributeReadOnlyOverUnions<T> = T extends any ? NonReadonly<T> : never;

type Writable<T> = Pick<T, WritableKeys<T>>;
type NonReadonly<T> = [T] extends [UnionToIntersection<T>]
? {
[P in keyof Writable<T>]: T[P] extends object
? NonReadonly<NonNullable<T[P]>>
: T[P];
}
: DistributeReadOnlyOverUnions<T>;

type AwaitedInput<T> = PromiseLike<T> | T;

type Awaited<O> = O extends AwaitedInput<infer T> ? T : never;
Expand All @@ -73,7 +47,7 @@ type Awaited<O> = O extends AwaitedInput<infer T> ? T : never;
* @summary List all pets
*/
const listPets = (params?: ListPetsParams, version: number = 1) => {
return customInstance<Pets>({
return customInstance<PetsArray>({
url: `/v${version}/pets`,
method: 'GET',
params,
Expand Down Expand Up @@ -417,7 +391,7 @@ export const createPets = (
createPetsBody: CreatePetsBody,
version: number = 1,
) => {
return customInstance<Pet>({
return customInstance<void>({
url: `/v${version}/pets`,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand Down Expand Up @@ -486,75 +460,105 @@ export const useCreatePets = <
};

/**
* @summary Update a pet
* @summary List all pets as nested array
*/
export const updatePets = (pet: NonReadonly<Pet>, version: number = 1) => {
return customInstance<Pet>({
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<PetsNestedArray>({
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<ReturnType<typeof listPetsNestedArray>>,
TError = ErrorType<Error>,
TContext = unknown,
>(options?: {
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof updatePets>>,
TError,
{ data: NonReadonly<Pet>; version?: number },
TContext
>;
}): UseMutationOptions<
Awaited<ReturnType<typeof updatePets>>,
TError,
{ data: NonReadonly<Pet>; version?: number },
TContext
> => {
const { mutation: mutationOptions } = options ?? {};
>(
params?: ListPetsNestedArrayParams,
version: number = 1,
options?: {
query?: Partial<
UseQueryOptions<
Awaited<ReturnType<typeof listPetsNestedArray>>,
TError,
TData
>
>;
},
) => {
const { query: queryOptions } = options ?? {};

const mutationFn: MutationFunction<
Awaited<ReturnType<typeof updatePets>>,
{ data: NonReadonly<Pet>; version?: number }
> = (props) => {
const { data, version } = props ?? {};
const queryKey =
queryOptions?.queryKey ?? getListPetsNestedArrayQueryKey(params, version);

return updatePets(data, version);
};
const queryFn: QueryFunction<
Awaited<ReturnType<typeof listPetsNestedArray>>
> = ({ signal }) => listPetsNestedArray(params, version, signal);

return { mutationFn, ...mutationOptions };
return {
queryKey,
queryFn,
enabled: !!version,
...queryOptions,
} as UseQueryOptions<
Awaited<ReturnType<typeof listPetsNestedArray>>,
TError,
TData
> & { queryKey: QueryKey };
};

export type UpdatePetsMutationResult = NonNullable<
Awaited<ReturnType<typeof updatePets>>
export type ListPetsNestedArrayQueryResult = NonNullable<
Awaited<ReturnType<typeof listPetsNestedArray>>
>;
export type UpdatePetsMutationBody = NonReadonly<Pet>;
export type UpdatePetsMutationError = ErrorType<Error>;
export type ListPetsNestedArrayQueryError = ErrorType<Error>;

/**
* @summary Update a pet
* @summary List all pets as nested array
*/
export const useUpdatePets = <
export const useListPetsNestedArray = <
TData = Awaited<ReturnType<typeof listPetsNestedArray>>,
TError = ErrorType<Error>,
TContext = unknown,
>(options?: {
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof updatePets>>,
TError,
{ data: NonReadonly<Pet>; version?: number },
TContext
>;
}): UseMutationResult<
Awaited<ReturnType<typeof updatePets>>,
TError,
{ data: NonReadonly<Pet>; version?: number },
TContext
> => {
const mutationOptions = getUpdatePetsMutationOptions(options);
>(
params?: ListPetsNestedArrayParams,
version: number = 1,
options?: {
query?: Partial<
UseQueryOptions<
Awaited<ReturnType<typeof listPetsNestedArray>>,
TError,
TData
>
>;
},
): UseQueryResult<TData, TError> & { queryKey: QueryKey } => {
const queryOptions = getListPetsNestedArrayQueryOptions(
params,
version,
options,
);

return useMutation(mutationOptions);
const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & {
queryKey: QueryKey;
};

query.queryKey = queryOptions.queryKey;

return query;
};

/**
Expand Down

0 comments on commit 9182fdf

Please sign in to comment.