Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions src/mutation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,61 @@ export type SWRMutationConfiguration<

type RemoveUndefined<T> = T extends undefined ? never : T
type IsUndefinedIncluded<T> = undefined extends T ? true : false
type TriggerOptions<
Data,
Error,
SWRMutationKey extends Key,
ExtraArg,
SWRData,
ThrowOnError extends boolean
> = SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData> & {
throwOnError?: ThrowOnError
}
type TriggerArgs<
Data,
Error,
SWRMutationKey extends Key,
ExtraArg,
SWRData,
ThrowOnError extends boolean
> =
| [
extraArgument: ExtraArg,
options?: TriggerOptions<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData,
ThrowOnError
>
]
| ([ExtraArg] extends [never]
? [
extraArgument?: any,
options?: TriggerOptions<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData,
ThrowOnError
>
]
: never)
| (IsUndefinedIncluded<ExtraArg> extends true
? [
extraArgument?: ExtraArg,
options?: TriggerOptions<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData,
ThrowOnError
>
]
: never)
export interface TriggerWithArgs<
Data = any,
Error = any,
Expand Down Expand Up @@ -164,6 +219,30 @@ export interface TriggerWithoutArgs<
): Promise<Data>
}

export interface Trigger<
Data = any,
Error = any,
SWRMutationKey extends Key = Key,
ExtraArg = never
> {
<SWRData = Data>(
...args: TriggerArgs<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData,
boolean
>
): Promise<Data>
<SWRData = Data>(
...args: TriggerArgs<Data, Error, SWRMutationKey, ExtraArg, SWRData, true>
): Promise<RemoveUndefined<Data>>
<SWRData = Data>(
...args: TriggerArgs<Data, Error, SWRMutationKey, ExtraArg, SWRData, false>
): Promise<Data | undefined>
}

export interface SWRMutationResponse<
Data = any,
Error = any,
Expand All @@ -178,11 +257,7 @@ export interface SWRMutationResponse<
* Function to trigger the mutation. You can also pass an extra argument to
* the fetcher, and override the options for the mutation hook.
*/
trigger: [ExtraArg] extends [never]
? TriggerWithoutArgs<Data, Error, SWRMutationKey, ExtraArg>
: IsUndefinedIncluded<ExtraArg> extends true
? TriggerWithOptionsArgs<Data, Error, SWRMutationKey, ExtraArg>
: TriggerWithArgs<Data, Error, SWRMutationKey, ExtraArg>
trigger: Trigger<Data, Error, SWRMutationKey, ExtraArg>
/**
* Function to reset the mutation state (`data`, `error`, and `isMutating`).
*/
Expand Down
15 changes: 14 additions & 1 deletion test/type/mutation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import useSWRMutation, { type TriggerWithoutArgs } from 'swr/mutation'
import useSWRMutation, {
type MutationFetcher,
type TriggerWithoutArgs
} from 'swr/mutation'
import { expectType } from './utils'

export function useConfigMutation() {
const { trigger } = useSWRMutation('key', k => k)
expectType<TriggerWithoutArgs<'key', any, string, never>>(trigger)
}

export function useGenericMutation<
Key extends string = string,
Data = unknown,
ExtraArg = unknown
>(key: Key, fetcher: MutationFetcher<Data, Key, ExtraArg>) {
const { trigger } = useSWRMutation(key, fetcher)

trigger({ someArg: 'value' } as ExtraArg)
}