forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.ts
286 lines (254 loc) · 7.84 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import * as revalidateEvents from './constants'
import { defaultConfig } from './utils/config'
export type GlobalState = [
Record<string, RevalidateCallback[]>, // EVENT_REVALIDATORS
Record<string, [number, number]>, // MUTATION: [ts, end_ts]
Record<string, [any, number]>, // FETCH: [data, ts]
ScopedMutator, // Mutator
(key: string, value: any, prev: any) => void, // Setter
(key: string, callback: (current: any, prev: any) => void) => () => void // Subscriber
]
export type FetcherResponse<Data = unknown> = Data | Promise<Data>
export type BareFetcher<Data = unknown> = (
...args: any[]
) => FetcherResponse<Data>
export type Fetcher<
Data = unknown,
SWRKey extends Key = Key
> = SWRKey extends () => infer Arg | null | undefined | false
? (args: Arg) => FetcherResponse<Data>
: SWRKey extends null | undefined | false
? never
: SWRKey extends infer Arg
? (args: Arg) => FetcherResponse<Data>
: never
// Configuration types that are only used internally, not exposed to the user.
export interface InternalConfiguration<T extends Cache = Cache> {
cache: T
mutate: ScopedMutator
}
export interface PublicConfiguration<
Data = any,
Error = any,
Fn extends Fetcher = BareFetcher
> {
errorRetryInterval: number
errorRetryCount?: number
loadingTimeout: number
focusThrottleInterval: number
dedupingInterval: number
refreshInterval?: number | ((latestData: Data | undefined) => number)
refreshWhenHidden?: boolean
refreshWhenOffline?: boolean
revalidateOnFocus: boolean
revalidateOnReconnect: boolean
revalidateOnMount?: boolean
revalidateIfStale: boolean
shouldRetryOnError: boolean | ((err: Error) => boolean)
keepPreviousData?: boolean
suspense?: boolean
fallbackData?: Data
fetcher?: Fn
use?: Middleware[]
fallback: { [key: string]: any }
isPaused: () => boolean
onLoadingSlow: (
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>
) => void
onSuccess: (
data: Data,
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>
) => void
onError: (
err: Error,
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>
) => void
onErrorRetry: (
err: Error,
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>,
revalidate: Revalidator,
revalidateOpts: Required<RevalidatorOptions>
) => void
onDiscarded: (key: string) => void
compare: (a: Data | undefined, b: Data | undefined) => boolean
isOnline: () => boolean
isVisible: () => boolean
}
export type FullConfiguration<T extends Cache = Cache> =
InternalConfiguration<T> & PublicConfiguration
export type ProviderConfiguration = {
initFocus: (callback: () => void) => (() => void) | void
initReconnect: (callback: () => void) => (() => void) | void
}
export interface SWRHook {
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey,
config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null,
config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined
): SWRResponse<Data, Error>
<Data = any, Error = any>(key: Key): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null
): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
config: SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined
): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null,
config: SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined
): SWRResponse<Data, Error>
}
// Middleware guarantee that a SWRHook receives a key, fetcher, and config as the argument
export type Middleware = (
useSWRNext: SWRHook
) => <Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null,
config: typeof defaultConfig &
SWRConfiguration<Data, Error, BareFetcher<Data>>
) => SWRResponse<Data, Error>
type ArgumentsTuple = [any, ...unknown[]] | readonly [any, ...unknown[]]
export type Arguments =
| string
| ArgumentsTuple
| Record<any, any>
| null
| undefined
| false
export type Key = Arguments | (() => Arguments)
export type MutatorCallback<Data = any> = (
currentData?: Data
) => Promise<undefined | Data> | undefined | Data
export type MutatorOptions<Data = any> = {
revalidate?: boolean
populateCache?:
| boolean
| ((result: any, currentData: Data | undefined) => Data)
optimisticData?: Data | ((currentData?: Data) => Data)
rollbackOnError?: boolean
}
export type MutatorConfig = {
revalidate?: boolean
populateCache?: boolean
}
export type Broadcaster<Data = any, Error = any> = (
cache: Cache<Data>,
key: string,
data: Data,
error?: Error,
isValidating?: boolean,
revalidate?: boolean,
populateCache?: boolean
) => Promise<Data>
export type State<Data, Error> = {
data?: Data
error?: Error
isValidating?: boolean
isLoading?: boolean
}
export type MutatorFn<Data = any> = (
cache: Cache,
key: Key,
data?: Data | Promise<Data> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data>
) => Promise<Data | undefined>
export type MutatorWrapper<Fn> = Fn extends (
...args: [...infer Parameters]
) => infer Result
? Parameters[3] extends boolean
? Result
: Parameters[3] extends Required<Pick<MutatorOptions, 'populateCache'>>
? Parameters[3]['populateCache'] extends false
? never
: Result
: Result
: never
export type Mutator<Data = any> = MutatorWrapper<MutatorFn<Data>>
export interface ScopedMutator<Data = any> {
/** This is used for bound mutator */
(
key: Key,
data?: Data | Promise<Data> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data>
): Promise<Data | undefined>
/** This is used for global mutator */
<T = any>(
key: Key,
data?: T | Promise<T> | MutatorCallback<T>,
opts?: boolean | MutatorOptions<Data>
): Promise<T | undefined>
}
export type KeyedMutator<Data> = (
data?: Data | Promise<Data> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data>
) => Promise<Data | undefined>
// Public types
export type SWRConfiguration<
Data = any,
Error = any,
Fn extends BareFetcher<any> = BareFetcher<any>
> = Partial<PublicConfiguration<Data, Error, Fn>>
export interface SWRResponse<Data = any, Error = any> {
data: Data | undefined
error: Error | undefined
mutate: KeyedMutator<Data>
isValidating: boolean
isLoading: boolean
}
export type KeyLoader<Args extends Arguments = Arguments> =
| ((index: number, previousPageData: any | null) => Args)
| null
export interface RevalidatorOptions {
retryCount?: number
dedupe?: boolean
}
export type Revalidator = (
revalidateOpts?: RevalidatorOptions
) => Promise<boolean> | void
export type RevalidateEvent =
| typeof revalidateEvents.FOCUS_EVENT
| typeof revalidateEvents.RECONNECT_EVENT
| typeof revalidateEvents.MUTATE_EVENT
type RevalidateCallbackReturnType = {
[revalidateEvents.FOCUS_EVENT]: void
[revalidateEvents.RECONNECT_EVENT]: void
[revalidateEvents.MUTATE_EVENT]: Promise<boolean>
}
export type RevalidateCallback = <K extends RevalidateEvent>(
type: K
) => RevalidateCallbackReturnType[K]
export interface Cache<Data = any> {
get(key: Key): CacheValue<Data> | undefined
set(key: Key, value: Data): void
delete(key: Key): void
}
export interface CacheValue<Data = any, Error = any> {
data?: Data
error?: Error
isValidating?: boolean
isLoading?: boolean
}
export interface StateDependencies {
data?: boolean
error?: boolean
isValidating?: boolean
isLoading?: boolean
}