import type { Action, AsyncThunkAction, Dispatch, Middleware, MiddlewareAPI, ThunkAction, ThunkDispatch, UnknownAction, } from '@reduxjs/toolkit' import type { Api, ApiContext } from '../../apiTypes' import type { AssertTagTypes, EndpointDefinitions, } from '../../endpointDefinitions' import type { QueryStatus, QuerySubState, RootState, SubscriptionInternalState, SubscriptionState, } from '../apiState' import type { InfiniteQueryThunk, MutationThunk, QueryThunk, QueryThunkArg, ThunkResult, } from '../buildThunks' import type { InfiniteQueryActionCreatorResult, MutationActionCreatorResult, QueryActionCreatorResult, } from '../buildInitiate' import type { AllSelectors } from '../buildSelectors' export type QueryStateMeta = Record export type TimeoutId = ReturnType type QueryPollState = { nextPollTimestamp: number timeout?: TimeoutId pollingInterval: number } export interface InternalMiddlewareState { currentSubscriptions: SubscriptionInternalState currentPolls: Map runningQueries: Map< string, | QueryActionCreatorResult | InfiniteQueryActionCreatorResult | undefined > runningMutations: Map | undefined> } export interface SubscriptionSelectors { getSubscriptions: () => SubscriptionInternalState getSubscriptionCount: (queryCacheKey: string) => number isRequestSubscribed: (queryCacheKey: string, requestId: string) => boolean } export interface BuildMiddlewareInput< Definitions extends EndpointDefinitions, ReducerPath extends string, TagTypes extends string, > { reducerPath: ReducerPath context: ApiContext queryThunk: QueryThunk mutationThunk: MutationThunk infiniteQueryThunk: InfiniteQueryThunk api: Api assertTagType: AssertTagTypes selectors: AllSelectors getRunningQueryThunk: ( endpointName: string, queryArgs: any, ) => (dispatch: Dispatch) => QueryActionCreatorResult | undefined getInternalState: (dispatch: Dispatch) => InternalMiddlewareState } export type SubMiddlewareApi = MiddlewareAPI< ThunkDispatch, RootState > export interface BuildSubMiddlewareInput extends BuildMiddlewareInput { internalState: InternalMiddlewareState refetchQuery( querySubState: Exclude< QuerySubState, { status: QueryStatus.uninitialized } >, ): ThunkAction, any, any, UnknownAction> isThisApiSliceAction: (action: Action) => boolean selectors: AllSelectors mwApi: MiddlewareAPI< ThunkDispatch, RootState > } export type SubMiddlewareBuilder = ( input: BuildSubMiddlewareInput, ) => Middleware< {}, RootState, ThunkDispatch > type MwNext = Parameters>[0] export type ApiMiddlewareInternalHandler = ( action: Action, mwApi: SubMiddlewareApi & { next: MwNext }, prevState: RootState, ) => Return export type InternalHandlerBuilder = ( input: BuildSubMiddlewareInput, ) => ApiMiddlewareInternalHandler export interface PromiseConstructorWithKnownReason { /** * Creates a new Promise with a known rejection reason. * @param executor A callback used to initialize the promise. This callback is passed two arguments: * a resolve callback used to resolve the promise with a value or the result of another promise, * and a reject callback used to reject the promise with a provided reason or error. */ new ( executor: ( resolve: (value: T | PromiseLike) => void, reject: (reason?: R) => void, ) => void, ): PromiseWithKnownReason } export type PromiseWithKnownReason = Omit< Promise, 'then' | 'catch' > & { /** * Attaches callbacks for the resolution and/or rejection of the Promise. * @param onfulfilled The callback to execute when the Promise is resolved. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of which ever callback is executed. */ then( onfulfilled?: | ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: | ((reason: R) => TResult2 | PromiseLike) | undefined | null, ): Promise /** * Attaches a callback for only the rejection of the Promise. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of the callback. */ catch( onrejected?: | ((reason: R) => TResult | PromiseLike) | undefined | null, ): Promise }