Skip to content

Commit

Permalink
fix: SWRConfiguration type (#2882)
Browse files Browse the repository at this point in the history
* fix: SWRConfiguration type

* add test

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
  • Loading branch information
samuelhulla and huozhi committed Feb 15, 2024
1 parent 3668c90 commit 744f692
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"build": "bunchee",
"build:e2e": "pnpm next build e2e/site",
"attw": "attw --pack",
"types:check": "pnpm -r run types:check",
"types:check": "tsc --noEmit",
"prepublishOnly": "pnpm clean && pnpm build",
"publish-beta": "pnpm publish --tag beta",
"format": "prettier --write ./**/*.{ts,tsx}",
Expand Down
5 changes: 4 additions & 1 deletion src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ export type SWRConfiguration<
Data = any,
Error = any,
Fn extends BareFetcher<any> = BareFetcher<any>
> = Partial<PublicConfiguration<Data, Error, Fn>>
> = Partial<PublicConfiguration<Data, Error, Fn>> &
Partial<ProviderConfiguration> & {
provider?: (cache: Readonly<Cache>) => Cache
}

export type IsLoadingResponse<
Data = any,
Expand Down
16 changes: 4 additions & 12 deletions src/_internal/utils/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@ import { initCache } from './cache'
import { mergeConfigs } from './merge-config'
import { UNDEFINED, mergeObjects, isFunction } from './shared'
import { useIsomorphicLayoutEffect } from './env'
import type {
SWRConfiguration,
FullConfiguration,
ProviderConfiguration,
Cache
} from '../types'

type Config = SWRConfiguration &
Partial<ProviderConfiguration> & {
provider?: (cache: Readonly<Cache>) => Cache
}
import type { SWRConfiguration, FullConfiguration } from '../types'

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

const SWRConfig: FC<
PropsWithChildren<{
value?: Config | ((parentConfig?: Config) => Config)
value?:
| SWRConfiguration
| ((parentConfig?: SWRConfiguration) => SWRConfiguration)
}>
> = props => {
const { value } = props
Expand Down
33 changes: 33 additions & 0 deletions test/type/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,36 @@ export function testFallbackDataConfig() {
expectType<Equal<typeof data, { value: string }>>(true)
expectType<Equal<typeof isLoading, boolean>>(true)
}

export function testProviderConfig() {
const GlobalSetting = ({ children }: { children: React.ReactNode }) => {
return (
<SWRConfig
value={{
provider: () => new Map(),
isOnline() {
/* Customize the network state detector */
return true
},
isVisible() {
/* Customize the visibility state detector */
return true
},
initFocus(_callback) {
/* Register the listener with your state provider */
},
initReconnect(_callback) {
/* Register the listener with your state provider */
}
}}
>
{children}
</SWRConfig>
)
}
return (
<GlobalSetting>
<div />
</GlobalSetting>
)
}

0 comments on commit 744f692

Please sign in to comment.