Skip to content

Commit

Permalink
Merge branch 'main' into simplify-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
promer94 committed Sep 10, 2023
2 parents 1ae35e4 + ed0453a commit 6c9799a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 22 deletions.
17 changes: 7 additions & 10 deletions _internal/src/types.ts
Expand Up @@ -231,6 +231,10 @@ export interface SWRHook {
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = Key>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null
): SWRResponse<Data, Error>
<
Data = any,
Error = any,
Expand Down Expand Up @@ -259,10 +263,6 @@ export interface SWRHook {
config: SWROptions
): SWRResponse<Data, Error, SWROptions>
<Data = any, Error = any>(key: Key): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null
): SWRResponse<Data, Error>
<
Data = any,
Error = any,
Expand Down Expand Up @@ -394,13 +394,10 @@ export interface ScopedMutator {
* @typeParam Data - The type of the data related to the key
* @typeParam MutationData - The type of the data returned by the mutator
*/
export type KeyedMutator<Data> = <MutationData>(
data?:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
export type KeyedMutator<Data> = <MutationData = Data>(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data, MutationData>
) => Promise<MutationData | undefined>
) => Promise<Data | MutationData | undefined>

export type SWRConfiguration<
Data = any,
Expand Down
2 changes: 1 addition & 1 deletion core/src/use-swr.ts
Expand Up @@ -5,7 +5,7 @@ import ReactExports, {
useDebugValue,
useMemo
} from 'react'
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'
import { useSyncExternalStore } from 'use-sync-external-store/shim'

import {
defaultConfig,
Expand Down
14 changes: 9 additions & 5 deletions infinite/src/index.ts
Expand Up @@ -33,7 +33,7 @@ import type {
SWRInfiniteCacheValue,
SWRInfiniteCompareFn
} from './types'
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'
import { useSyncExternalStore } from 'use-sync-external-store/shim'
import { getFirstPageKey } from './serialize'

// const INFINITE_PREFIX = '$inf$'
Expand Down Expand Up @@ -232,8 +232,12 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>

const mutate = useCallback(
// eslint-disable-next-line func-names
function <T>(
data?: undefined | T | Promise<T | undefined> | MutatorCallback<T>,
function <T = Data[]>(
data?:
| undefined
| Data[]
| Promise<Data[] | undefined>
| MutatorCallback<Data[]>,
opts?: undefined | boolean | MutatorOptions<Data[], T>
) {
// When passing as a boolean, it's explicitly used to disable/enable
Expand All @@ -257,8 +261,8 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
}

return arguments.length
? swr.mutate<T>(data, { ...options, revalidate: shouldRevalidate })
: swr.mutate<T>()
? swr.mutate(data, { ...options, revalidate: shouldRevalidate })
: swr.mutate()
},
// swr.mutate is always the same reference
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
10 changes: 8 additions & 2 deletions mutation/src/index.ts
Expand Up @@ -14,7 +14,10 @@ import type {
SWRMutationConfiguration,
SWRMutationResponse,
SWRMutationHook,
MutationFetcher
MutationFetcher,
TriggerWithArgs,
TriggerWithoutArgs,
TriggerWithOptionsArgs
} from './types'

const mutation = (<Data, Error>() =>
Expand Down Expand Up @@ -160,5 +163,8 @@ export {
SWRMutationConfiguration,
SWRMutationResponse,
SWRMutationHook,
MutationFetcher
MutationFetcher,
TriggerWithArgs,
TriggerWithoutArgs,
TriggerWithOptionsArgs
}
2 changes: 1 addition & 1 deletion mutation/src/types.ts
Expand Up @@ -88,7 +88,7 @@ export interface TriggerWithArgs<
): Promise<Data | undefined>
}

interface TriggerWithOptionsArgs<
export interface TriggerWithOptionsArgs<
Data = any,
Error = any,
SWRMutationKey extends Key = Key,
Expand Down
2 changes: 2 additions & 0 deletions test/type/fetcher.ts
Expand Up @@ -6,6 +6,8 @@ import type { Equal } from '@type-challenges/utils'
export function useDataErrorGeneric() {
useSWR<{ id: number }>('/api/', () => ({ id: 123 }))
useSWR<string, any>('/api/', (key: string) => key)
const fetcher = ({ url }: { url: string }) => url
useSWR({ url: '/api' }, fetcher)
useSWRInfinite<string[], any>(
(index, previousPageData) => {
expectType<Equal<number, typeof index>>(true)
Expand Down
4 changes: 3 additions & 1 deletion test/type/mutate.ts
Expand Up @@ -62,9 +62,11 @@ export function useMutatorTypes() {

mutate(async () => '1')

// @ts-expect-error
mutate(async () => 1)

mutate(async () => 1, { populateCache: false })
// FIXME: this should work.
// mutate(async () => 1, { populateCache: false })
}

export function useConfigMutate() {
Expand Down
5 changes: 3 additions & 2 deletions test/use-swr-local-mutation.test.tsx
Expand Up @@ -1580,10 +1580,11 @@ describe('useSWR - local mutation', () => {
}

function Page() {
const { data, mutate } = useSWR(key, () => serverData)
const { mutate } = useSWRConfig()
const { data } = useSWR(key, () => serverData)

appendData = () => {
return mutate(sendRequest('cherry'), {
return mutate<string[], string>(key, sendRequest('cherry'), {
optimisticData: [...data, 'cherry (optimistic)'],
populateCache: (result, currentData) => [
...currentData,
Expand Down

0 comments on commit 6c9799a

Please sign in to comment.