Skip to content

Commit 23a4edd

Browse files
committed
Refine mutationFn typing in mutation hooks
1 parent d48c6b3 commit 23a4edd

File tree

6 files changed

+74
-71
lines changed

6 files changed

+74
-71
lines changed

packages/ra-core/src/controller/create/useCreateController.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { useCallback } from 'react';
2-
import { UseMutationOptions } from '@tanstack/react-query';
32

43
import { useAuthenticated, useRequireAccess } from '../../auth';
5-
import {
6-
HttpError,
7-
useCreate,
8-
UseCreateMutateParams,
9-
} from '../../dataProvider';
4+
import { HttpError, useCreate, UseCreateOptions } from '../../dataProvider';
105
import { useRedirect, RedirectionSideEffect } from '../../routing';
116
import { useNotify } from '../../notification';
127
import {
@@ -225,10 +220,10 @@ export interface CreateControllerProps<
225220
redirect?: RedirectionSideEffect;
226221
resource?: string;
227222
mutationMode?: MutationMode;
228-
mutationOptions?: UseMutationOptions<
229-
ResultRecordType,
223+
mutationOptions?: UseCreateOptions<
224+
RecordType,
230225
MutationOptionsError,
231-
UseCreateMutateParams<RecordType>
226+
ResultRecordType
232227
> & { meta?: any };
233228
transform?: TransformData;
234229
}

packages/ra-core/src/dataProvider/useCreate.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ export const useCreate = <
9797
onSettled,
9898
...mutationOptions
9999
} = options;
100-
const wrappedCustomMutationFn = customMutationFn as
101-
| ((
102-
params: Partial<UseCreateMutateParams<RecordType>>
103-
) => Promise<ResultRecordType>)
104-
| undefined;
105100

