1
1
import * as revalidateEvents from './constants/revalidate-events'
2
2
3
- export type Fetcher < Data > = ( ...args : any ) => Data | Promise < Data >
3
+ export type Result < T = unknown > = T | Promise < T >
4
+
5
+ export type Fetcher < Data = unknown , Args extends Key = Key > =
6
+ /**
7
+ * () => [{ foo: string }, { bar: number }] | null
8
+ *
9
+ * () => ( [{ foo: string }, { bar: number } ] as const | null )
10
+ */
11
+ Args extends ( ( ) => readonly [ ...infer K ] | null )
12
+ ? ( ( ...args : [ ...K ] ) => Result < Data > )
13
+ /**
14
+ * [{ foo: string }, { bar: number } ] | null
15
+ *
16
+ * [{ foo: string }, { bar: number } ] as const | null
17
+ */
18
+ : Args extends ( readonly [ ...infer K ] )
19
+ ? ( ( ...args : [ ...K ] ) => Result < Data > )
20
+ /**
21
+ * () => string | null
22
+ * () => Record<any, any> | null
23
+ */
24
+ : Args extends ( ( ) => infer T | null )
25
+ ? ( ...args : [ T ] ) => Result < Data >
26
+ /**
27
+ * string | null
28
+ */
29
+ : Args extends ( string | null )
30
+ /**
31
+ * when key is Record<any, any> | null
32
+ * use { foo: string, bar: number } | null as example
33
+ *
34
+ * the fetcher would be
35
+ * (arg0: string) => any | (arg0: { foo: string, bar: number }) => any
36
+ * so we add this condition to make (arg0: string) => any to be never
37
+ */
38
+ ? Args extends ( Record < any , any > | null )
39
+ ? never
40
+ : ( ...args : [ string ] ) => Result < Data >
41
+ : Args extends ( infer T )
42
+ ? ( ...args : [ T ] ) => Result < Data >
43
+ : never
4
44
5
45
// Configuration types that are only used internally, not exposed to the user.
6
46
export interface InternalConfiguration {
@@ -11,7 +51,8 @@ export interface InternalConfiguration {
11
51
export interface PublicConfiguration <
12
52
Data = any ,
13
53
Error = any ,
14
- Fn extends Fetcher < Data > = Fetcher < Data >
54
+ Args extends Key = Key ,
55
+ Fn = Fetcher < Data , Args >
15
56
> {
16
57
errorRetryInterval : number
17
58
errorRetryCount ?: number
@@ -35,22 +76,22 @@ export interface PublicConfiguration<
35
76
isPaused : ( ) => boolean
36
77
onLoadingSlow : (
37
78
key : string ,
38
- config : Readonly < PublicConfiguration < Data , Error > >
79
+ config : Readonly < PublicConfiguration < Data , Error , Args , Fn > >
39
80
) => void
40
81
onSuccess : (
41
82
data : Data ,
42
83
key : string ,
43
- config : Readonly < PublicConfiguration < Data , Error > >
84
+ config : Readonly < PublicConfiguration < Data , Error , Args , Fn > >
44
85
) => void
45
86
onError : (
46
87
err : Error ,
47
88
key : string ,
48
- config : Readonly < PublicConfiguration < Data , Error > >
89
+ config : Readonly < PublicConfiguration < Data , Error , Args , Fn > >
49
90
) => void
50
91
onErrorRetry : (
51
92
err : Error ,
52
93
key : string ,
53
- config : Readonly < PublicConfiguration < Data , Error > > ,
94
+ config : Readonly < PublicConfiguration < Data , Error , Args , Fn > > ,
54
95
revalidate : Revalidator ,
55
96
revalidateOpts : Required < RevalidatorOptions >
56
97
) => void
@@ -67,29 +108,51 @@ export type ConfigOptions = {
67
108
initFocus : ( callback : ( ) => void ) => ( ( ) => void ) | void
68
109
initReconnect : ( callback : ( ) => void ) => ( ( ) => void ) | void
69
110
}
70
-
71
- export type SWRHook = < Data = any , Error = any > (
72
- ...args :
73
- | readonly [ Key ]
74
- | readonly [ Key , Fetcher < Data > | null ]
75
- | readonly [ Key , SWRConfiguration < Data , Error > | undefined ]
76
- | readonly [
77
- Key ,
78
- Fetcher < Data > | null ,
79
- SWRConfiguration < Data , Error > | undefined
80
- ]
81
- ) => SWRResponse < Data , Error >
111
+ export interface SWRHook {
112
+ < Data = any , Error = any , Args extends Key = Key > ( args : Args ) : SWRResponse <
113
+ Data ,
114
+ Error
115
+ >
116
+ < Data = any , Error = any , Args extends Key = Key > (
117
+ args : Args ,
118
+ fn : Fetcher < Data , Args > | null
119
+ ) : SWRResponse < Data , Error >
120
+ < Data = any , Error = any , Args extends Key = Key > (
121
+ args : Args ,
122
+ config : SWRConfiguration < Data , Error , Args , Fetcher < Data , Args > > | undefined
123
+ ) : SWRResponse < Data , Error >
124
+ < Data = any , Error = any , Args extends Key = Key > (
125
+ args : Args ,
126
+ fn : Fetcher < Data , Args > ,
127
+ config : SWRConfiguration < Data , Error , Args , Fetcher < Data , Args > >
128
+ ) : SWRResponse < Data , Error >
129
+ < Data = any , Error = any , Args extends Key = Key > (
130
+ ...args :
131
+ | [ Args ]
132
+ | [ Args , Fetcher < Data , Args > | null ]
133
+ | [
134
+ Args ,
135
+ SWRConfiguration < Data , Error , Args , Fetcher < Data , Args > > | undefined
136
+ ]
137
+ | [
138
+ Args ,
139
+ Fetcher < Data , Key > | null ,
140
+ SWRConfiguration < Data , Error , Args , Fetcher < Data , Args > >
141
+ ]
142
+ ) : SWRResponse < Data , Error >
143
+ }
82
144
83
145
// Middlewares guarantee that a SWRHook receives a key, fetcher, and config as the argument
84
146
type SWRHookWithMiddleware = < Data = any , Error = any > (
85
147
key : Key ,
86
- fetcher : Fetcher < Data > | null ,
148
+ fetcher : Fetcher < Data , Key > | null ,
87
149
config : SWRConfiguration < Data , Error >
88
150
) => SWRResponse < Data , Error >
89
151
90
152
export type Middleware = ( useSWRNext : SWRHook ) => SWRHookWithMiddleware
91
-
92
- export type ValueKey = string | any [ ] | object | null
153
+ export type TupleKey = [ any , ...unknown [ ] ] | readonly [ any , ...unknown [ ] ]
154
+ export type ValueKey = string | null | TupleKey | Record < any , any >
155
+ export type Key = ValueKey | ( ( ) => ValueKey )
93
156
94
157
export type MutatorCallback < Data = any > = (
95
158
currentValue ?: Data
@@ -142,10 +205,9 @@ export type KeyedMutator<Data> = (
142
205
export type SWRConfiguration <
143
206
Data = any ,
144
207
Error = any ,
145
- Fn extends Fetcher < Data > = Fetcher < Data >
146
- > = Partial < PublicConfiguration < Data , Error , Fn > >
147
-
148
- export type Key = ValueKey | ( ( ) => ValueKey )
208
+ Args extends Key = Key ,
209
+ Fn = Fetcher < any , Args >
210
+ > = Partial < PublicConfiguration < Data , Error , Args , Fn > >
149
211
150
212
export interface SWRResponse < Data , Error > {
151
213
data ?: Data
@@ -157,6 +219,7 @@ export interface SWRResponse<Data, Error> {
157
219
export type KeyLoader < Data = any > =
158
220
| ( ( index : number , previousPageData : Data | null ) => ValueKey )
159
221
| null
222
+
160
223
export interface RevalidatorOptions {
161
224
retryCount ?: number
162
225
dedupe ?: boolean
0 commit comments