Skip to content

Commit

Permalink
fix mutationconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
hervedombya committed Oct 18, 2023
1 parent ddac32e commit dad383b
Showing 1 changed file with 17 additions and 63 deletions.
80 changes: 17 additions & 63 deletions src/lib/components/toast/useMutationsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,18 @@ import {
import { useToast } from './ToastProvider';

export type MutationConfig<
T extends UseMutationResult<unknown, unknown, unknown, unknown>,
A = unknown,
B = unknown,
C = unknown,
D = unknown,
T extends UseMutationResult<A, B, C, D> = UseMutationResult<A, B, C, D>,
> = {
mutation: T;
name: string;
};

declare type MAXIMUM_DEPTH = 20;
declare type GetOptions<T extends any> = T extends {
mutationFnData: infer TMutationFnData;
error?: infer TError;
data: infer TData;
}
? UseMutationOptions<TMutationFnData, TError, TData>
: T extends {
mutationFnData: infer TMutationFnData;
error?: infer TError;
}
? UseMutationOptions<TMutationFnData, TError>
: T extends {
data: infer TData;
error?: infer TError;
}
? UseMutationOptions<TData, TError>
: T extends [infer TMutationFnData, infer TError, infer TData]
? UseMutationOptions<TMutationFnData, TError>
: T extends [infer TMutationFnData, infer TError]
? UseMutationOptions<TMutationFnData, TError>
: T extends [infer TMutationFnData]
? UseMutationOptions<TMutationFnData>
: T extends {
mutationFn?: MutationFunction<infer TMutationFnData>;
select: (data: any) => infer TData;
}
? UseMutationOptions<TMutationFnData, unknown, TData>
: T extends {
mutationFn?: MutationFunction<infer TMutationFnData>;
}
? UseMutationOptions<TMutationFnData>
: UseMutationOptions;

declare type GetResults<T> = T extends {
mutationFnData: any;
error?: infer TError;
Expand Down Expand Up @@ -84,30 +57,7 @@ declare type GetDescriptionBuilder<T> = T extends UseMutationResult<
>
? DescriptionBuilder<TData, TError>
: DescriptionBuilder;
/**
* MutationOptions reducer recursively unwraps function arguments to infer/enforce type param
*/
declare type MutationsOptions<
T extends any[],
Result extends any[] = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
? UseMutationOptions[]
: T extends []
? []
: T extends [infer Head]
? [...Result, GetOptions<Head>]
: T extends [infer Head, ...infer Tail]
? MutationsOptions<[...Tail], [...Result, GetOptions<Head>], [...Depth, 1]>
: unknown[] extends T
? T
: T extends UseMutationOptions<
infer TMutationFnData,
infer TError,
infer TData
>[]
? UseMutationOptions<TMutationFnData, TError, TData>[]
: UseMutationOptions[];

/**
* MutationResults reducer recursively maps type param to results
*/
Expand Down Expand Up @@ -178,13 +128,17 @@ declare type DescriptionBuilders<
: DescriptionBuilder[];

type Props<
MainMutationType extends UseMutationResult<
unknown,
unknown,
unknown,
unknown
A = unknown,
B = unknown,
C = unknown,
D = unknown,
MainMutationType extends UseMutationResult<A, B, C, D> = UseMutationResult<
A,
B,
C,
D
>,
T extends any[],
T extends any[] = any[],
> = {
mainMutation: MutationConfig<MainMutationType>;
dependantMutations?: readonly [
Expand Down

0 comments on commit dad383b

Please sign in to comment.