Skip to content

Commit 07b0a7c

Browse files
authored
Merge pull request #1052 from reduxjs/feature/remove-alpha.3-deprecations
2 parents ada1f4b + dbe24a2 commit 07b0a7c

File tree

5 files changed

+33
-141
lines changed

5 files changed

+33
-141
lines changed

etc/rtk-query-react.api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export type Api<BaseQuery extends BaseQueryFn, Definitions extends EndpointDefin
2020
overrideExisting?: boolean;
2121
}): Api<BaseQuery, Definitions & NewDefinitions, ReducerPath, TagTypes, Enhancers>;
2222
enhanceEndpoints<NewTagTypes extends string = never>(_: {
23-
addEntityTypes?: readonly NewTagTypes[];
2423
addTagTypes?: readonly NewTagTypes[];
2524
endpoints?: ReplaceTagTypes<Definitions, TagTypes | NoInfer<NewTagTypes>> extends infer NewDefinitions ? {
2625
[K in keyof NewDefinitions]?: Partial<NewDefinitions[K]> | ((definition: NewDefinitions[K]) => void);

src/query/createApi.ts

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -243,52 +243,8 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
243243
inject: Parameters<typeof api.injectEndpoints>[0]
244244
) {
245245
const evaluatedEndpoints = inject.endpoints({
246-
query: (x) => {
247-
// remove in final release
248-
if (x.onStart || x.onSuccess || x.onError) {
249-
if (
250-
typeof process !== 'undefined' &&
251-
process.env.NODE_ENV === 'development'
252-
) {
253-
console.warn(
254-
'`onStart`, `onSuccess` and `onError` have been replaced by `onQueryStarted`, please change your code accordingly'
255-
)
256-
}
257-
x.onQueryStarted ??= (arg, { queryFulfilled, ...api }) => {
258-
const queryApi = { ...api, context: {} }
259-
x.onStart?.(arg, queryApi)
260-
return queryFulfilled.then(
261-
(result) =>
262-
x.onSuccess?.(arg, queryApi, result.data, undefined),
263-
(reason) => x.onError?.(arg, queryApi, reason.error, undefined)
264-
)
265-
}
266-
}
267-
return { ...x, type: DefinitionType.query } as any
268-
},
269-
mutation: (x) => {
270-
// remove in final release
271-
if (x.onStart || x.onSuccess || x.onError) {
272-
if (
273-
typeof process !== 'undefined' &&
274-
process.env.NODE_ENV === 'development'
275-
) {
276-
console.warn(
277-
'`onStart`, `onSuccess` and `onError` have been replaced by `onQueryStarted`, please change your code accordingly'
278-
)
279-
}
280-
x.onQueryStarted ??= (arg, { queryFulfilled, ...api }) => {
281-
const queryApi = { ...api, context: {} }
282-
x.onStart?.(arg, queryApi)
283-
return queryFulfilled.then(
284-
(result) =>
285-
x.onSuccess?.(arg, queryApi, result.data, undefined),
286-
(reason) => x.onError?.(arg, queryApi, reason.error, undefined)
287-
)
288-
}
289-
}
290-
return { ...x, type: DefinitionType.mutation } as any
291-
},
246+
query: (x) => ({ ...x, type: DefinitionType.query } as any),
247+
mutation: (x) => ({ ...x, type: DefinitionType.mutation } as any),
292248
})
293249

294250
for (const [endpointName, definition] of Object.entries(

src/query/endpointDefinitions.ts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,6 @@ export interface QueryExtraOptions<
241241
* Not to be used. A query should not invalidate tags in the cache.
242242
*/
243243
invalidatesTags?: never
244-
/** @deprecated please use `onQueryStarted` instead */
245-
onStart?(arg: QueryArg, queryApi: QueryApi<ReducerPath, any>): void
246-
/** @deprecated please use `onQueryStarted` instead */
247-
onError?(
248-
arg: QueryArg,
249-
queryApi: QueryApi<ReducerPath, any>,
250-
error: unknown,
251-
meta: undefined
252-
): void
253-
/** @deprecated please use `onQueryStarted` instead */
254-
onSuccess?(
255-
arg: QueryArg,
256-
queryApi: QueryApi<ReducerPath, any>,
257-
result: ResultType,
258-
meta: undefined
259-
): void
260244
}
261245

262246
export type QueryDefinition<
@@ -268,30 +252,6 @@ export type QueryDefinition<
268252
> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType> &
269253
QueryExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>
270254

271-
/** @deprecated please use `onQueryStarted` instead */
272-
export interface MutationApi<ReducerPath extends string, Context extends {}> {
273-
/**
274-
* The dispatch method for the store
275-
*/
276-
dispatch: ThunkDispatch<any, any, AnyAction>
277-
/**
278-
* A method to get the current state
279-
*/
280-
getState(): RootState<any, any, ReducerPath>
281-
/**
282-
* `extra` as provided as `thunk.extraArgument` to the `configureStore` `getDefaultMiddleware` option.
283-
*/
284-
extra: unknown
285-
/**
286-
* A unique ID generated for the mutation
287-
*/
288-
requestId: string
289-
/**
290-
* A variable shared between `onStart`, `onError` and `onSuccess` of one request to pass data forward between them
291-
*/
292-
context: Context
293-
}
294-
295255
export interface MutationExtraOptions<
296256
TagTypes extends string,
297257
ResultType,
@@ -355,22 +315,6 @@ export interface MutationExtraOptions<
355315
* Not to be used. A mutation should not provide tags to the cache.
356316
*/
357317
providesTags?: never
358-
/** @deprecated please use `onQueryStarted` instead */
359-
onStart?(arg: QueryArg, mutationApi: MutationApi<ReducerPath, any>): void
360-
/** @deprecated please use `onQueryStarted` instead */
361-
onError?(
362-
arg: QueryArg,
363-
mutationApi: MutationApi<ReducerPath, any>,
364-
error: unknown,
365-
meta: BaseQueryMeta<BaseQuery>
366-
): void
367-
/** @deprecated please use `onQueryStarted` instead */
368-
onSuccess?(
369-
arg: QueryArg,
370-
mutationApi: MutationApi<ReducerPath, any>,
371-
result: ResultType,
372-
meta: BaseQueryMeta<BaseQuery>
373-
): void
374318
}
375319

376320
export type MutationDefinition<

src/query/tests/createApi.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -597,26 +597,26 @@ describe('query endpoint lifecycles - onStart, onSuccess, onError', () => {
597597
}),
598598
query: build.query<SuccessResponse, void>({
599599
query: () => '/success',
600-
onStart: (_, api) => {
600+
async onQueryStarted(_, api) {
601601
api.dispatch(setCount(0))
602-
},
603-
onSuccess: (_, api) => {
604-
api.dispatch(setCount(1))
605-
},
606-
onError: (_, api) => {
607-
api.dispatch(setCount(-1))
602+
try {
603+
await api.queryFulfilled
604+
api.dispatch(setCount(1))
605+
} catch {
606+
api.dispatch(setCount(-1))
607+
}
608608
},
609609
}),
610610
mutation: build.mutation<SuccessResponse, void>({
611611
query: () => ({ url: '/success', method: 'POST' }),
612-
onStart: (_, api) => {
612+
async onQueryStarted(_, api) {
613613
api.dispatch(setCount(0))
614-
},
615-
onSuccess: (_, api) => {
616-
api.dispatch(setCount(1))
617-
},
618-
onError: (_, api) => {
619-
api.dispatch(setCount(-1))
614+
try {
615+
await api.queryFulfilled
616+
api.dispatch(setCount(1))
617+
} catch {
618+
api.dispatch(setCount(-1))
619+
}
620620
},
621621
}),
622622
}),

src/query/tests/optimisticUpdates.test.tsx

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ import { createApi } from '@reduxjs/toolkit/query/react'
22
import { actionsReducer, hookWaitFor, setupApiStore, waitMs } from './helpers'
33
import { renderHook, act } from '@testing-library/react-hooks'
44

5-
interface Patch {
6-
op: 'replace' | 'remove' | 'add'
7-
path: (string | number)[]
8-
value?: any
9-
}
10-
115
interface Post {
126
id: string
137
title: string
@@ -62,9 +56,15 @@ describe('basic lifecycle', () => {
6256
endpoints: (build) => ({
6357
test: build.mutation({
6458
query: (x) => x,
65-
onStart,
66-
onError,
67-
onSuccess,
59+
async onQueryStarted(arg, api) {
60+
onStart(arg)
61+
try {
62+
const result = await api.queryFulfilled
63+
onSuccess(result)
64+
} catch (e) {
65+
onError(e)
66+
}
67+
},
6868
}),
6969
}),
7070
overrideExisting: true,
@@ -89,19 +89,14 @@ describe('basic lifecycle', () => {
8989
expect(onStart).not.toHaveBeenCalled()
9090
expect(baseQuery).not.toHaveBeenCalled()
9191
act(() => void result.current[0]('arg'))
92-
expect(onStart).toHaveBeenCalledWith('arg', expect.any(Object))
92+
expect(onStart).toHaveBeenCalledWith('arg')
9393
expect(baseQuery).toHaveBeenCalledWith('arg', expect.any(Object), undefined)
9494

9595
expect(onError).not.toHaveBeenCalled()
9696
expect(onSuccess).not.toHaveBeenCalled()
9797
await act(() => waitMs(5))
9898
expect(onError).not.toHaveBeenCalled()
99-
expect(onSuccess).toHaveBeenCalledWith(
100-
'arg',
101-
expect.any(Object),
102-
'success',
103-
undefined
104-
)
99+
expect(onSuccess).toHaveBeenCalledWith({ data: 'success', meta: 'meta' })
105100
})
106101

107102
test('error', async () => {
@@ -117,18 +112,16 @@ describe('basic lifecycle', () => {
117112
expect(onStart).not.toHaveBeenCalled()
118113
expect(baseQuery).not.toHaveBeenCalled()
119114
act(() => void result.current[0]('arg'))
120-
expect(onStart).toHaveBeenCalledWith('arg', expect.any(Object))
115+
expect(onStart).toHaveBeenCalledWith('arg')
121116
expect(baseQuery).toHaveBeenCalledWith('arg', expect.any(Object), undefined)
122117

123118
expect(onError).not.toHaveBeenCalled()
124119
expect(onSuccess).not.toHaveBeenCalled()
125120
await act(() => waitMs(5))
126-
expect(onError).toHaveBeenCalledWith(
127-
'arg',
128-
expect.any(Object),
129-
{ message: 'error' },
130-
undefined
131-
)
121+
expect(onError).toHaveBeenCalledWith({
122+
error: { message: 'error' },
123+
isUnhandledError: true,
124+
})
132125
expect(onSuccess).not.toHaveBeenCalled()
133126
})
134127
})
@@ -176,7 +169,7 @@ describe('updateQueryData', () => {
176169

177170
act(() => {
178171
storeRef.store.dispatch(
179-
api.util.patchQueryData('post', '3', returnValue.inversePatches)
172+
api.util.patchQueryResult('post', '3', returnValue.inversePatches)
180173
)
181174
})
182175

0 commit comments

Comments
 (0)