Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into issue-1933
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainPants committed Dec 18, 2022
2 parents a028877 + 9ea4a45 commit 3b64fe5
Show file tree
Hide file tree
Showing 16 changed files with 1,411 additions and 1,195 deletions.
4 changes: 2 additions & 2 deletions _internal/package.json
Expand Up @@ -5,8 +5,8 @@
"exports": "./dist/index.mjs",
"private": true,
"scripts": {
"watch": "bunchee --target es2018 index.ts -w",
"build": "bunchee --target es2018 index.ts",
"watch": "bunchee index.ts -w",
"build": "bunchee index.ts",
"types:check": "tsc --noEmit",
"clean": "rimraf dist"
},
Expand Down
21 changes: 13 additions & 8 deletions _internal/types.ts
Expand Up @@ -35,11 +35,11 @@ export type Fetcher<
Data = unknown,
SWRKey extends Key = Key
> = SWRKey extends () => infer Arg | null | undefined | false
? (args: Arg, options: FetcherOptions) => FetcherResponse<Data>
? (arg: Arg, options: FetcherOptions) => FetcherResponse<Data>
: SWRKey extends null | undefined | false
? never
: SWRKey extends infer Arg
? (args: Arg, options: FetcherOptions) => FetcherResponse<Data>
? (arg: Arg, options: FetcherOptions) => FetcherResponse<Data>
: never

// Configuration types that are only used internally, not exposed to the user.
Expand Down Expand Up @@ -214,7 +214,11 @@ export interface PublicConfiguration<
abortDiscardedRequests?: boolean
}

export type FullConfiguration = InternalConfiguration & PublicConfiguration
export type FullConfiguration<
Data = any,
Error = any,
Fn extends Fetcher = BareFetcher
> = InternalConfiguration & PublicConfiguration<Data, Error, Fn>

