Skip to content

Commit 9f37400

Browse files
authored
Clean up types (#1016)
* deprecate ConfigInterface; fix exported types * deprecate SWRInfiniteConfigInterface and SWRInfiniteResponseInterface * deprecate CacheInterface * deprecate RevalidateOptionInterface and keyInterface * deprecate revalidateType * deprecate responseInterface * reorder * clean up internal types
1 parent cb4ee96 commit 9f37400

File tree

7 files changed

+240
-157
lines changed

7 files changed

+240
-157
lines changed

src/cache.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { CacheInterface, keyInterface, cacheListener } from './types'
1+
import { Cache as CacheType, Key, CacheListener } from './types'
22
import hash from './libs/hash'
33

4-
export default class Cache implements CacheInterface {
4+
export default class Cache implements CacheType {
55
private __cache: Map<string, any>
6-
private __listeners: cacheListener[]
6+
private __listeners: CacheListener[]
77

88
constructor(initialData: any = {}) {
99
this.__cache = new Map(Object.entries(initialData))
1010
this.__listeners = []
1111
}
1212

13-
get(key: keyInterface): any {
13+
get(key: Key): any {
1414
const [_key] = this.serializeKey(key)
1515
return this.__cache.get(_key)
1616
}
1717

18-
set(key: keyInterface, value: any): any {
18+
set(key: Key, value: any): any {
1919
const [_key] = this.serializeKey(key)
2020
this.__cache.set(_key, value)
2121
this.notify()
@@ -25,7 +25,7 @@ export default class Cache implements CacheInterface {
2525
return Array.from(this.__cache.keys())
2626
}
2727

28-
has(key: keyInterface) {
28+
has(key: Key) {
2929
const [_key] = this.serializeKey(key)
3030
return this.__cache.has(_key)
3131
}
@@ -35,14 +35,14 @@ export default class Cache implements CacheInterface {
3535
this.notify()
3636
}
3737

38-
delete(key: keyInterface) {
38+
delete(key: Key) {
3939
const [_key] = this.serializeKey(key)
4040
this.__cache.delete(_key)
4141
this.notify()
4242
}
4343

4444
// TODO: introduce namespace for the cache
45-
serializeKey(key: keyInterface): [string, any, string, string] {
45+
serializeKey(key: Key): [string, any, string, string] {
4646
let args = null
4747
if (typeof key === 'function') {
4848
try {
@@ -68,7 +68,7 @@ export default class Cache implements CacheInterface {
6868
return [key, args, errorKey, isValidatingKey]
6969
}
7070

71-
subscribe(listener: cacheListener) {
71+
subscribe(listener: CacheListener) {
7272
if (typeof listener !== 'function') {
7373
throw new Error('Expected the listener to be a function.')
7474
}

src/config.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { dequal } from 'dequal/lite'
2-
import {
3-
ConfigInterface,
4-
RevalidateOptionInterface,
5-
revalidateType
6-
} from './types'
2+
import { SWRConfiguration, RevalidatorOptions, Revalidator } from './types'
73
import Cache from './cache'
84
import webPreset from './libs/web-preset'
95

@@ -14,9 +10,9 @@ const cache = new Cache()
1410
function onErrorRetry(
1511
_,
1612
__,
17-
config: ConfigInterface,
18-
revalidate: revalidateType,
19-
opts: RevalidateOptionInterface
13+
config: SWRConfiguration,
14+
revalidate: Revalidator,
15+
opts: RevalidatorOptions
2016
): void {
2117
if (!config.isDocumentVisible()) {
2218
// if it's hidden, stop
@@ -47,7 +43,7 @@ const slowConnection =
4743
['slow-2g', '2g'].indexOf(navigator['connection'].effectiveType) !== -1
4844

4945
// config
50-
const defaultConfig: ConfigInterface = Object.assign(
46+
const defaultConfig: SWRConfiguration = Object.assign(
5147
{
5248
// events
5349
onLoadingSlow: () => {},

src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
export * from './use-swr'
1+
// `useSWR` and related APIs
22
import { default as useSWR } from './use-swr'
3-
export {
4-
useSWRInfinite,
5-
SWRInfiniteConfigInterface,
6-
SWRInfiniteResponseInterface
7-
} from './use-swr-infinite'
3+
export default useSWR
4+
export * from './use-swr'
5+
6+
// `useSWRInfinite`
7+
export { useSWRInfinite } from './use-swr-infinite'
8+
9+
// Cache related, to be replaced by the new APIs
810
export { cache } from './config'
11+
12+
// Types
913
export {
14+
SWRConfiguration,
15+
SWRInfiniteConfiguration,
16+
SWRInfiniteResponse,
17+
Revalidator,
18+
RevalidatorOptions,
19+
Key,
20+
SWRResponse,
21+
Cache,
22+
// Legacy, for backwards compatibility
1023
ConfigInterface,
24+
SWRInfiniteConfigInterface,
25+
SWRInfiniteResponseInterface,
1126
revalidateType,
1227
RevalidateOptionInterface,
1328
keyInterface,
1429
responseInterface,
1530
CacheInterface
1631
} from './types'
17-
export default useSWR

src/swr-config-context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createContext } from 'react'
22

3-
import { ConfigInterface } from './types'
3+
import { SWRConfiguration } from './types'
44

5-
const SWRConfigContext = createContext<Partial<ConfigInterface>>({})
5+
const SWRConfigContext = createContext<SWRConfiguration>({})
66
SWRConfigContext.displayName = 'SWRConfigContext'
77

88
export default SWRConfigContext

0 commit comments

Comments
 (0)