-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
types.ts
71 lines (65 loc) · 1.88 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { SWRResponse, Key, MutatorOptions } from 'swr'
type FetcherResponse<Data> = Data | Promise<Data>
type FetcherOptions<ExtraArg = unknown> = Readonly<{
arg: ExtraArg
}>
export type MutationFetcher<
Data = unknown,
ExtraArg = unknown,
SWRKey extends Key = Key
> = SWRKey extends () => infer Arg | null | undefined | false
? (key: Arg, options: FetcherOptions<ExtraArg>) => FetcherResponse<Data>
: SWRKey extends null | undefined | false
? never
: SWRKey extends infer Arg
? (key: Arg, options: FetcherOptions<ExtraArg>) => FetcherResponse<Data>
: never
export type SWRMutationConfiguration<
Data,
Error,
ExtraArg = any,
SWRMutationKey extends Key = Key
> = MutatorOptions<Data> & {
fetcher?: MutationFetcher<Data, ExtraArg, SWRMutationKey>
onSuccess?: (
data: Data,
key: string,
config: Readonly<
SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg>
>
) => void
onError?: (
err: Error,
key: string,
config: Readonly<
SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg>
>
) => void
}
export interface SWRMutationResponse<
Data = any,
Error = any,
ExtraArg = any,
SWRMutationKey extends Key = Key
> extends Pick<SWRResponse<Data, Error>, 'data' | 'error'> {
isMutating: boolean
trigger: (
extraArgument?: ExtraArg,
options?: SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey>
) => Promise<Data | undefined>
reset: () => void
}
export type SWRMutationHook = <
Data = any,
Error = any,
SWRMutationKey extends Key = Key,
ExtraArg = any
>(
...args:
| readonly [SWRMutationKey, MutationFetcher<Data, ExtraArg, SWRMutationKey>]
| readonly [
SWRMutationKey,
MutationFetcher<Data, ExtraArg, SWRMutationKey>,
SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey>
]
) => SWRMutationResponse<Data, Error, ExtraArg, SWRMutationKey>