Skip to content

Commit 7513d3b

Browse files
committed
Consolidate CacheLifecycleMutationExtraOptions
1 parent 2db682a commit 7513d3b

File tree

1 file changed

+17
-68
lines changed

1 file changed

+17
-68
lines changed

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import type {
99
BaseQueryResult,
1010
QueryReturnValue,
1111
} from './baseQueryTypes'
12+
import type { QuerySubState, RootState } from './core'
1213
import type {
13-
MutationCacheLifecycleApi,
14-
MutationLifecycleApi,
15-
QuerySubState,
16-
RootState,
17-
} from './core'
18-
import type { CacheLifecycleQueryExtraOptions } from './core/buildMiddleware/cacheLifecycle'
19-
import type { QueryLifecycleQueryExtraOptions } from './core/buildMiddleware/queryLifecycle'
14+
CacheLifecycleMutationExtraOptions,
15+
CacheLifecycleQueryExtraOptions,
16+
} from './core/buildMiddleware/cacheLifecycle'
17+
import type {
18+
QueryLifecycleMutationExtraOptions,
19+
QueryLifecycleQueryExtraOptions,
20+
} from './core/buildMiddleware/queryLifecycle'
2021
import type { SerializeQueryArgs } from './defaultSerializeQueryArgs'
2122
import type { NEVER } from './fakeBaseQuery'
2223
import type {
@@ -563,71 +564,19 @@ export interface MutationExtraOptions<
563564
QueryArg,
564565
BaseQuery extends BaseQueryFn,
565566
ReducerPath extends string = string,
566-
> {
567-
type: DefinitionType.mutation
568-
569-
onCacheEntryAdded?(
570-
arg: QueryArg,
571-
api: MutationCacheLifecycleApi<
567+
> extends CacheLifecycleMutationExtraOptions<
568+
ResultType,
572569
QueryArg,
573570
BaseQuery,
574-
ResultType,
575571
ReducerPath
576572
>,
577-
): Promise<void> | void
578-
579-
/**
580-
* A function that is called when the individual mutation is started. The function is called with a lifecycle api object containing properties such as `queryFulfilled`, allowing code to be run when a query is started, when it succeeds, and when it fails (i.e. throughout the lifecycle of an individual query/mutation call).
581-
*
582-
* Can be used for `optimistic updates`.
583-
*
584-
* @example
585-
*
586-
* ```ts
587-
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
588-
* export interface Post {
589-
* id: number
590-
* name: string
591-
* }
592-
*
593-
* const api = createApi({
594-
* baseQuery: fetchBaseQuery({
595-
* baseUrl: '/',
596-
* }),
597-
* tagTypes: ['Post'],
598-
* endpoints: (build) => ({
599-
* getPost: build.query<Post, number>({
600-
* query: (id) => `post/${id}`,
601-
* providesTags: ['Post'],
602-
* }),
603-
* updatePost: build.mutation<void, Pick<Post, 'id'> & Partial<Post>>({
604-
* query: ({ id, ...patch }) => ({
605-
* url: `post/${id}`,
606-
* method: 'PATCH',
607-
* body: patch,
608-
* }),
609-
* invalidatesTags: ['Post'],
610-
* async onQueryStarted({ id, ...patch }, { dispatch, queryFulfilled }) {
611-
* const patchResult = dispatch(
612-
* api.util.updateQueryData('getPost', id, (draft) => {
613-
* Object.assign(draft, patch)
614-
* })
615-
* )
616-
* try {
617-
* await queryFulfilled
618-
* } catch {
619-
* patchResult.undo()
620-
* }
621-
* },
622-
* }),
623-
* }),
624-
* })
625-
* ```
626-
*/
627-
onQueryStarted?(
628-
arg: QueryArg,
629-
api: MutationLifecycleApi<QueryArg, BaseQuery, ResultType, ReducerPath>,
630-
): Promise<void> | void
573+
QueryLifecycleMutationExtraOptions<
574+
ResultType,
575+
QueryArg,
576+
BaseQuery,
577+
ReducerPath
578+
> {
579+
type: DefinitionType.mutation
631580

632581
/**
633582
* Used by `mutation` endpoints. Determines which cached data should be either re-fetched or removed from the cache.

0 commit comments

Comments
 (0)