From 92d97f034dce46140de56bbd2a4e3bb2854b85da Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 9 Dec 2024 14:50:34 +0100 Subject: [PATCH] chore: reorganize entries (#3048) --- package.json | 16 ++++++++-------- src/core/index.ts | 30 ------------------------------ src/immutable/index.ts | 4 ++-- src/index/index.react-server.ts | 2 +- src/index/index.ts | 32 ++++++++++++++++++++++++++++++-- src/{core => index}/serialize.ts | 0 src/{core => index}/use-swr.ts | 2 +- src/infinite/index.ts | 13 ++----------- src/mutation/index.ts | 2 +- src/mutation/types.ts | 2 +- src/subscription/index.ts | 4 ++-- src/subscription/types.ts | 2 +- 12 files changed, 49 insertions(+), 60 deletions(-) delete mode 100644 src/core/index.ts rename src/{core => index}/serialize.ts (100%) rename src/{core => index}/use-swr.ts (99%) diff --git a/package.json b/package.json index 8e68abaca2..b00a0d45e6 100644 --- a/package.json +++ b/package.json @@ -11,21 +11,21 @@ "fetch" ], "packageManager": "pnpm@8.4.0", - "main": "./dist/core/index.js", - "module": "./dist/core/index.mjs", - "types": "./dist/core/index.d.ts", + "main": "./dist/index/index.js", + "module": "./dist/index/index.mjs", + "types": "./dist/index/index.d.ts", "sideEffects": false, "exports": { "./package.json": "./package.json", ".": { - "react-server": "./dist/core/react-server.mjs", + "react-server": "./dist/index/react-server.mjs", "import": { - "types": "./dist/core/index.d.mts", - "default": "./dist/core/index.mjs" + "types": "./dist/index/index.d.mts", + "default": "./dist/index/index.mjs" }, "require": { - "types": "./dist/core/index.d.ts", - "default": "./dist/core/index.js" + "types": "./dist/index/index.d.ts", + "default": "./dist/index/index.js" } }, "./infinite": { diff --git a/src/core/index.ts b/src/core/index.ts deleted file mode 100644 index b5419ddb53..0000000000 --- a/src/core/index.ts +++ /dev/null @@ -1,30 +0,0 @@ -// useSWR -import useSWR from './use-swr' -export default useSWR -// Core APIs -export { SWRConfig } from './use-swr' -export { unstable_serialize } from './serialize' -export { useSWRConfig } from '../_internal' -export { mutate } from '../_internal' -export { preload } from '../_internal' - -// Types -export type { - SWRConfiguration, - Revalidator, - RevalidatorOptions, - Key, - KeyLoader, - KeyedMutator, - SWRHook, - SWRResponse, - Cache, - BareFetcher, - Fetcher, - MutatorCallback, - MutatorOptions, - Middleware, - Arguments, - State, - ScopedMutator -} from '../_internal' diff --git a/src/immutable/index.ts b/src/immutable/index.ts index 6d1ad7d12f..f7210a3110 100644 --- a/src/immutable/index.ts +++ b/src/immutable/index.ts @@ -1,5 +1,5 @@ -import type { Middleware } from '../core' -import useSWR from '../core' +import type { Middleware } from '../index' +import useSWR from '../index' import { withMiddleware } from '../_internal' export const immutable: Middleware = useSWRNext => (key, fetcher, config) => { diff --git a/src/index/index.react-server.ts b/src/index/index.react-server.ts index ad596e7caa..50b3528d1c 100644 --- a/src/index/index.react-server.ts +++ b/src/index/index.react-server.ts @@ -1,2 +1,2 @@ -export { unstable_serialize } from '../core/serialize' +export { unstable_serialize } from './serialize' export { SWRConfig } from './config' diff --git a/src/index/index.ts b/src/index/index.ts index fb309c0699..b5419ddb53 100644 --- a/src/index/index.ts +++ b/src/index/index.ts @@ -1,2 +1,30 @@ -export * from '../core' -export { default } from '../core' +// useSWR +import useSWR from './use-swr' +export default useSWR +// Core APIs +export { SWRConfig } from './use-swr' +export { unstable_serialize } from './serialize' +export { useSWRConfig } from '../_internal' +export { mutate } from '../_internal' +export { preload } from '../_internal' + +// Types +export type { + SWRConfiguration, + Revalidator, + RevalidatorOptions, + Key, + KeyLoader, + KeyedMutator, + SWRHook, + SWRResponse, + Cache, + BareFetcher, + Fetcher, + MutatorCallback, + MutatorOptions, + Middleware, + Arguments, + State, + ScopedMutator +} from '../_internal' diff --git a/src/core/serialize.ts b/src/index/serialize.ts similarity index 100% rename from src/core/serialize.ts rename to src/index/serialize.ts diff --git a/src/core/use-swr.ts b/src/index/use-swr.ts similarity index 99% rename from src/core/use-swr.ts rename to src/index/use-swr.ts index 0bd601dd44..98ef34f1f2 100644 --- a/src/core/use-swr.ts +++ b/src/index/use-swr.ts @@ -267,7 +267,7 @@ export const useSWRHandler = ( const cachedData = cached.data const data = isUndefined(cachedData) - ? (fallback && isPromiseLike(fallback)) + ? fallback && isPromiseLike(fallback) ? use(fallback) : fallback : cachedData diff --git a/src/infinite/index.ts b/src/infinite/index.ts index f410b5befb..4292ea4486 100644 --- a/src/infinite/index.ts +++ b/src/infinite/index.ts @@ -2,8 +2,8 @@ // hook where `key` and return type are not like the normal `useSWR` types. import { useRef, useCallback } from 'react' -import type { SWRConfig } from '../core' -import useSWR from '../core' +import type { SWRConfig } from '../index' +import useSWR from '../index' import { isUndefined, isFunction, @@ -37,19 +37,10 @@ import type { import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js' import { getFirstPageKey } from './serialize' -// const INFINITE_PREFIX = '$inf$' const EMPTY_PROMISE = Promise.resolve() as Promise -// const getFirstPageKey = (getKey: SWRInfiniteKeyLoader) => { -// return serialize(getKey ? getKey(0, null) : null)[0] -// } - export { unstable_serialize } from './serialize' -// export const unstable_serialize = (getKey: SWRInfiniteKeyLoader) => { -// return INFINITE_PREFIX + getFirstPageKey(getKey) -// } - export const infinite = ((useSWRNext: SWRHook) => ( getKey: SWRInfiniteKeyLoader, diff --git a/src/mutation/index.ts b/src/mutation/index.ts index e15468596c..2b32967d34 100644 --- a/src/mutation/index.ts +++ b/src/mutation/index.ts @@ -1,5 +1,5 @@ import { useCallback, useRef } from 'react' -import useSWR, { useSWRConfig } from '../core' +import useSWR, { useSWRConfig } from '../index' import type { Middleware, Key } from '../_internal' import { useStateWithDeps, startTransition } from './state' import { diff --git a/src/mutation/types.ts b/src/mutation/types.ts index 4706c10ee7..1aa984f97f 100644 --- a/src/mutation/types.ts +++ b/src/mutation/types.ts @@ -1,4 +1,4 @@ -import type { SWRResponse, Key, Arguments } from '../core' +import type { SWRResponse, Key, Arguments } from '../index' type FetcherResponse = Data | Promise diff --git a/src/subscription/index.ts b/src/subscription/index.ts index 1ecfb1ab0e..f7c2cba818 100644 --- a/src/subscription/index.ts +++ b/src/subscription/index.ts @@ -4,14 +4,14 @@ import type { Middleware, SWRConfiguration, SWRConfig -} from '../core' +} from '../index' import type { SWRSubscriptionOptions, SWRSubscription, SWRSubscriptionResponse, SWRSubscriptionHook } from './types' -import useSWR from '../core' +import useSWR from '../index' import { withMiddleware, serialize, diff --git a/src/subscription/types.ts b/src/subscription/types.ts index 34f2ae0811..c9345db0fd 100644 --- a/src/subscription/types.ts +++ b/src/subscription/types.ts @@ -1,4 +1,4 @@ -import type { Key, SWRConfiguration, MutatorCallback } from '../core' +import type { Key, SWRConfiguration, MutatorCallback } from '../index' export type SWRSubscriptionOptions = { next: (err?: Error | null, data?: Data | MutatorCallback) => void