export type ProviderConfiguration = {
initFocus: (callback: () => void) => (() => void) | void
Expand All @@ -227,18 +231,18 @@ export type ProviderConfiguration = {
* ```
*/
export interface SWRHook {
<Data = any, Error = any, SWRKey extends Key = null>(
<Data = any, Error = any, SWRKey extends Key = StrictKey>(
key: SWRKey
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
<Data = any, Error = any, SWRKey extends Key = StrictKey>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
<Data = any, Error = any, SWRKey extends Key = StrictKey>(
key: SWRKey,
config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
<Data = any, Error = any, SWRKey extends Key = StrictKey>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null,
config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined
Expand Down Expand Up @@ -278,7 +282,8 @@ export type Arguments =
| undefined
| false
export type Key = Arguments | (() => Arguments)

export type StrictTupleKey = ArgumentsTuple | null | undefined | false
type StrictKey = StrictTupleKey | (() => StrictTupleKey)
export type MutatorCallback<Data = any> = (
currentData?: Data
) => Promise<undefined | Data> | undefined | Data
Expand Down
4 changes: 2 additions & 2 deletions core/package.json
Expand Up @@ -5,8 +5,8 @@
"exports": "./dist/index.mjs",
"private": true,
"scripts": {
"watch": "bunchee --target es2018 index.ts -w",
"build": "bunchee --target es2018 index.ts",
"watch": "bunchee index.ts -w",
"build": "bunchee index.ts",
"types:check": "tsc --noEmit",
"clean": "rimraf dist"
},
Expand Down
5 changes: 3 additions & 2 deletions core/use-swr.ts
Expand Up @@ -54,7 +54,8 @@ type DefinitelyTruthy<T> = false extends T
export const useSWRHandler = <Data = any, Error = any>(
_key: Key,
fetcher: Fetcher<Data> | null,
config: typeof defaultConfig & SWRConfiguration<Data, Error>
config: FullConfiguration<Data, Error, Fetcher<Data>> &
SWRConfiguration<Data, Error>
) => {
const {
cache,
Expand Down Expand Up @@ -443,7 +444,7 @@ export const useSWRHandler = <Data = any, Error = any>(
getConfig().onSuccess(newData, key, config)
}
}
} catch (err) {
} catch (err: any) {
cleanupState()

const currentConfig = getConfig()
Expand Down
4 changes: 2 additions & 2 deletions immutable/package.json
Expand Up @@ -5,8 +5,8 @@
"exports": "./dist/index.mjs",
"private": true,
"scripts": {
"watch": "bunchee --target es2018 index.ts -w",
"build": "bunchee --target es2018 index.ts",
"watch": "bunchee index.ts -w",
"build": "bunchee index.ts",
"types:check": "tsc --noEmit",
"clean": "rimraf dist"
},
Expand Down
4 changes: 2 additions & 2 deletions infinite/package.json
Expand Up @@ -5,8 +5,8 @@
"exports": "./dist/index.mjs",
"private": true,
"scripts": {
"watch": "bunchee --target es2018 index.ts -w",
"build": "bunchee --target es2018 index.ts",
"watch": "bunchee index.ts -w",
"build": "bunchee index.ts",
"types:check": "tsc --noEmit",
"clean": "rimraf dist"
},
Expand Down
19 changes: 10 additions & 9 deletions infinite/types.ts
Expand Up @@ -3,7 +3,8 @@ import type {
SWRResponse,
Arguments,
BareFetcher,
State
State,
StrictTupleKey
} from 'swr/_internal'

type FetcherResponse<Data = unknown> = Data | Promise<Data>
Expand All @@ -17,10 +18,10 @@ export type SWRInfiniteFetcher<
: never
: never

export type SWRInfiniteKeyLoader<Data = any> = (
index: number,
previousPageData: Data | null
) => Arguments
export type SWRInfiniteKeyLoader<
Data = any,
Args extends Arguments = Arguments
> = (index: number, previousPageData: Data | null) => Args

export interface SWRInfiniteConfiguration<
Data = any,
Expand Down Expand Up @@ -49,7 +50,7 @@ export interface SWRInfiniteHook {
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
) => StrictTupleKey
>(
getKey: KeyLoader
): SWRInfiniteResponse<Data, Error>
Expand All @@ -59,7 +60,7 @@ export interface SWRInfiniteHook {
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
) => StrictTupleKey
>(
getKey: KeyLoader,
fetcher: SWRInfiniteFetcher<Data, KeyLoader> | null
Expand All @@ -70,7 +71,7 @@ export interface SWRInfiniteHook {
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
) => StrictTupleKey
>(
getKey: KeyLoader,
config:
Expand All @@ -87,7 +88,7 @@ export interface SWRInfiniteHook {
KeyLoader extends SWRInfiniteKeyLoader = (
index: number,
previousPageData: Data | null
) => null
) => StrictTupleKey
>(
getKey: KeyLoader,
fetcher: SWRInfiniteFetcher<Data, KeyLoader> | null,
Expand Down
6 changes: 4 additions & 2 deletions mutation/index.ts
Expand Up @@ -26,6 +26,7 @@ const mutation = (<Data, Error>() =>
const { mutate } = useSWRConfig()

const keyRef = useRef(key)
const fetcherRef = useRef(fetcher)
// Ditch all mutation results that happened earlier than this timestamp.
const ditchMutationsUntilRef = useRef(0)

Expand All @@ -40,7 +41,7 @@ const mutation = (<Data, Error>() =>
async (arg: any, opts?: SWRMutationConfiguration<Data, Error>) => {
const [serializedKey, resolvedKey] = serialize(keyRef.current)

if (!fetcher) {
if (!fetcherRef.current) {
throw new Error('Can’t trigger the mutation: missing fetcher.')
}
if (!serializedKey) {
Expand All @@ -64,7 +65,7 @@ const mutation = (<Data, Error>() =>
try {
const data = await mutate<Data>(
serializedKey,
(fetcher as any)(resolvedKey, { arg }),
(fetcherRef.current as any)(resolvedKey, { arg }),
// We must throw the error here so we can catch and update the states.
mergeObjects(options, { throwOnError: true })
)
Expand Down Expand Up @@ -99,6 +100,7 @@ const mutation = (<Data, Error>() =>

useIsomorphicLayoutEffect(() => {
keyRef.current = key
fetcherRef.current = fetcher
})

// We don't return `mutate` here as it can be pretty confusing (e.g. people
Expand Down
4 changes: 2 additions & 2 deletions mutation/package.json
Expand Up @@ -5,8 +5,8 @@
"exports": "./dist/index.mjs",
"private": true,
"scripts": {
"watch": "bunchee --target es2018 index.ts -w",
"build": "bunchee --target es2018 index.ts",
"watch": "bunchee index.ts -w",
"build": "bunchee index.ts",
"types:check": "tsc --noEmit",
"clean": "rimraf dist"
},
Expand Down
56 changes: 28 additions & 28 deletions package.json
Expand Up @@ -64,12 +64,12 @@
"license": "MIT",
"scripts": {
"prepare": "husky install",
"csb:install": "pnpm i -g pnpm@7",
"csb:build": "pnpm install && pnpm build",
"csb:install": "pnpm install",
"csb:build": "pnpm build",
"clean": "pnpm -r run clean",
"watch": "pnpm -r run watch",
"build": "pnpm build-package _internal && pnpm build-package core && pnpm build-package infinite && pnpm build-package immutable && pnpm build-package mutation",
"build-package": "bunchee --target es2018 index.ts --cwd",
"build-package": "bunchee index.ts --cwd",
"types:check": "pnpm -r run types:check",
"prepublishOnly": "pnpm clean && pnpm build",
"publish-beta": "pnpm publish --tag beta",
Expand All @@ -88,36 +88,36 @@
]
},
"devDependencies": {
"@swc/core": "^1.3.5",
"@swc/jest": "0.2.23",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.1.1",
"@swc/core": "^1.3.22",
"@swc/jest": "0.2.24",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@type-challenges/utils": "0.1.1",
"@types/jest": "^27.5.1",
"@types/node": "^18.7.14",
"@types/react": "^18.0.9",
"@types/jest": "^29.2.4",
"@types/node": "^18.11.13",
"@types/react": "^18.0.26",
"@types/use-sync-external-store": "^0.0.3",
"@typescript-eslint/eslint-plugin": "5.36.1",
"@typescript-eslint/parser": "5.36.1",
"bunchee": "2.1.7",
"eslint": "8.15.0",
"@typescript-eslint/eslint-plugin": "5.46.1",
"@typescript-eslint/parser": "5.46.1",
"bunchee": "2.2.0",
"eslint": "8.29.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-jest-dom": "4.0.1",
"eslint-plugin-react": "7.30.0",
"eslint-plugin-react-hooks": "4.5.0",
"eslint-plugin-testing-library": "5.5.0",
"husky": "8.0.1",
"jest": "28.1.0",
"jest-environment-jsdom": "28.1.0",
"eslint-plugin-jest-dom": "4.0.3",
"eslint-plugin-react": "7.31.11",
"eslint-plugin-react-hooks": "4.6.0",
"eslint-plugin-testing-library": "5.9.1",
"husky": "8.0.2",
"jest": "29.3.1",
"jest-environment-jsdom": "29.3.1",
"lint-staged": "13.0.3",
"next": "^12.1.6",
"prettier": "2.6.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"next": "^13.0.6",
"prettier": "2.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "3.0.2",
"swr": "workspace:*",
"tslib": "2.4.0",
"typescript": "4.8.2"
"tslib": "2.4.1",
"typescript": "4.9.4"
},
"peerDependencies": {
"react": "^16.11.0 || ^17.0.0 || ^18.0.0"
Expand All @@ -136,4 +136,4 @@
"dependencies": {
"use-sync-external-store": "^1.2.0"
}
}
}

0 comments on commit 3b64fe5

Please sign in to comment.