Skip to content

Commit 744f692

Browse files
samuelhullahuozhi
andauthored
fix: SWRConfiguration type (#2882)
* fix: SWRConfiguration type * add test --------- Co-authored-by: Jiachi Liu <inbox@huozhi.im>
1 parent 3668c90 commit 744f692

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"build": "bunchee",
107107
"build:e2e": "pnpm next build e2e/site",
108108
"attw": "attw --pack",
109-
"types:check": "pnpm -r run types:check",
109+
"types:check": "tsc --noEmit",
110110
"prepublishOnly": "pnpm clean && pnpm build",
111111
"publish-beta": "pnpm publish --tag beta",
112112
"format": "prettier --write ./**/*.{ts,tsx}",

src/_internal/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,10 @@ export type SWRConfiguration<
403403
Data = any,
404404
Error = any,
405405
Fn extends BareFetcher<any> = BareFetcher<any>
406-
> = Partial<PublicConfiguration<Data, Error, Fn>>
406+
> = Partial<PublicConfiguration<Data, Error, Fn>> &
407+
Partial<ProviderConfiguration> & {
408+
provider?: (cache: Readonly<Cache>) => Cache
409+
}
407410

408411
export type IsLoadingResponse<
409412
Data = any,

src/_internal/utils/config-context.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,15 @@ import { initCache } from './cache'
1111
import { mergeConfigs } from './merge-config'
1212
import { UNDEFINED, mergeObjects, isFunction } from './shared'
1313
import { useIsomorphicLayoutEffect } from './env'
14-
import type {
15-
SWRConfiguration,
16-
FullConfiguration,
17-
ProviderConfiguration,
18-
Cache
19-
} from '../types'
20-
21-
type Config = SWRConfiguration &
22-
Partial<ProviderConfiguration> & {
23-
provider?: (cache: Readonly<Cache>) => Cache
24-
}
14+
import type { SWRConfiguration, FullConfiguration } from '../types'
2515

2616
export const SWRConfigContext = createContext<Partial<FullConfiguration>>({})
2717

2818
const SWRConfig: FC<
2919
PropsWithChildren<{
30-
value?: Config | ((parentConfig?: Config) => Config)
20+
value?:
21+
| SWRConfiguration
22+
| ((parentConfig?: SWRConfiguration) => SWRConfiguration)
3123
}>
3224
> = props => {
3325
const { value } = props

test/type/config.tsx

+33
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,36 @@ export function testFallbackDataConfig() {
157157
expectType<Equal<typeof data, { value: string }>>(true)
158158
expectType<Equal<typeof isLoading, boolean>>(true)
159159
}
160+
161+
export function testProviderConfig() {
162+
const GlobalSetting = ({ children }: { children: React.ReactNode }) => {
163+
return (
164+
<SWRConfig
165+
value={{
166+
provider: () => new Map(),
167+
isOnline() {
168+
/* Customize the network state detector */
169+
return true
170+
},
171+
isVisible() {
172+
/* Customize the visibility state detector */
173+
return true
174+
},
175+
initFocus(_callback) {
176+
/* Register the listener with your state provider */
177+
},
178+
initReconnect(_callback) {
179+
/* Register the listener with your state provider */
180+
}
181+
}}
182+
>
183+
{children}
184+
</SWRConfig>
185+
)
186+
}
187+
return (
188+
<GlobalSetting>
189+
<div />
190+
</GlobalSetting>
191+
)
192+
}

0 commit comments

Comments
 (0)