106101
const dataProviderCreate = useEvent((resource: string, params) =>
107102
dataProvider.create<RecordType, ResultRecordType>(
@@ -120,9 +115,9 @@ export const useCreate = <
120115
...mutationOptions,
121116
mutationKey: [resource, 'create', params],
122117
mutationMode,
123-
mutationFn: wrappedCustomMutationFn
118+
mutationFn: customMutationFn
124119
? async params => ({
125-
data: await wrappedCustomMutationFn(params),
120+
data: await customMutationFn(params),
126121
})
127122
: ({ resource, ...params }) => {
128123
if (resource == null) {
@@ -196,9 +191,9 @@ export const useCreate = <
196191
// Immediately get the function with middlewares applied so that even if the middlewares gets unregistered (because of a redirect for instance),
197192
// we still have them applied when users have called the mutate function.
198193
const mutateWithMiddlewares = getMutateWithMiddlewares(
199-
wrappedCustomMutationFn
194+
customMutationFn
200195
? (resource, params) =>
201-
wrappedCustomMutationFn({
196+
customMutationFn({
202197
resource,
203198
...params,
204199
} as Partial<
@@ -280,11 +275,17 @@ export type UseCreateOptions<
280275
RecordType extends Omit<RaRecord, 'id'> = any,
281276
MutationError = unknown,
282277
ResultRecordType extends RaRecord = RecordType & { id: Identifier },
283-
> = UseMutationOptions<
284-
ResultRecordType,
285-
MutationError,
286-
Partial<UseCreateMutateParams<RecordType>>
278+
> = Omit<
279+
UseMutationOptions<
280+
ResultRecordType,
281+
MutationError,
282+
Partial<UseCreateMutateParams<RecordType>>
283+
>,
284+
'mutationFn'
287285
> & {
286+
mutationFn?: (
287+
params: Partial<UseCreateMutateParams<RecordType>>
288+
) => Promise<ResultRecordType>;
288289
mutationMode?: MutationMode;
289290
returnPromise?: boolean;
290291
getMutateWithMiddlewares?: <

packages/ra-core/src/dataProvider/useDelete.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ export const useDelete = <
9696
onSettled,
9797
...mutationOptions
9898
} = options;
99-
const wrappedCustomMutationFn = customMutationFn as
100-
| ((
101-
params: Partial<UseDeleteMutateParams<RecordType>>
102-
) => Promise<RecordType>)
103-
| undefined;
10499

105100
const [mutate, mutationResult] = useMutationWithMutationMode<
106101
MutationError,
@@ -112,9 +107,9 @@ export const useDelete = <
112107
...mutationOptions,
113108
mutationKey: [resource, 'delete', params],
114109
mutationMode,
115-
mutationFn: wrappedCustomMutationFn
110+
mutationFn: customMutationFn
116111
? async params => ({
117-
data: await wrappedCustomMutationFn(params),
112+
data: await customMutationFn(params),
118113
})
119114
: ({ resource, ...params }) => {
120115
if (resource == null) {
@@ -289,11 +284,17 @@ export interface UseDeleteMutateParams<RecordType extends RaRecord = any> {
289284
export type UseDeleteOptions<
290285
RecordType extends RaRecord = any,
291286
MutationError = unknown,
292-
> = UseMutationOptions<
293-
RecordType,
294-
MutationError,
295-
Partial<UseDeleteMutateParams<RecordType>>
287+
> = Omit<
288+
UseMutationOptions<
289+
RecordType,
290+
MutationError,
291+
Partial<UseDeleteMutateParams<RecordType>>
292+
>,
293+
'mutationFn'
296294
> & {
295+
mutationFn?: (
296+
params: Partial<UseDeleteMutateParams<RecordType>>
297+
) => Promise<RecordType>;
297298
mutationMode?: MutationMode;
298299
returnPromise?: boolean;
299300
};

packages/ra-core/src/dataProvider/useDeleteMany.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ export const useDeleteMany = <
9696
onSettled,
9797
...mutationOptions
9898
} = options;
99-
const wrappedCustomMutationFn = customMutationFn as
100-
| ((
101-
params: Partial<UseDeleteManyMutateParams<RecordType>>
102-
) => Promise<Array<RecordType['id']> | undefined>)
103-
| undefined;
10499

105100
const [mutate, mutationResult] = useMutationWithMutationMode<
106101
MutationError,
@@ -112,9 +107,9 @@ export const useDeleteMany = <
112107
...mutationOptions,
113108
mutationKey: [resource, 'deleteMany', params],
114109
mutationMode,
115-
mutationFn: wrappedCustomMutationFn
110+
mutationFn: customMutationFn
116111
? async params => ({
117-
data: await wrappedCustomMutationFn(params),
112+
data: await customMutationFn(params),
118113
})
119114
: ({ resource, ...params }) => {
120115
if (resource == null) {
@@ -313,11 +308,20 @@ export type UseDeleteManyOptions<
313308
RecordType extends RaRecord = any,
314309
MutationError = unknown,
315310
TReturnPromise extends boolean = boolean,
316-
> = UseMutationOptions<
317-
Array<RecordType['id']> | undefined,
318-
MutationError,
319-
Partial<UseDeleteManyMutateParams<RecordType>>
320-
> & { mutationMode?: MutationMode; returnPromise?: TReturnPromise };
311+
> = Omit<
312+
UseMutationOptions<
313+
Array<RecordType['id']> | undefined,
314+
MutationError,
315+
Partial<UseDeleteManyMutateParams<RecordType>>
316+
>,
317+
'mutationFn'
318+
> & {
319+
mutationFn?: (
320+
params: Partial<UseDeleteManyMutateParams<RecordType>>
321+
) => Promise<Array<RecordType['id']> | undefined>;
322+
mutationMode?: MutationMode;
323+
returnPromise?: TReturnPromise;
324+
};
321325

322326
export type UseDeleteManyResult<
323327
RecordType extends RaRecord = any,

packages/ra-core/src/dataProvider/useUpdate.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ export const useUpdate = <RecordType extends RaRecord = any, ErrorType = Error>(
9595
mutationFn: customMutationFn,
9696
...mutationOptions
9797
} = options;
98-
const wrappedCustomMutationFn = customMutationFn as
99-
| ((
100-
params: Partial<UseUpdateMutateParams<RecordType>>
101-
) => Promise<RecordType>)
102-
| undefined;
10398

10499
const dataProviderUpdate = useEvent(
105100
(resource: string, params: UpdateParams<RecordType>) =>
@@ -116,9 +111,9 @@ export const useUpdate = <RecordType extends RaRecord = any, ErrorType = Error>(
116111
...mutationOptions,
117112
mutationKey: [resource, 'update', params],
118113
mutationMode,
119-
mutationFn: wrappedCustomMutationFn
114+
mutationFn: customMutationFn
120115
? async params => ({
121-
data: await wrappedCustomMutationFn(params),
116+
data: await customMutationFn(params),
122117
})
123118
: ({ resource, ...params }) => {
124119
if (resource == null) {
@@ -264,9 +259,9 @@ export const useUpdate = <RecordType extends RaRecord = any, ErrorType = Error>(
264259
// Immediately get the function with middlewares applied so that even if the middlewares gets unregistered (because of a redirect for instance),
265260
// we still have them applied when users have called the mutate function.
266261
const mutateWithMiddlewares = getMutateWithMiddlewares(
267-
wrappedCustomMutationFn
262+
customMutationFn
268263
? (resource, params) =>
269-
wrappedCustomMutationFn({
264+
customMutationFn({
270265
resource,
271266
...params,
272267
} as Partial<
@@ -325,11 +320,17 @@ export interface UseUpdateMutateParams<RecordType extends RaRecord = any> {
325320
export type UseUpdateOptions<
326321
RecordType extends RaRecord = any,
327322
ErrorType = Error,
328-
> = UseMutationOptions<
329-
RecordType,
330-
ErrorType,
331-
Partial<UseUpdateMutateParams<RecordType>>
323+
> = Omit<
324+
UseMutationOptions<
325+
RecordType,
326+
ErrorType,
327+
Partial<UseUpdateMutateParams<RecordType>>
328+
>,
329+
'mutationFn'
332330
> & {
331+
mutationFn?: (
332+
params: Partial<UseUpdateMutateParams<RecordType>>
333+
) => Promise<RecordType>;
333334
mutationMode?: MutationMode;
334335
returnPromise?: boolean;
335336
getMutateWithMiddlewares?: <

packages/ra-core/src/dataProvider/useUpdateMany.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ export const useUpdateMany = <
9191
mutationFn: customMutationFn,
9292
...mutationOptions
9393
} = options;
94-
const wrappedCustomMutationFn = customMutationFn as
95-
| ((
96-
params: Partial<UseUpdateManyMutateParams<RecordType>>
97-
) => Promise<Array<RecordType['id']>>)
98-
| undefined;
9994

10095
const dataProviderUpdateMany = useEvent(
10196
(resource: string, params: UpdateManyParams<RecordType>) =>
@@ -112,9 +107,9 @@ export const useUpdateMany = <
112107
...mutationOptions,
113108
mutationKey: [resource, 'updateMany', params],
114109
mutationMode,
115-
mutationFn: wrappedCustomMutationFn
110+
mutationFn: customMutationFn
116111
? async params => ({
117-
data: await wrappedCustomMutationFn(params),
112+
data: await customMutationFn(params),
118113
})
119114
: ({ resource, ...params }) => {
120115
if (resource == null) {
@@ -245,9 +240,9 @@ export const useUpdateMany = <
245240
// Immediately get the function with middlewares applied so that even if the middlewares gets unregistered (because of a redirect for instance),
246241
// we still have them applied when users have called the mutate function.
247242
const mutateWithMiddlewares = getMutateWithMiddlewares(
248-
wrappedCustomMutationFn
243+
customMutationFn
249244
? (resource, params) =>
250-
wrappedCustomMutationFn({
245+
customMutationFn({
251246
resource,
252247
...params,
253248
} as Partial<
@@ -305,11 +300,17 @@ export interface UseUpdateManyMutateParams<RecordType extends RaRecord = any> {
305300
export type UseUpdateManyOptions<
306301
RecordType extends RaRecord = any,
307302
MutationError = unknown,
308-
> = UseMutationOptions<
309-
Array<RecordType['id']>,
310-
MutationError,
311-
Partial<UseUpdateManyMutateParams<RecordType>>
303+
> = Omit<
304+
UseMutationOptions<
305+
Array<RecordType['id']>,
306+
MutationError,
307+
Partial<UseUpdateManyMutateParams<RecordType>>
308+
>,
309+
'mutationFn'
312310
> & {
311+
mutationFn?: (
312+
params: Partial<UseUpdateManyMutateParams<RecordType>>
313+
) => Promise<Array<RecordType['id']>>;
313314
mutationMode?: MutationMode;
314315
returnPromise?: boolean;
315316
getMutateWithMiddlewares?: <

0 commit comments

Comments
 (0)