Skip to content

Commit

Permalink
type(infinite): export SWRInfinteHook and InfiniteKeyLoader from infi…
Browse files Browse the repository at this point in the history
…nite (vercel#1714)

* type: export SWRInfinteHook and InfiniteKeyLoader from infinite

* rename InfiniteKeyLoader and InfiniteFetcher
  • Loading branch information
houkanshan authored and nevilm-lt committed Apr 22, 2022
1 parent c88902e commit e8750e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
18 changes: 12 additions & 6 deletions infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ import {
SWRInfiniteConfiguration,
SWRInfiniteResponse,
SWRInfiniteHook,
InfiniteKeyLoader,
InfiniteFetcher
SWRInfiniteKeyLoader,
SWRInfiniteFetcher
} from './types'

const INFINITE_PREFIX = '$inf$'

const getFirstPageKey = (getKey: InfiniteKeyLoader) => {
const getFirstPageKey = (getKey: SWRInfiniteKeyLoader) => {
return serialize(getKey ? getKey(0, null) : null)[0]
}

export const unstable_serialize = (getKey: InfiniteKeyLoader) => {
export const unstable_serialize = (getKey: SWRInfiniteKeyLoader) => {
return INFINITE_PREFIX + getFirstPageKey(getKey)
}

export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
(
getKey: InfiniteKeyLoader,
getKey: SWRInfiniteKeyLoader,
fn: BareFetcher<Data> | null,
config: Omit<typeof SWRConfig.default, 'fetcher'> &
Omit<SWRInfiniteConfiguration<Data, Error>, 'fetcher'>
Expand Down Expand Up @@ -267,4 +267,10 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
}) as unknown as Middleware

export default withMiddleware(useSWR, infinite) as SWRInfiniteHook
export { SWRInfiniteConfiguration, SWRInfiniteResponse, InfiniteFetcher }
export {
SWRInfiniteConfiguration,
SWRInfiniteResponse,
SWRInfiniteHook,
SWRInfiniteKeyLoader,
SWRInfiniteFetcher
}
40 changes: 24 additions & 16 deletions infinite/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { SWRConfiguration, SWRResponse, Arguments, BareFetcher } from 'swr'

type FetcherResponse<Data = unknown> = Data | Promise<Data>

export type InfiniteFetcher<
export type SWRInfiniteFetcher<
Data = any,
KeyLoader extends InfiniteKeyLoader = InfiniteKeyLoader
KeyLoader extends SWRInfiniteKeyLoader = SWRInfiniteKeyLoader
> = KeyLoader extends (...args: any[]) => any
? ReturnType<KeyLoader> extends
| readonly [...infer K]
Expand All @@ -17,15 +17,15 @@ export type InfiniteFetcher<
: never
: never

export type InfiniteKeyLoader = (
export type SWRInfiniteKeyLoader = (
index: number,
previousPageData: any | null
) => Arguments

export interface SWRInfiniteConfiguration<
Data = any,
Error = any,
Fn extends InfiniteFetcher<Data> = BareFetcher<Data>
Fn extends SWRInfiniteFetcher<Data> = BareFetcher<Data>
> extends SWRConfiguration<Data[], Error> {
initialSize?: number
revalidateAll?: boolean
Expand All @@ -46,7 +46,7 @@ export interface SWRInfiniteHook {
<
Data = any,
Error = any,
KeyLoader extends InfiniteKeyLoader = (
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
Expand All @@ -56,55 +56,63 @@ export interface SWRInfiniteHook {
<
Data = any,
Error = any,
KeyLoader extends InfiniteKeyLoader = (
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
>(
getKey: KeyLoader,
fetcher: InfiniteFetcher<Data, KeyLoader> | null
fetcher: SWRInfiniteFetcher<Data, KeyLoader> | null
): SWRInfiniteResponse<Data, Error>
<
Data = any,
Error = any,
KeyLoader extends InfiniteKeyLoader = (
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
>(
getKey: KeyLoader,
config:
| SWRInfiniteConfiguration<Data, Error, InfiniteFetcher<Data, KeyLoader>>
| SWRInfiniteConfiguration<
Data,
Error,
SWRInfiniteFetcher<Data, KeyLoader>
>
| undefined
): SWRInfiniteResponse<Data, Error>
<
Data = any,
Error = any,
KeyLoader extends InfiniteKeyLoader = (
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
>(
getKey: KeyLoader,
fetcher: InfiniteFetcher<Data, KeyLoader> | null,
fetcher: SWRInfiniteFetcher<Data, KeyLoader> | null,
config:
| SWRInfiniteConfiguration<Data, Error, InfiniteFetcher<Data, KeyLoader>>
| SWRInfiniteConfiguration<
Data,
Error,
SWRInfiniteFetcher<Data, KeyLoader>
>
| undefined
): SWRInfiniteResponse<Data, Error>
<Data = any, Error = any>(getKey: InfiniteKeyLoader): SWRInfiniteResponse<
<Data = any, Error = any>(getKey: SWRInfiniteKeyLoader): SWRInfiniteResponse<
Data,
Error
>
<Data = any, Error = any>(
getKey: InfiniteKeyLoader,
getKey: SWRInfiniteKeyLoader,
fetcher: BareFetcher<Data> | null
): SWRInfiniteResponse<Data, Error>
<Data = any, Error = any>(
getKey: InfiniteKeyLoader,
getKey: SWRInfiniteKeyLoader,
config: SWRInfiniteConfiguration<Data, Error, BareFetcher<Data>> | undefined
): SWRInfiniteResponse<Data, Error>
<Data = any, Error = any>(
getKey: InfiniteKeyLoader,
getKey: SWRInfiniteKeyLoader,
fetcher: BareFetcher<Data> | null,
config: SWRInfiniteConfiguration<Data, Error, BareFetcher<Data>> | undefined
): SWRInfiniteResponse<Data, Error>
Expand Down

0 comments on commit e8750e1

Please sign in to comment.