Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SWRConfiguration type #2882

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
)
}
Loading