Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: improve useSWRMutation type. #2604

Merged
merged 6 commits into from
May 18, 2023
Merged
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
229 changes: 171 additions & 58 deletions mutation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type FetcherOptions<ExtraArg = unknown> = Readonly<{

export type MutationFetcher<
Data = unknown,
ExtraArg = unknown,
SWRKey extends Key = Key
SWRKey extends Key = Key,
ExtraArg = unknown
> = SWRKey extends () => infer Arg | null | undefined | false
? (key: Arg, options: FetcherOptions<ExtraArg>) => FetcherResponse<Data>
: SWRKey extends null | undefined | false
Expand All @@ -21,8 +21,8 @@ export type MutationFetcher<
export type SWRMutationConfiguration<
Data,
Error,
ExtraArg = any,
SWRMutationKey extends Key = Key,
ExtraArg = any,
SWRData = any
> = {
revalidate?: boolean
Expand All @@ -31,29 +31,105 @@ export type SWRMutationConfiguration<
| ((result: Data, currentData: SWRData | undefined) => SWRData)
optimisticData?: SWRData | ((currentData?: SWRData) => SWRData)
rollbackOnError?: boolean | ((error: unknown) => boolean)
throwOnError?: boolean
fetcher?: MutationFetcher<Data, ExtraArg, SWRMutationKey>
fetcher?: MutationFetcher<Data, SWRMutationKey, ExtraArg>
onSuccess?: (
data: Data,
key: string,
config: Readonly<
SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey, SWRData>
SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData>
>
) => void
onError?: (
err: Error,
key: string,
config: Readonly<
SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey, SWRData>
SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData>
>
) => void
}

type RemoveUndefined<T> = T extends undefined ? never : T
interface TriggerWithArgs<
Data = any,
Error = any,
SWRMutationKey extends Key = Key,
ExtraArg = never
> {
<SWRData = Data>(
extraArgument: ExtraArg,
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData
>
): Promise<Data>
<SWRData = Data>(
extraArgument: ExtraArg,
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData
> & { throwOnError: true }
): Promise<RemoveUndefined<Data>>
<SWRData = Data>(
extraArgument: ExtraArg,
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData
> & { throwOnError: false }
): Promise<Data | undefined>
}

interface TriggerWithoutArgs<
Data = any,
Error = any,
SWRMutationKey extends Key = Key,
ExtraArg = never
> {
<SWRData = Data>(
extraArgument?: null,
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData
>
): Promise<Data>
<SWRData = Data>(
extraArgument?: null,
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData
> & { throwOnError: true }
): Promise<RemoveUndefined<Data>>
<SWRData = Data>(
extraArgument?: null,
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg,
SWRData
> & { throwOnError: false }
): Promise<Data>
}

export interface SWRMutationResponse<
Data = any,
Error = any,
ExtraArg = never,
SWRMutationKey extends Key = Key
SWRMutationKey extends Key = Key,
ExtraArg = never
> extends Pick<SWRResponse<Data, Error>, 'data' | 'error'> {
/**
* Indicates if the mutation is in progress.
Expand All @@ -64,58 +140,95 @@ export interface SWRMutationResponse<
* the fetcher, and override the options for the mutation hook.
*/
trigger: [ExtraArg] extends [never]
? <SWRData = Data>(
extraArgument?: null,
options?: SWRMutationConfiguration<
Data,
Error,
ExtraArg,
SWRMutationKey,
SWRData
>
) => Promise<Data | undefined>
: <SWRData = Data>(
extraArgument: ExtraArg,
options?: SWRMutationConfiguration<
Data,
Error,
ExtraArg,
SWRMutationKey,
SWRData
>
) => Promise<Data | undefined>
? TriggerWithoutArgs<Data, Error, ExtraArg, SWRMutationKey>
: TriggerWithArgs<Data, Error, SWRMutationKey, ExtraArg>
/**
* Function to reset the mutation state (`data`, `error`, and `isMutating`).
*/
reset: () => void
}

export type SWRMutationHook = <
Data = any,
Error = any,
SWRMutationKey extends Key = Key,
ExtraArg = never
>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, ExtraArg, SWRMutationKey>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey>
) => SWRMutationResponse<Data, Error, ExtraArg, SWRMutationKey>
export interface SWRMutationHook {
<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, SWRMutationKey, ExtraArg>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg>
): SWRMutationResponse<Data, Error, SWRMutationKey, ExtraArg>
<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, SWRMutationKey, ExtraArg>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg
> & { throwOnError: false }
): SWRMutationResponse<Data | undefined, Error, SWRMutationKey, ExtraArg>
<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, SWRMutationKey, ExtraArg>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<
Data,
Error,
SWRMutationKey,
ExtraArg
> & { throwOnError: true }
): SWRMutationResponse<Data, Error, SWRMutationKey, ExtraArg>
}
35 changes: 33 additions & 2 deletions test/type/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import useSWR from 'swr'
type ExpectType = <T>(value: T) => void
const expectType: ExpectType = () => {}

type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B
? 1
: 2
? true
: false

// Test the Equal type
expectType<Equal<number, string>>(false) // should be false
Expand All @@ -25,7 +29,7 @@ export function useTrigger() {

// The argument of `trigger` should be number or undefined.
expectType<Equal<Parameters<typeof trigger>[0], number>>(true)
expectType<Promise<string | undefined>>(trigger(1))
expectType<Promise<string>>(trigger(1))

// Other return values
expectType<Equal<typeof reset, () => void>>(true)
Expand All @@ -46,9 +50,36 @@ export function useTriggerWithParameter() {
}
)

// The argument of `trigger` should be number or undefined.
expectType<Equal<Parameters<typeof trigger>[0], number>>(true)
expectType<Promise<string>>(trigger(1))
expectType<Promise<string | undefined>>(
trigger(1, {
throwOnError: false
})
)
}

export function useOnErrorThrowFalse() {
const { trigger } = useSWRMutation<string, any, string, number>(
'/api/user',
(_, opts) => {
expectType<Equal<typeof opts, Readonly<{ arg: number }>>>(true)
return String(opts.arg)
},
{
throwOnError: false
}
)

// The argument of `trigger` should be number or undefined.
expectType<Equal<Parameters<typeof trigger>[0], number>>(true)
expectType<Promise<string | undefined>>(trigger(1))
expectType<Promise<string>>(
trigger(1, {
throwOnError: true
})
)
}

export function useTestSWRMutation() {
Expand Down