Skip to content
Draft
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
14 changes: 13 additions & 1 deletion packages/toolkit/src/query/core/apiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,19 @@ export type MutationState<D extends EndpointDefinitions> = {
[requestId: string]: MutationSubState<D[string]> | undefined
}

export type RootState<
/**
* Get the root state type for an RTK Query API slice.
*
* **Note**: This type was previously named **`RootState`**.
*
* @template Definitions - Endpoint definitions for the API.
* @template TagTypes - Tag types used by the API.
* @template ReducerPath - The **`reducerPath`** key the API reducer is mounted under.
*
* @since 3.0.0
* @public
*/
export type ApiRootState<
Definitions extends EndpointDefinitions,
TagTypes extends string,
ReducerPath extends string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
DefinitionType,
} from '../../endpointDefinitions'
import { isAnyQueryDefinition } from '../../endpointDefinitions'
import type { QueryCacheKey, RootState } from '../apiState'
import type { QueryCacheKey, ApiRootState } from '../apiState'
import type {
MutationResultSelectorResult,
QueryResultSelectorResult,
Expand Down Expand Up @@ -78,7 +78,7 @@ type LifecycleApi<ReducerPath extends string = string> = {
/**
* A method to get the current state
*/
getState(): RootState<any, any, ReducerPath>
getState(): ApiRootState<any, any, ReducerPath>
/**
* `extra` as provided as `thunk.extraArgument` to the `configureStore` `getDefaultMiddleware` option.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/core/buildMiddleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
EndpointDefinitions,
FullTagDescription,
} from '../../endpointDefinitions'
import type { QueryStatus, QuerySubState, RootState } from '../apiState'
import type { QueryStatus, QuerySubState, ApiRootState } from '../apiState'
import type { QueryThunkArg } from '../buildThunks'
import { createAction, isAction } from '../rtkImports'
import { buildBatchedActionsHandler } from './batchActions'
Expand Down Expand Up @@ -68,7 +68,7 @@ export function buildMiddleware<

const middleware: Middleware<
{},
RootState<Definitions, string, ReducerPath>,
ApiRootState<Definitions, string, ReducerPath>,
ThunkDispatch<any, any, UnknownAction>
> = (mwApi) => {
let initialized = false
Expand Down
10 changes: 5 additions & 5 deletions packages/toolkit/src/query/core/buildMiddleware/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
import type {
QueryStatus,
QuerySubState,
RootState,
ApiRootState,
SubscriptionInternalState,
SubscriptionState,
} from '../apiState'
Expand Down Expand Up @@ -83,7 +83,7 @@ export interface BuildMiddlewareInput<

export type SubMiddlewareApi = MiddlewareAPI<
ThunkDispatch<any, any, UnknownAction>,
RootState<EndpointDefinitions, string, string>
ApiRootState<EndpointDefinitions, string, string>
>

export interface BuildSubMiddlewareInput
Expand All @@ -99,15 +99,15 @@ export interface BuildSubMiddlewareInput
selectors: AllSelectors
mwApi: MiddlewareAPI<
ThunkDispatch<any, any, UnknownAction>,
RootState<EndpointDefinitions, string, string>
ApiRootState<EndpointDefinitions, string, string>
>
}

export type SubMiddlewareBuilder = (
input: BuildSubMiddlewareInput,
) => Middleware<
{},
RootState<EndpointDefinitions, string, string>,
ApiRootState<EndpointDefinitions, string, string>,
ThunkDispatch<any, any, UnknownAction>
>

Expand All @@ -116,7 +116,7 @@ type MwNext = Parameters<ReturnType<Middleware>>[0]
export type ApiMiddlewareInternalHandler<Return = void> = (
action: Action,
mwApi: SubMiddlewareApi & { next: MwNext },
prevState: RootState<EndpointDefinitions, string, string>,
prevState: ApiRootState<EndpointDefinitions, string, string>,
) => Return

export type InternalHandlerBuilder<ReturnType = void> = (
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/core/buildSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
QueryState,
QuerySubState,
RequestStatusFlags,
RootState as _RootState,
ApiRootState as _RootState,
QueryStatus,
} from './apiState'
import { STATUS_UNINITIALIZED, getRequestStatusFlags } from './apiState'
Expand Down
16 changes: 8 additions & 8 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
import { HandledError } from '../HandledError'
import type { UnwrapPromise } from '../tsHelpers'
import type {
RootState,
ApiRootState,
QueryKeys,
QuerySubstateIdentifier,
InfiniteData,
Expand Down Expand Up @@ -355,7 +355,7 @@ export function buildThunks<
catchSchemaFailure: SchemaFailureConverter<BaseQuery> | undefined
skipSchemaValidation: boolean | SchemaType[] | undefined
}) {
type State = RootState<any, string, ReducerPath>
type State = ApiRootState<any, string, ReducerPath>

const patchQueryData: PatchQueryDataThunk<EndpointDefinitions, State> =
(endpointName, arg, patches, updateProvided) => (dispatch, getState) => {
Expand All @@ -377,7 +377,7 @@ export function buildThunks<

const newValue = api.endpoints[endpointName].select(arg)(
// Work around TS 4.1 mismatch
getState() as RootState<any, any, any>,
getState() as ApiRootState<any, any, any>,
)

const providedTags = calculateProvidedBy(
Expand Down Expand Up @@ -411,7 +411,7 @@ export function buildThunks<

const currentState = endpointDefinition.select(arg)(
// Work around TS 4.1 mismatch
getState() as RootState<any, any, any>,
getState() as ApiRootState<any, any, any>,
)

const ret: PatchCollection = {
Expand Down Expand Up @@ -494,7 +494,7 @@ export function buildThunks<
const executeEndpoint: AsyncThunkPayloadCreator<
ThunkResult,
QueryThunkArg | MutationThunkArg | InfiniteQueryThunkArg<any>,
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
ThunkApiMetaConfig & { state: ApiRootState<any, string, ReducerPath> }
> = async (
arg,
{
Expand Down Expand Up @@ -884,7 +884,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`

function isForcedQuery(
arg: QueryThunkArg,
state: RootState<any, string, ReducerPath>,
state: ApiRootState<any, string, ReducerPath>,
) {
const requestState = selectors.selectQueryEntry(state, arg.queryCacheKey)
const baseFetchOnMountOrArgChange =
Expand All @@ -910,7 +910,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
const generatedQueryThunk = createAsyncThunk<
ThunkResult,
ThunkArgType,
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
ThunkApiMetaConfig & { state: ApiRootState<any, string, ReducerPath> }
>(`${reducerPath}/executeQuery`, executeEndpoint, {
getPendingMeta({ arg }) {
const endpointDefinition = endpointDefinitions[arg.endpointName]
Expand Down Expand Up @@ -984,7 +984,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
const mutationThunk = createAsyncThunk<
ThunkResult,
MutationThunkArg,
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
ThunkApiMetaConfig & { state: ApiRootState<any, string, ReducerPath> }
>(`${reducerPath}/executeMutation`, executeEndpoint, {
getPendingMeta() {
return addShouldAutoBatch({ startedTimeStamp: Date.now() })
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type {
QueryCacheKey,
QueryKeys,
QuerySubState,
RootState,
ApiRootState,
SubscriptionOptions,
} from './apiState'
export type {
Expand Down
14 changes: 7 additions & 7 deletions packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type {
CombinedState,
MutationKeys,
QueryKeys,
RootState,
ApiRootState,
} from './apiState'
import type {
BuildInitiateApiEndpointMutation,
Expand Down Expand Up @@ -157,7 +157,7 @@ export interface ApiModules<
*/
middleware: Middleware<
{},
RootState<Definitions, string, ReducerPath>,
ApiRootState<Definitions, string, ReducerPath>,
ThunkDispatch<any, any, UnknownAction>
>
/**
Expand Down Expand Up @@ -274,7 +274,7 @@ export interface ApiModules<
*/
updateQueryData: UpdateQueryDataThunk<
Definitions,
RootState<Definitions, string, ReducerPath>
ApiRootState<Definitions, string, ReducerPath>
>

/**
Expand All @@ -298,7 +298,7 @@ export interface ApiModules<
*/
upsertQueryData: UpsertQueryDataThunk<
Definitions,
RootState<Definitions, string, ReducerPath>
ApiRootState<Definitions, string, ReducerPath>
>
/**
* A Redux thunk that applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
Expand Down Expand Up @@ -328,7 +328,7 @@ export interface ApiModules<
*/
patchQueryData: PatchQueryDataThunk<
Definitions,
RootState<Definitions, string, ReducerPath>
ApiRootState<Definitions, string, ReducerPath>
>

/**
Expand Down Expand Up @@ -381,7 +381,7 @@ export interface ApiModules<
* Can be used for mutations that want to do optimistic updates instead of invalidating a set of tags, but don't know exactly what they need to update.
*/
selectInvalidatedBy: (
state: RootState<Definitions, string, ReducerPath>,
state: ApiRootState<Definitions, string, ReducerPath>,
tags: ReadonlyArray<TagDescription<TagTypes> | null | undefined>,
) => Array<{
endpointName: string
Expand All @@ -395,7 +395,7 @@ export interface ApiModules<
* Can be used for mutations that want to do optimistic updates instead of invalidating a set of tags, but don't know exactly what they need to update.
*/
selectCachedArgsForQuery: <QueryName extends AllQueryKeys<Definitions>>(
state: RootState<Definitions, string, ReducerPath>,
state: ApiRootState<Definitions, string, ReducerPath>,
queryName: QueryName,
) => Array<QueryArgFromAnyQuery<Definitions[QueryName]>>
}
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
InfiniteData,
InfiniteQueryConfigOptions,
QuerySubState,
RootState,
ApiRootState,
} from './core/index'
import type { SerializeQueryArgs } from './defaultSerializeQueryArgs'
import type { NEVER } from './fakeBaseQuery'
Expand Down Expand Up @@ -855,7 +855,7 @@ export interface QueryExtraOptions<
forceRefetch?(params: {
currentArg: QueryArg | undefined
previousArg: QueryArg | undefined
state: RootState<any, any, string>
state: ApiRootState<any, any, string>
endpointState?: QuerySubState<any>
}): boolean

Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type {
QueryCacheKey,
QueryKeys,
QuerySubState,
RootState,
ApiRootState,
SubscriptionOptions,
} from './core/apiState'
export { QueryStatus } from './core/apiState'
Expand Down
18 changes: 9 additions & 9 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
QueryResultSelectorResult,
QuerySubState,
ResultTypeFrom,
RootState,
ApiRootState,
SerializeQueryArgs,
SkipToken,
SubscriptionOptions,
Expand Down Expand Up @@ -1847,11 +1847,11 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
>
const stableArg = useStableQueryArgs(skip ? skipToken : arg)

type ApiRootState = Parameters<ReturnType<typeof select>>[0]
type RootStateType = Parameters<ReturnType<typeof select>>[0]

const lastValue = useRef<any>(undefined)

const selectDefaultResult: Selector<ApiRootState, any, [any]> = useMemo(
const selectDefaultResult: Selector<RootStateType, any, [any]> = useMemo(
() =>
// Normally ts-ignores are bad and should be avoided, but we're
// already casting this selector to be `Selector<any>` anyway,
Expand All @@ -1861,8 +1861,8 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
[
// @ts-ignore
select(stableArg),
(_: ApiRootState, lastResult: any) => lastResult,
(_: ApiRootState) => stableArg,
(_: RootStateType, lastResult: any) => lastResult,
(_: RootStateType) => stableArg,
],
preSelector,
{
Expand All @@ -1874,7 +1874,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
[select, stableArg],
)

const querySelector: Selector<ApiRootState, any, [any]> = useMemo(
const querySelector: Selector<RootStateType, any, [any]> = useMemo(
() =>
selectFromResult
? createSelector([selectDefaultResult], selectFromResult, {
Expand All @@ -1885,12 +1885,12 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
)

const currentState = useSelector(
(state: RootState<Definitions, any, any>) =>
(state: ApiRootState<Definitions, any, any>) =>
querySelector(state, lastValue.current),
shallowEqual,
)

const store = useStore<RootState<Definitions, any, any>>()
const store = useStore<ApiRootState<Definitions, any, any>>()
const newLastValue = selectDefaultResult(
store.getState(),
lastValue.current,
Expand Down Expand Up @@ -2252,7 +2252,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
[fixedCacheKey, promise, select],
)
const mutationSelector = useMemo(
(): Selector<RootState<Definitions, any, any>, any> =>
(): Selector<ApiRootState<Definitions, any, any>, any> =>
selectFromResult
? createSelector([selectDefaultResult], selectFromResult)
: selectDefaultResult,
Expand Down
6 changes: 3 additions & 3 deletions packages/toolkit/src/query/tests/queryLifecycle.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { PatchCollection, Recipe } from '@internal/query/core/buildThunks'
import type { ThunkDispatch, UnknownAction } from '@reduxjs/toolkit'
import type {
ApiRootState,
FetchBaseQueryError,
FetchBaseQueryMeta,
RootState,
TypedMutationOnQueryStarted,
TypedQueryOnQueryStarted,
} from '@reduxjs/toolkit/query'
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('type tests', () => {
expectTypeOf(extra).toBeUnknown()

expectTypeOf(getState).toEqualTypeOf<
() => RootState<any, any, 'postsApi'>
() => ApiRootState<any, any, 'postsApi'>
>()

expectTypeOf(requestId).toBeString()
Expand Down Expand Up @@ -325,7 +325,7 @@ describe('type tests', () => {
expectTypeOf(extra).toBeUnknown()

expectTypeOf(getState).toEqualTypeOf<
() => RootState<any, any, 'postsApi'>
() => ApiRootState<any, any, 'postsApi'>
>()

expectTypeOf(requestId).toBeString()
Expand Down
Loading